Merge branch 'hp-multi-client' into auth-providers-clients

This commit is contained in:
miloschwartz
2025-04-20 16:15:40 -04:00
70 changed files with 27368 additions and 159 deletions

View File

@@ -6,10 +6,11 @@ import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import logger from "@server/logger";
import { findNextAvailableCidr } from "@server/lib/ip";
import { findNextAvailableCidr, getNextAvailableClientSubnet } from "@server/lib/ip";
import { generateId } from "@server/auth/sessions/app";
import config from "@server/lib/config";
import { OpenAPITags, registry } from "@server/openApi";
import { fromError } from "zod-validation-error";
import { z } from "zod";
export type PickSiteDefaultsResponse = {
@@ -22,6 +23,7 @@ export type PickSiteDefaultsResponse = {
subnet: string;
newtId: string;
newtSecret: string;
clientAddress: string;
};
registry.registerPath({
@@ -38,12 +40,29 @@ registry.registerPath({
responses: {}
});
const pickSiteDefaultsSchema = z
.object({
orgId: z.string()
})
.strict();
export async function pickSiteDefaults(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
try {
const parsedParams = pickSiteDefaultsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString()
)
);
}
const { orgId } = parsedParams.data;
// TODO: more intelligent way to pick the exit node
// make sure there is an exit node by counting the exit nodes table
@@ -89,6 +108,18 @@ export async function pickSiteDefaults(
);
}
const newClientAddress = await getNextAvailableClientSubnet(orgId);
if (!newClientAddress) {
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"No available subnet found"
)
);
}
const clientAddress = newClientAddress.split("/")[0];
const newtId = generateId(15);
const secret = generateId(48);
@@ -100,7 +131,9 @@ export async function pickSiteDefaults(
name: exitNode.name,
listenPort: exitNode.listenPort,
endpoint: exitNode.endpoint,
// subnet: `${newSubnet.split("/")[0]}/${config.getRawConfig().gerbil.block_size}`, // we want the block size of the whole subnet
subnet: newSubnet,
clientAddress: clientAddress,
newtId,
newtSecret: secret
},