Seperate get relays

This commit is contained in:
Owen
2025-08-13 21:35:06 -07:00
parent 23079d9ac0
commit ac87345b7a

View File

@@ -1,6 +1,15 @@
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
import { z } from "zod"; import { z } from "zod";
import { clients, exitNodes, newts, olms, Site, sites, clientSites } from "@server/db"; import {
clients,
exitNodes,
newts,
olms,
Site,
sites,
clientSites,
ExitNode
} from "@server/db";
import { db } from "@server/db"; import { db } from "@server/db";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
@@ -10,7 +19,7 @@ import { fromError } from "zod-validation-error";
// Define Zod schema for request validation // Define Zod schema for request validation
const getAllRelaysSchema = z.object({ const getAllRelaysSchema = z.object({
publicKey: z.string().optional(), publicKey: z.string().optional()
}); });
// Type for peer destination // Type for peer destination
@@ -44,22 +53,48 @@ export async function getAllRelays(
const { publicKey } = parsedParams.data; const { publicKey } = parsedParams.data;
if (!publicKey) { if (!publicKey) {
return next(createHttpError(HttpCode.BAD_REQUEST, 'publicKey is required')); return next(
createHttpError(HttpCode.BAD_REQUEST, "publicKey is required")
);
} }
// Fetch exit node // Fetch exit node
const [exitNode] = await db.select().from(exitNodes).where(eq(exitNodes.publicKey, publicKey)); const [exitNode] = await db
.select()
.from(exitNodes)
.where(eq(exitNodes.publicKey, publicKey));
if (!exitNode) { if (!exitNode) {
return next(createHttpError(HttpCode.NOT_FOUND, "Exit node not found")); return next(
createHttpError(HttpCode.NOT_FOUND, "Exit node not found")
);
} }
const mappings = await generateRelayMappings(exitNode);
logger.debug(
`Returning mappings for ${Object.keys(mappings).length} endpoints`
);
return res.status(HttpCode.OK).send({ mappings });
} catch (error) {
logger.error(error);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred..."
)
);
}
}
export async function generateRelayMappings(exitNode: ExitNode) {
// Fetch sites for this exit node // Fetch sites for this exit node
const sitesRes = await db.select().from(sites).where(eq(sites.exitNodeId, exitNode.exitNodeId)); const sitesRes = await db
.select()
.from(sites)
.where(eq(sites.exitNodeId, exitNode.exitNodeId));
if (sitesRes.length === 0) { if (sitesRes.length === 0) {
return res.status(HttpCode.OK).send({ return {};
mappings: {}
});
} }
// Initialize mappings object for multi-peer support // Initialize mappings object for multi-peer support
@@ -95,7 +130,8 @@ export async function getAllRelays(
// Check if this destination is already in the array to avoid duplicates // Check if this destination is already in the array to avoid duplicates
const isDuplicate = mappings[clientSite.endpoint].destinations.some( const isDuplicate = mappings[clientSite.endpoint].destinations.some(
dest => dest.destinationIP === destination.destinationIP && (dest) =>
dest.destinationIP === destination.destinationIP &&
dest.destinationPort === destination.destinationPort dest.destinationPort === destination.destinationPort
); );
@@ -113,7 +149,12 @@ export async function getAllRelays(
for (const peer of orgSites) { for (const peer of orgSites) {
// Skip self // Skip self
if (peer.siteId === site.siteId || !peer.endpoint || !peer.subnet || !peer.listenPort) { if (
peer.siteId === site.siteId ||
!peer.endpoint ||
!peer.subnet ||
!peer.listenPort
) {
continue; continue;
} }
@@ -129,7 +170,8 @@ export async function getAllRelays(
// Check for duplicates // Check for duplicates
const isDuplicate = mappings[site.endpoint].destinations.some( const isDuplicate = mappings[site.endpoint].destinations.some(
dest => dest.destinationIP === destination.destinationIP && (dest) =>
dest.destinationIP === destination.destinationIP &&
dest.destinationPort === destination.destinationPort dest.destinationPort === destination.destinationPort
); );
@@ -140,15 +182,5 @@ export async function getAllRelays(
} }
} }
logger.debug(`Returning mappings for ${Object.keys(mappings).length} endpoints`); return mappings;
return res.status(HttpCode.OK).send({ mappings });
} catch (error) {
logger.error(error);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred..."
)
);
}
} }