feat: 启动和连接时设置 hostname 为 claw-<claw_id>
- start(): 有 claw_id 时立即设置(重启恢复) - _onConnected(): 每次连上 VPS 后设置(首次注册或重连确认) - 用 exec 异步执行,不阻塞启动流程 Made-with: Cursor
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user