From 029f7fe0a9d7de2e4d3f1a5a72cebf6a2eb08836 Mon Sep 17 00:00:00 2001 From: stswangzhiping <59632378+stswangzhiping@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:07:22 +0800 Subject: [PATCH] fix: edit openclaw config.yaml directly and restart as sts user Made-with: Cursor --- lib/client.js | 56 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/lib/client.js b/lib/client.js index efbce80..6e8419a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -398,31 +398,51 @@ class ClawClient { // ── OpenClaw 配置 ──────────────────────────────────────────────────────────── _configureOpenClaw(clawId) { - // 查找 openclaw 二进制(可能在用户目录下,不在 root PATH 中) - const candidates = [ - '/home/sts/.npm-global/bin/openclaw', - '/usr/local/bin/openclaw', - '/usr/bin/openclaw', - ]; - const { existsSync } = require('fs'); - const bin = candidates.find(p => existsSync(p)); - if (!bin) { - log.warn('clawd', 'openclaw 未找到,跳过 allowedOrigins 配置'); + const { existsSync, readFileSync, writeFileSync } = require('fs'); + + // 1. 直接修改 openclaw 配置文件(避免用 root 执行 openclaw CLI 超时问题) + const configFile = '/home/sts/.openclaw/config/config.yaml'; + if (!existsSync(configFile)) { + log.warn('clawd', `openclaw 配置文件不存在: ${configFile}`); return; } try { - const origins = JSON.stringify([ - 'http://0.0.0.0:18789', - `https://${clawId}.claw.cutos.ai`, - ]); - execFileSync(bin, ['config', 'set', 'gateway.controlUi.allowedOrigins', origins], { - timeout: 5000, + let content = readFileSync(configFile, 'utf8'); + const newOrigin = `https://${clawId}.claw.cutos.ai`; + + // 替换 https://xxxx.claw.cutos.ai 为当前 clawId 对应的域名 + const updated = content.replace( + /https:\/\/\d+\.claw\.cutos\.ai/g, + newOrigin + ); + + if (updated === content) { + log.info('clawd', `openclaw allowedOrigins 已是最新,无需修改`); + } else { + writeFileSync(configFile, updated, 'utf8'); + log.info('clawd', `openclaw config 已更新: ${newOrigin}`); + } + } catch (e) { + log.warn('clawd', `openclaw config 修改失败: ${e.message}`); + return; + } + + // 2. 用 sts 身份执行 openclaw gateway restart + const openclaw = '/home/sts/.npm-global/bin/openclaw'; + if (!existsSync(openclaw)) { + log.warn('clawd', 'openclaw 未找到,跳过 gateway restart'); + return; + } + + try { + execFileSync('su', ['-', 'sts', '-c', `${openclaw} gateway restart`], { + timeout: 15000, stdio: 'ignore', }); - log.info('clawd', `openclaw allowedOrigins 已配置: ${clawId}.claw.cutos.ai`); + log.info('clawd', 'openclaw gateway restart 完成'); } catch (e) { - log.warn('clawd', `openclaw 配置失败: ${e.message}`); + log.warn('clawd', `openclaw gateway restart 失败: ${e.message}`); } }