feat: 启动和连接时设置 hostname 为 claw-<claw_id>

- start(): 有 claw_id 时立即设置(重启恢复)
- _onConnected(): 每次连上 VPS 后设置(首次注册或重连确认)
- 用 exec 异步执行,不阻塞启动流程

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-04-03 22:58:14 +08:00
parent fb8a408f93
commit 46dae37079

View File

@@ -3,7 +3,7 @@
const fs = require('fs'); const fs = require('fs');
const { getNotifySocket } = require('./systemd-env'); const { getNotifySocket } = require('./systemd-env');
const WebSocket = require('ws'); const WebSocket = require('ws');
const { execFileSync } = require('child_process'); const { execFileSync, exec } = require('child_process');
const config = require('./config'); const config = require('./config');
const log = require('./logger'); const log = require('./logger');
const { getBoxId } = require('./fingerprint'); const { getBoxId } = require('./fingerprint');
@@ -101,6 +101,10 @@ class ClawClient {
async start() { async start() {
log.info('clawd', `启动中... 服务器 = ${this._cfg.server}`); log.info('clawd', `启动中... 服务器 = ${this._cfg.server}`);
if (this._cfg.claw_id) {
this._setHostname(this._cfg.claw_id);
}
// 启动时关 alarm若本地已记为已激活立即亮 pwr不等首包 connected // 启动时关 alarm若本地已记为已激活立即亮 pwr不等首包 connected
led.status.off(); led.status.off();
if (this._cfg.activated) { if (this._cfg.activated) {
@@ -416,6 +420,7 @@ class ClawClient {
this._cfg.claw_id = msg.claw_id; this._cfg.claw_id = msg.claw_id;
this._cfg.token = msg.token; this._cfg.token = msg.token;
config.save(this._cfg); config.save(this._cfg);
this._setHostname(msg.claw_id);
if (isNew) { if (isNew) {
log.info('clawd', `注册成功claw_id = ${msg.claw_id}`); 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 配置 ──────────────────────────────────────────────────────────── // ── OpenClaw 配置 ────────────────────────────────────────────────────────────
_updateOpenClawOrigin(targetId) { _updateOpenClawOrigin(targetId) {