Skip to content

Commit e447549

Browse files
revert changes around sites assigned to exit nodes
1 parent e77909d commit e447549

File tree

7 files changed

+24
-96
lines changed

7 files changed

+24
-96
lines changed

server/db/pg/schema/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const targets = pgTable("targets", {
126126
pathMatchType: text("pathMatchType"), // exact, prefix, regex
127127
rewritePath: text("rewritePath"), // if set, rewrites the path to this value before sending to the target
128128
rewritePathType: text("rewritePathType"), // exact, prefix, regex, stripPrefix
129-
priority: integer("priority").default(100)
129+
priority: integer("priority").notNull().default(100)
130130
});
131131

132132
export const targetHealthCheck = pgTable("targetHealthCheck", {

server/db/sqlite/schema/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const targets = sqliteTable("targets", {
138138
pathMatchType: text("pathMatchType"), // exact, prefix, regex
139139
rewritePath: text("rewritePath"), // if set, rewrites the path to this value before sending to the target
140140
rewritePathType: text("rewritePathType"), // exact, prefix, regex, stripPrefix
141-
priority: integer("priority").default(100)
141+
priority: integer("priority").notNull().default(100)
142142
});
143143

144144
export const targetHealthCheck = sqliteTable("targetHealthCheck", {

server/lib/traefik/getTraefikConfig.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ export async function getTraefikConfig(
8888
and(
8989
eq(targets.enabled, true),
9090
eq(resources.enabled, true),
91-
eq(sites.exitNodeId, exitNodeId),
91+
or(
92+
eq(sites.exitNodeId, exitNodeId),
93+
and(isNull(sites.exitNodeId), eq(sites.type, "local"))
94+
),
9295
or(
9396
ne(targetHealthCheck.hcHealth, "unhealthy"), // Exclude unhealthy targets
9497
isNull(targetHealthCheck.hcHealth) // Include targets with no health check record

server/private/lib/traefik/getTraefikConfig.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
CertificateResult,
4141
getValidCertificatesForDomains
4242
} from "#private/lib/certificates";
43+
import { build } from "@server/build";
4344

4445
const redirectHttpsMiddlewareName = "redirect-to-https";
4546
const redirectToRootMiddlewareName = "redirect-to-root";
@@ -120,7 +121,15 @@ export async function getTraefikConfig(
120121
and(
121122
eq(targets.enabled, true),
122123
eq(resources.enabled, true),
123-
eq(sites.exitNodeId, exitNodeId),
124+
or(
125+
eq(sites.exitNodeId, exitNodeId),
126+
and(
127+
build != "saas" // so it runs in enterprise
128+
? isNull(sites.exitNodeId)
129+
: sql`0 = 1`,
130+
eq(sites.type, "local")
131+
)
132+
),
124133
or(
125134
ne(targetHealthCheck.hcHealth, "unhealthy"), // Exclude unhealthy targets
126135
isNull(targetHealthCheck.hcHealth) // Include targets with no health check record

server/routers/site/createSite.ts

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -267,50 +267,10 @@ export async function createSite(
267267
})
268268
.returning();
269269
} else if (type == "local") {
270-
let exitNodeIdToCreate = exitNodeId;
271-
if (!exitNodeIdToCreate) {
272-
if (build == "saas") {
273-
return next(
274-
createHttpError(
275-
HttpCode.BAD_REQUEST,
276-
"Exit node ID of a remote node is required for local sites"
277-
)
278-
);
279-
}
280-
281-
// select the exit node for local sites
282-
// TODO: THIS SHOULD BE CHOSEN IN THE FRONTEND OR SOMETHING BECAUSE
283-
// YOU CAN HAVE MORE THAN ONE NODE IN THE SYSTEM AND YOU SHOULD SELECT
284-
// WHICH GERBIL NODE TO PUT THE SITE ON BUT FOR NOW THIS WILL DO
285-
const [localExitNode] = await trx
286-
.select()
287-
.from(exitNodes)
288-
.where(eq(exitNodes.type, "gerbil"))
289-
.limit(1);
290-
291-
if (!localExitNode) {
292-
return next(
293-
createHttpError(
294-
HttpCode.BAD_REQUEST,
295-
"No gerbil exit node found for organization. Please create a gerbil exit node first."
296-
)
297-
);
298-
}
299-
300-
exitNodeIdToCreate = localExitNode.exitNodeId;
301-
} else {
302-
return next(
303-
createHttpError(
304-
HttpCode.BAD_REQUEST,
305-
"Site type not recognized"
306-
)
307-
);
308-
}
309-
310270
[newSite] = await trx
311271
.insert(sites)
312272
.values({
313-
exitNodeId: exitNodeIdToCreate,
273+
exitNodeId: exitNodeId || null,
314274
orgId,
315275
name,
316276
niceId,
@@ -321,6 +281,13 @@ export async function createSite(
321281
subnet: "0.0.0.0/32"
322282
})
323283
.returning();
284+
} else {
285+
return next(
286+
createHttpError(
287+
HttpCode.BAD_REQUEST,
288+
"Site type not recognized"
289+
)
290+
);
324291
}
325292

326293
const adminRole = await trx

server/setup/scriptsPg/1.11.1.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,6 @@ export default async function migration() {
99
try {
1010
await db.execute(sql`BEGIN`);
1111

12-
// Get the first exit node with type 'gerbil'
13-
const exitNodesQuery = await db.execute(
14-
sql`SELECT * FROM "exitNodes" WHERE "type" = 'gerbil' LIMIT 1`
15-
);
16-
const exitNodes = exitNodesQuery.rows as {
17-
exitNodeId: number;
18-
}[];
19-
20-
const exitNodeId = exitNodes.length > 0 ? exitNodes[0].exitNodeId : null;
21-
22-
// Get all sites with type 'local'
23-
const sitesQuery = await db.execute(
24-
sql`SELECT "siteId" FROM "sites" WHERE "type" = 'local'`
25-
);
26-
const sites = sitesQuery.rows as {
27-
siteId: number;
28-
}[];
29-
30-
// Update sites to use the exit node
31-
for (const site of sites) {
32-
await db.execute(sql`
33-
UPDATE "sites" SET "exitNode" = ${exitNodeId} WHERE "siteId" = ${site.siteId}
34-
`);
35-
}
36-
3712
await db.execute(sql`UPDATE "exitNodes" SET "online" = true`); // Mark exit nodes as online
3813

3914
await db.execute(sql`COMMIT`);

server/setup/scriptsSqlite/1.11.1.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,6 @@ export default async function migration() {
1111
const db = new Database(location);
1212

1313
db.transaction(() => {
14-
const exitNodes = db
15-
.prepare(`SELECT * FROM exitNodes WHERE type = 'gerbil' LIMIT 1`)
16-
.all() as {
17-
exitNodeId: number;
18-
name: string;
19-
}[];
20-
21-
const exitNodeId =
22-
exitNodes.length > 0 ? exitNodes[0].exitNodeId : null;
23-
24-
// get all of the targets
25-
const sites = db
26-
.prepare(`SELECT * FROM sites WHERE type = 'local'`)
27-
.all() as {
28-
siteId: number;
29-
exitNodeId: number | null;
30-
}[];
31-
32-
const defineExitNodeOnSite = db.prepare(
33-
`UPDATE sites SET exitNode = ? WHERE siteId = ?`
34-
);
35-
36-
for (const site of sites) {
37-
defineExitNodeOnSite.run(exitNodeId, site.siteId);
38-
}
39-
4014
db.prepare(`UPDATE exitNodes SET online = 1`).run(); // mark exit nodes as online
4115
})();
4216

0 commit comments

Comments
 (0)