Allow picking ips when creating stuff

This commit is contained in:
Owen
2025-04-18 14:41:27 -04:00
parent d664aa204f
commit 581fdd67b1
9 changed files with 212 additions and 156 deletions

View File

@@ -80,7 +80,7 @@ export async function createClient(
);
}
if (subnet && !isValidIP(subnet)) {
if (!isValidIP(subnet)) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
@@ -103,7 +103,7 @@ export async function createClient(
);
}
if (subnet && !isIpInCidr(subnet, org.subnet)) {
if (!isIpInCidr(subnet, org.subnet)) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
@@ -114,6 +114,22 @@ export async function createClient(
const updatedSubnet = `${subnet}/${org.subnet.split("/")[1]}`; // we want the block size of the whole org
// make sure the subnet is unique
const subnetExists = await db
.select()
.from(clients)
.where(eq(clients.subnet, updatedSubnet))
.limit(1);
if (subnetExists.length > 0) {
return next(
createHttpError(
HttpCode.CONFLICT,
`Subnet ${subnet} already exists`
)
);
}
await db.transaction(async (trx) => {
// TODO: more intelligent way to pick the exit node

View File

@@ -5,7 +5,6 @@ import createHttpError from "http-errors";
import logger from "@server/logger";
import { generateId } from "@server/auth/sessions/app";
import { getNextAvailableClientSubnet } from "@server/lib/ip";
import config from "@server/lib/config";
import { z } from "zod";
import { fromError } from "zod-validation-error";
@@ -43,6 +42,14 @@ export async function pickClientDefaults(
const secret = generateId(48);
const newSubnet = await getNextAvailableClientSubnet(orgId);
if (!newSubnet) {
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"No available subnet found"
)
);
}
const subnet = newSubnet.split("/")[0];