fix: read dashboard token from ~/.openclaw/openclaw.json instead of running openclaw dashboard command

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-18 14:00:57 +08:00
parent 5f5b976f5b
commit d66558c9de
2 changed files with 36 additions and 33 deletions

View File

@@ -19,43 +19,33 @@ const TTYD_VERSION = '1.7.7';
const TTYD_PORT = 7681;
/**
* 启动 openclaw dashboard后台运行轮询日志文件等待 Dashboard URL 出现,
* 解析并返回 { dashboard_token, dashboard_port }。
* 超时10s或命令不存在时返回 {}
* openclaw 配置文件中提取 dashboard token 和端口。
* openclaw 将 gateway token 持久化存储在 ~/.openclaw/openclaw.json 中,
* 直接读取比执行命令更可靠(不依赖 PATH、不需要进程启动等待
* systemd 服务的 ProtectHome=read-only 允许读取 /home 下的文件。
*/
function getDashboardInfo() {
return new Promise((resolve) => {
const tmpLog = '/tmp/clawd-dashboard.log';
const configCandidates = [
path.join(os.homedir(), '.openclaw', 'openclaw.json'),
'/home/sts/.openclaw/openclaw.json',
'/root/.openclaw/openclaw.json',
];
for (const cfgPath of configCandidates) {
try {
execSync(`openclaw dashboard > ${tmpLog} 2>&1 &`, { shell: true, timeout: 3000 });
} catch (e) {
// 已在运行或命令不存在,继续轮询
}
let attempts = 0;
const interval = setInterval(() => {
attempts++;
try {
const content = fs.readFileSync(tmpLog, 'utf8');
const match = content.match(/Dashboard URL:.*:(\d+)\/#token=([a-f0-9]+)/);
if (match) {
clearInterval(interval);
const port = parseInt(match[1], 10);
const token = match[2];
log.info('dashboard', `openclaw dashboard: port=${port}, token=${token.substring(0, 8)}...`);
resolve({ dashboard_port: port, dashboard_token: token });
return;
}
} catch (e) { /* 文件暂时不存在 */ }
if (attempts >= 10) {
clearInterval(interval);
log.debug('dashboard', 'openclaw dashboard 未检测到,跳过');
resolve({});
const raw = fs.readFileSync(cfgPath, 'utf8');
const config = JSON.parse(raw);
const token = config?.gateway?.auth?.token;
const port = config?.gateway?.port || 18789;
if (token) {
log.info('dashboard', `从配置文件读取: port=${port}, token=${token.substring(0, 8)}...`);
return Promise.resolve({ dashboard_port: port, dashboard_token: token });
}
}, 1000);
});
} catch (_) { /* 文件不存在或格式错误,尝试下一个路径 */ }
}
log.debug('dashboard', 'openclaw 配置文件未找到或无 token跳过 dashboard 信息获取');
return Promise.resolve({});
}
async function downloadFrpc() {