mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-03 01:24:58 +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
|
@ -10,6 +10,7 @@ import logger from "@server/logger";
|
|||
import { fromError } from "zod-validation-error";
|
||||
import { addPeer } from "../gerbil/peers";
|
||||
import { addTargets } from "../newt/targets";
|
||||
import { pickPort } from "./ports";
|
||||
|
||||
// Regular expressions for validation
|
||||
const DOMAIN_REGEX =
|
||||
|
@ -84,15 +85,14 @@ export async function updateTarget(
|
|||
}
|
||||
|
||||
const { targetId } = parsedParams.data;
|
||||
const updateData = parsedBody.data;
|
||||
|
||||
const [updatedTarget] = await db
|
||||
.update(targets)
|
||||
.set(updateData)
|
||||
const [target] = await db
|
||||
.select()
|
||||
.from(targets)
|
||||
.where(eq(targets.targetId, targetId))
|
||||
.returning();
|
||||
.limit(1);
|
||||
|
||||
if (!updatedTarget) {
|
||||
if (!target) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.NOT_FOUND,
|
||||
|
@ -107,13 +107,13 @@ export async function updateTarget(
|
|||
siteId: resources.siteId
|
||||
})
|
||||
.from(resources)
|
||||
.where(eq(resources.resourceId, updatedTarget.resourceId!));
|
||||
.where(eq(resources.resourceId, target.resourceId!));
|
||||
|
||||
if (!resource) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.NOT_FOUND,
|
||||
`Resource with ID ${updatedTarget.resourceId} not found`
|
||||
`Resource with ID ${target.resourceId} not found`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -132,24 +132,29 @@ export async function updateTarget(
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { internalPort, targetIps } = await pickPort(site.siteId!);
|
||||
|
||||
if (!internalPort) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
`No available internal port`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const [updatedTarget] = await db
|
||||
.update(targets)
|
||||
.set({
|
||||
...parsedBody.data,
|
||||
internalPort
|
||||
})
|
||||
.where(eq(targets.targetId, targetId))
|
||||
.returning();
|
||||
|
||||
if (site.pubKey) {
|
||||
if (site.type == "wireguard") {
|
||||
// TODO: is this all inefficient?
|
||||
// Fetch resources for this site
|
||||
const resourcesRes = await db.query.resources.findMany({
|
||||
where: eq(resources.siteId, site.siteId)
|
||||
});
|
||||
|
||||
// Fetch targets for all resources of this site
|
||||
const targetIps = await Promise.all(
|
||||
resourcesRes.map(async (resource) => {
|
||||
const targetsRes = await db.query.targets.findMany({
|
||||
where: eq(targets.resourceId, resource.resourceId)
|
||||
});
|
||||
return targetsRes.map((target) => `${target.ip}/32`);
|
||||
})
|
||||
);
|
||||
|
||||
await addPeer(site.exitNodeId!, {
|
||||
publicKey: site.pubKey,
|
||||
allowedIps: targetIps.flat()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue