From 33a1d9bd37d9eb7facb3a7d280dbda11672b99c7 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 24 Apr 2026 14:31:59 +0000 Subject: [PATCH] Serve OpenClaw proxy bootstrap as external script --- app/api/openshell/dashboard/proxy/shared.ts | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app/api/openshell/dashboard/proxy/shared.ts b/app/api/openshell/dashboard/proxy/shared.ts index 1355313..3a767d4 100644 --- a/app/api/openshell/dashboard/proxy/shared.ts +++ b/app/api/openshell/dashboard/proxy/shared.ts @@ -5,6 +5,7 @@ import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority' const LEGACY_PROXY_PREFIX = '/api/openshell/dashboard/proxy' const INSTANCES_PREFIX = '/api/openshell/instances/' const DASHBOARD_PROXY_SUFFIX = '/dashboard/proxy' +const BOOTSTRAP_SCRIPT_NAME = '__nemoclaw_openclaw_bootstrap.js' const HOP_BY_HOP_HEADERS = new Set([ 'accept-encoding', 'connection', @@ -134,22 +135,47 @@ function proxiedAssetPath(value: string, proxyPrefix: string) { return `${proxyPrefix}/${normalized}` } +function isBootstrapScriptRequest(requestUrl: URL, proxyPrefix: string) { + return requestUrl.pathname === `${proxyPrefix}/${BOOTSTRAP_SCRIPT_NAME}` +} + +function bootstrapScriptResponse(proxyPrefix: string) { + return new NextResponse( + `window.__OPENCLAW_CONTROL_UI_BASE_PATH__=${JSON.stringify(proxyPrefix)};\n`, + { + status: 200, + headers: { + 'cache-control': 'no-store', + 'content-type': 'application/javascript; charset=utf-8', + }, + } + ) +} + function injectOpenClawBootstrap(body: string, proxyPrefix: string) { - const bootstrap = `` + const bootstrap = `` return body.replace(/]*)?>/i, (match) => `${match}${bootstrap}`) } function rewriteHtml(body: string, proxyPrefix: string) { - return injectOpenClawBootstrap(body, proxyPrefix).replace( + const rewritten = body.replace( /(<(?:script|img|link)\b[^>]*\s(?:src|href)=)(["'])([^"']+)(["'])/gi, (_match, prefix: string, quote: string, value: string, suffix: string) => `${prefix}${quote}${proxiedAssetPath(value, proxyPrefix)}${suffix}` ) + + return injectOpenClawBootstrap(rewritten, proxyPrefix) } export async function proxyOpenClawDashboard(request: Request) { const requestUrl = new URL(request.url) + const resolution = resolveProxyTarget(requestUrl) + + if (isBootstrapScriptRequest(requestUrl, resolution.proxyPrefix)) { + return bootstrapScriptResponse(resolution.proxyPrefix) + } + const { target, proxyPrefix, authorityMode, bridgeActive } = buildTargetUrl(requestUrl) const method = request.method.toUpperCase() const headers = copyRequestHeaders(request, target)