Working on handlers

This commit is contained in:
Owen 2025-02-21 10:55:38 -05:00
parent e112fcba29
commit b9de0f8e38
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD
5 changed files with 62 additions and 171 deletions

View file

@ -0,0 +1,46 @@
import db from '@server/db';
import { newts, sites } from '@server/db/schema';
import { eq } from 'drizzle-orm';
import { sendToClient } from '../ws';
export async function addPeer(siteId: number, peer: {
publicKey: string;
allowedIps: string[];
}) {
const [site] = await db.select().from(sites).where(eq(sites.siteId, siteId)).limit(1);
if (!site) {
throw new Error(`Exit node with ID ${siteId} not found`);
}
// get the newt on the site
const [newt] = await db.select().from(newts).where(eq(newts.siteId, siteId)).limit(1);
if (!newt) {
throw new Error(`Newt not found for site ${siteId}`);
}
sendToClient(newt.newtId, {
type: 'add_peer',
data: peer
});
}
export async function deletePeer(siteId: number, publicKey: string) {
const [site] = await db.select().from(sites).where(eq(sites.siteId, siteId)).limit(1);
if (!site) {
throw new Error(`Exit node with ID ${siteId} not found`);
}
// get the newt on the site
const [newt] = await db.select().from(newts).where(eq(newts.siteId, siteId)).limit(1);
if (!newt) {
throw new Error(`Newt not found for site ${siteId}`);
}
sendToClient(newt.newtId, {
type: 'delete_peer',
data: {
publicKey
}
});
}