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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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