Send all hp data now

This commit is contained in:
Owen
2025-08-04 20:22:13 -07:00
parent 1a9de1e5c5
commit 5889efd74a

View File

@@ -1,13 +1,6 @@
import { db, ExitNode } from "@server/db";
import { MessageHandler } from "../ws";
import {
clients,
clientSites,
exitNodes,
Olm,
olms,
sites
} from "@server/db";
import { clients, clientSites, exitNodes, Olm, olms, sites } from "@server/db";
import { and, eq, inArray } from "drizzle-orm";
import { addPeer, deletePeer } from "../newt/peers";
import logger from "@server/logger";
@@ -30,7 +23,9 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
const clientId = olm.clientId;
const { publicKey, relay } = message.data;
logger.debug(`Olm client ID: ${clientId}, Public Key: ${publicKey}, Relay: ${relay}`);
logger.debug(
`Olm client ID: ${clientId}, Public Key: ${publicKey}, Relay: ${relay}`
);
if (!publicKey) {
logger.warn("Public key not provided");
@@ -50,22 +45,25 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
}
if (client.exitNodeId) {
// Get the exit node for this site
const [exitNode] = await db
.select()
.from(exitNodes)
.where(eq(exitNodes.exitNodeId, client.exitNodeId))
.limit(1);
// TODO: FOR NOW WE ARE JUST HOLEPUNCHING ALL EXIT NODES BUT IN THE FUTURE WE SHOULD HANDLE THIS BETTER
// Send holepunch message for each site
sendToClient(olm.olmId, {
// Get the exit node
const allExitNodes = await db.select().from(exitNodes);
const exitNodesHpData = allExitNodes.map((exitNode: ExitNode) => {
return {
serverPubKey: exitNode.publicKey,
endpoint: exitNode.endpoint
};
});
// Send holepunch message
await sendToClient(olm.olmId, {
type: "olm/wg/holepunch",
data: {
serverPubKey: exitNode.publicKey,
endpoint: exitNode.endpoint,
exitNodes: exitNodesHpData,
}
});
}
if (now - (client.lastHolePunch || 0) > 6) {
@@ -103,7 +101,9 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
// Prepare an array to store site configurations
let siteConfigurations = [];
logger.debug(`Found ${sitesData.length} sites for client ${client.clientId}`);
logger.debug(
`Found ${sitesData.length} sites for client ${client.clientId}`
);
if (sitesData.length === 0) {
sendToClient(olm.olmId, {
@@ -150,10 +150,12 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
const [clientSite] = await db
.select()
.from(clientSites)
.where(and(
eq(clientSites.clientId, client.clientId),
eq(clientSites.siteId, site.siteId)
))
.where(
and(
eq(clientSites.clientId, client.clientId),
eq(clientSites.siteId, site.siteId)
)
)
.limit(1);
// Add the peer to the exit node for this site
@@ -163,7 +165,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
);
await addPeer(site.siteId, {
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: relay ? "" : clientSite.endpoint
});
} else {
@@ -197,7 +199,7 @@ export const handleOlmRegisterMessage: MessageHandler = async (context) => {
});
}
// REMOVED THIS SO IT CREATES THE INTERFACE AND JUST WAITS FOR THE SITES
// REMOVED THIS SO IT CREATES THE INTERFACE AND JUST WAITS FOR THE SITES
// if (siteConfigurations.length === 0) {
// logger.warn("No valid site configurations found");
// return;