Add exit node to create site

This commit is contained in:
Owen 2025-04-01 21:52:03 -04:00
parent fb5c428147
commit 8c267489c7
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD

View file

@ -8,7 +8,8 @@ import {
roleClients, roleClients,
userClients, userClients,
olms, olms,
clientSites clientSites,
exitNodes
} from "@server/db/schema"; } from "@server/db/schema";
import response from "@server/lib/response"; import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
@ -57,8 +58,7 @@ export async function createClient(
); );
} }
const { name, type, siteIds, olmId, secret } = const { name, type, siteIds, olmId, secret } = parsedBody.data;
parsedBody.data;
const parsedParams = createClientParamsSchema.safeParse(req.params); const parsedParams = createClientParamsSchema.safeParse(req.params);
if (!parsedParams.success) { if (!parsedParams.success) {
@ -80,15 +80,29 @@ export async function createClient(
const newSubnet = await getNextAvailableClientSubnet(orgId); const newSubnet = await getNextAvailableClientSubnet(orgId);
const subnet = `${newSubnet.split("/")[0]}/${config.getRawConfig().orgs.block_size}` // we want the block size of the whole org const subnet = `${newSubnet.split("/")[0]}/${config.getRawConfig().orgs.block_size}`; // we want the block size of the whole org
await db.transaction(async (trx) => { await db.transaction(async (trx) => {
// TODO: more intelligent way to pick the exit node
// make sure there is an exit node by counting the exit nodes table
const nodes = await db.select().from(exitNodes);
if (nodes.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
"No exit nodes available"
)
);
}
// get the first exit node
const exitNode = nodes[0];
const adminRole = await trx const adminRole = await trx
.select() .select()
.from(roles) .from(roles)
.where( .where(and(eq(roles.isAdmin, true), eq(roles.orgId, orgId)))
and(eq(roles.isAdmin, true), eq(roles.orgId, orgId))
)
.limit(1); .limit(1);
if (adminRole.length === 0) { if (adminRole.length === 0) {
@ -101,6 +115,7 @@ export async function createClient(
const [newClient] = await trx const [newClient] = await trx
.insert(clients) .insert(clients)
.values({ .values({
exitNodeId: exitNode.exitNodeId,
orgId, orgId,
name, name,
subnet, subnet,
@ -124,7 +139,7 @@ export async function createClient(
// Create site to client associations // Create site to client associations
if (siteIds && siteIds.length > 0) { if (siteIds && siteIds.length > 0) {
await trx.insert(clientSites).values( await trx.insert(clientSites).values(
siteIds.map(siteId => ({ siteIds.map((siteId) => ({
clientId: newClient.clientId, clientId: newClient.clientId,
siteId siteId
})) }))