Newt working

This commit is contained in:
Owen Schwartz 2024-11-23 16:28:50 -05:00
parent f9e0c33368
commit 0670d48ac7
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
7 changed files with 112 additions and 53 deletions

View file

@ -4,8 +4,6 @@ import { exitNodes, resources, sites, targets } from "@server/db/schema";
import { eq, inArray } from "drizzle-orm";
import { addPeer, deletePeer } from "../gerbil/peers";
import logger from "@server/logger";
import { findNextAvailableCidr } from "@server/utils/ip";
import { exit } from "process";
export const handleRegisterMessage: MessageHandler = async (context) => {
const { message, newt, sendToClient } = context;
@ -28,13 +26,18 @@ export const handleRegisterMessage: MessageHandler = async (context) => {
return;
}
// const [site] = await db
// .select()
// .from(sites)
// .where(eq(sites.siteId, siteId))
// .limit(1);
const [site] = await db
.select()
.from(sites)
.where(eq(sites.siteId, siteId))
.limit(1);
if (!site || !site.exitNodeId) {
logger.warn("Site not found or does not have exit node");
return;
}
const [updatedSite] = await db
.update(sites)
.set({
pubKey: publicKey
@ -43,11 +46,6 @@ export const handleRegisterMessage: MessageHandler = async (context) => {
.returning();
if (!site || !site.exitNodeId) {
logger.warn("Site not found or does not have exit node");
return;
}
const [exitNode] = await db
.select()
.from(exitNodes)
@ -100,10 +98,10 @@ export const handleRegisterMessage: MessageHandler = async (context) => {
message: {
type: "newt/wg/connect",
data: {
endpoint: exitNode.endpoint,
endpoint: `${exitNode.endpoint}:${exitNode.listenPort}`,
publicKey: exitNode.publicKey,
serverIP: exitNode.address,
tunnelIP: site.subnet,
serverIP: exitNode.address.split("/")[0],
tunnelIP: site.subnet.split("/")[0],
targets: {
udp: udpTargets,
tcp: tcpTargets,