2025-02-20 22:34:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { db } from "@server/db";
|
2025-02-21 11:57:01 -05:00
|
|
|
import { clients, olms, sites } from "@server/db/schema";
|
2025-02-20 22:34:51 -05:00
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
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 { generateId } from "@server/auth/sessions/app";
|
|
|
|
import config from "@server/lib/config";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
|
|
|
|
const getSiteSchema = z
|
|
|
|
.object({
|
2025-02-21 16:58:30 -05:00
|
|
|
siteId: z.string().transform(Number).pipe(z.number())
|
2025-02-20 22:34:51 -05:00
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
export type PickClientDefaultsResponse = {
|
|
|
|
siteId: number;
|
|
|
|
address: string;
|
|
|
|
publicKey: string;
|
|
|
|
name: string;
|
|
|
|
listenPort: number;
|
|
|
|
endpoint: string;
|
|
|
|
subnet: string;
|
2025-02-21 16:58:30 -05:00
|
|
|
olmId: string;
|
|
|
|
olmSecret: string;
|
2025-02-20 22:34:51 -05:00
|
|
|
};
|
|
|
|
|
2025-02-21 11:57:01 -05:00
|
|
|
export async function pickClientDefaults(
|
2025-02-20 22:34:51 -05:00
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
|
|
|
try {
|
|
|
|
const parsedParams = getSiteSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedParams.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { siteId } = parsedParams.data;
|
|
|
|
|
|
|
|
const [site] = await db
|
|
|
|
.select()
|
|
|
|
.from(sites)
|
|
|
|
.where(eq(sites.siteId, siteId));
|
|
|
|
|
|
|
|
if (!site) {
|
|
|
|
return next(createHttpError(HttpCode.NOT_FOUND, "Site not found"));
|
|
|
|
}
|
|
|
|
|
2025-02-21 16:58:30 -05:00
|
|
|
if (site.type !== "newt") {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"Site is not a newt site"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-20 22:34:51 -05:00
|
|
|
// make sure all the required fields are present
|
2025-02-21 11:57:01 -05:00
|
|
|
|
|
|
|
const sitesRequiredFields = z.object({
|
|
|
|
address: z.string(),
|
|
|
|
publicKey: z.string(),
|
|
|
|
listenPort: z.number(),
|
|
|
|
endpoint: z.string()
|
|
|
|
});
|
|
|
|
|
|
|
|
const parsedSite = sitesRequiredFields.safeParse(site);
|
|
|
|
if (!parsedSite.success) {
|
2025-02-21 17:13:23 -05:00
|
|
|
logger.error("Unable to pick client defaults because: " + fromError(parsedSite.error).toString());
|
2025-02-20 22:34:51 -05:00
|
|
|
return next(
|
2025-02-21 11:57:01 -05:00
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2025-02-21 17:13:23 -05:00
|
|
|
"Site is not configured to accept client connectivity"
|
2025-02-21 11:57:01 -05:00
|
|
|
)
|
2025-02-20 22:34:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:57:01 -05:00
|
|
|
const { address, publicKey, listenPort, endpoint } = parsedSite.data;
|
|
|
|
|
2025-02-20 22:34:51 -05:00
|
|
|
const clientsQuery = await db
|
|
|
|
.select({
|
2025-02-21 11:57:01 -05:00
|
|
|
subnet: clients.subnet
|
2025-02-20 22:34:51 -05:00
|
|
|
})
|
2025-02-21 11:57:01 -05:00
|
|
|
.from(clients)
|
|
|
|
.where(eq(clients.siteId, site.siteId));
|
2025-02-20 22:34:51 -05:00
|
|
|
|
|
|
|
let subnets = clientsQuery.map((client) => client.subnet);
|
|
|
|
|
|
|
|
// exclude the exit node address by replacing after the / with a site block size
|
|
|
|
subnets.push(
|
2025-02-21 11:57:01 -05:00
|
|
|
address.replace(
|
2025-02-20 22:34:51 -05:00
|
|
|
/\/\d+$/,
|
2025-02-21 18:51:16 -05:00
|
|
|
`/${config.getRawConfig().newt.site_block_size}`
|
2025-02-20 22:34:51 -05:00
|
|
|
)
|
|
|
|
);
|
2025-02-21 18:51:16 -05:00
|
|
|
|
2025-02-20 22:34:51 -05:00
|
|
|
const newSubnet = findNextAvailableCidr(
|
|
|
|
subnets,
|
2025-02-21 18:51:16 -05:00
|
|
|
config.getRawConfig().newt.site_block_size,
|
2025-02-21 11:57:01 -05:00
|
|
|
address
|
2025-02-20 22:34:51 -05:00
|
|
|
);
|
|
|
|
if (!newSubnet) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
"No available subnets"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-21 16:58:30 -05:00
|
|
|
const olmId = generateId(15);
|
2025-02-20 22:34:51 -05:00
|
|
|
const secret = generateId(48);
|
|
|
|
|
|
|
|
return response<PickClientDefaultsResponse>(res, {
|
|
|
|
data: {
|
|
|
|
siteId: site.siteId,
|
2025-02-21 11:57:01 -05:00
|
|
|
address: address,
|
|
|
|
publicKey: publicKey,
|
2025-02-20 22:34:51 -05:00
|
|
|
name: site.name,
|
2025-02-21 11:57:01 -05:00
|
|
|
listenPort: listenPort,
|
|
|
|
endpoint: endpoint,
|
2025-02-24 20:21:57 -05:00
|
|
|
// subnet: `${newSubnet.split("/")[0]}/${config.getRawConfig().newt.block_size}`, // we want the block size of the whole subnet
|
|
|
|
subnet: newSubnet,
|
2025-02-21 16:58:30 -05:00
|
|
|
olmId: olmId,
|
|
|
|
olmSecret: secret
|
2025-02-20 22:34:51 -05:00
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Organization retrieved successfully",
|
|
|
|
status: HttpCode.OK
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|