mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-18 16:28:26 +02:00
Filter by exit node
This commit is contained in:
parent
8199202dc3
commit
cdfeb2ff86
1 changed files with 48 additions and 30 deletions
|
@ -1,11 +1,12 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { db } from "@server/db";
|
import { db, exitNodes } from "@server/db";
|
||||||
import { and, eq, inArray } from "drizzle-orm";
|
import { and, eq, inArray } from "drizzle-orm";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import { orgs, resources, sites, Target, targets } from "@server/db";
|
import { orgs, resources, sites, Target, targets } from "@server/db";
|
||||||
import { sql } from "drizzle-orm";
|
|
||||||
|
let currentExitNodeName: string;
|
||||||
|
|
||||||
export async function traefikConfigProvider(
|
export async function traefikConfigProvider(
|
||||||
_: Request,
|
_: Request,
|
||||||
|
@ -15,6 +16,24 @@ export async function traefikConfigProvider(
|
||||||
// Get all resources with related data
|
// Get all resources with related data
|
||||||
const allResources = await db.transaction(async (tx) => {
|
const allResources = await db.transaction(async (tx) => {
|
||||||
// First query to get resources with site and org info
|
// First query to get resources with site and org info
|
||||||
|
// Get the current exit node name from config
|
||||||
|
if (config.getRawConfig().gerbil.exit_node_name) {
|
||||||
|
currentExitNodeName =
|
||||||
|
config.getRawConfig().gerbil.exit_node_name!;
|
||||||
|
} else {
|
||||||
|
const [exitNode] = await tx
|
||||||
|
.select({
|
||||||
|
name: exitNodes.name
|
||||||
|
})
|
||||||
|
.from(exitNodes);
|
||||||
|
if (!exitNode) {
|
||||||
|
logger.error("No exit node found in the database");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
currentExitNodeName = exitNode.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the site(s) on this exit node
|
||||||
const resourcesWithRelations = await tx
|
const resourcesWithRelations = await tx
|
||||||
.select({
|
.select({
|
||||||
// Resource fields
|
// Resource fields
|
||||||
|
@ -47,7 +66,8 @@ export async function traefikConfigProvider(
|
||||||
})
|
})
|
||||||
.from(resources)
|
.from(resources)
|
||||||
.innerJoin(sites, eq(sites.siteId, resources.siteId))
|
.innerJoin(sites, eq(sites.siteId, resources.siteId))
|
||||||
.innerJoin(orgs, eq(resources.orgId, orgs.orgId));
|
.innerJoin(orgs, eq(resources.orgId, orgs.orgId))
|
||||||
|
.where(eq(sites.name, currentExitNodeName));
|
||||||
|
|
||||||
// Get all resource IDs from the first query
|
// Get all resource IDs from the first query
|
||||||
const resourceIds = resourcesWithRelations.map((r) => r.resourceId);
|
const resourceIds = resourcesWithRelations.map((r) => r.resourceId);
|
||||||
|
@ -192,14 +212,9 @@ export async function traefikConfigProvider(
|
||||||
|
|
||||||
const configDomain = config.getDomain(resource.domainId);
|
const configDomain = config.getDomain(resource.domainId);
|
||||||
|
|
||||||
if (!configDomain) {
|
let tls = {};
|
||||||
logger.error(
|
if (configDomain) {
|
||||||
`Failed to get domain from config for resource ${resource.resourceId}`
|
tls = {
|
||||||
);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tls = {
|
|
||||||
certResolver: configDomain.cert_resolver,
|
certResolver: configDomain.cert_resolver,
|
||||||
...(configDomain.prefer_wildcard_cert
|
...(configDomain.prefer_wildcard_cert
|
||||||
? {
|
? {
|
||||||
|
@ -211,6 +226,7 @@ export async function traefikConfigProvider(
|
||||||
}
|
}
|
||||||
: {})
|
: {})
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const additionalMiddlewares =
|
const additionalMiddlewares =
|
||||||
config.getRawConfig().traefik.additional_middlewares || [];
|
config.getRawConfig().traefik.additional_middlewares || [];
|
||||||
|
@ -311,7 +327,9 @@ export async function traefikConfigProvider(
|
||||||
// if defined in the static config and here. if not set, self-signed certs won't work
|
// if defined in the static config and here. if not set, self-signed certs won't work
|
||||||
insecureSkipVerify: true
|
insecureSkipVerify: true
|
||||||
};
|
};
|
||||||
config_output.http.services![serviceName].loadBalancer.serversTransport = transportName;
|
config_output.http.services![
|
||||||
|
serviceName
|
||||||
|
].loadBalancer.serversTransport = transportName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the host header middleware
|
// Add the host header middleware
|
||||||
|
@ -319,8 +337,7 @@ export async function traefikConfigProvider(
|
||||||
if (!config_output.http.middlewares) {
|
if (!config_output.http.middlewares) {
|
||||||
config_output.http.middlewares = {};
|
config_output.http.middlewares = {};
|
||||||
}
|
}
|
||||||
config_output.http.middlewares[hostHeaderMiddlewareName] =
|
config_output.http.middlewares[hostHeaderMiddlewareName] = {
|
||||||
{
|
|
||||||
headers: {
|
headers: {
|
||||||
customRequestHeaders: {
|
customRequestHeaders: {
|
||||||
Host: resource.setHostHeader
|
Host: resource.setHostHeader
|
||||||
|
@ -328,7 +345,8 @@ export async function traefikConfigProvider(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (!config_output.http.routers![routerName].middlewares) {
|
if (!config_output.http.routers![routerName].middlewares) {
|
||||||
config_output.http.routers![routerName].middlewares = [];
|
config_output.http.routers![routerName].middlewares =
|
||||||
|
[];
|
||||||
}
|
}
|
||||||
config_output.http.routers![routerName].middlewares = [
|
config_output.http.routers![routerName].middlewares = [
|
||||||
...config_output.http.routers![routerName].middlewares,
|
...config_output.http.routers![routerName].middlewares,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue