mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-22 23:31:49 +00:00
Add batch messaging functions to rebuild function
This commit is contained in:
@@ -21,10 +21,10 @@ import {
|
||||
} from "@server/db";
|
||||
import { and, count, eq, inArray, ne } from "drizzle-orm";
|
||||
|
||||
import { deletePeer as newtDeletePeer } from "@server/routers/newt/peers";
|
||||
import { deletePeersBatch as newtDeletePeersBatch } from "@server/routers/newt/peers";
|
||||
import {
|
||||
initPeerAddHandshake,
|
||||
deletePeer as olmDeletePeer
|
||||
initPeerAddHandshakeBatch,
|
||||
deletePeersBatch as olmDeletePeersBatch
|
||||
} from "@server/routers/olm/peers";
|
||||
import { sendToExitNode } from "#dynamic/lib/exitNodes";
|
||||
import logger from "@server/logger";
|
||||
@@ -35,10 +35,10 @@ import {
|
||||
parseEndpoint
|
||||
} from "@server/lib/ip";
|
||||
import {
|
||||
addPeerData,
|
||||
addTargets as addSubnetProxyTargets,
|
||||
removePeerData,
|
||||
removeTargets as removeSubnetProxyTargets
|
||||
addPeerDataBatch,
|
||||
addTargetsBatch as addSubnetProxyTargetsBatch,
|
||||
removePeerDataBatch,
|
||||
removeTargetsBatch as removeSubnetProxyTargetsBatch
|
||||
} from "@server/routers/client/targets";
|
||||
import { lockManager } from "#dynamic/lib/lock";
|
||||
import { rebuildQueue } from "#dynamic/lib/rebuildQueue";
|
||||
@@ -559,6 +559,28 @@ async function handleMessagesForSiteClients(
|
||||
const newtJobs: Promise<any>[] = [];
|
||||
const olmJobs: Promise<any>[] = [];
|
||||
const exitNodeJobs: Promise<any>[] = [];
|
||||
const newtPeerDeletes: {
|
||||
siteId: number;
|
||||
publicKey: string;
|
||||
newtId: string;
|
||||
}[] = [];
|
||||
const olmPeerDeletes: {
|
||||
clientId: number;
|
||||
siteId: number;
|
||||
publicKey: string;
|
||||
olmId: string;
|
||||
}[] = [];
|
||||
const olmPeerAddHandshakes: {
|
||||
clientId: number;
|
||||
peer: {
|
||||
siteId: number;
|
||||
exitNode: {
|
||||
publicKey: string;
|
||||
endpoint: string;
|
||||
};
|
||||
};
|
||||
olmId: string;
|
||||
}[] = [];
|
||||
|
||||
// Combine all clients that need processing (those being added or removed)
|
||||
const clientsToProcess = new Map<
|
||||
@@ -638,15 +660,17 @@ async function handleMessagesForSiteClients(
|
||||
}
|
||||
|
||||
if (isDelete) {
|
||||
newtJobs.push(newtDeletePeer(siteId, client.pubKey, newt.newtId));
|
||||
olmJobs.push(
|
||||
olmDeletePeer(
|
||||
client.clientId,
|
||||
siteId,
|
||||
site.publicKey,
|
||||
olm.olmId
|
||||
)
|
||||
);
|
||||
newtPeerDeletes.push({
|
||||
siteId,
|
||||
publicKey: client.pubKey,
|
||||
newtId: newt.newtId
|
||||
});
|
||||
olmPeerDeletes.push({
|
||||
clientId: client.clientId,
|
||||
siteId,
|
||||
publicKey: site.publicKey,
|
||||
olmId: olm.olmId
|
||||
});
|
||||
}
|
||||
|
||||
if (isAdd) {
|
||||
@@ -658,23 +682,34 @@ async function handleMessagesForSiteClients(
|
||||
continue;
|
||||
}
|
||||
|
||||
await initPeerAddHandshake(
|
||||
// this will kick off the add peer process for the client
|
||||
client.clientId,
|
||||
{
|
||||
olmPeerAddHandshakes.push({
|
||||
clientId: client.clientId,
|
||||
peer: {
|
||||
siteId,
|
||||
exitNode: {
|
||||
publicKey: exitNode.publicKey,
|
||||
endpoint: exitNode.endpoint
|
||||
}
|
||||
},
|
||||
olm.olmId
|
||||
);
|
||||
olmId: olm.olmId
|
||||
});
|
||||
}
|
||||
|
||||
exitNodeJobs.push(updateClientSiteDestinations(client, trx));
|
||||
}
|
||||
|
||||
if (newtPeerDeletes.length > 0) {
|
||||
newtJobs.push(newtDeletePeersBatch(newtPeerDeletes));
|
||||
}
|
||||
|
||||
if (olmPeerDeletes.length > 0) {
|
||||
olmJobs.push(olmDeletePeersBatch(olmPeerDeletes));
|
||||
}
|
||||
|
||||
if (olmPeerAddHandshakes.length > 0) {
|
||||
olmJobs.push(initPeerAddHandshakeBatch(olmPeerAddHandshakes));
|
||||
}
|
||||
|
||||
Promise.all(exitNodeJobs).catch((error) => {
|
||||
logger.error(
|
||||
`rebuildClientAssociations: Error updating client site destinations for site ${site.siteId}:`,
|
||||
@@ -867,24 +902,28 @@ async function handleSubnetProxyTargetUpdates(
|
||||
|
||||
if (targetsToAdd) {
|
||||
proxyJobs.push(
|
||||
addSubnetProxyTargets(
|
||||
newt.newtId,
|
||||
targetsToAdd,
|
||||
newt.version
|
||||
)
|
||||
addSubnetProxyTargetsBatch([
|
||||
{
|
||||
newtId: newt.newtId,
|
||||
targets: targetsToAdd,
|
||||
version: newt.version
|
||||
}
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
for (const client of addedClients) {
|
||||
olmJobs.push(
|
||||
addPeerData(
|
||||
client.clientId,
|
||||
olmJobs.push(
|
||||
addPeerDataBatch(
|
||||
addedClients.map((client) => ({
|
||||
clientId: client.clientId,
|
||||
siteId,
|
||||
generateRemoteSubnets([siteResource]),
|
||||
generateAliasConfig([siteResource])
|
||||
)
|
||||
);
|
||||
}
|
||||
remoteSubnets: generateRemoteSubnets([
|
||||
siteResource
|
||||
]),
|
||||
aliases: generateAliasConfig([siteResource])
|
||||
}))
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -904,14 +943,23 @@ async function handleSubnetProxyTargetUpdates(
|
||||
|
||||
if (targetsToRemove) {
|
||||
proxyJobs.push(
|
||||
removeSubnetProxyTargets(
|
||||
newt.newtId,
|
||||
targetsToRemove,
|
||||
newt.version
|
||||
)
|
||||
removeSubnetProxyTargetsBatch([
|
||||
{
|
||||
newtId: newt.newtId,
|
||||
targets: targetsToRemove,
|
||||
version: newt.version
|
||||
}
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
const peerDataRemovals: {
|
||||
clientId: number;
|
||||
siteId: number;
|
||||
remoteSubnets: string[];
|
||||
aliases: ReturnType<typeof generateAliasConfig>;
|
||||
}[] = [];
|
||||
|
||||
for (const client of removedClients) {
|
||||
if (!siteResource.destination) {
|
||||
continue;
|
||||
@@ -959,14 +1007,16 @@ async function handleSubnetProxyTargetUpdates(
|
||||
? []
|
||||
: generateRemoteSubnets([siteResource]);
|
||||
|
||||
olmJobs.push(
|
||||
removePeerData(
|
||||
client.clientId,
|
||||
siteId,
|
||||
remoteSubnetsToRemove,
|
||||
generateAliasConfig([siteResource])
|
||||
)
|
||||
);
|
||||
peerDataRemovals.push({
|
||||
clientId: client.clientId,
|
||||
siteId,
|
||||
remoteSubnets: remoteSubnetsToRemove,
|
||||
aliases: generateAliasConfig([siteResource])
|
||||
});
|
||||
}
|
||||
|
||||
if (peerDataRemovals.length > 0) {
|
||||
olmJobs.push(removePeerDataBatch(peerDataRemovals));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1277,6 +1327,28 @@ async function handleMessagesForClientSites(
|
||||
const newtJobs: Promise<any>[] = [];
|
||||
const olmJobs: Promise<any>[] = [];
|
||||
const exitNodeJobs: Promise<any>[] = [];
|
||||
const newtPeerDeletes: {
|
||||
siteId: number;
|
||||
publicKey: string;
|
||||
newtId: string;
|
||||
}[] = [];
|
||||
const olmPeerDeletes: {
|
||||
clientId: number;
|
||||
siteId: number;
|
||||
publicKey: string;
|
||||
olmId: string;
|
||||
}[] = [];
|
||||
const olmPeerAddHandshakes: {
|
||||
clientId: number;
|
||||
peer: {
|
||||
siteId: number;
|
||||
exitNode: {
|
||||
publicKey: string;
|
||||
endpoint: string;
|
||||
};
|
||||
};
|
||||
olmId: string;
|
||||
}[] = [];
|
||||
|
||||
const totalSitesOnClient = await trx
|
||||
.select({ count: count(clientSitesAssociationsCache.siteId) })
|
||||
@@ -1308,19 +1380,19 @@ async function handleMessagesForClientSites(
|
||||
|
||||
if (isRemove) {
|
||||
// Remove peer from newt
|
||||
newtJobs.push(
|
||||
newtDeletePeer(site.siteId, client.pubKey, newt.newtId)
|
||||
);
|
||||
newtPeerDeletes.push({
|
||||
siteId: site.siteId,
|
||||
publicKey: client.pubKey,
|
||||
newtId: newt.newtId
|
||||
});
|
||||
try {
|
||||
// Remove peer from olm
|
||||
olmJobs.push(
|
||||
olmDeletePeer(
|
||||
client.clientId,
|
||||
site.siteId,
|
||||
site.publicKey,
|
||||
olmId
|
||||
)
|
||||
);
|
||||
olmPeerDeletes.push({
|
||||
clientId: client.clientId,
|
||||
siteId: site.siteId,
|
||||
publicKey: site.publicKey,
|
||||
olmId
|
||||
});
|
||||
} catch (error) {
|
||||
// if the error includes not found then its just because the olm does not exist anymore or yet and its fine if we dont send
|
||||
if (
|
||||
@@ -1352,10 +1424,9 @@ async function handleMessagesForClientSites(
|
||||
continue;
|
||||
}
|
||||
|
||||
await initPeerAddHandshake(
|
||||
// this will kick off the add peer process for the client
|
||||
client.clientId,
|
||||
{
|
||||
olmPeerAddHandshakes.push({
|
||||
clientId: client.clientId,
|
||||
peer: {
|
||||
siteId: site.siteId,
|
||||
exitNode: {
|
||||
publicKey: exitNode.publicKey,
|
||||
@@ -1363,7 +1434,7 @@ async function handleMessagesForClientSites(
|
||||
}
|
||||
},
|
||||
olmId
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Update exit node destinations
|
||||
@@ -1379,6 +1450,18 @@ async function handleMessagesForClientSites(
|
||||
);
|
||||
}
|
||||
|
||||
if (newtPeerDeletes.length > 0) {
|
||||
newtJobs.push(newtDeletePeersBatch(newtPeerDeletes));
|
||||
}
|
||||
|
||||
if (olmPeerDeletes.length > 0) {
|
||||
olmJobs.push(olmDeletePeersBatch(olmPeerDeletes));
|
||||
}
|
||||
|
||||
if (olmPeerAddHandshakes.length > 0) {
|
||||
olmJobs.push(initPeerAddHandshakeBatch(olmPeerAddHandshakes));
|
||||
}
|
||||
|
||||
Promise.all(exitNodeJobs).catch((error) => {
|
||||
logger.error(
|
||||
`rebuildClientAssociations: Error updating client site destinations for client ${client.clientId}:`,
|
||||
@@ -1477,6 +1560,20 @@ async function handleMessagesForClientResources(
|
||||
continue;
|
||||
}
|
||||
|
||||
const targetsToAddBatch: {
|
||||
newtId: string;
|
||||
targets: NonNullable<
|
||||
Awaited<ReturnType<typeof generateSubnetProxyTargetV2>>
|
||||
>;
|
||||
version: string | null;
|
||||
}[] = [];
|
||||
const peerDataAdds: {
|
||||
clientId: number;
|
||||
siteId: number;
|
||||
remoteSubnets: string[];
|
||||
aliases: ReturnType<typeof generateAliasConfig>;
|
||||
}[] = [];
|
||||
|
||||
for (const resource of resources) {
|
||||
const targets = await generateSubnetProxyTargetV2(resource, [
|
||||
{
|
||||
@@ -1487,25 +1584,21 @@ async function handleMessagesForClientResources(
|
||||
]);
|
||||
|
||||
if (targets) {
|
||||
proxyJobs.push(
|
||||
addSubnetProxyTargets(
|
||||
newt.newtId,
|
||||
targets,
|
||||
newt.version
|
||||
)
|
||||
);
|
||||
targetsToAddBatch.push({
|
||||
newtId: newt.newtId,
|
||||
targets,
|
||||
version: newt.version
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
// Add peer data to olm
|
||||
olmJobs.push(
|
||||
addPeerData(
|
||||
client.clientId,
|
||||
siteId,
|
||||
generateRemoteSubnets([resource]),
|
||||
generateAliasConfig([resource])
|
||||
)
|
||||
);
|
||||
peerDataAdds.push({
|
||||
clientId: client.clientId,
|
||||
siteId,
|
||||
remoteSubnets: generateRemoteSubnets([resource]),
|
||||
aliases: generateAliasConfig([resource])
|
||||
});
|
||||
} catch (error) {
|
||||
// if the error includes not found then its just because the olm does not exist anymore or yet and its fine if we dont send
|
||||
if (
|
||||
@@ -1520,6 +1613,14 @@ async function handleMessagesForClientResources(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetsToAddBatch.length > 0) {
|
||||
proxyJobs.push(addSubnetProxyTargetsBatch(targetsToAddBatch));
|
||||
}
|
||||
|
||||
if (peerDataAdds.length > 0) {
|
||||
olmJobs.push(addPeerDataBatch(peerDataAdds));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1586,6 +1687,20 @@ async function handleMessagesForClientResources(
|
||||
continue;
|
||||
}
|
||||
|
||||
const targetsToRemoveBatch: {
|
||||
newtId: string;
|
||||
targets: NonNullable<
|
||||
Awaited<ReturnType<typeof generateSubnetProxyTargetV2>>
|
||||
>;
|
||||
version: string | null;
|
||||
}[] = [];
|
||||
const peerDataRemovals: {
|
||||
clientId: number;
|
||||
siteId: number;
|
||||
remoteSubnets: string[];
|
||||
aliases: ReturnType<typeof generateAliasConfig>;
|
||||
}[] = [];
|
||||
|
||||
for (const resource of resources) {
|
||||
const targets = await generateSubnetProxyTargetV2(resource, [
|
||||
{
|
||||
@@ -1596,13 +1711,11 @@ async function handleMessagesForClientResources(
|
||||
]);
|
||||
|
||||
if (targets) {
|
||||
proxyJobs.push(
|
||||
removeSubnetProxyTargets(
|
||||
newt.newtId,
|
||||
targets,
|
||||
newt.version
|
||||
)
|
||||
);
|
||||
targetsToRemoveBatch.push({
|
||||
newtId: newt.newtId,
|
||||
targets,
|
||||
version: newt.version
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -1653,14 +1766,12 @@ async function handleMessagesForClientResources(
|
||||
: generateRemoteSubnets([resource]);
|
||||
|
||||
// Remove peer data from olm
|
||||
olmJobs.push(
|
||||
removePeerData(
|
||||
client.clientId,
|
||||
siteId,
|
||||
remoteSubnetsToRemove,
|
||||
generateAliasConfig([resource])
|
||||
)
|
||||
);
|
||||
peerDataRemovals.push({
|
||||
clientId: client.clientId,
|
||||
siteId,
|
||||
remoteSubnets: remoteSubnetsToRemove,
|
||||
aliases: generateAliasConfig([resource])
|
||||
});
|
||||
} catch (error) {
|
||||
// if the error includes not found then its just because the olm does not exist anymore or yet and its fine if we dont send
|
||||
if (
|
||||
@@ -1675,6 +1786,16 @@ async function handleMessagesForClientResources(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetsToRemoveBatch.length > 0) {
|
||||
proxyJobs.push(
|
||||
removeSubnetProxyTargetsBatch(targetsToRemoveBatch)
|
||||
);
|
||||
}
|
||||
|
||||
if (peerDataRemovals.length > 0) {
|
||||
olmJobs.push(removePeerDataBatch(peerDataRemovals));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1928,7 +2049,15 @@ export async function cleanupSiteAssociations(
|
||||
for (const client of allClients) {
|
||||
// Tell each olm to drop the site's WireGuard peer.
|
||||
if (site.publicKey) {
|
||||
jobs.push(olmDeletePeer(client.clientId, siteId, site.publicKey));
|
||||
jobs.push(
|
||||
olmDeletePeersBatch([
|
||||
{
|
||||
clientId: client.clientId,
|
||||
siteId,
|
||||
publicKey: site.publicKey
|
||||
}
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
// Recompute and push updated relay destinations (now excluding this site).
|
||||
|
||||
Reference in New Issue
Block a user