mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-31 16:14:46 +02:00
Start new relay method
This commit is contained in:
parent
410207f3ca
commit
68ebdda1ff
2 changed files with 204 additions and 41 deletions
|
@ -1,6 +1,6 @@
|
||||||
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 } from "@server/db/schema";
|
import { clients, exitNodes, newts, olms, Site, sites, clientSites } from "@server/db/schema";
|
||||||
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";
|
||||||
|
@ -13,6 +13,17 @@ const getAllRelaysSchema = z.object({
|
||||||
publicKey: z.string().optional(),
|
publicKey: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Type for peer destination
|
||||||
|
interface PeerDestination {
|
||||||
|
destinationIP: string;
|
||||||
|
destinationPort: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Updated mappings type to support multiple destinations per endpoint
|
||||||
|
interface ProxyMapping {
|
||||||
|
destinations: PeerDestination[];
|
||||||
|
}
|
||||||
|
|
||||||
export async function getAllRelays(
|
export async function getAllRelays(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
|
@ -46,38 +57,96 @@ export async function getAllRelays(
|
||||||
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 {
|
return res.status(HttpCode.OK).send({
|
||||||
mappings: {}
|
mappings: {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize mappings object for multi-peer support
|
||||||
|
let mappings: { [key: string]: ProxyMapping } = {};
|
||||||
|
|
||||||
|
// Process each site
|
||||||
|
for (const site of sitesRes) {
|
||||||
|
if (!site.endpoint || !site.subnet || !site.listenPort) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find all clients associated with this site through clientSites
|
||||||
|
const clientSitesRes = await db
|
||||||
|
.select()
|
||||||
|
.from(clientSites)
|
||||||
|
.where(eq(clientSites.siteId, site.siteId));
|
||||||
|
|
||||||
|
for (const clientSite of clientSitesRes) {
|
||||||
|
// Get client information
|
||||||
|
const [client] = await db
|
||||||
|
.select()
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.clientId, clientSite.clientId));
|
||||||
|
|
||||||
|
if (!client || !client.endpoint) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add this site as a destination for the client
|
||||||
|
if (!mappings[client.endpoint]) {
|
||||||
|
mappings[client.endpoint] = { destinations: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add site as a destination for this client
|
||||||
|
const destination: PeerDestination = {
|
||||||
|
destinationIP: site.subnet.split("/")[0],
|
||||||
|
destinationPort: site.listenPort
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if this destination is already in the array to avoid duplicates
|
||||||
|
const isDuplicate = mappings[client.endpoint].destinations.some(
|
||||||
|
dest => dest.destinationIP === destination.destinationIP &&
|
||||||
|
dest.destinationPort === destination.destinationPort
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isDuplicate) {
|
||||||
|
mappings[client.endpoint].destinations.push(destination);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// // get the clients on each site and map them to the site
|
// Also handle site-to-site communication (all sites in the same org)
|
||||||
// const sitesAndClients = await Promise.all(sitesRes.map(async (site) => {
|
if (site.orgId) {
|
||||||
// const clientsRes = await db.select().from(clients).where(eq(clients.siteId, site.siteId));
|
const orgSites = await db
|
||||||
// return {
|
.select()
|
||||||
// site,
|
.from(sites)
|
||||||
// clients: clientsRes
|
.where(eq(sites.orgId, site.orgId));
|
||||||
// };
|
|
||||||
// }));
|
|
||||||
|
|
||||||
let mappings: { [key: string]: {
|
for (const peer of orgSites) {
|
||||||
destinationIp: string;
|
// Skip self
|
||||||
destinationPort: number;
|
if (peer.siteId === site.siteId || !peer.endpoint || !peer.subnet || !peer.listenPort) {
|
||||||
} } = {};
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// for (const siteAndClients of sitesAndClients) {
|
// Add peer site as a destination for this site
|
||||||
// const { site, clients } = siteAndClients;
|
if (!mappings[site.endpoint]) {
|
||||||
// for (const client of clients) {
|
mappings[site.endpoint] = { destinations: [] };
|
||||||
// if (!client.endpoint || !site.endpoint || !site.subnet) {
|
}
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// mappings[client.endpoint] = {
|
|
||||||
// destinationIp: site.subnet.split("/")[0],
|
|
||||||
// destinationPort: parseInt(site.endpoint.split(":")[1])
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
const destination: PeerDestination = {
|
||||||
|
destinationIP: peer.subnet.split("/")[0],
|
||||||
|
destinationPort: peer.listenPort
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check for duplicates
|
||||||
|
const isDuplicate = mappings[site.endpoint].destinations.some(
|
||||||
|
dest => dest.destinationIP === destination.destinationIP &&
|
||||||
|
dest.destinationPort === destination.destinationPort
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isDuplicate) {
|
||||||
|
mappings[site.endpoint].destinations.push(destination);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug(`Returning mappings for ${Object.keys(mappings).length} endpoints`);
|
||||||
return res.status(HttpCode.OK).send({ mappings });
|
return res.status(HttpCode.OK).send({ mappings });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { clients, newts, olms, Site, sites } from "@server/db/schema";
|
import { clients, newts, olms, Site, sites, clientSites } from "@server/db/schema";
|
||||||
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";
|
||||||
|
@ -20,6 +20,12 @@ const updateHolePunchSchema = z.object({
|
||||||
timestamp: z.number()
|
timestamp: z.number()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// New response type with multi-peer destination support
|
||||||
|
interface PeerDestination {
|
||||||
|
destinationIP: string;
|
||||||
|
destinationPort: number;
|
||||||
|
}
|
||||||
|
|
||||||
export async function updateHolePunch(
|
export async function updateHolePunch(
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
|
@ -41,7 +47,8 @@ export async function updateHolePunch(
|
||||||
|
|
||||||
// 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;
|
let currentSiteId: number | undefined;
|
||||||
|
let destinations: PeerDestination[] = [];
|
||||||
|
|
||||||
if (olmId) {
|
if (olmId) {
|
||||||
const { session, olm: olmSession } =
|
const { session, olm: olmSession } =
|
||||||
|
@ -80,10 +87,42 @@ export async function updateHolePunch(
|
||||||
.where(eq(clients.clientId, olm.clientId))
|
.where(eq(clients.clientId, olm.clientId))
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
// [site] = await db
|
if (!client) {
|
||||||
// .select()
|
logger.warn(`Client not found for olm: ${olmId}`);
|
||||||
// .from(sites)
|
return next(
|
||||||
// .where(eq(sites.siteId, client.siteId));
|
createHttpError(HttpCode.NOT_FOUND, "Client not found")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all sites that this client is connected to
|
||||||
|
const clientSitePairs = await db
|
||||||
|
.select()
|
||||||
|
.from(clientSites)
|
||||||
|
.where(eq(clientSites.clientId, client.clientId));
|
||||||
|
|
||||||
|
if (clientSitePairs.length === 0) {
|
||||||
|
logger.warn(`No sites found for client: ${client.clientId}`);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.NOT_FOUND, "No sites found for client")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all sites details
|
||||||
|
const siteIds = clientSitePairs.map(pair => pair.siteId);
|
||||||
|
|
||||||
|
for (const siteId of siteIds) {
|
||||||
|
const [site] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, siteId));
|
||||||
|
|
||||||
|
if (site && site.subnet && site.listenPort) {
|
||||||
|
destinations.push({
|
||||||
|
destinationIP: site.subnet.split("/")[0],
|
||||||
|
destinationPort: site.listenPort
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else if (newtId) {
|
} else if (newtId) {
|
||||||
const { session, newt: newtSession } =
|
const { session, newt: newtSession } =
|
||||||
|
@ -114,7 +153,10 @@ export async function updateHolePunch(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
[site] = await db
|
currentSiteId = newt.siteId;
|
||||||
|
|
||||||
|
// Update the current site with the new endpoint
|
||||||
|
const [updatedSite] = await db
|
||||||
.update(sites)
|
.update(sites)
|
||||||
.set({
|
.set({
|
||||||
endpoint: `${ip}:${port}`,
|
endpoint: `${ip}:${port}`,
|
||||||
|
@ -122,19 +164,71 @@ export async function updateHolePunch(
|
||||||
})
|
})
|
||||||
.where(eq(sites.siteId, newt.siteId))
|
.where(eq(sites.siteId, newt.siteId))
|
||||||
.returning();
|
.returning();
|
||||||
}
|
|
||||||
|
|
||||||
if (!site || !site.endpoint || !site.subnet) {
|
if (!updatedSite || !updatedSite.subnet) {
|
||||||
logger.warn(
|
logger.warn(`Site not found: ${newt.siteId}`);
|
||||||
`Site not found for olmId: ${olmId} or newtId: ${newtId}`
|
return next(
|
||||||
|
createHttpError(HttpCode.NOT_FOUND, "Site not found")
|
||||||
);
|
);
|
||||||
return next(createHttpError(HttpCode.NOT_FOUND, "Site not found"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(HttpCode.OK).send({
|
// Find all clients that connect to this site
|
||||||
destinationIp: site.subnet.split("/")[0],
|
const sitesClientPairs = await db
|
||||||
|
.select()
|
||||||
|
.from(clientSites)
|
||||||
|
.where(eq(clientSites.siteId, newt.siteId));
|
||||||
|
|
||||||
|
// Get client details for each client
|
||||||
|
for (const pair of sitesClientPairs) {
|
||||||
|
const [client] = await db
|
||||||
|
.select()
|
||||||
|
.from(clients)
|
||||||
|
.where(eq(clients.clientId, pair.clientId));
|
||||||
|
|
||||||
|
if (client && client.endpoint) {
|
||||||
|
const [host, portStr] = client.endpoint.split(':');
|
||||||
|
if (host && portStr) {
|
||||||
|
destinations.push({
|
||||||
|
destinationIP: host,
|
||||||
|
destinationPort: parseInt(portStr, 10)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is a newt/site, also add other sites in the same org
|
||||||
|
if (updatedSite.orgId) {
|
||||||
|
const orgSites = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.orgId, updatedSite.orgId));
|
||||||
|
|
||||||
|
for (const site of orgSites) {
|
||||||
|
// Don't add the current site to the destinations
|
||||||
|
if (site.siteId !== currentSiteId && site.subnet && site.endpoint && site.listenPort) {
|
||||||
|
const [host, portStr] = site.endpoint.split(':');
|
||||||
|
if (host && portStr) {
|
||||||
|
destinations.push({
|
||||||
|
destinationIP: host,
|
||||||
destinationPort: site.listenPort
|
destinationPort: site.listenPort
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destinations.length === 0) {
|
||||||
|
logger.warn(
|
||||||
|
`No peer destinations found for olmId: ${olmId} or newtId: ${newtId}`
|
||||||
|
);
|
||||||
|
return next(createHttpError(HttpCode.NOT_FOUND, "No peer destinations found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the new multi-peer structure
|
||||||
|
return res.status(HttpCode.OK).send({
|
||||||
|
destinations: destinations
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
return next(
|
return next(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue