From 73a4d7d3515d6c91ac61795399212e456a6c0a3e Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 13 May 2026 11:57:02 -0700 Subject: [PATCH 1/3] Quiet log message --- server/lib/rebuildClientAssociations.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/server/lib/rebuildClientAssociations.ts b/server/lib/rebuildClientAssociations.ts index a5c9a9321..a7a134f6d 100644 --- a/server/lib/rebuildClientAssociations.ts +++ b/server/lib/rebuildClientAssociations.ts @@ -20,9 +20,7 @@ import { } from "@server/db"; import { and, eq, inArray, ne } from "drizzle-orm"; -import { - deletePeer as newtDeletePeer -} from "@server/routers/newt/peers"; +import { deletePeer as newtDeletePeer } from "@server/routers/newt/peers"; import { initPeerAddHandshake, deletePeer as olmDeletePeer @@ -33,7 +31,7 @@ import { generateAliasConfig, generateRemoteSubnets, generateSubnetProxyTargetV2, - parseEndpoint, + parseEndpoint } from "@server/lib/ip"; import { addPeerData, @@ -51,10 +49,7 @@ export async function getClientSiteResourceAccess( ? await trx .select() .from(sites) - .innerJoin( - siteNetworks, - eq(siteNetworks.siteId, sites.siteId) - ) + .innerJoin(siteNetworks, eq(siteNetworks.siteId, sites.siteId)) .where(eq(siteNetworks.networkId, siteResource.networkId)) .then((rows) => rows.map((row) => row.sites)) : []; @@ -362,7 +357,8 @@ export async function rebuildClientAssociationsFromSiteResource( .where(inArray(clients.clientId, existingClientSiteIds)) : []; - const otherResourceClientIds = clientsFromOtherResourcesBySite.get(siteId) ?? new Set(); + const otherResourceClientIds = + clientsFromOtherResourcesBySite.get(siteId) ?? new Set(); logger.debug( `rebuildClientAssociations: [rebuildClientAssociationsFromSiteResource] siteId=${siteId} otherResourceClientIds=[${[...otherResourceClientIds].join(", ")}] mergedAllClientIds=[${mergedAllClientIds.join(", ")}]` @@ -709,7 +705,7 @@ export async function updateClientSiteDestinations( sourcePort: destination.sourcePort, destinations: destination.destinations }; - logger.info( + logger.debug( `Payload for update-destinations: ${JSON.stringify(payload, null, 2)}` ); From c16d2ff2ed7794aef3d5b7f3f76f0161b6889a23 Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 13 May 2026 13:52:35 -0700 Subject: [PATCH 2/3] Fix log message --- server/routers/olm/handleOlmServerInitAddPeerHandshake.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/routers/olm/handleOlmServerInitAddPeerHandshake.ts b/server/routers/olm/handleOlmServerInitAddPeerHandshake.ts index 05a83a146..b3aaffb93 100644 --- a/server/routers/olm/handleOlmServerInitAddPeerHandshake.ts +++ b/server/routers/olm/handleOlmServerInitAddPeerHandshake.ts @@ -17,7 +17,7 @@ import { initPeerAddHandshake } from "./peers"; export const handleOlmServerInitAddPeerHandshake: MessageHandler = async ( context ) => { - logger.info("Handling register olm message!"); + logger.info("Handle Olm Server Init Add Peer Handshake Message"); const { message, client: c, sendToClient } = context; const olm = c as Olm; From 92a06e0ea34eb5387e257a9b6463174a5d4f0bee Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 13 May 2026 14:00:20 -0700 Subject: [PATCH 3/3] Handle jit mode with syncs --- server/routers/olm/buildConfiguration.ts | 18 +++++------ server/routers/olm/sync.ts | 39 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/server/routers/olm/buildConfiguration.ts b/server/routers/olm/buildConfiguration.ts index 4182725d3..640031bca 100644 --- a/server/routers/olm/buildConfiguration.ts +++ b/server/routers/olm/buildConfiguration.ts @@ -27,11 +27,11 @@ export async function buildSiteConfigurationForOlmClient( ) { const siteConfigurations: { siteId: number; - name?: string - endpoint?: string - publicKey?: string - serverIP?: string | null - serverPort?: number | null + name?: string; + endpoint?: string; + publicKey?: string; + serverIP?: string | null; + serverPort?: number | null; remoteSubnets?: string[]; aliases: Alias[]; }[] = []; @@ -79,7 +79,6 @@ export async function buildSiteConfigurationForOlmClient( ) ); - if (jitMode) { // Add site configuration to the array siteConfigurations.push({ @@ -109,10 +108,9 @@ export async function buildSiteConfigurationForOlmClient( continue; } - if (!site.publicKey || site.publicKey == "") { // the site is not ready to accept new peers - logger.warn( - `Site ${site.siteId} has no public key, skipping` - ); + if (!site.publicKey || site.publicKey == "") { + // the site is not ready to accept new peers + logger.warn(`Site ${site.siteId} has no public key, skipping`); continue; } diff --git a/server/routers/olm/sync.ts b/server/routers/olm/sync.ts index c994b2c73..46e1fbd88 100644 --- a/server/routers/olm/sync.ts +++ b/server/routers/olm/sync.ts @@ -9,16 +9,50 @@ import { import { buildSiteConfigurationForOlmClient } from "./buildConfiguration"; import { sendToClient } from "#dynamic/routers/ws"; 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 { canCompress } from "@server/lib/clientVersionChecks"; +import { build } from "@server/build"; 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 const siteConfigurations = await buildSiteConfigurationForOlmClient( client, client.pubKey, - false + false, + jitMode ); // 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 } }, - { compress: canCompress(olm.version, "olm") }