From 7b23d61a4d9457d70ebff5622f7af791f2cf0331 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 24 Apr 2026 14:12:46 +0000 Subject: [PATCH] Stabilize OpenClaw dashboard websocket bootstrap --- app/api/openshell/dashboard/open/route.ts | 31 +++++++++++++++++---- app/api/openshell/dashboard/proxy/shared.ts | 8 +++++- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/api/openshell/dashboard/open/route.ts b/app/api/openshell/dashboard/open/route.ts index 4a26c99..23c494c 100644 --- a/app/api/openshell/dashboard/open/route.ts +++ b/app/api/openshell/dashboard/open/route.ts @@ -10,19 +10,40 @@ function parseInventoryCount(requestUrl: URL) { return Number.isFinite(count) && count >= 0 ? count : null } -function buildLaunchUrl(proxiedUrl: string, bootstrapUrl: string | null) { - if (!bootstrapUrl) return proxiedUrl +function firstHeaderValue(value: string | null) { + return value?.split(',')[0]?.trim() || null +} + +function buildBrowserGatewayUrl(request: Request, requestUrl: URL, proxiedUrl: string) { + const forwardedProtocol = firstHeaderValue(request.headers.get('x-forwarded-proto')) + const forwardedHost = firstHeaderValue(request.headers.get('x-forwarded-host')) + const host = forwardedHost || request.headers.get('host') || requestUrl.host + const protocol = (forwardedProtocol || requestUrl.protocol.replace(/:$/, '')) === 'https' ? 'wss:' : 'ws:' + + return `${protocol}//${host}${proxiedUrl}` +} + +function buildLaunchUrl(request: Request, requestUrl: URL, proxiedUrl: string, bootstrapUrl: string | null) { + const hashParams = new URLSearchParams({ + gatewayUrl: buildBrowserGatewayUrl(request, requestUrl, proxiedUrl), + }) + + if (!bootstrapUrl) return `${proxiedUrl}#${hashParams.toString()}` const bootstrap = new URL(bootstrapUrl) - const browserHash = bootstrap.hash + const browserHash = bootstrap.hash.startsWith('#') ? bootstrap.hash.slice(1) : bootstrap.hash bootstrap.hash = '' + new URLSearchParams(browserHash).forEach((value, key) => { + hashParams.set(key, value) + }) + const params = new URLSearchParams({ path: '/', bootstrapUrl: bootstrap.toString(), }) - return `${proxiedUrl}?${params.toString()}${browserHash}` + return `${proxiedUrl}?${params.toString()}#${hashParams.toString()}` } export async function GET(request: Request) { @@ -55,7 +76,7 @@ export async function GET(request: Request) { : 'verified' const proxiedUrl = authority.explicitInstanceOverride ? instanceQualifiedProxyBase : '/api/openshell/dashboard/proxy' - const launchUrl = buildLaunchUrl(proxiedUrl, probe.bootstrapUrl) + const launchUrl = buildLaunchUrl(request, requestUrl, proxiedUrl, probe.bootstrapUrl) return NextResponse.json({ ok: true, diff --git a/app/api/openshell/dashboard/proxy/shared.ts b/app/api/openshell/dashboard/proxy/shared.ts index b49f588..1355313 100644 --- a/app/api/openshell/dashboard/proxy/shared.ts +++ b/app/api/openshell/dashboard/proxy/shared.ts @@ -134,8 +134,14 @@ function proxiedAssetPath(value: string, proxyPrefix: string) { return `${proxyPrefix}/${normalized}` } +function injectOpenClawBootstrap(body: string, proxyPrefix: string) { + const bootstrap = `` + + return body.replace(/]*)?>/i, (match) => `${match}${bootstrap}`) +} + function rewriteHtml(body: string, proxyPrefix: string) { - return body.replace( + return injectOpenClawBootstrap(body, proxyPrefix).replace( /(<(?:script|img|link)\b[^>]*\s(?:src|href)=)(["'])([^"']+)(["'])/gi, (_match, prefix: string, quote: string, value: string, suffix: string) => `${prefix}${quote}${proxiedAssetPath(value, proxyPrefix)}${suffix}`