Add crud for browser targets

This commit is contained in:
Owen
2026-05-15 14:07:48 -07:00
parent 00e1675f7b
commit 75b5afd544
20 changed files with 934 additions and 27 deletions

View File

@@ -18,6 +18,7 @@ import logger from "@server/logger";
import { initPeerAddHandshake, updatePeer } from "../olm/peers";
import { eq, and } from "drizzle-orm";
import config from "@server/lib/config";
import { decrypt } from "@server/lib/crypto";
import {
formatEndpoint,
generateSubnetProxyTargetV2,
@@ -311,12 +312,17 @@ export async function buildTargetConfigurationForNewtClient(
(target) => target !== null
);
const browserGatewayTargets = allBrowserGatewayTargets.map((t) => ({
id: t.browserGatewayTargetId,
type: t.type,
destination: t.destination,
destinationPort: t.destinationPort
}));
const serverSecret = config.getRawConfig().server.secret!;
const browserGatewayTargets = allBrowserGatewayTargets.map((t) => {
const decryptAuthToken = decrypt(t.authToken, serverSecret);
return {
id: t.browserGatewayTargetId,
type: t.type,
destination: t.destination,
destinationPort: t.destinationPort,
authToken: decryptAuthToken
};
});
return {
validHealthCheckTargets,

View File

@@ -2,6 +2,8 @@ import { BrowserGatewayTarget, Target, TargetHealthCheck } from "@server/db";
import { sendToClient } from "#dynamic/routers/ws";
import logger from "@server/logger";
import { canCompress } from "@server/lib/clientVersionChecks";
import { decrypt } from "@server/lib/crypto";
import config from "@server/lib/config";
export async function addTargets(
newtId: string,
@@ -247,14 +249,21 @@ export async function sendBrowserGatewayTargets(
) {
if (targets.length === 0) return;
const payload = targets.map((t) => ({
id: t.browserGatewayTargetId,
resourceId: t.resourceId,
siteId: t.siteId,
type: t.type,
destination: t.destination,
destinationPort: t.destinationPort
}));
const payload = targets.map((t) => {
const decryptAuthToken = decrypt(
t.authToken,
config.getRawConfig().server.secret!
);
return {
id: t.browserGatewayTargetId,
resourceId: t.resourceId,
siteId: t.siteId,
type: t.type,
destination: t.destination,
destinationPort: t.destinationPort,
authToken: decryptAuthToken
};
});
await sendToClient(
newtId,

View File

@@ -8,6 +8,8 @@ import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
import { decrypt } from "@server/lib/crypto";
import config from "@server/lib/config";
const getBrowserTargetSchema = z
.object({
@@ -18,6 +20,7 @@ const getBrowserTargetSchema = z
export type GetBrowserTargetResponse = {
ip: string;
port: number;
authToken: string;
};
export async function getBrowserTarget(
@@ -43,7 +46,8 @@ export async function getBrowserTarget(
const [browserTarget] = await db
.select({
destination: browserGatewayTarget.destination,
destinationPort: browserGatewayTarget.destinationPort
destinationPort: browserGatewayTarget.destinationPort,
authToken: browserGatewayTarget.authToken
})
.from(browserGatewayTarget)
.innerJoin(
@@ -53,6 +57,11 @@ export async function getBrowserTarget(
.where(eq(resources.fullDomain, fullDomain))
.limit(1);
const decryptAuthToken = decrypt(
browserTarget.authToken,
config.getRawConfig().server.secret!
);
if (!browserTarget) {
return next(
createHttpError(
@@ -65,7 +74,8 @@ export async function getBrowserTarget(
return response<GetBrowserTargetResponse>(res, {
data: {
ip: browserTarget.destination,
port: browserTarget.destinationPort
port: browserTarget.destinationPort,
authToken: decryptAuthToken
},
success: true,
error: false,