From 46dae37079ab8d1b2fbfbdd23a0fdffd5b482d0d Mon Sep 17 00:00:00 2001 From: stswangzhiping <59632378+stswangzhiping@users.noreply.github.com> Date: Fri, 3 Apr 2026 22:58:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E5=8A=A8=E5=92=8C=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=97=B6=E8=AE=BE=E7=BD=AE=20hostname=20=E4=B8=BA=20c?= =?UTF-8?q?law-?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - start(): 有 claw_id 时立即设置(重启恢复) - _onConnected(): 每次连上 VPS 后设置(首次注册或重连确认) - 用 exec 异步执行,不阻塞启动流程 Made-with: Cursor --- lib/client.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 855ecbb..ce2e0f1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -3,7 +3,7 @@ const fs = require('fs'); const { getNotifySocket } = require('./systemd-env'); const WebSocket = require('ws'); -const { execFileSync } = require('child_process'); +const { execFileSync, exec } = require('child_process'); const config = require('./config'); const log = require('./logger'); const { getBoxId } = require('./fingerprint'); @@ -101,6 +101,10 @@ class ClawClient { async start() { log.info('clawd', `启动中... 服务器 = ${this._cfg.server}`); + if (this._cfg.claw_id) { + this._setHostname(this._cfg.claw_id); + } + // 启动时关 alarm;若本地已记为已激活,立即亮 pwr(不等首包 connected) led.status.off(); if (this._cfg.activated) { @@ -416,6 +420,7 @@ class ClawClient { this._cfg.claw_id = msg.claw_id; this._cfg.token = msg.token; config.save(this._cfg); + this._setHostname(msg.claw_id); if (isNew) { log.info('clawd', `注册成功!claw_id = ${msg.claw_id}`); @@ -469,6 +474,19 @@ class ClawClient { } } + // ── Hostname ───────────────────────────────────────────────────────────────── + + _setHostname(clawId) { + const hostname = `claw-${clawId}`; + exec(`sudo hostname ${hostname}`, (err) => { + if (err) { + log.warn('clawd', `设置 hostname 失败: ${err.message}`); + } else { + log.info('clawd', `hostname → ${hostname}`); + } + }); + } + // ── OpenClaw 配置 ──────────────────────────────────────────────────────────── _updateOpenClawOrigin(targetId) {