Stabilize OpenClaw dashboard websocket bootstrap
This commit is contained in:
@@ -10,19 +10,40 @@ function parseInventoryCount(requestUrl: URL) {
|
|||||||
return Number.isFinite(count) && count >= 0 ? count : null
|
return Number.isFinite(count) && count >= 0 ? count : null
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildLaunchUrl(proxiedUrl: string, bootstrapUrl: string | null) {
|
function firstHeaderValue(value: string | null) {
|
||||||
if (!bootstrapUrl) return proxiedUrl
|
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 bootstrap = new URL(bootstrapUrl)
|
||||||
const browserHash = bootstrap.hash
|
const browserHash = bootstrap.hash.startsWith('#') ? bootstrap.hash.slice(1) : bootstrap.hash
|
||||||
bootstrap.hash = ''
|
bootstrap.hash = ''
|
||||||
|
|
||||||
|
new URLSearchParams(browserHash).forEach((value, key) => {
|
||||||
|
hashParams.set(key, value)
|
||||||
|
})
|
||||||
|
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
path: '/',
|
path: '/',
|
||||||
bootstrapUrl: bootstrap.toString(),
|
bootstrapUrl: bootstrap.toString(),
|
||||||
})
|
})
|
||||||
|
|
||||||
return `${proxiedUrl}?${params.toString()}${browserHash}`
|
return `${proxiedUrl}?${params.toString()}#${hashParams.toString()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
@@ -55,7 +76,7 @@ export async function GET(request: Request) {
|
|||||||
: 'verified'
|
: 'verified'
|
||||||
|
|
||||||
const proxiedUrl = authority.explicitInstanceOverride ? instanceQualifiedProxyBase : '/api/openshell/dashboard/proxy'
|
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({
|
return NextResponse.json({
|
||||||
ok: true,
|
ok: true,
|
||||||
|
|||||||
@@ -134,8 +134,14 @@ function proxiedAssetPath(value: string, proxyPrefix: string) {
|
|||||||
return `${proxyPrefix}/${normalized}`
|
return `${proxyPrefix}/${normalized}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function injectOpenClawBootstrap(body: string, proxyPrefix: string) {
|
||||||
|
const bootstrap = `<script>window.__OPENCLAW_CONTROL_UI_BASE_PATH__=${JSON.stringify(proxyPrefix)};</script>`
|
||||||
|
|
||||||
|
return body.replace(/<head(\s[^>]*)?>/i, (match) => `${match}${bootstrap}`)
|
||||||
|
}
|
||||||
|
|
||||||
function rewriteHtml(body: string, proxyPrefix: string) {
|
function rewriteHtml(body: string, proxyPrefix: string) {
|
||||||
return body.replace(
|
return injectOpenClawBootstrap(body, proxyPrefix).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}`
|
||||||
|
|||||||
Reference in New Issue
Block a user