Fixes for siteResources with clients

This commit is contained in:
Owen
2025-08-20 18:49:58 -07:00
parent 77796e8a75
commit 49cb2ae260
8 changed files with 48 additions and 57 deletions

View File

@@ -5,11 +5,9 @@ export async function addTargets(
destinationIp: string,
destinationPort: number,
protocol: string,
port: number | null = null
port: number
) {
const target = `${port ? port + ":" : ""}${
destinationIp
}:${destinationPort}`;
const target = `${port}:${destinationIp}:${destinationPort}`;
await sendToClient(newtId, {
type: `newt/wg/${protocol}/add`,
@@ -24,11 +22,9 @@ export async function removeTargets(
destinationIp: string,
destinationPort: number,
protocol: string,
port: number | null = null
port: number
) {
const target = `${port ? port + ":" : ""}${
destinationIp
}:${destinationPort}`;
const target = `${port}:${destinationIp}:${destinationPort}`;
await sendToClient(newtId, {
type: `newt/wg/${protocol}/remove`,

View File

@@ -7,6 +7,7 @@ import {
ExitNode,
exitNodes,
resources,
siteResources,
Target,
targets
} from "@server/db";
@@ -208,33 +209,23 @@ export const handleGetConfigMessage: MessageHandler = async (context) => {
const validPeers = peers.filter((peer) => peer !== null);
// Get all enabled targets with their resource protocol information
const allTargets = await db
.select({
resourceId: targets.resourceId,
targetId: targets.targetId,
ip: targets.ip,
method: targets.method,
port: targets.port,
internalPort: targets.internalPort,
enabled: targets.enabled,
protocol: resources.protocol
})
.from(targets)
.innerJoin(resources, eq(targets.resourceId, resources.resourceId))
.where(and(eq(targets.siteId, siteId), eq(targets.enabled, true)));
const allSiteResources = await db
.select()
.from(siteResources)
.where(eq(siteResources.siteId, siteId));
const { tcpTargets, udpTargets } = allTargets.reduce(
(acc, target) => {
const { tcpTargets, udpTargets } = allSiteResources.reduce(
(acc, resource) => {
// Filter out invalid targets
if (!target.internalPort || !target.ip || !target.port) {
if (!resource.proxyPort || !resource.destinationIp || !resource.destinationPort) {
return acc;
}
// Format target into string
const formattedTarget = `${target.internalPort}:${target.ip}:${target.port}`;
const formattedTarget = `${resource.proxyPort}:${resource.destinationIp}:${resource.destinationPort}`;
// Add to the appropriate protocol array
if (target.protocol === "tcp") {
if (resource.protocol === "tcp") {
acc.tcpTargets.push(formattedTarget);
} else {
acc.udpTargets.push(formattedTarget);

View File

@@ -24,7 +24,7 @@ const createSiteResourceSchema = z
protocol: z.enum(["tcp", "udp"]),
proxyPort: z.number().int().positive(),
destinationPort: z.number().int().positive(),
destinationIp: z.string().ip(),
destinationIp: z.string(),
enabled: z.boolean().default(true)
})
.strict();
@@ -146,7 +146,7 @@ export async function createSiteResource(
return next(createHttpError(HttpCode.NOT_FOUND, "Newt not found"));
}
await addTargets(newt.newtId, destinationIp, destinationPort, protocol);
await addTargets(newt.newtId, destinationIp, destinationPort, protocol, proxyPort);
logger.info(
`Created site resource ${newSiteResource.siteResourceId} for site ${siteId}`

View File

@@ -105,7 +105,8 @@ export async function deleteSiteResource(
newt.newtId,
existingSiteResource.destinationIp,
existingSiteResource.destinationPort,
existingSiteResource.protocol
existingSiteResource.protocol,
existingSiteResource.proxyPort
);
logger.info(`Deleted site resource ${siteResourceId} for site ${siteId}`);

View File

@@ -170,7 +170,8 @@ export async function updateSiteResource(
newt.newtId,
updatedSiteResource.destinationIp,
updatedSiteResource.destinationPort,
updatedSiteResource.protocol
updatedSiteResource.protocol,
updatedSiteResource.proxyPort
);
logger.info(

View File

@@ -54,29 +54,31 @@ export async function traefikConfigProvider(
config.getRawConfig().traefik.site_types
);
traefikConfig.http.middlewares[badgerMiddlewareName] = {
plugin: {
[badgerMiddlewareName]: {
apiBaseUrl: new URL(
"/api/v1",
`http://${
config.getRawConfig().server.internal_hostname
}:${config.getRawConfig().server.internal_port}`
).href,
userSessionCookieName:
config.getRawConfig().server.session_cookie_name,
if (traefikConfig?.http?.middlewares) { // BECAUSE SOMETIMES THE CONFIG CAN BE EMPTY IF THERE IS NOTHING
traefikConfig.http.middlewares[badgerMiddlewareName] = {
plugin: {
[badgerMiddlewareName]: {
apiBaseUrl: new URL(
"/api/v1",
`http://${
config.getRawConfig().server.internal_hostname
}:${config.getRawConfig().server.internal_port}`
).href,
userSessionCookieName:
config.getRawConfig().server.session_cookie_name,
// deprecated
accessTokenQueryParam:
config.getRawConfig().server
.resource_access_token_param,
// deprecated
accessTokenQueryParam:
config.getRawConfig().server
.resource_access_token_param,
resourceSessionRequestParam:
config.getRawConfig().server
.resource_session_request_param
resourceSessionRequestParam:
config.getRawConfig().server
.resource_session_request_param
}
}
}
};
};
}
return res.status(HttpCode.OK).json(traefikConfig);
} catch (e) {
@@ -320,11 +322,11 @@ export async function getTraefikConfig(
loadBalancer: {
servers: (() => {
// Check if any sites are online
// THIS IS SO THAT THERE IS SOME IMMEDIATE FEEDBACK
// THIS IS SO THAT THERE IS SOME IMMEDIATE FEEDBACK
// EVEN IF THE SITES HAVE NOT UPDATED YET FROM THE
// RECEIVE BANDWIDTH ENDPOINT.
// TODO: HOW TO HANDLE ^^^^^^ BETTER
// RECEIVE BANDWIDTH ENDPOINT.
// TODO: HOW TO HANDLE ^^^^^^ BETTER
const anySitesOnline = (
targets as TargetWithSite[]
).some((target: TargetWithSite) => target.site.online);

View File

@@ -87,7 +87,7 @@ export default function CreateInternalResourceDialog({
.positive()
.min(1, t("createInternalResourceDialogProxyPortMin"))
.max(65535, t("createInternalResourceDialogProxyPortMax")),
destinationIp: z.string().ip(t("createInternalResourceDialogInvalidIPAddressFormat")),
destinationIp: z.string(),
destinationPort: z
.number()
.int()

View File

@@ -73,7 +73,7 @@ export default function EditInternalResourceDialog({
name: z.string().min(1, t("editInternalResourceDialogNameRequired")).max(255, t("editInternalResourceDialogNameMaxLength")),
protocol: z.enum(["tcp", "udp"]),
proxyPort: z.number().int().positive().min(1, t("editInternalResourceDialogProxyPortMin")).max(65535, t("editInternalResourceDialogProxyPortMax")),
destinationIp: z.string().ip(t("editInternalResourceDialogInvalidIPAddressFormat")),
destinationIp: z.string(),
destinationPort: z.number().int().positive().min(1, t("editInternalResourceDialogDestinationPortMin")).max(65535, t("editInternalResourceDialogDestinationPortMax"))
});