mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-28 07:46:36 +00:00
Basic relay working!
This commit is contained in:
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 "./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(
|
||||
|
||||
Reference in New Issue
Block a user