This commit is contained in:
Owen Schwartz
2024-10-26 12:02:34 -04:00
33 changed files with 458 additions and 366 deletions

View File

@@ -1,27 +1,26 @@
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync } from 'fs';
import { db } from '@server/db';
import { sites } from './schema';
import { eq, and } from 'drizzle-orm';
// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { join } from "path";
import { readFileSync } from "fs";
import { db } from "@server/db";
import { sites } from "./schema";
import { eq, and } from "drizzle-orm";
import { __DIRNAME } from "@server/config";
// Load the names from the names.json file
const file = join(__dirname, 'names.json');
export const names = JSON.parse(readFileSync(file, 'utf-8'));
const file = join(__DIRNAME, "names.json");
export const names = JSON.parse(readFileSync(file, "utf-8"));
export async function getUniqueName(orgId: string): Promise<string> {
let loops = 0;
while (true) {
if (loops > 100) {
throw new Error('Could not generate a unique name');
throw new Error("Could not generate a unique name");
}
const name = generateName();
const count = await db.select({ niceId: sites.niceId, orgId: sites.orgId }).from(sites).where(and(eq(sites.niceId, name), eq(sites.orgId, orgId)));
const count = await db
.select({ niceId: sites.niceId, orgId: sites.orgId })
.from(sites)
.where(and(eq(sites.niceId, name), eq(sites.orgId, orgId)));
if (count.length === 0) {
return name;
}
@@ -31,7 +30,12 @@ export async function getUniqueName(orgId: string): Promise<string> {
export function generateName(): string {
return (
names.descriptors[Math.floor(Math.random() * names.descriptors.length)] + "-" +
names.descriptors[
Math.floor(Math.random() * names.descriptors.length)
] +
"-" +
names.animals[Math.floor(Math.random() * names.animals.length)]
).toLowerCase().replace(/\s/g, '-');
}
)
.toLowerCase()
.replace(/\s/g, "-");
}

View File

@@ -24,7 +24,8 @@ export const sites = sqliteTable("sites", {
});
export const resources = sqliteTable("resources", {
resourceId: text("resourceId", { length: 2048 }).primaryKey(),
resourceId: integer("resourceId").primaryKey({ autoIncrement: true }),
fullDomain: text("fullDomain", { length: 2048 }),
siteId: integer("siteId").references(() => sites.siteId, {
onDelete: "cascade",
}),
@@ -45,6 +46,7 @@ export const targets = sqliteTable("targets", {
port: integer("port").notNull(),
protocol: text("protocol"),
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
ssl: integer("ssl", { mode: "boolean" }).notNull().default(false),
});
export const exitNodes = sqliteTable("exitNodes", {