From 417b8542a7926f03f2f12ef7a3b794011b1f1444 Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Sat, 26 Oct 2024 17:02:11 -0400 Subject: [PATCH] Updated gerbil key handeling --- server/db/ensureActions.ts | 4 +++- server/db/names.ts | 8 ++++++- server/routers/gerbil/getConfig.ts | 26 +++++++++++------------ server/routers/internal.ts | 2 +- server/routers/resource/createResource.ts | 2 +- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/server/db/ensureActions.ts b/server/db/ensureActions.ts index 8db1e066..81ea5c68 100644 --- a/server/db/ensureActions.ts +++ b/server/db/ensureActions.ts @@ -20,9 +20,10 @@ export async function ensureActions() { // Add new actions for (const actionId of actionsToAdd) { + logger.debug(`Adding action: ${actionId}`); await db.insert(actions).values({ actionId }).execute(); // Add new actions to the Default role - if (defaultRoles.length === 0) { + if (defaultRoles.length != 0) { await db.insert(roleActions) .values(defaultRoles.map(role => ({ roleId: role.roleId!, actionId, orgId: role.orgId! }))) .execute(); @@ -31,6 +32,7 @@ export async function ensureActions() { // Remove deprecated actions if (actionsToRemove.length > 0) { + logger.debug(`Removing actions: ${actionsToRemove.join(', ')}`); await db.delete(actions).where(inArray(actions.actionId, actionsToRemove)).execute(); await db.delete(roleActions).where(inArray(roleActions.actionId, actionsToRemove)).execute(); } diff --git a/server/db/names.ts b/server/db/names.ts index 28fda9b8..440895f8 100644 --- a/server/db/names.ts +++ b/server/db/names.ts @@ -6,7 +6,13 @@ 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"); +const dev = process.env.ENVIRONMENT !== "prod"; +let file; +if (!dev) { + file = join(__DIRNAME, "names.json"); +} else { + file = join(__DIRNAME, "/db/names.json"); +} export const names = JSON.parse(readFileSync(file, "utf-8")); export async function getUniqueSiteName(orgId: string): Promise { diff --git a/server/routers/gerbil/getConfig.ts b/server/routers/gerbil/getConfig.ts index 6b41c303..41eedbee 100644 --- a/server/routers/gerbil/getConfig.ts +++ b/server/routers/gerbil/getConfig.ts @@ -27,7 +27,7 @@ export type GetConfigResponse = { export async function getConfig(req: Request, res: Response, next: NextFunction): Promise { try { // Validate request parameters - const parsedParams = getConfigSchema.safeParse(req.query); + const parsedParams = getConfigSchema.safeParse(req.body); if (!parsedParams.success) { return next( createHttpError( @@ -44,9 +44,9 @@ export async function getConfig(req: Request, res: Response, next: NextFunction) } // Fetch exit node - let exitNode = await db.select().from(exitNodes).where(eq(exitNodes.publicKey, publicKey)); - - if (!exitNode) { + let exitNodeQuery = await db.select().from(exitNodes).where(eq(exitNodes.publicKey, publicKey)); + let exitNode; + if (exitNodeQuery.length === 0) { const address = await getNextAvailableSubnet(); const listenPort = await getNextAvailablePort(); const subEndpoint = await getUniqueExitNodeEndpointName(); @@ -61,6 +61,8 @@ export async function getConfig(req: Request, res: Response, next: NextFunction) }).returning().execute(); logger.info(`Created new exit node ${exitNode[0].name} with address ${exitNode[0].address} and port ${exitNode[0].listenPort}`); + } else { + exitNode = exitNodeQuery; } if (!exitNode) { @@ -98,16 +100,11 @@ export async function getConfig(req: Request, res: Response, next: NextFunction) peers, }; - return response(res, { - data: configResponse, - success: true, - error: false, - message: "Configuration retrieved successfully", - status: HttpCode.OK, - }); + logger.debug("Sending config: ", configResponse); + return res.status(HttpCode.OK).send(configResponse); } catch (error) { - logger.error('Error from getConfig:', error); + logger.error(error); return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred...")); } } @@ -119,10 +116,13 @@ async function getNextAvailableSubnet(): Promise { }).from(exitNodes); const addresses = existingAddresses.map(a => a.address); - const subnet = findNextAvailableCidr(addresses, config.gerbil.block_size, config.gerbil.subnet_group); + let subnet = findNextAvailableCidr(addresses, config.gerbil.block_size, config.gerbil.subnet_group); if (!subnet) { throw new Error('No available subnets remaining in space'); } + + // replace the last octet with 1 + subnet = subnet.split('.').slice(0, 3).join('.') + '.1'; return subnet; } diff --git a/server/routers/internal.ts b/server/routers/internal.ts index 08bd2a21..d477e696 100644 --- a/server/routers/internal.ts +++ b/server/routers/internal.ts @@ -17,7 +17,7 @@ internalRouter.get("/traefik-config", traefik.traefikConfigProvider); const gerbilRouter = Router(); internalRouter.use("/gerbil", gerbilRouter); -gerbilRouter.get("/get-config", gerbil.getConfig); +gerbilRouter.post("/get-config", gerbil.getConfig); gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth); // Badger routes diff --git a/server/routers/resource/createResource.ts b/server/routers/resource/createResource.ts index 9b447ad1..cb054243 100644 --- a/server/routers/resource/createResource.ts +++ b/server/routers/resource/createResource.ts @@ -75,7 +75,7 @@ export async function createResource(req: Request, res: Response, next: NextFunc } // Generate a unique resourceId - const fullDomain = `${subdomain}.${org[0].orgId}.${org[0].domain}`; + const fullDomain = `${subdomain}.${org[0].domain}`; // Create new resource in the database const newResource = await db.insert(resources).values({