2025-02-20 22:34:51 -05:00
|
|
|
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-03-25 22:01:08 -04:00
|
|
|
import { clients, clientSites, Newt, Site, sites } from "@server/db/schema";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
import { getNextAvailableClientSubnet } from "@server/lib/ip";
|
2025-04-01 12:49:02 -04:00
|
|
|
import config from "@server/lib/config";
|
2025-02-20 22:34:51 -05:00
|
|
|
|
|
|
|
const inputSchema = z.object({
|
|
|
|
publicKey: z.string(),
|
2025-03-27 22:13:57 -04:00
|
|
|
port: z.number().int().positive(),
|
2025-02-20 22:34:51 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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-20 22:34:51 -05:00
|
|
|
|
2025-02-21 16:12:21 -05:00
|
|
|
logger.debug(JSON.stringify(message.data));
|
2025-02-21 17:13:23 -05:00
|
|
|
|
2025-02-20 22:34:51 -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-03-27 22:13:57 -04:00
|
|
|
const { publicKey, port } = message.data as Input;
|
2025-02-20 22:34:51 -05:00
|
|
|
|
|
|
|
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) {
|
2025-03-25 22:01:08 -04:00
|
|
|
let address = await getNextAvailableClientSubnet(siteRes.orgId);
|
2025-04-01 12:49:02 -04:00
|
|
|
if (!address) {
|
|
|
|
logger.error("handleGetConfigMessage: No available address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
address = `${address.split("/")[0]}/${config.getRawConfig().orgs.block_size}` // we want the block size of the whole org
|
2025-02-20 22:34:51 -05:00
|
|
|
|
|
|
|
// create a new exit node
|
|
|
|
const [updateRes] = await db
|
|
|
|
.update(sites)
|
|
|
|
.set({
|
|
|
|
publicKey,
|
|
|
|
address,
|
2025-03-27 22:13:57 -04:00
|
|
|
listenPort: port,
|
2025-02-20 22:34:51 -05:00
|
|
|
})
|
|
|
|
.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-03-27 22:13:57 -04:00
|
|
|
publicKey,
|
|
|
|
listenPort: port,
|
2025-02-21 17:23:22 -05:00
|
|
|
})
|
|
|
|
.where(eq(sites.siteId, siteId))
|
|
|
|
.returning();
|
|
|
|
|
2025-02-20 22:34:51 -05:00
|
|
|
site = siteRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!site) {
|
|
|
|
logger.error("handleGetConfigMessage: Failed to update site");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientsRes = await db
|
|
|
|
.select()
|
|
|
|
.from(clients)
|
2025-03-25 22:01:08 -04:00
|
|
|
.innerJoin(clientSites, eq(clients.clientId, clientSites.clientId))
|
|
|
|
.where(eq(clientSites.siteId, siteId));
|
2025-02-20 22:34:51 -05:00
|
|
|
|
2025-02-22 11:20:56 -05:00
|
|
|
const now = new Date().getTime() / 1000;
|
2025-02-20 22:34:51 -05:00
|
|
|
const peers = await Promise.all(
|
2025-02-22 11:20:56 -05:00
|
|
|
clientsRes
|
|
|
|
.filter((client) => {
|
2025-03-25 22:01:08 -04:00
|
|
|
// This filter wasn't returning anything - fixed to properly filter clients
|
|
|
|
if (
|
|
|
|
!client.clients.lastHolePunch ||
|
|
|
|
now - client.clients.lastHolePunch > 6
|
|
|
|
) {
|
2025-02-22 11:20:56 -05:00
|
|
|
logger.warn("Client last hole punch is too old");
|
2025-03-25 22:01:08 -04:00
|
|
|
return false;
|
2025-02-22 11:20:56 -05:00
|
|
|
}
|
2025-03-25 22:01:08 -04:00
|
|
|
return true;
|
2025-02-22 11:20:56 -05:00
|
|
|
})
|
|
|
|
.map(async (client) => {
|
|
|
|
return {
|
2025-03-25 22:01:08 -04:00
|
|
|
publicKey: client.clients.pubKey,
|
|
|
|
allowedIps: [client.clients.subnet],
|
|
|
|
endpoint: client.clients.endpoint
|
2025-02-22 11:20:56 -05:00
|
|
|
};
|
|
|
|
})
|
2025-02-20 22:34:51 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
const configResponse = {
|
2025-02-21 16:12:21 -05:00
|
|
|
ipAddress: site.address,
|
2025-02-20 22:34:51 -05:00
|
|
|
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?
|
2025-02-20 22:34:51 -05:00
|
|
|
data: {
|
2025-02-21 17:13:20 -05:00
|
|
|
...configResponse
|
2025-02-20 22:34:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
broadcast: false, // Send to all clients
|
|
|
|
excludeSender: false // Include sender in broadcast
|
|
|
|
};
|
2025-03-25 22:01:08 -04:00
|
|
|
};
|