Add internal api get for proxy information

This commit is contained in:
Owen
2026-05-13 21:48:07 -07:00
parent 795a3d351e
commit 7d922ac95f
9 changed files with 229 additions and 106 deletions

View File

@@ -42,6 +42,8 @@ internalRouter.get("/idp", idp.listIdps);
internalRouter.get("/idp/:idpId", idp.getIdp);
internalRouter.get("/resource/browser-target", resource.getBrowserTarget);
// Gerbil routes
const gerbilRouter = Router();
internalRouter.use("/gerbil", gerbilRouter);

View File

@@ -0,0 +1,76 @@
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { resources, targets } from "@server/db";
import { eq } from "drizzle-orm";
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import { fromError } from "zod-validation-error";
import logger from "@server/logger";
const getBrowserTargetSchema = z
.object({
fullDomain: z.string().min(1, "fullDomain is required")
})
.strict();
export type GetBrowserTargetResponse = {
ip: string;
port: number;
};
export async function getBrowserTarget(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
try {
const parsed = getBrowserTargetSchema.safeParse(req.query);
if (!parsed.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsed.error).toString()
)
);
}
const { fullDomain } = parsed.data;
const [row] = await db
.select({
ip: targets.ip,
port: targets.port
})
.from(targets)
.innerJoin(resources, eq(targets.resourceId, resources.resourceId))
.where(eq(resources.fullDomain, fullDomain))
.limit(1);
if (!row) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
"No resource found for this domain"
)
);
}
return response<GetBrowserTargetResponse>(res, {
data: { ip: row.ip, port: row.port },
success: true,
error: false,
message: "Browser target retrieved successfully",
status: HttpCode.OK
});
} catch (error) {
logger.error(error);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred while retrieving the browser target"
)
);
}
}

View File

@@ -33,3 +33,4 @@ export * from "./removeUserFromResource";
export * from "./listAllResourceNames";
export * from "./removeEmailFromResourceWhitelist";
export * from "./getStatusHistory";
export * from "./getBrowserTarget";

View File

@@ -32,10 +32,14 @@ declare module "react" {
}
}
type Target = {
ip: string;
port: number;
};
type FormState = {
username: string;
password: string;
hostname: string;
domain: string;
kdcProxyUrl: string;
pcb: string;
@@ -53,11 +57,16 @@ const isIronError = (error: unknown): error is IronError => {
);
};
export default function RdpClient() {
export default function RdpClient({
target,
error
}: {
target: Target | null;
error: string | null;
}) {
const [form, setForm] = useState<FormState>({
username: "Administrator",
password: "Password123!",
hostname: "172.31.3.58:3389",
username: "",
password: "",
domain: "",
kdcProxyUrl: "",
pcb: "",
@@ -214,11 +223,15 @@ export default function RdpClient() {
);
}
const destination = target
? `${target.ip}:${target.port}`
: "";
const builder = userInteraction
.configBuilder()
.withUsername(form.username)
.withPassword(form.password)
.withDestination(form.hostname)
.withDestination(destination)
.withProxyAddress(
`${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/gateway/rdp`
)
@@ -283,6 +296,14 @@ export default function RdpClient() {
setCursorOverrideActive((v) => !v);
};
if (error) {
return (
<div className="min-h-screen bg-background flex items-center justify-center">
<p className="text-destructive">{error}</p>
</div>
);
}
return (
<div className="min-h-screen bg-background">
{showLogin && (
@@ -292,15 +313,6 @@ export default function RdpClient() {
</h1>
<div className="space-y-4">
<Field label="Hostname" id="hostname">
<Input
id="hostname"
value={form.hostname}
onChange={(e) =>
update("hostname", e.target.value)
}
/>
</Field>
<Field label="Domain" id="domain">
<Input
id="domain"

View File

@@ -1,3 +1,7 @@
import { headers } from "next/headers";
import { internal } from "@app/lib/api";
import { AxiosResponse } from "axios";
import { GetBrowserTargetResponse } from "@server/routers/resource";
import RdpClient from "./RdpClient";
export const dynamic = "force-dynamic";
@@ -6,6 +10,22 @@ export const metadata = {
title: "RDP Test"
};
export default function RdpPage() {
return <RdpClient />;
export default async function RdpPage() {
const headersList = await headers();
const host = headersList.get("host") || "";
const hostname = host.split(":")[0];
let target: { ip: string; port: number } | null = null;
let error: string | null = null;
try {
const res = await internal.get<AxiosResponse<GetBrowserTargetResponse>>(
`/resource/browser-target?fullDomain=${encodeURIComponent(hostname)}`
);
target = res.data.data;
} catch {
error = "No resource found for this domain";
}
return <RdpClient target={target} error={error} />;
}

View File

@@ -6,24 +6,31 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
type Target = {
ip: string;
port: number;
};
type FormState = {
hostname: string;
port: string;
username: string;
password: string;
};
export default function SshClient() {
export default function SshClient({
target,
error
}: {
target: Target | null;
error: string | null;
}) {
const [form, setForm] = useState<FormState>({
hostname: "",
port: "22",
username: "",
password: ""
});
const [connected, setConnected] = useState(false);
const [connecting, setConnecting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [connectError, setConnectError] = useState<string | null>(null);
const terminalRef = useRef<HTMLDivElement>(null);
const xtermRef = useRef<import("@xterm/xterm").Terminal | null>(null);
@@ -115,18 +122,14 @@ export default function SshClient() {
}, []);
function connect() {
setError(null);
setConnectError(null);
setConnecting(true);
const proxyAddress = `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/gateway/ssh`;
const url = new URL(proxyAddress);
// Pass connection parameters as query params so the proxy can route
// before any application-level framing is needed.
url.searchParams.set("host", form.hostname);
url.searchParams.set("port", form.port);
url.searchParams.set("host", target?.ip ?? "");
url.searchParams.set("port", String(target?.port ?? 22));
url.searchParams.set("username", form.username);
// Auth token is sent as a query param; the proxy validates it before
// forwarding any data.
url.searchParams.set("authToken", "test-token");
const ws = new WebSocket(url.toString(), ["ssh"]);
@@ -166,7 +169,7 @@ export default function SshClient() {
ws.onerror = () => {
setConnecting(false);
setConnected(false);
setError("WebSocket connection failed");
setConnectError("WebSocket connection failed");
};
ws.onclose = (evt) => {
@@ -185,6 +188,14 @@ export default function SshClient() {
setConnected(false);
}
if (error) {
return (
<div className="flex flex-col h-screen bg-black text-white p-4 items-center justify-center">
<p className="text-red-400">{error}</p>
</div>
);
}
return (
<div className="flex flex-col h-screen bg-black text-white p-4 gap-4">
<h1 className="text-xl font-semibold text-white">SSH Terminal</h1>
@@ -192,43 +203,7 @@ export default function SshClient() {
{!connected && (
<div className="bg-neutral-900 rounded-lg p-6 max-w-lg w-full mx-auto flex flex-col gap-4">
<div className="grid grid-cols-2 gap-4">
<div className="flex flex-col gap-1">
<Label
htmlFor="hostname"
className="text-neutral-300"
>
Host
</Label>
<Input
id="hostname"
value={form.hostname}
onChange={(e) =>
setForm({
...form,
hostname: e.target.value
})
}
placeholder="192.168.1.1"
className="bg-neutral-800 border-neutral-700 text-white"
/>
</div>
<div className="flex flex-col gap-1">
<Label htmlFor="port" className="text-neutral-300">
Port
</Label>
<Input
id="port"
value={form.port}
onChange={(e) =>
setForm({ ...form, port: e.target.value })
}
placeholder="22"
className="bg-neutral-800 border-neutral-700 text-white"
/>
</div>
<div className="flex flex-col gap-1">
<div className="flex flex-col gap-1 col-span-2">
<Label
htmlFor="username"
className="text-neutral-300"
@@ -249,7 +224,7 @@ export default function SshClient() {
/>
</div>
<div className="flex flex-col gap-1">
<div className="flex flex-col gap-1 col-span-2">
<Label
htmlFor="password"
className="text-neutral-300"
@@ -271,13 +246,13 @@ export default function SshClient() {
</div>
</div>
{error && <p className="text-red-400 text-sm">{error}</p>}
{connectError && (
<p className="text-red-400 text-sm">{connectError}</p>
)}
<Button
onClick={connect}
disabled={
connecting || !form.hostname || !form.username
}
disabled={connecting || !form.username}
className="w-full"
>
{connecting ? "Connecting…" : "Connect"}

View File

@@ -1,3 +1,7 @@
import { headers } from "next/headers";
import { internal } from "@app/lib/api";
import { AxiosResponse } from "axios";
import { GetBrowserTargetResponse } from "@server/routers/resource";
import SshClient from "./SshClient";
export const dynamic = "force-dynamic";
@@ -6,6 +10,22 @@ export const metadata = {
title: "SSH Terminal"
};
export default function SshPage() {
return <SshClient />;
export default async function SshPage() {
const headersList = await headers();
const host = headersList.get("host") || "";
const hostname = host.split(":")[0];
let target: { ip: string; port: number } | null = null;
let error: string | null = null;
try {
const res = await internal.get<AxiosResponse<GetBrowserTargetResponse>>(
`/resource/browser-target?fullDomain=${encodeURIComponent(hostname)}`
);
target = res.data.data;
} catch {
error = "No resource found for this domain";
}
return <SshClient target={target} error={error} />;
}

View File

@@ -6,16 +6,23 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { toast } from "@app/hooks/useToast";
type Target = {
ip: string;
port: number;
};
type FormState = {
host: string;
port: string;
password: string;
};
export default function VncClient() {
export default function VncClient({
target,
error
}: {
target: Target | null;
error: string | null;
}) {
const [form, setForm] = useState<FormState>({
host: "",
port: "5900",
password: ""
});
@@ -43,11 +50,11 @@ export default function VncClient() {
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const connect = async () => {
if (!form.host) {
if (!target) {
toast({
variant: "destructive",
title: "Missing host",
description: "Enter the VNC server hostname or IP"
title: "No target",
description: "No resource target is available"
});
return;
}
@@ -78,12 +85,12 @@ export default function VncClient() {
}
// Build the proxy WebSocket URL:
// ws://<proxyAddress>?authToken=<token>&host=<host>&port=<port>
// ws://<proxyAddress>?authToken=<token>&host=<ip>&port=<port>
const proxyAddress = `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/gateway/vnc`;
const base = proxyAddress.replace(/\/$/, "");
const params = new URLSearchParams({
host: form.host,
port: form.port,
host: target.ip,
port: String(target.port),
authToken: "test-token"
});
const wsUrl = `${base}?${params.toString()}`;
@@ -135,6 +142,14 @@ export default function VncClient() {
rfbRef.current = rfb;
};
if (error) {
return (
<div className="min-h-screen bg-background flex items-center justify-center">
<p className="text-destructive">{error}</p>
</div>
);
}
return (
<div className="min-h-screen bg-background">
{!connected && (
@@ -144,24 +159,6 @@ export default function VncClient() {
</h1>
<div className="space-y-4">
<Field label="VNC Host" id="host">
<Input
id="host"
placeholder="192.168.1.100"
value={form.host}
onChange={(e) => update("host", e.target.value)}
/>
</Field>
<Field label="VNC Port" id="port">
<Input
id="port"
type="number"
value={form.port}
onChange={(e) => update("port", e.target.value)}
/>
</Field>
<Field label="Password (optional)" id="password">
<Input
id="password"

View File

@@ -1,3 +1,7 @@
import { headers } from "next/headers";
import { internal } from "@app/lib/api";
import { AxiosResponse } from "axios";
import { GetBrowserTargetResponse } from "@server/routers/resource";
import VncClient from "./VncClient";
export const dynamic = "force-dynamic";
@@ -6,6 +10,22 @@ export const metadata = {
title: "VNC Test"
};
export default function VncPage() {
return <VncClient />;
export default async function VncPage() {
const headersList = await headers();
const host = headersList.get("host") || "";
const hostname = host.split(":")[0];
let target: { ip: string; port: number } | null = null;
let error: string | null = null;
try {
const res = await internal.get<AxiosResponse<GetBrowserTargetResponse>>(
`/resource/browser-target?fullDomain=${encodeURIComponent(hostname)}`
);
target = res.data.data;
} catch {
error = "No resource found for this domain";
}
return <VncClient target={target} error={error} />;
}