mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-31 15:09:39 +02:00
Pick always a new port for newt
This commit is contained in:
parent
72d7ecb2ed
commit
2c8f824240
3 changed files with 80 additions and 56 deletions
|
@ -7,10 +7,11 @@ import HttpCode from "@server/types/HttpCode";
|
|||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { addPeer } from "../gerbil/peers";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { isIpInCidr } from "@server/lib/ip";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { addTargets } from "../newt/targets";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { pickPort } from "./ports";
|
||||
|
||||
// Regular expressions for validation
|
||||
const DOMAIN_REGEX =
|
||||
|
@ -147,37 +148,7 @@ export async function createTarget(
|
|||
);
|
||||
}
|
||||
|
||||
// Fetch resources for this site
|
||||
const resourcesRes = await db.query.resources.findMany({
|
||||
where: eq(resources.siteId, site.siteId)
|
||||
});
|
||||
|
||||
// TODO: is this all inefficient?
|
||||
// Fetch targets for all resources of this site
|
||||
let targetIps: string[] = [];
|
||||
let targetInternalPorts: number[] = [];
|
||||
await Promise.all(
|
||||
resourcesRes.map(async (resource) => {
|
||||
const targetsRes = await db.query.targets.findMany({
|
||||
where: eq(targets.resourceId, resource.resourceId)
|
||||
});
|
||||
targetsRes.forEach((target) => {
|
||||
targetIps.push(`${target.ip}/32`);
|
||||
if (target.internalPort) {
|
||||
targetInternalPorts.push(target.internalPort);
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
let internalPort!: number;
|
||||
// pick a port
|
||||
for (let i = 40000; i < 65535; i++) {
|
||||
if (!targetInternalPorts.includes(i)) {
|
||||
internalPort = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const { internalPort, targetIps } = await pickPort(site.siteId!);
|
||||
|
||||
if (!internalPort) {
|
||||
return next(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue