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