fix: combine machine-id+cpu+disk serial for box_id to prevent ghost clone collision

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-14 22:21:13 +08:00
parent aba21bf8b7
commit 3b7fc0c0d4

View File

@@ -6,13 +6,14 @@ const crypto = require('crypto');
const { execSync } = require('child_process'); const { execSync } = require('child_process');
/** /**
* 生成硬件唯一指纹作为 box_id,优先级: * 生成硬件唯一指纹作为 box_id
* *
* 1. /etc/machine-id — systemd 生成,现代 Linux 标配,重装系统才变 * 策略:
* 2. CPU Serial — /proc/cpuinfo 中的 Serial 字段ARM/RPi 常见) * 优先 将 machine-id + CPU serial + 磁盘 serial 拼接后取 SHA-256 前 32 字符,
* 3. 主存储设备序列号 — /sys/block/<dev>/device/serialsda/nvme/mmcblk * 三者至少能拿到一个即可用此方案,防止 ghost clone 场景下 machine-id 相同的问题。
* 4. DMI 产品 UUID — /sys/class/dmi/id/product_uuidx86 主板) *
* 5. 随机 UUID 持久化 — 生成后写入 /etc/clawd/.box_id确保重启不变 * 若均拿不到则依次退化:
* DMI UUID → 持久化随机 UUID
* *
* 注意MAC 地址故意排除,网卡更换/虚拟化/Docker 都会导致其变化。 * 注意MAC 地址故意排除,网卡更换/虚拟化/Docker 都会导致其变化。
*/ */
@@ -110,11 +111,17 @@ function getPersistentUUID() {
// ── 主函数 ──────────────────────────────────────────────────────────────────── // ── 主函数 ────────────────────────────────────────────────────────────────────
function getBoxId() { function getBoxId() {
return getMachineId() const machineId = getMachineId();
|| getCpuSerial() const cpuSerial = getCpuSerial();
|| getDiskSerial() const diskSerial = getDiskSerial();
|| getDmiUuid()
|| getPersistentUUID(); // 只要能拿到其中任意一项,就把三者拼接后取哈希,避免 ghost clone 场景
if (machineId || cpuSerial || diskSerial) {
const raw = [machineId || '', cpuSerial || '', diskSerial || ''].join(':');
return crypto.createHash('sha256').update(raw).digest('hex').slice(0, 32);
}
return getDmiUuid() || getPersistentUUID();
} }
module.exports = { getBoxId }; module.exports = { getBoxId };