fosrl.pangolin/server/routers/newt/handleGetConfigMessage.ts

157 lines
4.3 KiB
TypeScript
Raw Normal View History

import { z } from "zod";
import { MessageHandler } from "../ws";
import logger from "@server/logger";
import { fromError } from "zod-validation-error";
import db from "@server/db";
2025-04-13 21:28:11 -04:00
import {
clients,
clientSites,
Newt,
Site,
sites,
olms
} from "@server/db/schema";
import { eq } from "drizzle-orm";
import { getNextAvailableClientSubnet } from "@server/lib/ip";
import config from "@server/lib/config";
2025-04-13 21:28:11 -04:00
import { addPeer } from "../olm/peers";
const inputSchema = z.object({
publicKey: z.string(),
2025-04-13 21:28:11 -04:00
port: z.number().int().positive()
});
type Input = z.infer<typeof inputSchema>;
export const handleGetConfigMessage: MessageHandler = async (context) => {
2025-02-21 12:17:56 -05:00
const { message, client, sendToClient } = context;
const newt = client as Newt;
2025-02-21 16:12:21 -05:00
logger.debug(JSON.stringify(message.data));
2025-02-21 17:13:23 -05:00
logger.debug("Handling Newt get config message!");
if (!newt) {
logger.warn("Newt not found");
return;
}
if (!newt.siteId) {
logger.warn("Newt has no site!"); // TODO: Maybe we create the site here?
return;
}
const parsed = inputSchema.safeParse(message.data);
if (!parsed.success) {
logger.error(
"handleGetConfigMessage: Invalid input: " +
fromError(parsed.error).toString()
);
return;
}
const { publicKey, port } = message.data as Input;
const siteId = newt.siteId;
2025-04-13 21:28:11 -04:00
// Get the current site data
const [existingSite] = await db
.select()
.from(sites)
.where(eq(sites.siteId, siteId));
2025-04-13 21:28:11 -04:00
if (!existingSite) {
logger.warn("handleGetConfigMessage: Site not found");
return;
}
2025-04-18 14:41:27 -04:00
// update the endpoint and the public key
const [site] = await db
.update(sites)
.set({
publicKey,
listenPort: port
})
.where(eq(sites.siteId, siteId))
.returning();
if (!site) {
logger.error("handleGetConfigMessage: Failed to update site");
return;
}
2025-04-13 21:28:11 -04:00
// Get all clients connected to this site
const clientsRes = await db
.select()
.from(clients)
.innerJoin(clientSites, eq(clients.clientId, clientSites.clientId))
.where(eq(clientSites.siteId, siteId));
2025-04-13 21:28:11 -04:00
// Prepare peers data for the response
const peers = await Promise.all(
2025-02-22 11:20:56 -05:00
clientsRes
.filter((client) => {
if (!client.clients.pubKey) {
return false;
2025-02-22 11:20:56 -05:00
}
if (!client.clients.subnet) {
return false;
}
if (!client.clients.endpoint) {
return false;
}
if (!client.clients.online) {
return false;
}
return true;
2025-02-22 11:20:56 -05:00
})
.map(async (client) => {
2025-04-13 21:28:11 -04:00
const peerData = {
publicKey: client.clients.pubKey!,
allowedIps: [client.clients.subnet!],
2025-04-18 14:41:27 -04:00
endpoint: client.clientSites.isRelayed
? ""
: client.clients.endpoint! // if its relayed it should be localhost
2025-02-22 11:20:56 -05:00
};
2025-04-13 21:28:11 -04:00
// Add or update this peer on the olm if it is connected
try {
await addPeer(client.clients.clientId, {
...peerData,
siteId: siteId,
serverIP: site.address,
serverPort: site.listenPort
});
} catch (error) {
logger.error(
`Failed to add/update peer ${client.clients.pubKey} to newt ${newt.newtId}: ${error}`
);
}
return peerData;
2025-02-22 11:20:56 -05:00
})
);
2025-04-13 21:28:11 -04:00
// Filter out any null values from peers that didn't have an olm
const validPeers = peers.filter((peer) => peer !== null);
// Build the configuration response
const configResponse = {
2025-02-21 16:12:21 -05:00
ipAddress: site.address,
2025-04-13 21:28:11 -04:00
peers: validPeers
};
logger.debug("Sending config: ", configResponse);
return {
message: {
2025-04-13 21:28:11 -04:00
type: "newt/wg/receive-config",
data: {
2025-02-21 17:13:20 -05:00
...configResponse
}
},
2025-04-13 21:28:11 -04:00
broadcast: false,
excludeSender: false
};
2025-04-13 21:28:11 -04:00
};