mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-29 06:08:15 +02:00
Properly generate all wireguard options
This commit is contained in:
parent
261b3c7e31
commit
d78312fad8
12 changed files with 303 additions and 65 deletions
|
@ -1,7 +1,7 @@
|
|||
import { join } from "path";
|
||||
import { readFileSync } from "fs";
|
||||
import { db } from "@server/db";
|
||||
import { sites } from "./schema";
|
||||
import { exitNodes, sites } from "./schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { __DIRNAME } from "@server/config";
|
||||
|
||||
|
@ -9,7 +9,7 @@ import { __DIRNAME } from "@server/config";
|
|||
const file = join(__DIRNAME, "names.json");
|
||||
export const names = JSON.parse(readFileSync(file, "utf-8"));
|
||||
|
||||
export async function getUniqueName(orgId: string): Promise<string> {
|
||||
export async function getUniqueSiteName(orgId: string): Promise<string> {
|
||||
let loops = 0;
|
||||
while (true) {
|
||||
if (loops > 100) {
|
||||
|
@ -28,6 +28,30 @@ export async function getUniqueName(orgId: string): Promise<string> {
|
|||
}
|
||||
}
|
||||
|
||||
export async function getUniqueExitNodeEndpointName(): Promise<string> {
|
||||
let loops = 0;
|
||||
const count = await db
|
||||
.select()
|
||||
.from(exitNodes);
|
||||
while (true) {
|
||||
if (loops > 100) {
|
||||
throw new Error("Could not generate a unique name");
|
||||
}
|
||||
|
||||
const name = generateName();
|
||||
|
||||
for (const node of count) {
|
||||
if (node.endpoint.includes(name)) {
|
||||
loops++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function generateName(): string {
|
||||
return (
|
||||
names.descriptors[
|
||||
|
|
|
@ -13,12 +13,12 @@ export const sites = sqliteTable("sites", {
|
|||
onDelete: "cascade",
|
||||
}),
|
||||
niceId: text("niceId").notNull(),
|
||||
exitNode: integer("exitNode").references(() => exitNodes.exitNodeId, {
|
||||
exitNodeId: integer("exitNode").references(() => exitNodes.exitNodeId, {
|
||||
onDelete: "set null",
|
||||
}),
|
||||
name: text("name").notNull(),
|
||||
pubKey: text("pubKey"),
|
||||
subnet: text("subnet"),
|
||||
pubKey: text("pubKey").notNull(),
|
||||
subnet: text("subnet").notNull(),
|
||||
megabytesIn: integer("bytesIn"),
|
||||
megabytesOut: integer("bytesOut"),
|
||||
});
|
||||
|
@ -53,16 +53,9 @@ export const exitNodes = sqliteTable("exitNodes", {
|
|||
exitNodeId: integer("exitNodeId").primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull(),
|
||||
address: text("address").notNull(),
|
||||
endpoint: text("endpoint").notNull(),
|
||||
publicKey: text("pubicKey").notNull(),
|
||||
listenPort: integer("listenPort"),
|
||||
});
|
||||
|
||||
export const routes = sqliteTable("routes", {
|
||||
routeId: integer("routeId").primaryKey({ autoIncrement: true }),
|
||||
exitNodeId: integer("exitNodeId").references(() => exitNodes.exitNodeId, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
subnet: text("subnet").notNull(),
|
||||
listenPort: integer("listenPort").notNull(),
|
||||
});
|
||||
|
||||
export const users = sqliteTable("user", {
|
||||
|
@ -217,7 +210,6 @@ export type User = InferSelectModel<typeof users>;
|
|||
export type Site = InferSelectModel<typeof sites>;
|
||||
export type Resource = InferSelectModel<typeof resources>;
|
||||
export type ExitNode = InferSelectModel<typeof exitNodes>;
|
||||
export type Route = InferSelectModel<typeof routes>;
|
||||
export type Target = InferSelectModel<typeof targets>;
|
||||
export type Session = InferSelectModel<typeof sessions>;
|
||||
export type EmailVerificationCode = InferSelectModel<
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue