mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-22 00:35:22 +00:00
@@ -20,9 +20,7 @@ import {
|
|||||||
} from "@server/db";
|
} from "@server/db";
|
||||||
import { and, eq, inArray, ne } from "drizzle-orm";
|
import { and, eq, inArray, ne } from "drizzle-orm";
|
||||||
|
|
||||||
import {
|
import { deletePeer as newtDeletePeer } from "@server/routers/newt/peers";
|
||||||
deletePeer as newtDeletePeer
|
|
||||||
} from "@server/routers/newt/peers";
|
|
||||||
import {
|
import {
|
||||||
initPeerAddHandshake,
|
initPeerAddHandshake,
|
||||||
deletePeer as olmDeletePeer
|
deletePeer as olmDeletePeer
|
||||||
@@ -33,7 +31,7 @@ import {
|
|||||||
generateAliasConfig,
|
generateAliasConfig,
|
||||||
generateRemoteSubnets,
|
generateRemoteSubnets,
|
||||||
generateSubnetProxyTargetV2,
|
generateSubnetProxyTargetV2,
|
||||||
parseEndpoint,
|
parseEndpoint
|
||||||
} from "@server/lib/ip";
|
} from "@server/lib/ip";
|
||||||
import {
|
import {
|
||||||
addPeerData,
|
addPeerData,
|
||||||
@@ -51,10 +49,7 @@ export async function getClientSiteResourceAccess(
|
|||||||
? await trx
|
? await trx
|
||||||
.select()
|
.select()
|
||||||
.from(sites)
|
.from(sites)
|
||||||
.innerJoin(
|
.innerJoin(siteNetworks, eq(siteNetworks.siteId, sites.siteId))
|
||||||
siteNetworks,
|
|
||||||
eq(siteNetworks.siteId, sites.siteId)
|
|
||||||
)
|
|
||||||
.where(eq(siteNetworks.networkId, siteResource.networkId))
|
.where(eq(siteNetworks.networkId, siteResource.networkId))
|
||||||
.then((rows) => rows.map((row) => row.sites))
|
.then((rows) => rows.map((row) => row.sites))
|
||||||
: [];
|
: [];
|
||||||
@@ -362,7 +357,8 @@ export async function rebuildClientAssociationsFromSiteResource(
|
|||||||
.where(inArray(clients.clientId, existingClientSiteIds))
|
.where(inArray(clients.clientId, existingClientSiteIds))
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
const otherResourceClientIds = clientsFromOtherResourcesBySite.get(siteId) ?? new Set<number>();
|
const otherResourceClientIds =
|
||||||
|
clientsFromOtherResourcesBySite.get(siteId) ?? new Set<number>();
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`rebuildClientAssociations: [rebuildClientAssociationsFromSiteResource] siteId=${siteId} otherResourceClientIds=[${[...otherResourceClientIds].join(", ")}] mergedAllClientIds=[${mergedAllClientIds.join(", ")}]`
|
`rebuildClientAssociations: [rebuildClientAssociationsFromSiteResource] siteId=${siteId} otherResourceClientIds=[${[...otherResourceClientIds].join(", ")}] mergedAllClientIds=[${mergedAllClientIds.join(", ")}]`
|
||||||
@@ -709,7 +705,7 @@ export async function updateClientSiteDestinations(
|
|||||||
sourcePort: destination.sourcePort,
|
sourcePort: destination.sourcePort,
|
||||||
destinations: destination.destinations
|
destinations: destination.destinations
|
||||||
};
|
};
|
||||||
logger.info(
|
logger.debug(
|
||||||
`Payload for update-destinations: ${JSON.stringify(payload, null, 2)}`
|
`Payload for update-destinations: ${JSON.stringify(payload, null, 2)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -27,11 +27,11 @@ export async function buildSiteConfigurationForOlmClient(
|
|||||||
) {
|
) {
|
||||||
const siteConfigurations: {
|
const siteConfigurations: {
|
||||||
siteId: number;
|
siteId: number;
|
||||||
name?: string
|
name?: string;
|
||||||
endpoint?: string
|
endpoint?: string;
|
||||||
publicKey?: string
|
publicKey?: string;
|
||||||
serverIP?: string | null
|
serverIP?: string | null;
|
||||||
serverPort?: number | null
|
serverPort?: number | null;
|
||||||
remoteSubnets?: string[];
|
remoteSubnets?: string[];
|
||||||
aliases: Alias[];
|
aliases: Alias[];
|
||||||
}[] = [];
|
}[] = [];
|
||||||
@@ -79,7 +79,6 @@ export async function buildSiteConfigurationForOlmClient(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
if (jitMode) {
|
if (jitMode) {
|
||||||
// Add site configuration to the array
|
// Add site configuration to the array
|
||||||
siteConfigurations.push({
|
siteConfigurations.push({
|
||||||
@@ -109,10 +108,9 @@ export async function buildSiteConfigurationForOlmClient(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!site.publicKey || site.publicKey == "") { // the site is not ready to accept new peers
|
if (!site.publicKey || site.publicKey == "") {
|
||||||
logger.warn(
|
// the site is not ready to accept new peers
|
||||||
`Site ${site.siteId} has no public key, skipping`
|
logger.warn(`Site ${site.siteId} has no public key, skipping`);
|
||||||
);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import { initPeerAddHandshake } from "./peers";
|
|||||||
export const handleOlmServerInitAddPeerHandshake: MessageHandler = async (
|
export const handleOlmServerInitAddPeerHandshake: MessageHandler = async (
|
||||||
context
|
context
|
||||||
) => {
|
) => {
|
||||||
logger.info("Handling register olm message!");
|
logger.info("Handle Olm Server Init Add Peer Handshake Message");
|
||||||
const { message, client: c, sendToClient } = context;
|
const { message, client: c, sendToClient } = context;
|
||||||
const olm = c as Olm;
|
const olm = c as Olm;
|
||||||
|
|
||||||
|
|||||||
@@ -9,16 +9,50 @@ import {
|
|||||||
import { buildSiteConfigurationForOlmClient } from "./buildConfiguration";
|
import { buildSiteConfigurationForOlmClient } from "./buildConfiguration";
|
||||||
import { sendToClient } from "#dynamic/routers/ws";
|
import { sendToClient } from "#dynamic/routers/ws";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { eq, inArray } from "drizzle-orm";
|
import { count, eq, inArray } from "drizzle-orm";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import { canCompress } from "@server/lib/clientVersionChecks";
|
import { canCompress } from "@server/lib/clientVersionChecks";
|
||||||
|
import { build } from "@server/build";
|
||||||
|
|
||||||
export async function sendOlmSyncMessage(olm: Olm, client: Client) {
|
export async function sendOlmSyncMessage(olm: Olm, client: Client) {
|
||||||
|
// Get all sites data
|
||||||
|
const sitesCountResult = await db
|
||||||
|
.select({ count: count() })
|
||||||
|
.from(sites)
|
||||||
|
.innerJoin(
|
||||||
|
clientSitesAssociationsCache,
|
||||||
|
eq(sites.siteId, clientSitesAssociationsCache.siteId)
|
||||||
|
)
|
||||||
|
.where(eq(clientSitesAssociationsCache.clientId, client.clientId));
|
||||||
|
|
||||||
|
// Extract the count value from the result array
|
||||||
|
const sitesCount =
|
||||||
|
sitesCountResult.length > 0 ? sitesCountResult[0].count : 0;
|
||||||
|
|
||||||
|
// Prepare an array to store site configurations
|
||||||
|
logger.debug(
|
||||||
|
`[handleOlmRegisterMessage] Found ${sitesCount} sites for client ${client.clientId}`,
|
||||||
|
{ orgId: client.orgId }
|
||||||
|
);
|
||||||
|
|
||||||
|
let jitMode = false;
|
||||||
|
if (sitesCount > 250 && build == "saas") {
|
||||||
|
// THIS IS THE MAX ON THE BUSINESS TIER
|
||||||
|
// we have too many sites
|
||||||
|
// If we have too many sites we need to drop into fully JIT mode by not sending any of the sites
|
||||||
|
logger.info(
|
||||||
|
`[handleOlmRegisterMessage] Too many sites (${sitesCount}), dropping into JIT mode`,
|
||||||
|
{ orgId: client.orgId }
|
||||||
|
);
|
||||||
|
jitMode = true;
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: WE ARE HARDCODING THE RELAY PARAMETER TO FALSE HERE BUT IN THE REGISTER MESSAGE ITS DEFINED BY THE CLIENT
|
// NOTE: WE ARE HARDCODING THE RELAY PARAMETER TO FALSE HERE BUT IN THE REGISTER MESSAGE ITS DEFINED BY THE CLIENT
|
||||||
const siteConfigurations = await buildSiteConfigurationForOlmClient(
|
const siteConfigurations = await buildSiteConfigurationForOlmClient(
|
||||||
client,
|
client,
|
||||||
client.pubKey,
|
client.pubKey,
|
||||||
false
|
false,
|
||||||
|
jitMode
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get all exit nodes from sites where the client has peers
|
// Get all exit nodes from sites where the client has peers
|
||||||
@@ -82,7 +116,6 @@ export async function sendOlmSyncMessage(olm: Olm, client: Client) {
|
|||||||
exitNodes: exitNodesData
|
exitNodes: exitNodesData
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
compress: canCompress(olm.version, "olm")
|
compress: canCompress(olm.version, "olm")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user