From e447549de13d942ed1eda3e6372c1b7370bb1a4f Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 21 Oct 2025 11:11:31 -0700 Subject: [PATCH] revert changes around sites assigned to exit nodes --- server/db/pg/schema/schema.ts | 2 +- server/db/sqlite/schema/schema.ts | 2 +- server/lib/traefik/getTraefikConfig.ts | 5 +- .../private/lib/traefik/getTraefikConfig.ts | 11 ++++- server/routers/site/createSite.ts | 49 +++---------------- server/setup/scriptsPg/1.11.1.ts | 25 ---------- server/setup/scriptsSqlite/1.11.1.ts | 26 ---------- 7 files changed, 24 insertions(+), 96 deletions(-) diff --git a/server/db/pg/schema/schema.ts b/server/db/pg/schema/schema.ts index 4bed23f8..2e307c5f 100644 --- a/server/db/pg/schema/schema.ts +++ b/server/db/pg/schema/schema.ts @@ -126,7 +126,7 @@ export const targets = pgTable("targets", { pathMatchType: text("pathMatchType"), // exact, prefix, regex rewritePath: text("rewritePath"), // if set, rewrites the path to this value before sending to the target rewritePathType: text("rewritePathType"), // exact, prefix, regex, stripPrefix - priority: integer("priority").default(100) + priority: integer("priority").notNull().default(100) }); export const targetHealthCheck = pgTable("targetHealthCheck", { diff --git a/server/db/sqlite/schema/schema.ts b/server/db/sqlite/schema/schema.ts index 3d6c6b0d..2c19a1c7 100644 --- a/server/db/sqlite/schema/schema.ts +++ b/server/db/sqlite/schema/schema.ts @@ -138,7 +138,7 @@ export const targets = sqliteTable("targets", { pathMatchType: text("pathMatchType"), // exact, prefix, regex rewritePath: text("rewritePath"), // if set, rewrites the path to this value before sending to the target rewritePathType: text("rewritePathType"), // exact, prefix, regex, stripPrefix - priority: integer("priority").default(100) + priority: integer("priority").notNull().default(100) }); export const targetHealthCheck = sqliteTable("targetHealthCheck", { diff --git a/server/lib/traefik/getTraefikConfig.ts b/server/lib/traefik/getTraefikConfig.ts index 75ea907f..6a013114 100644 --- a/server/lib/traefik/getTraefikConfig.ts +++ b/server/lib/traefik/getTraefikConfig.ts @@ -88,7 +88,10 @@ export async function getTraefikConfig( and( eq(targets.enabled, true), eq(resources.enabled, true), - eq(sites.exitNodeId, exitNodeId), + or( + eq(sites.exitNodeId, exitNodeId), + and(isNull(sites.exitNodeId), eq(sites.type, "local")) + ), or( ne(targetHealthCheck.hcHealth, "unhealthy"), // Exclude unhealthy targets isNull(targetHealthCheck.hcHealth) // Include targets with no health check record diff --git a/server/private/lib/traefik/getTraefikConfig.ts b/server/private/lib/traefik/getTraefikConfig.ts index 5e919fda..2ee3f774 100644 --- a/server/private/lib/traefik/getTraefikConfig.ts +++ b/server/private/lib/traefik/getTraefikConfig.ts @@ -40,6 +40,7 @@ import { CertificateResult, getValidCertificatesForDomains } from "#private/lib/certificates"; +import { build } from "@server/build"; const redirectHttpsMiddlewareName = "redirect-to-https"; const redirectToRootMiddlewareName = "redirect-to-root"; @@ -120,7 +121,15 @@ export async function getTraefikConfig( and( eq(targets.enabled, true), eq(resources.enabled, true), - eq(sites.exitNodeId, exitNodeId), + or( + eq(sites.exitNodeId, exitNodeId), + and( + build != "saas" // so it runs in enterprise + ? isNull(sites.exitNodeId) + : sql`0 = 1`, + eq(sites.type, "local") + ) + ), or( ne(targetHealthCheck.hcHealth, "unhealthy"), // Exclude unhealthy targets isNull(targetHealthCheck.hcHealth) // Include targets with no health check record diff --git a/server/routers/site/createSite.ts b/server/routers/site/createSite.ts index 36e049bc..f98a01dc 100644 --- a/server/routers/site/createSite.ts +++ b/server/routers/site/createSite.ts @@ -267,50 +267,10 @@ export async function createSite( }) .returning(); } else if (type == "local") { - let exitNodeIdToCreate = exitNodeId; - if (!exitNodeIdToCreate) { - if (build == "saas") { - return next( - createHttpError( - HttpCode.BAD_REQUEST, - "Exit node ID of a remote node is required for local sites" - ) - ); - } - - // select the exit node for local sites - // TODO: THIS SHOULD BE CHOSEN IN THE FRONTEND OR SOMETHING BECAUSE - // YOU CAN HAVE MORE THAN ONE NODE IN THE SYSTEM AND YOU SHOULD SELECT - // WHICH GERBIL NODE TO PUT THE SITE ON BUT FOR NOW THIS WILL DO - const [localExitNode] = await trx - .select() - .from(exitNodes) - .where(eq(exitNodes.type, "gerbil")) - .limit(1); - - if (!localExitNode) { - return next( - createHttpError( - HttpCode.BAD_REQUEST, - "No gerbil exit node found for organization. Please create a gerbil exit node first." - ) - ); - } - - exitNodeIdToCreate = localExitNode.exitNodeId; - } else { - return next( - createHttpError( - HttpCode.BAD_REQUEST, - "Site type not recognized" - ) - ); - } - [newSite] = await trx .insert(sites) .values({ - exitNodeId: exitNodeIdToCreate, + exitNodeId: exitNodeId || null, orgId, name, niceId, @@ -321,6 +281,13 @@ export async function createSite( subnet: "0.0.0.0/32" }) .returning(); + } else { + return next( + createHttpError( + HttpCode.BAD_REQUEST, + "Site type not recognized" + ) + ); } const adminRole = await trx diff --git a/server/setup/scriptsPg/1.11.1.ts b/server/setup/scriptsPg/1.11.1.ts index 79f5ff80..4fd5f3ba 100644 --- a/server/setup/scriptsPg/1.11.1.ts +++ b/server/setup/scriptsPg/1.11.1.ts @@ -9,31 +9,6 @@ export default async function migration() { try { await db.execute(sql`BEGIN`); - // Get the first exit node with type 'gerbil' - const exitNodesQuery = await db.execute( - sql`SELECT * FROM "exitNodes" WHERE "type" = 'gerbil' LIMIT 1` - ); - const exitNodes = exitNodesQuery.rows as { - exitNodeId: number; - }[]; - - const exitNodeId = exitNodes.length > 0 ? exitNodes[0].exitNodeId : null; - - // Get all sites with type 'local' - const sitesQuery = await db.execute( - sql`SELECT "siteId" FROM "sites" WHERE "type" = 'local'` - ); - const sites = sitesQuery.rows as { - siteId: number; - }[]; - - // Update sites to use the exit node - for (const site of sites) { - await db.execute(sql` - UPDATE "sites" SET "exitNode" = ${exitNodeId} WHERE "siteId" = ${site.siteId} - `); - } - await db.execute(sql`UPDATE "exitNodes" SET "online" = true`); // Mark exit nodes as online await db.execute(sql`COMMIT`); diff --git a/server/setup/scriptsSqlite/1.11.1.ts b/server/setup/scriptsSqlite/1.11.1.ts index d8b9b0d1..3aa4ec3d 100644 --- a/server/setup/scriptsSqlite/1.11.1.ts +++ b/server/setup/scriptsSqlite/1.11.1.ts @@ -11,32 +11,6 @@ export default async function migration() { const db = new Database(location); db.transaction(() => { - const exitNodes = db - .prepare(`SELECT * FROM exitNodes WHERE type = 'gerbil' LIMIT 1`) - .all() as { - exitNodeId: number; - name: string; - }[]; - - const exitNodeId = - exitNodes.length > 0 ? exitNodes[0].exitNodeId : null; - - // get all of the targets - const sites = db - .prepare(`SELECT * FROM sites WHERE type = 'local'`) - .all() as { - siteId: number; - exitNodeId: number | null; - }[]; - - const defineExitNodeOnSite = db.prepare( - `UPDATE sites SET exitNode = ? WHERE siteId = ?` - ); - - for (const site of sites) { - defineExitNodeOnSite.run(exitNodeId, site.siteId); - } - db.prepare(`UPDATE exitNodes SET online = 1`).run(); // mark exit nodes as online })();