Support relaying on register

This commit is contained in:
Owen
2025-07-24 14:48:24 -07:00
parent 5c929badeb
commit 59cb06acf4

View File

@@ -1,4 +1,4 @@
import { db } from "@server/db"; import { db, ExitNode } from "@server/db";
import { MessageHandler } from "../ws"; import { MessageHandler } from "../ws";
import { import {
clients, clients,
@@ -28,7 +28,10 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
return; return;
} }
const clientId = olm.clientId; const clientId = olm.clientId;
const { publicKey } = message.data; const { publicKey, relay } = message.data;
logger.debug(`Olm client ID: ${clientId}, Public Key: ${publicKey}, Relay: ${relay}`);
if (!publicKey) { if (!publicKey) {
logger.warn("Public key not provided"); logger.warn("Public key not provided");
return; return;
@@ -62,6 +65,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
endpoint: exitNode.endpoint, endpoint: exitNode.endpoint,
} }
}); });
} }
if (now - (client.lastHolePunch || 0) > 6) { if (now - (client.lastHolePunch || 0) > 6) {
@@ -85,7 +89,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
await db await db
.update(clientSites) .update(clientSites)
.set({ .set({
isRelayed: false isRelayed: relay == true
}) })
.where(eq(clientSites.clientId, olm.clientId)); .where(eq(clientSites.clientId, olm.clientId));
} }
@@ -98,8 +102,8 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
.where(eq(clientSites.clientId, client.clientId)); .where(eq(clientSites.clientId, client.clientId));
// Prepare an array to store site configurations // Prepare an array to store site configurations
const siteConfigurations = []; let siteConfigurations = [];
logger.debug(`Found ${sitesData.length} sites for client ${client.clientId}`);
// Process each site // Process each site
for (const { sites: site } of sitesData) { for (const { sites: site } of sitesData) {
if (!site.exitNodeId) { if (!site.exitNodeId) {
@@ -115,7 +119,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
continue; continue;
} }
if (site.lastHolePunch && now - site.lastHolePunch > 6) { if (site.lastHolePunch && now - site.lastHolePunch > 6 && relay) {
logger.warn( logger.warn(
`Site ${site.siteId} last hole punch is too old, skipping` `Site ${site.siteId} last hole punch is too old, skipping`
); );
@@ -143,7 +147,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
await addPeer(site.siteId, { await addPeer(site.siteId, {
publicKey: publicKey, publicKey: publicKey,
allowedIps: [`${client.subnet.split('/')[0]}/32`], // we want to only allow from that client allowedIps: [`${client.subnet.split('/')[0]}/32`], // we want to only allow from that client
endpoint: client.endpoint endpoint: relay ? "" : client.endpoint
}); });
} else { } else {
logger.warn( logger.warn(
@@ -151,10 +155,24 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
); );
} }
let endpoint = site.endpoint;
if (relay) {
const [exitNode] = await db
.select()
.from(exitNodes)
.where(eq(exitNodes.exitNodeId, site.exitNodeId))
.limit(1);
if (!exitNode) {
logger.warn(`Exit node not found for site ${site.siteId}`);
continue;
}
endpoint = `${exitNode.endpoint}:21820`;
}
// Add site configuration to the array // Add site configuration to the array
siteConfigurations.push({ siteConfigurations.push({
siteId: site.siteId, siteId: site.siteId,
endpoint: site.endpoint, endpoint: endpoint,
publicKey: site.publicKey, publicKey: site.publicKey,
serverIP: site.address, serverIP: site.address,
serverPort: site.listenPort serverPort: site.listenPort