Serve OpenClaw proxy bootstrap as external script

This commit is contained in:
Codex
2026-04-24 14:31:59 +00:00
parent 7b23d61a4d
commit 33a1d9bd37
+28 -2
View File
@@ -5,6 +5,7 @@ import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority'
const LEGACY_PROXY_PREFIX = '/api/openshell/dashboard/proxy' const LEGACY_PROXY_PREFIX = '/api/openshell/dashboard/proxy'
const INSTANCES_PREFIX = '/api/openshell/instances/' const INSTANCES_PREFIX = '/api/openshell/instances/'
const DASHBOARD_PROXY_SUFFIX = '/dashboard/proxy' const DASHBOARD_PROXY_SUFFIX = '/dashboard/proxy'
const BOOTSTRAP_SCRIPT_NAME = '__nemoclaw_openclaw_bootstrap.js'
const HOP_BY_HOP_HEADERS = new Set([ const HOP_BY_HOP_HEADERS = new Set([
'accept-encoding', 'accept-encoding',
'connection', 'connection',
@@ -134,22 +135,47 @@ function proxiedAssetPath(value: string, proxyPrefix: string) {
return `${proxyPrefix}/${normalized}` 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) { function injectOpenClawBootstrap(body: string, proxyPrefix: string) {
const bootstrap = `<script>window.__OPENCLAW_CONTROL_UI_BASE_PATH__=${JSON.stringify(proxyPrefix)};</script>` const bootstrap = `<script src="${proxyPrefix}/${BOOTSTRAP_SCRIPT_NAME}"></script>`
return body.replace(/<head(\s[^>]*)?>/i, (match) => `${match}${bootstrap}`) return body.replace(/<head(\s[^>]*)?>/i, (match) => `${match}${bootstrap}`)
} }
function rewriteHtml(body: string, proxyPrefix: string) { function rewriteHtml(body: string, proxyPrefix: string) {
return injectOpenClawBootstrap(body, proxyPrefix).replace( const rewritten = body.replace(
/(<(?:script|img|link)\b[^>]*\s(?:src|href)=)(["'])([^"']+)(["'])/gi, /(<(?:script|img|link)\b[^>]*\s(?:src|href)=)(["'])([^"']+)(["'])/gi,
(_match, prefix: string, quote: string, value: string, suffix: string) => (_match, prefix: string, quote: string, value: string, suffix: string) =>
`${prefix}${quote}${proxiedAssetPath(value, proxyPrefix)}${suffix}` `${prefix}${quote}${proxiedAssetPath(value, proxyPrefix)}${suffix}`
) )
return injectOpenClawBootstrap(rewritten, proxyPrefix)
} }
export async function proxyOpenClawDashboard(request: Request) { export async function proxyOpenClawDashboard(request: Request) {
const requestUrl = new URL(request.url) 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 { target, proxyPrefix, authorityMode, bridgeActive } = buildTargetUrl(requestUrl)
const method = request.method.toUpperCase() const method = request.method.toUpperCase()
const headers = copyRequestHeaders(request, target) const headers = copyRequestHeaders(request, target)