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

190 lines
5 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-02-21 12:17:56 -05:00
import { clients, Newt, Site, sites } from "@server/db/schema";
import { eq, isNotNull } from "drizzle-orm";
import { findNextAvailableCidr } from "@server/lib/ip";
import config from "@server/lib/config";
const inputSchema = z.object({
publicKey: z.string(),
});
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;
}
2025-02-23 16:49:41 -05:00
const { publicKey } = message.data as Input;
const siteId = newt.siteId;
const [siteRes] = await db
.select()
.from(sites)
.where(eq(sites.siteId, siteId));
if (!siteRes) {
logger.warn("handleGetConfigMessage: Site not found");
return;
}
let site: Site | undefined;
2025-02-22 12:53:35 -05:00
if (!siteRes.address) {
const address = await getNextAvailableSubnet();
2025-02-21 17:13:23 -05:00
const listenPort = await getNextAvailablePort();
// create a new exit node
const [updateRes] = await db
.update(sites)
.set({
publicKey,
address,
listenPort
})
.where(eq(sites.siteId, siteId))
.returning();
site = updateRes;
logger.info(`Updated site ${siteId} with new WG Newt info`);
} else {
2025-02-21 17:23:22 -05:00
// update the endpoint and the public key
const [siteRes] = await db
.update(sites)
.set({
2025-02-22 11:20:56 -05:00
publicKey
2025-02-21 17:23:22 -05:00
})
.where(eq(sites.siteId, siteId))
.returning();
site = siteRes;
}
if (!site) {
logger.error("handleGetConfigMessage: Failed to update site");
return;
}
const clientsRes = await db
.select()
.from(clients)
.where(eq(clients.siteId, siteId));
2025-02-22 11:20:56 -05:00
const now = new Date().getTime() / 1000;
const peers = await Promise.all(
2025-02-22 11:20:56 -05:00
clientsRes
.filter((client) => {
if (client.lastHolePunch && now - client.lastHolePunch > 6) {
logger.warn("Client last hole punch is too old");
return;
}
})
.map(async (client) => {
return {
publicKey: client.pubKey,
allowedIps: [client.subnet],
endpoint: client.endpoint
};
})
);
const configResponse = {
2025-02-21 16:12:21 -05:00
listenPort: site.listenPort,
ipAddress: site.address,
peers
};
logger.debug("Sending config: ", configResponse);
return {
message: {
2025-02-21 12:34:05 -05:00
type: "newt/wg/receive-config", // what to make the response type?
data: {
2025-02-21 17:13:20 -05:00
...configResponse
}
},
broadcast: false, // Send to all clients
excludeSender: false // Include sender in broadcast
};
};
async function getNextAvailableSubnet(): Promise<string> {
const existingAddresses = await db
.select({
address: sites.address
})
.from(sites)
.where(isNotNull(sites.address));
const addresses = existingAddresses
.map((a) => a.address)
.filter((a) => a) as string[];
let subnet = findNextAvailableCidr(
addresses,
2025-02-21 18:51:16 -05:00
config.getRawConfig().newt.block_size,
config.getRawConfig().newt.subnet_group
);
if (!subnet) {
throw new Error("No available subnets remaining in space");
}
// replace the last octet with 1
subnet =
subnet.split(".").slice(0, 3).join(".") +
".1" +
"/" +
subnet.split("/")[1];
return subnet;
}
2025-02-21 12:52:24 -05:00
async function getNextAvailablePort(): Promise<number> {
// Get all existing ports from exitNodes table
2025-02-22 11:20:56 -05:00
const existingPorts = await db
.select({
listenPort: sites.listenPort
})
.from(sites);
2025-02-21 12:52:24 -05:00
// Find the first available port between 1024 and 65535
2025-02-21 18:51:16 -05:00
let nextPort = config.getRawConfig().newt.start_port;
2025-02-21 12:52:24 -05:00
for (const port of existingPorts) {
if (port.listenPort && port.listenPort > nextPort) {
break;
}
nextPort++;
if (nextPort > 65535) {
2025-02-22 11:20:56 -05:00
throw new Error("No available ports remaining in space");
2025-02-21 12:52:24 -05:00
}
}
return nextPort;
}