mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-01 00:06:38 +00:00
Seperate get gerbil config
This commit is contained in:
@@ -8,6 +8,7 @@ import { addPeer, deletePeer } from "./routers/gerbil/peers";
|
|||||||
import { db, exitNodes } from "./db";
|
import { db, exitNodes } from "./db";
|
||||||
import { TraefikConfigManager } from "./lib/remoteTraefikConfig";
|
import { TraefikConfigManager } from "./lib/remoteTraefikConfig";
|
||||||
import { tokenManager } from "./lib/tokenManager";
|
import { tokenManager } from "./lib/tokenManager";
|
||||||
|
import { APP_VERSION } from "./lib/consts";
|
||||||
|
|
||||||
export async function createHybridClientServer() {
|
export async function createHybridClientServer() {
|
||||||
const monitor = new TraefikConfigManager();
|
const monitor = new TraefikConfigManager();
|
||||||
@@ -67,6 +68,9 @@ export async function createHybridClientServer() {
|
|||||||
// Listen to connection events
|
// Listen to connection events
|
||||||
client.on("connect", () => {
|
client.on("connect", () => {
|
||||||
console.log("Connected to WebSocket server");
|
console.log("Connected to WebSocket server");
|
||||||
|
client.sendMessage("remoteExitNode/register", {
|
||||||
|
remoteExitNodeVersion: APP_VERSION
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("disconnect", () => {
|
client.on("disconnect", () => {
|
||||||
|
|||||||
@@ -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 { sites, resources, targets, exitNodes } from "@server/db";
|
import { sites, resources, targets, exitNodes, ExitNode } from "@server/db";
|
||||||
import { db } from "@server/db";
|
import { db } from "@server/db";
|
||||||
import { eq, isNotNull, and } from "drizzle-orm";
|
import { eq, isNotNull, and } from "drizzle-orm";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
@@ -11,6 +11,7 @@ import { getUniqueExitNodeEndpointName } from "../../db/names";
|
|||||||
import { findNextAvailableCidr } from "@server/lib/ip";
|
import { findNextAvailableCidr } from "@server/lib/ip";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { getAllowedIps } from "../target/helpers";
|
import { getAllowedIps } from "../target/helpers";
|
||||||
|
import { proxyToRemote } from "@server/lib/remoteProxy";
|
||||||
// Define Zod schema for request validation
|
// Define Zod schema for request validation
|
||||||
const getConfigSchema = z.object({
|
const getConfigSchema = z.object({
|
||||||
publicKey: z.string(),
|
publicKey: z.string(),
|
||||||
@@ -101,42 +102,12 @@ export async function getConfig(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const sitesRes = await db
|
// STOP HERE IN HYBRID MODE
|
||||||
.select()
|
if (config.isHybridMode()) {
|
||||||
.from(sites)
|
return proxyToRemote(req, res, next, "gerbil/get-config");
|
||||||
.where(
|
}
|
||||||
and(
|
|
||||||
eq(sites.exitNodeId, exitNode[0].exitNodeId),
|
|
||||||
isNotNull(sites.pubKey),
|
|
||||||
isNotNull(sites.subnet)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const peers = await Promise.all(
|
const configResponse = await generateGerbilConfig(exitNode[0]);
|
||||||
sitesRes.map(async (site) => {
|
|
||||||
if (site.type === "wireguard") {
|
|
||||||
return {
|
|
||||||
publicKey: site.pubKey,
|
|
||||||
allowedIps: await getAllowedIps(site.siteId)
|
|
||||||
};
|
|
||||||
} else if (site.type === "newt") {
|
|
||||||
return {
|
|
||||||
publicKey: site.pubKey,
|
|
||||||
allowedIps: [site.subnet!]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
publicKey: null,
|
|
||||||
allowedIps: []
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
const configResponse: GetConfigResponse = {
|
|
||||||
listenPort: exitNode[0].listenPort || 51820,
|
|
||||||
ipAddress: exitNode[0].address,
|
|
||||||
peers
|
|
||||||
};
|
|
||||||
|
|
||||||
logger.debug("Sending config: ", configResponse);
|
logger.debug("Sending config: ", configResponse);
|
||||||
|
|
||||||
@@ -152,6 +123,47 @@ export async function getConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function generateGerbilConfig(exitNode: ExitNode) {
|
||||||
|
const sitesRes = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(sites.exitNodeId, exitNode.exitNodeId),
|
||||||
|
isNotNull(sites.pubKey),
|
||||||
|
isNotNull(sites.subnet)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
const peers = await Promise.all(
|
||||||
|
sitesRes.map(async (site) => {
|
||||||
|
if (site.type === "wireguard") {
|
||||||
|
return {
|
||||||
|
publicKey: site.pubKey,
|
||||||
|
allowedIps: await getAllowedIps(site.siteId)
|
||||||
|
};
|
||||||
|
} else if (site.type === "newt") {
|
||||||
|
return {
|
||||||
|
publicKey: site.pubKey,
|
||||||
|
allowedIps: [site.subnet!]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
publicKey: null,
|
||||||
|
allowedIps: []
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const configResponse: GetConfigResponse = {
|
||||||
|
listenPort: exitNode.listenPort || 51820,
|
||||||
|
ipAddress: exitNode.address,
|
||||||
|
peers
|
||||||
|
};
|
||||||
|
|
||||||
|
return configResponse;
|
||||||
|
}
|
||||||
|
|
||||||
async function getNextAvailableSubnet(): Promise<string> {
|
async function getNextAvailableSubnet(): Promise<string> {
|
||||||
// Get all existing subnets from routes table
|
// Get all existing subnets from routes table
|
||||||
const existingAddresses = await db
|
const existingAddresses = await db
|
||||||
|
|||||||
@@ -54,10 +54,6 @@ internalRouter.use("/gerbil", gerbilRouter);
|
|||||||
if (config.isHybridMode()) {
|
if (config.isHybridMode()) {
|
||||||
// Use proxy router to forward requests to remote cloud server
|
// Use proxy router to forward requests to remote cloud server
|
||||||
// Proxy endpoints for each gerbil route
|
// Proxy endpoints for each gerbil route
|
||||||
gerbilRouter.post("/get-config", (req, res, next) =>
|
|
||||||
proxyToRemote(req, res, next, "gerbil/get-config")
|
|
||||||
);
|
|
||||||
|
|
||||||
gerbilRouter.post("/receive-bandwidth", (req, res, next) =>
|
gerbilRouter.post("/receive-bandwidth", (req, res, next) =>
|
||||||
proxyToRemote(req, res, next, "gerbil/receive-bandwidth")
|
proxyToRemote(req, res, next, "gerbil/receive-bandwidth")
|
||||||
);
|
);
|
||||||
@@ -69,6 +65,9 @@ if (config.isHybridMode()) {
|
|||||||
gerbilRouter.post("/get-all-relays", (req, res, next) =>
|
gerbilRouter.post("/get-all-relays", (req, res, next) =>
|
||||||
proxyToRemote(req, res, next, "gerbil/get-all-relays")
|
proxyToRemote(req, res, next, "gerbil/get-all-relays")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// GET CONFIG IS HANDLED IN THE ORIGINAL HANDLER
|
||||||
|
// SO IT CAN REGISTER THE LOCAL EXIT NODE
|
||||||
} else {
|
} else {
|
||||||
// Use local gerbil endpoints
|
// Use local gerbil endpoints
|
||||||
gerbilRouter.post("/get-config", gerbil.getConfig);
|
gerbilRouter.post("/get-config", gerbil.getConfig);
|
||||||
|
|||||||
Reference in New Issue
Block a user