mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-19 08:37:48 +01:00
Can adjust sites
This commit is contained in:
parent
8ad8f98f48
commit
285b99f1b7
4 changed files with 98 additions and 10 deletions
|
@ -54,7 +54,6 @@ function queryClients(orgId: string, accessibleClientIds: number[]) {
|
||||||
})
|
})
|
||||||
.from(clients)
|
.from(clients)
|
||||||
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
||||||
.leftJoin(orgs, eq(clients.orgId, orgs.orgId))
|
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
inArray(clients.clientId, accessibleClientIds),
|
inArray(clients.clientId, accessibleClientIds),
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { db } from "@server/db";
|
import { db } from "@server/db";
|
||||||
import {
|
import { clients, clientSites } from "@server/db/schemas";
|
||||||
clients,
|
|
||||||
clientSites
|
|
||||||
} from "@server/db/schemas";
|
|
||||||
import response from "@server/lib/response";
|
import response from "@server/lib/response";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import createHttpError from "http-errors";
|
import createHttpError from "http-errors";
|
||||||
|
@ -12,6 +9,14 @@ import logger from "@server/logger";
|
||||||
import { eq, and } from "drizzle-orm";
|
import { eq, and } from "drizzle-orm";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
import {
|
||||||
|
addPeer as newtAddPeer,
|
||||||
|
deletePeer as newtDeletePeer
|
||||||
|
} from "../newt/peers";
|
||||||
|
import {
|
||||||
|
addPeer as olmAddPeer,
|
||||||
|
deletePeer as olmDeletePeer
|
||||||
|
} from "../olm/peers";
|
||||||
|
|
||||||
const updateClientParamsSchema = z
|
const updateClientParamsSchema = z
|
||||||
.object({
|
.object({
|
||||||
|
@ -22,7 +27,9 @@ const updateClientParamsSchema = z
|
||||||
const updateClientSchema = z
|
const updateClientSchema = z
|
||||||
.object({
|
.object({
|
||||||
name: z.string().min(1).max(255).optional(),
|
name: z.string().min(1).max(255).optional(),
|
||||||
siteIds: z.array(z.string().transform(Number).pipe(z.number())).optional()
|
siteIds: z
|
||||||
|
.array(z.string().transform(Number).pipe(z.number()))
|
||||||
|
.optional()
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|
||||||
|
@ -92,6 +99,81 @@ export async function updateClient(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (siteIds) {
|
||||||
|
let sitesAdded = [];
|
||||||
|
let sitesRemoved = [];
|
||||||
|
|
||||||
|
// Fetch existing site associations
|
||||||
|
const existingSites = await db
|
||||||
|
.select({ siteId: clientSites.siteId })
|
||||||
|
.from(clientSites)
|
||||||
|
.where(eq(clientSites.clientId, clientId));
|
||||||
|
|
||||||
|
const existingSiteIds = existingSites.map((site) => site.siteId);
|
||||||
|
|
||||||
|
// Determine which sites were added and removed
|
||||||
|
sitesAdded = siteIds.filter(
|
||||||
|
(siteId) => !existingSiteIds.includes(siteId)
|
||||||
|
);
|
||||||
|
sitesRemoved = existingSiteIds.filter(
|
||||||
|
(siteId) => !siteIds.includes(siteId)
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
`Adding ${sitesAdded.length} new sites to client ${client.clientId}`
|
||||||
|
);
|
||||||
|
for (const siteId of sitesAdded) {
|
||||||
|
if (!client.subnet || !client.pubKey || !client.endpoint) {
|
||||||
|
logger.debug("Client subnet, pubKey or endpoint is not set");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const site = await newtAddPeer(siteId, {
|
||||||
|
publicKey: client.pubKey,
|
||||||
|
allowedIps: [`${client.subnet.split("/")[0]}/32`], // we want to only allow from that client
|
||||||
|
endpoint: client.endpoint
|
||||||
|
});
|
||||||
|
if (!site) {
|
||||||
|
logger.debug("Failed to add peer to newt - missing site");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!site.endpoint || !site.publicKey) {
|
||||||
|
logger.debug("Site endpoint or publicKey is not set");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
await olmAddPeer(client.clientId, {
|
||||||
|
siteId: siteId,
|
||||||
|
endpoint: site.endpoint,
|
||||||
|
publicKey: site.publicKey,
|
||||||
|
serverIP: site.address,
|
||||||
|
serverPort: site.listenPort
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
`Removing ${sitesRemoved.length} sites from client ${client.clientId}`
|
||||||
|
);
|
||||||
|
for (const siteId of sitesRemoved) {
|
||||||
|
if (!client.pubKey) {
|
||||||
|
logger.debug("Client pubKey is not set");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const site = await newtDeletePeer(siteId, client.pubKey);
|
||||||
|
if (!site) {
|
||||||
|
logger.debug(
|
||||||
|
"Failed to delete peer from newt - missing site"
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!site.endpoint || !site.publicKey) {
|
||||||
|
logger.debug("Site endpoint or publicKey is not set");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
await olmDeletePeer(client.clientId, site.siteId, site.publicKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
await db.transaction(async (trx) => {
|
||||||
// Update client name if provided
|
// Update client name if provided
|
||||||
if (name) {
|
if (name) {
|
||||||
|
@ -111,7 +193,7 @@ export async function updateClient(
|
||||||
// Create new site associations
|
// Create new site associations
|
||||||
if (siteIds.length > 0) {
|
if (siteIds.length > 0) {
|
||||||
await trx.insert(clientSites).values(
|
await trx.insert(clientSites).values(
|
||||||
siteIds.map(siteId => ({
|
siteIds.map((siteId) => ({
|
||||||
clientId,
|
clientId,
|
||||||
siteId
|
siteId
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -37,6 +37,8 @@ export async function addPeer(
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Added peer ${peer.publicKey} to newt ${newt.newtId}`);
|
logger.info(`Added peer ${peer.publicKey} to newt ${newt.newtId}`);
|
||||||
|
|
||||||
|
return site;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deletePeer(siteId: number, publicKey: string) {
|
export async function deletePeer(siteId: number, publicKey: string) {
|
||||||
|
@ -67,6 +69,8 @@ export async function deletePeer(siteId: number, publicKey: string) {
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Deleted peer ${publicKey} from newt ${newt.newtId}`);
|
logger.info(`Deleted peer ${publicKey} from newt ${newt.newtId}`);
|
||||||
|
|
||||||
|
return site;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updatePeer(
|
export async function updatePeer(
|
||||||
|
@ -105,4 +109,6 @@ export async function updatePeer(
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Updated peer ${publicKey} on newt ${newt.newtId}`);
|
logger.info(`Updated peer ${publicKey} on newt ${newt.newtId}`);
|
||||||
|
|
||||||
|
return site;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import db from "@server/db";
|
import db from "@server/db";
|
||||||
import { clients, olms, newts } from "@server/db/schemas";
|
import { clients, olms, newts, sites } from "@server/db/schemas";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { sendToClient } from "../ws";
|
import { sendToClient } from "../ws";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
|
@ -37,7 +37,7 @@ export async function addPeer(
|
||||||
logger.info(`Added peer ${peer.publicKey} to olm ${olm.olmId}`);
|
logger.info(`Added peer ${peer.publicKey} to olm ${olm.olmId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deletePeer(clientId: number, publicKey: string) {
|
export async function deletePeer(clientId: number, siteId: number, publicKey: string) {
|
||||||
const [olm] = await db
|
const [olm] = await db
|
||||||
.select()
|
.select()
|
||||||
.from(olms)
|
.from(olms)
|
||||||
|
@ -50,7 +50,8 @@ export async function deletePeer(clientId: number, publicKey: string) {
|
||||||
sendToClient(olm.olmId, {
|
sendToClient(olm.olmId, {
|
||||||
type: "olm/wg/peer/remove",
|
type: "olm/wg/peer/remove",
|
||||||
data: {
|
data: {
|
||||||
publicKey
|
publicKey,
|
||||||
|
siteId: siteId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue