Stabilize OpenClaw dashboard websocket sidecar

This commit is contained in:
Codex
2026-04-24 15:28:47 +00:00
parent 90112c3ff9
commit a5eca0d188
4 changed files with 150 additions and 48 deletions
+5 -1
View File
@@ -19,8 +19,12 @@ function buildBrowserGatewayUrl(request: Request, requestUrl: URL, proxiedUrl: s
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:'
const wsProxyPort = process.env.OPENCLAW_DASHBOARD_WS_PROXY_PORT?.trim() || '3001'
const gatewayHost = wsProxyPort
? new URL(`${requestUrl.protocol}//${host}`).hostname + `:${wsProxyPort}`
: host
return `${protocol}//${host}${proxiedUrl}`
return `${protocol}//${gatewayHost}${proxiedUrl}`
}
function buildLaunchUrl(request: Request, requestUrl: URL, proxiedUrl: string, bootstrapUrl: string | null) {
+30 -1
View File
@@ -140,8 +140,37 @@ function isBootstrapScriptRequest(requestUrl: URL, proxyPrefix: string) {
}
function bootstrapScriptResponse(proxyPrefix: string) {
const wsProxyPort = process.env.OPENCLAW_DASHBOARD_WS_PROXY_PORT?.trim() || '3001'
const script = `
(() => {
const proxyPrefix = ${JSON.stringify(proxyPrefix)};
const wsProxyPort = ${JSON.stringify(wsProxyPort)};
window.__OPENCLAW_CONTROL_UI_BASE_PATH__ = proxyPrefix;
try {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const gatewayHost = wsProxyPort ? window.location.hostname + ':' + wsProxyPort : window.location.host;
const gatewayUrl = protocol + '//' + gatewayHost + proxyPrefix;
const settingsKey = 'openclaw.control.settings.v1';
const tokenPrefix = 'openclaw.control.token.v1:';
const tokenScope = gatewayUrl;
const hashParams = new URLSearchParams(window.location.hash.startsWith('#') ? window.location.hash.slice(1) : window.location.hash);
const token = (hashParams.get('token') || '').trim();
const rawSettings = window.localStorage.getItem(settingsKey);
const settings = rawSettings ? JSON.parse(rawSettings) : {};
settings.gatewayUrl = gatewayUrl;
window.localStorage.setItem(settingsKey, JSON.stringify(settings));
window.sessionStorage.removeItem('openclaw.control.token.v1');
if (token) window.sessionStorage.setItem(tokenPrefix + tokenScope, token);
} catch {
// Best-effort compatibility bridge for OpenClaw's persisted UI settings.
}
})();
`
return new NextResponse(
`window.__OPENCLAW_CONTROL_UI_BASE_PATH__=${JSON.stringify(proxyPrefix)};\n`,
script,
{
status: 200,
headers: {