Working on targets

This commit is contained in:
Owen
2025-11-18 13:53:04 -05:00
parent 97c707248e
commit e72e2b53aa
11 changed files with 262 additions and 427 deletions

View File

@@ -1,30 +1,32 @@
import { sendToClient } from "#dynamic/routers/ws";
import { SubnetProxyTarget } from "@server/lib/ip";
export async function addTarget(newtId: string, target: SubnetProxyTarget) {
export async function addTargets(newtId: string, targets: SubnetProxyTarget[]) {
await sendToClient(newtId, {
type: `newt/wg/target/add`,
data: target
data: targets
});
}
export async function removeTarget(newtId: string, target: SubnetProxyTarget) {
export async function removeTargets(
newtId: string,
targets: SubnetProxyTarget[]
) {
await sendToClient(newtId, {
type: `newt/wg/target/remove`,
data: target
data: targets
});
}
export async function updateTarget(
export async function updateTargets(
newtId: string,
oldTarget: SubnetProxyTarget,
newTarget: SubnetProxyTarget
targets: {
oldTargets: SubnetProxyTarget[],
newTargets: SubnetProxyTarget[]
}
) {
await sendToClient(newtId, {
type: `newt/wg/target/update`,
data: {
oldTarget,
newTarget
}
data: targets
});
}
}

View File

@@ -15,7 +15,10 @@ import { clients, clientSites, Newt, sites } from "@server/db";
import { eq, and, inArray } from "drizzle-orm";
import { updatePeer } from "../olm/peers";
import { sendToExitNode } from "#dynamic/lib/exitNodes";
import { generateRemoteSubnetsStr, generateSubnetProxyTargets } from "@server/lib/ip";
import {
generateRemoteSubnetsStr,
generateSubnetProxyTargets
} from "@server/lib/ip";
const inputSchema = z.object({
publicKey: z.string(),
@@ -195,7 +198,8 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
publicKey: site.publicKey,
serverIP: site.address,
serverPort: site.listenPort,
remoteSubnets: generateRemoteSubnetsStr(allSiteResources)
remoteSubnets:
generateRemoteSubnetsStr(allSiteResources)
});
} catch (error) {
logger.error(
@@ -222,11 +226,13 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
.from(siteResources)
.where(eq(siteResources.siteId, siteId));
const targetsToSend = await generateSubnetProxyTargets(allSiteResources);
// Build the configuration response
const configResponse = {
ipAddress: site.address,
peers: validPeers,
targets: generateSubnetProxyTargets(allSiteResources)
targets: targetsToSend
};
logger.debug("Sending config: ", configResponse);

View File

@@ -9,7 +9,7 @@ import { eq, and } from "drizzle-orm";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi";
import { addTarget } from "../client/targets";
import { addTargets } from "../client/targets";
import { getUniqueSiteResourceName } from "@server/db/names";
import { rebuildSiteClientAssociations } from "@server/lib/rebuildSiteClientAssociations";
import { generateSubnetProxyTargets } from "@server/lib/ip";
@@ -24,29 +24,29 @@ const createSiteResourceSchema = z
name: z.string().min(1).max(255),
mode: z.enum(["host", "cidr", "port"]),
protocol: z.enum(["tcp", "udp"]).optional(),
proxyPort: z.int().positive().optional(),
destinationPort: z.int().positive().optional(),
// proxyPort: z.int().positive().optional(),
// destinationPort: z.int().positive().optional(),
destination: z.string().min(1),
enabled: z.boolean().default(true),
alias: z.string().optional()
})
.strict()
.refine(
(data) => {
if (data.mode === "port") {
return (
data.protocol !== undefined &&
data.proxyPort !== undefined &&
data.destinationPort !== undefined
);
}
return true;
},
{
message:
"Protocol, proxy port, and destination port are required for port mode"
}
)
// .refine(
// (data) => {
// if (data.mode === "port") {
// return (
// data.protocol !== undefined &&
// data.proxyPort !== undefined &&
// data.destinationPort !== undefined
// );
// }
// return true;
// },
// {
// message:
// "Protocol, proxy port, and destination port are required for port mode"
// }
// )
.refine(
(data) => {
if (data.mode === "host") {
@@ -139,8 +139,8 @@ export async function createSiteResource(
name,
mode,
protocol,
proxyPort,
destinationPort,
// proxyPort,
// destinationPort,
destination,
enabled,
alias
@@ -157,29 +157,29 @@ export async function createSiteResource(
return next(createHttpError(HttpCode.NOT_FOUND, "Site not found"));
}
// check if resource with same protocol and proxy port already exists (only for port mode)
if (mode === "port" && protocol && proxyPort) {
const [existingResource] = await db
.select()
.from(siteResources)
.where(
and(
eq(siteResources.siteId, siteId),
eq(siteResources.orgId, orgId),
eq(siteResources.protocol, protocol),
eq(siteResources.proxyPort, proxyPort)
)
)
.limit(1);
if (existingResource && existingResource.siteResourceId) {
return next(
createHttpError(
HttpCode.CONFLICT,
"A resource with the same protocol and proxy port already exists"
)
);
}
}
// // check if resource with same protocol and proxy port already exists (only for port mode)
// if (mode === "port" && protocol && proxyPort) {
// const [existingResource] = await db
// .select()
// .from(siteResources)
// .where(
// and(
// eq(siteResources.siteId, siteId),
// eq(siteResources.orgId, orgId),
// eq(siteResources.protocol, protocol),
// eq(siteResources.proxyPort, proxyPort)
// )
// )
// .limit(1);
// if (existingResource && existingResource.siteResourceId) {
// return next(
// createHttpError(
// HttpCode.CONFLICT,
// "A resource with the same protocol and proxy port already exists"
// )
// );
// }
// }
const niceId = await getUniqueSiteResourceName(orgId);
@@ -195,8 +195,8 @@ export async function createSiteResource(
name,
mode,
protocol: mode === "port" ? protocol : null,
proxyPort: mode === "port" ? proxyPort : null,
destinationPort: mode === "port" ? destinationPort : null,
// proxyPort: mode === "port" ? proxyPort : null,
// destinationPort: mode === "port" ? destinationPort : null,
destination,
enabled,
alias: alias || null
@@ -232,9 +232,8 @@ export async function createSiteResource(
);
}
const [target] = generateSubnetProxyTargets([newSiteResource]);
await addTarget(newt.newtId, target);
const targets = await generateSubnetProxyTargets([newSiteResource], trx);
await addTargets(newt.newtId, targets);
await rebuildSiteClientAssociations(newSiteResource, trx); // we need to call this because we added to the admin role
});

View File

@@ -9,7 +9,7 @@ import { eq, and } from "drizzle-orm";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi";
import { removeTarget } from "../client/targets";
import { removeTargets } from "../client/targets";
import { rebuildSiteClientAssociations } from "@server/lib/rebuildSiteClientAssociations";
import { generateSubnetProxyTargets } from "@server/lib/ip";
@@ -108,8 +108,8 @@ export async function deleteSiteResource(
);
}
const [target] = generateSubnetProxyTargets([removedSiteResource]);
await removeTarget(newt.newtId, target);
const targets = await generateSubnetProxyTargets([removedSiteResource], trx);
await removeTargets(newt.newtId, targets);
await rebuildSiteClientAssociations(existingSiteResource, trx);
});

View File

@@ -9,7 +9,7 @@ import { eq, and } from "drizzle-orm";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { OpenAPITags, registry } from "@server/openApi";
import { updateTarget } from "@server/routers/client/targets";
import { updateTargets } from "@server/routers/client/targets";
import { generateSubnetProxyTargets } from "@server/lib/ip";
const updateSiteResourceParamsSchema = z.strictObject({
@@ -21,10 +21,11 @@ const updateSiteResourceParamsSchema = z.strictObject({
const updateSiteResourceSchema = z
.strictObject({
name: z.string().min(1).max(255).optional(),
mode: z.enum(["host", "cidr", "port"]).optional(),
// mode: z.enum(["host", "cidr", "port"]).optional(),
mode: z.enum(["host", "cidr"]).optional(),
protocol: z.enum(["tcp", "udp"]).nullish(),
proxyPort: z.int().positive().nullish(),
destinationPort: z.int().positive().nullish(),
// proxyPort: z.int().positive().nullish(),
// destinationPort: z.int().positive().nullish(),
destination: z.string().min(1).optional(),
enabled: z.boolean().optional(),
alias: z.string().nullish()
@@ -115,8 +116,8 @@ export async function updateSiteResource(
// Determine the final mode and validate port mode requirements
const finalMode = updateData.mode || existingSiteResource.mode;
const finalProtocol = updateData.protocol !== undefined ? updateData.protocol : existingSiteResource.protocol;
const finalProxyPort = updateData.proxyPort !== undefined ? updateData.proxyPort : existingSiteResource.proxyPort;
const finalDestinationPort = updateData.destinationPort !== undefined ? updateData.destinationPort : existingSiteResource.destinationPort;
// const finalProxyPort = updateData.proxyPort !== undefined ? updateData.proxyPort : existingSiteResource.proxyPort;
// const finalDestinationPort = updateData.destinationPort !== undefined ? updateData.destinationPort : existingSiteResource.destinationPort;
// Prepare update data
const updateValues: any = {};
@@ -136,25 +137,25 @@ export async function updateSiteResource(
}
// Handle port mode fields - include in update if explicitly provided (null or value) or if mode changed
const isModeChangingFromPort =
existingSiteResource.mode === "port" &&
updateData.mode &&
updateData.mode !== "port";
// const isModeChangingFromPort =
// existingSiteResource.mode === "port" &&
// updateData.mode &&
// updateData.mode !== "port";
if (updateData.protocol !== undefined || isModeChangingFromPort) {
updateValues.protocol = finalMode === "port" ? finalProtocol : null;
}
if (updateData.proxyPort !== undefined || isModeChangingFromPort) {
updateValues.proxyPort =
finalMode === "port" ? finalProxyPort : null;
}
if (
updateData.destinationPort !== undefined ||
isModeChangingFromPort
) {
updateValues.destinationPort =
finalMode === "port" ? finalDestinationPort : null;
}
// if (updateData.protocol !== undefined || isModeChangingFromPort) {
// updateValues.protocol = finalMode === "port" ? finalProtocol : null;
// }
// if (updateData.proxyPort !== undefined || isModeChangingFromPort) {
// updateValues.proxyPort =
// finalMode === "port" ? finalProxyPort : null;
// }
// if (
// updateData.destinationPort !== undefined ||
// isModeChangingFromPort
// ) {
// updateValues.destinationPort =
// finalMode === "port" ? finalDestinationPort : null;
// }
// Update the site resource
const [updatedSiteResource] = await db
@@ -179,10 +180,13 @@ export async function updateSiteResource(
return next(createHttpError(HttpCode.NOT_FOUND, "Newt not found"));
}
const [oldTarget] = generateSubnetProxyTargets([existingSiteResource]);
const [newTarget] = generateSubnetProxyTargets([updatedSiteResource]);
const oldTargets = await generateSubnetProxyTargets([existingSiteResource]);
const newTargets = await generateSubnetProxyTargets([updatedSiteResource]);
await updateTarget(newt.newtId, oldTarget, newTarget);
await updateTargets(newt.newtId, {
oldTargets: oldTargets,
newTargets: newTargets
});
logger.info(
`Updated site resource ${siteResourceId} for site ${siteId}`