Add sandbox dashboard token fallback

This commit is contained in:
mmckeen-nv
2026-04-29 11:31:08 -07:00
parent 4170b318a5
commit 611fae2f86
2 changed files with 45 additions and 3 deletions
+42 -3
View File
@@ -263,6 +263,37 @@ function rewriteDashboardBootstrapOrigin(bootstrapUrl: string, dashboardUrl: str
return bootstrap.toString()
}
function normalizeDashboardToken(value: string | null | undefined) {
const token = value?.trim() || ''
if (!token || /\s/.test(token)) return null
return token
}
function withDashboardToken(bootstrapUrl: string, token: string | null) {
if (!token || hasDashboardToken(bootstrapUrl)) return bootstrapUrl
try {
const parsed = new URL(bootstrapUrl)
const hashParams = new URLSearchParams(parsed.hash.startsWith('#') ? parsed.hash.slice(1) : parsed.hash)
hashParams.set('token', token)
parsed.hash = hashParams.toString()
return parsed.toString()
} catch {
const separator = bootstrapUrl.includes('#') ? '&' : '#'
return `${bootstrapUrl}${separator}token=${encodeURIComponent(token)}`
}
}
async function readSandboxOpenClawDashboardToken(sandboxName: string) {
const script = [
'const fs=require("fs")',
'const cfg=JSON.parse(fs.readFileSync("/sandbox/.openclaw/openclaw.json","utf8"))',
'const token=cfg?.gateway?.auth?.token||cfg?.gateway?.token||""',
'if(token) process.stdout.write(String(token))',
].join(';')
const { stdout } = await execSandboxSsh(sandboxName, `node -e '${script}'`, 5000)
return normalizeDashboardToken(stdout)
}
export async function resolveOpenClawDashboardBootstrap(instanceId?: string | null) {
const instance = resolveOpenClawInstance(instanceId)
const defaultInstance = getDefaultOpenClawInstance()
@@ -277,11 +308,19 @@ export async function resolveOpenClawDashboardBootstrap(instanceId?: string | nu
const bootstrapUrl = rawBootstrapUrl
? rewriteDashboardBootstrapOrigin(rawBootstrapUrl, instance.dashboardUrl)
: null
const bootstrapTokenPresent = hasDashboardToken(bootstrapUrl)
let tokenizedBootstrapUrl = bootstrapUrl
let bootstrapTokenPresent = hasDashboardToken(tokenizedBootstrapUrl)
if (bootstrapUrl) {
if (tokenizedBootstrapUrl && !bootstrapTokenPresent) {
await execSandboxSsh(sandboxInstance.sandboxId, 'openclaw dashboard', 15000).catch(() => null)
const token = await readSandboxOpenClawDashboardToken(sandboxInstance.sandboxId).catch(() => null)
tokenizedBootstrapUrl = withDashboardToken(tokenizedBootstrapUrl, token)
bootstrapTokenPresent = hasDashboardToken(tokenizedBootstrapUrl)
}
if (tokenizedBootstrapUrl) {
return {
bootstrapUrl,
bootstrapUrl: tokenizedBootstrapUrl,
bootstrapTokenPresent,
bootstrapSource: 'openclaw-cli' as const,
bootstrapAuthority: bootstrapTokenPresent ? ('tokenized-cli' as const) : ('static-fallback' as const),