diff --git a/app/lib/openshellHost.ts b/app/lib/openshellHost.ts index 961786a..ded30c0 100644 --- a/app/lib/openshellHost.ts +++ b/app/lib/openshellHost.ts @@ -269,6 +269,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() @@ -283,11 +314,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),