mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-18 16:28:26 +02:00
Basic relay working!
This commit is contained in:
parent
bebe40c8e8
commit
afd87d07a3
8 changed files with 125 additions and 17 deletions
89
server/routers/gerbil/getAllRelays.ts
Normal file
89
server/routers/gerbil/getAllRelays.ts
Normal file
|
@ -0,0 +1,89 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { clients, exitNodes, newts, olms, Site, sites } from "@server/db/schema";
|
||||
import { db } from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
// Define Zod schema for request validation
|
||||
const getAllRelaysSchema = z.object({
|
||||
publicKey: z.string().optional(),
|
||||
});
|
||||
|
||||
export async function getAllRelays(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
// Validate request parameters
|
||||
const parsedParams = getAllRelaysSchema.safeParse(req.body);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedParams.error).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { publicKey } = parsedParams.data;
|
||||
|
||||
if (!publicKey) {
|
||||
return next(createHttpError(HttpCode.BAD_REQUEST, 'publicKey is required'));
|
||||
}
|
||||
|
||||
// Fetch exit node
|
||||
let [exitNode] = await db.select().from(exitNodes).where(eq(exitNodes.publicKey, publicKey));
|
||||
if (!exitNode) {
|
||||
return next(createHttpError(HttpCode.NOT_FOUND, "Exit node not found"));
|
||||
}
|
||||
|
||||
// Fetch sites for this exit node
|
||||
const sitesRes = await db.select().from(sites).where(eq(sites.exitNodeId, exitNode.exitNodeId));
|
||||
|
||||
if (sitesRes.length === 0) {
|
||||
return next(createHttpError(HttpCode.NOT_FOUND, "No sites found for this exit node"));
|
||||
}
|
||||
|
||||
// get the clients on each site and map them to the site
|
||||
const sitesAndClients = await Promise.all(sitesRes.map(async (site) => {
|
||||
const clientsRes = await db.select().from(clients).where(eq(clients.siteId, site.siteId));
|
||||
return {
|
||||
site,
|
||||
clients: clientsRes
|
||||
};
|
||||
}));
|
||||
|
||||
let mappings: { [key: string]: {
|
||||
destinationIp: string;
|
||||
destinationPort: number;
|
||||
} } = {};
|
||||
|
||||
for (const siteAndClients of sitesAndClients) {
|
||||
const { site, clients } = siteAndClients;
|
||||
for (const client of clients) {
|
||||
if (!client.endpoint || !site.endpoint || !site.subnet) {
|
||||
continue;
|
||||
}
|
||||
mappings[client.endpoint] = {
|
||||
destinationIp: site.subnet.split("/")[0],
|
||||
destinationPort: parseInt(site.endpoint.split(":")[1])
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(HttpCode.OK).send({ mappings });
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"An error occurred..."
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -79,9 +79,7 @@ export async function getConfig(req: Request, res: Response, next: NextFunction)
|
|||
}
|
||||
|
||||
// Fetch sites for this exit node
|
||||
const sitesRes = await db.query.sites.findMany({
|
||||
where: eq(sites.exitNodeId, exitNode[0].exitNodeId),
|
||||
});
|
||||
const sitesRes = await db.select().from(sites).where(eq(sites.exitNodeId, exitNode[0].exitNodeId));
|
||||
|
||||
const peers = await Promise.all(sitesRes.map(async (site) => {
|
||||
return {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
export * from "./getConfig";
|
||||
export * from "./receiveBandwidth";
|
||||
export * from "./updateHolePunch";
|
||||
export * from "./getAllRelays";
|
|
@ -1,6 +1,6 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { clients, newts, olms, sites } from "@server/db/schema";
|
||||
import { clients, newts, olms, Site, sites } from "@server/db/schema";
|
||||
import { db } from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
|
@ -36,7 +36,9 @@ export async function updateHolePunch(
|
|||
|
||||
const { olmId, newtId, ip, port, timestamp } = parsedParams.data;
|
||||
|
||||
logger.debug(`Got hole punch with ip: ${ip}, port: ${port} for olmId: ${olmId} or newtId: ${newtId}`);
|
||||
// logger.debug(`Got hole punch with ip: ${ip}, port: ${port} for olmId: ${olmId} or newtId: ${newtId}`);
|
||||
|
||||
let site: Site | undefined;
|
||||
|
||||
if (olmId) {
|
||||
const [olm] = await db
|
||||
|
@ -51,13 +53,19 @@ export async function updateHolePunch(
|
|||
);
|
||||
}
|
||||
|
||||
await db
|
||||
const [client] = await db
|
||||
.update(clients)
|
||||
.set({
|
||||
endpoint: `${ip}:${port}`,
|
||||
lastHolePunch: timestamp
|
||||
})
|
||||
.where(eq(clients.clientId, olm.clientId));
|
||||
.where(eq(clients.clientId, olm.clientId))
|
||||
.returning();
|
||||
|
||||
[site] = await db
|
||||
.select()
|
||||
.from(sites)
|
||||
.where(eq(sites.siteId, client.siteId));
|
||||
} else if (newtId) {
|
||||
const [newt] = await db
|
||||
.select()
|
||||
|
@ -71,16 +79,27 @@ export async function updateHolePunch(
|
|||
);
|
||||
}
|
||||
|
||||
await db
|
||||
[site] = await db
|
||||
.update(sites)
|
||||
.set({
|
||||
endpoint: `${ip}:${port}`,
|
||||
lastHolePunch: timestamp
|
||||
})
|
||||
.where(eq(sites.siteId, newt.siteId));
|
||||
.where(eq(sites.siteId, newt.siteId))
|
||||
.returning();
|
||||
}
|
||||
|
||||
return res.status(HttpCode.OK).send({});
|
||||
if (!site || !site.endpoint || !site.subnet) {
|
||||
logger.warn(`Site not found for olmId: ${olmId} or newtId: ${newtId}`);
|
||||
return next(
|
||||
createHttpError(HttpCode.NOT_FOUND, "Site not found")
|
||||
);
|
||||
}
|
||||
|
||||
return res.status(HttpCode.OK).send({
|
||||
destinationIp: site.subnet.split("/")[0],
|
||||
destinationPort: parseInt(site.endpoint.split(":")[1])
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
|
|
|
@ -35,6 +35,7 @@ internalRouter.use("/gerbil", gerbilRouter);
|
|||
gerbilRouter.post("/get-config", gerbil.getConfig);
|
||||
gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth);
|
||||
gerbilRouter.post("/update-hole-punch", gerbil.updateHolePunch);
|
||||
gerbilRouter.post("/get-all-relays", gerbil.getAllRelays);
|
||||
|
||||
// Badger routes
|
||||
const badgerRouter = Router();
|
||||
|
|
|
@ -10,7 +10,6 @@ import config from "@server/lib/config";
|
|||
|
||||
const inputSchema = z.object({
|
||||
publicKey: z.string(),
|
||||
endpoint: z.string()
|
||||
});
|
||||
|
||||
type Input = z.infer<typeof inputSchema>;
|
||||
|
@ -42,7 +41,7 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
|
|||
return;
|
||||
}
|
||||
|
||||
const { publicKey, endpoint } = message.data as Input;
|
||||
const { publicKey } = message.data as Input;
|
||||
|
||||
const siteId = newt.siteId;
|
||||
|
||||
|
@ -66,7 +65,6 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
|
|||
.update(sites)
|
||||
.set({
|
||||
publicKey,
|
||||
// endpoint,
|
||||
address,
|
||||
listenPort
|
||||
})
|
||||
|
@ -82,7 +80,6 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
|
|||
.update(sites)
|
||||
.set({
|
||||
publicKey
|
||||
// endpoint
|
||||
})
|
||||
.where(eq(sites.siteId, siteId))
|
||||
.returning();
|
||||
|
|
|
@ -2,6 +2,7 @@ import db from '@server/db';
|
|||
import { newts, sites } from '@server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { sendToClient } from '../ws';
|
||||
import logger from '@server/logger';
|
||||
|
||||
export async function addPeer(siteId: number, peer: {
|
||||
publicKey: string;
|
||||
|
@ -24,6 +25,8 @@ export async function addPeer(siteId: number, peer: {
|
|||
type: 'newt/wg/peer/add',
|
||||
data: peer
|
||||
});
|
||||
|
||||
logger.info(`Added peer ${peer.publicKey} to newt ${newt.newtId}`);
|
||||
}
|
||||
|
||||
export async function deletePeer(siteId: number, publicKey: string) {
|
||||
|
@ -44,4 +47,6 @@ export async function deletePeer(siteId: number, publicKey: string) {
|
|||
publicKey
|
||||
}
|
||||
});
|
||||
|
||||
logger.info(`Deleted peer ${publicKey} from newt ${newt.newtId}`);
|
||||
}
|
|
@ -74,8 +74,6 @@ export async function getOlmToken(
|
|||
);
|
||||
}
|
||||
|
||||
logger.debug("Existing olm: ", existingOlmRes);
|
||||
|
||||
const existingOlm = existingOlmRes[0];
|
||||
|
||||
const validSecret = await verifyPassword(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue