fix: 用 js-yaml 正确解析 config.yaml 获取 dashboard token

替换正则提取方式,使用 js-yaml 解析 YAML 文件,
通过 config.gateway.auth.token 和 config.gateway.port 读取字段。

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-24 17:00:33 +08:00
parent cdb2ddc688
commit 935d5ba176
3 changed files with 25 additions and 5 deletions

View File

@@ -25,6 +25,7 @@ const TTYD_PORT = 7681;
* systemd 服务的 ProtectHome=read-only 允许读取 /home 下的文件。 * systemd 服务的 ProtectHome=read-only 允许读取 /home 下的文件。
*/ */
function getDashboardInfo() { function getDashboardInfo() {
const yaml = require('js-yaml');
const configCandidates = [ const configCandidates = [
path.join(os.homedir(), '.openclaw', 'config', 'config.yaml'), path.join(os.homedir(), '.openclaw', 'config', 'config.yaml'),
'/home/sts/.openclaw/config/config.yaml', '/home/sts/.openclaw/config/config.yaml',
@@ -34,10 +35,9 @@ function getDashboardInfo() {
for (const cfgPath of configCandidates) { for (const cfgPath of configCandidates) {
try { try {
const raw = fs.readFileSync(cfgPath, 'utf8'); const raw = fs.readFileSync(cfgPath, 'utf8');
const tokenMatch = raw.match(/^\s*token:\s*["']?([A-Za-z0-9_\-\.]+)["']?\s*$/m); const config = yaml.load(raw);
const portMatch = raw.match(/^\s*port:\s*(\d+)\s*$/m); const token = config?.gateway?.auth?.token;
const token = tokenMatch?.[1]; const port = config?.gateway?.port || 18789;
const port = portMatch ? Number(portMatch[1]) : 18789;
if (token) { if (token) {
log.info('dashboard', `从配置文件读取: port=${port}, token=${token.substring(0, 8)}...`); log.info('dashboard', `从配置文件读取: port=${port}, token=${token.substring(0, 8)}...`);
return Promise.resolve({ dashboard_port: port, dashboard_token: token }); return Promise.resolve({ dashboard_port: port, dashboard_token: token });

19
package-lock.json generated
View File

@@ -9,6 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"js-yaml": "^4.1.1",
"ssh2": "^1.17.0", "ssh2": "^1.17.0",
"systeminformation": "^5.25.0", "systeminformation": "^5.25.0",
"ws": "^8.18.0" "ws": "^8.18.0"
@@ -20,6 +21,12 @@
"node": ">=18.0.0" "node": ">=18.0.0"
} }
}, },
"node_modules/argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
"license": "Python-2.0"
},
"node_modules/asn1": { "node_modules/asn1": {
"version": "0.2.6", "version": "0.2.6",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
@@ -61,6 +68,18 @@
"node": ">=10.0.0" "node": ">=10.0.0"
} }
}, },
"node_modules/js-yaml": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.1.tgz",
"integrity": "sha512-qQKT4zQxXl8lLwBtHMWwaTcGfFOZviOJet3Oy/xmGk2gZH677CJM9EvtfdSkgWcATZhj/55JZ0rmy3myCT5lsA==",
"license": "MIT",
"dependencies": {
"argparse": "^2.0.1"
},
"bin": {
"js-yaml": "bin/js-yaml.js"
}
},
"node_modules/nan": { "node_modules/nan": {
"version": "2.26.1", "version": "2.26.1",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.26.1.tgz", "resolved": "https://registry.npmjs.org/nan/-/nan-2.26.1.tgz",

View File

@@ -21,6 +21,7 @@
"node": ">=18.0.0" "node": ">=18.0.0"
}, },
"dependencies": { "dependencies": {
"js-yaml": "^4.1.1",
"ssh2": "^1.17.0", "ssh2": "^1.17.0",
"systeminformation": "^5.25.0", "systeminformation": "^5.25.0",
"ws": "^8.18.0" "ws": "^8.18.0"