fix: getDashboardInfo 改为读取 config.yaml

openclaw 配置文件路径从 openclaw.json 迁移到
config/config.yaml,用正则提取 token 和 port 字段。

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-24 16:47:35 +08:00
parent 7c03a59c57
commit cdb2ddc688

View File

@@ -20,23 +20,24 @@ const TTYD_PORT = 7681;
/**
* 从 openclaw 配置文件中提取 dashboard token 和端口。
* openclaw 将 gateway token 持久化存储在 ~/.openclaw/openclaw.json 中,
* openclaw 将配置持久化存储在 ~/.openclaw/config/config.yaml 中,
* 直接读取比执行命令更可靠(不依赖 PATH、不需要进程启动等待
* systemd 服务的 ProtectHome=read-only 允许读取 /home 下的文件。
*/
function getDashboardInfo() {
const configCandidates = [
path.join(os.homedir(), '.openclaw', 'openclaw.json'),
'/home/sts/.openclaw/openclaw.json',
'/root/.openclaw/openclaw.json',
path.join(os.homedir(), '.openclaw', 'config', 'config.yaml'),
'/home/sts/.openclaw/config/config.yaml',
'/root/.openclaw/config/config.yaml',
];
for (const cfgPath of configCandidates) {
try {
const raw = fs.readFileSync(cfgPath, 'utf8');
const config = JSON.parse(raw);
const token = config?.gateway?.auth?.token;
const port = config?.gateway?.port || 18789;
const raw = fs.readFileSync(cfgPath, 'utf8');
const tokenMatch = raw.match(/^\s*token:\s*["']?([A-Za-z0-9_\-\.]+)["']?\s*$/m);
const portMatch = raw.match(/^\s*port:\s*(\d+)\s*$/m);
const token = tokenMatch?.[1];
const port = portMatch ? Number(portMatch[1]) : 18789;
if (token) {
log.info('dashboard', `从配置文件读取: port=${port}, token=${token.substring(0, 8)}...`);
return Promise.resolve({ dashboard_port: port, dashboard_token: token });