mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 01:55:10 +02:00
Working on handlers
This commit is contained in:
parent
e112fcba29
commit
b9de0f8e38
5 changed files with 62 additions and 171 deletions
46
server/routers/newt/peers.ts
Normal file
46
server/routers/newt/peers.ts
Normal 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
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue