Files
clawd/lib/fingerprint.js
2026-03-14 20:41:26 +08:00

44 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const fs = require('fs');
const crypto = require('crypto');
const os = require('os');
/**
* 生成硬件唯一指纹作为 box_id优先级
* 1. /etc/machine-id systemd 生成,现代 Linux 标配)
* 2. /proc/sys/kernel/random/boot_id (内核 boot UUID重启会变但稳定
* 3. 第一块网卡 MAC 地址的 SHA-256 前 16 字节
* 4. 随机 UUID最后兜底存入配置防止每次变化
*/
function getBoxId() {
// 1. /etc/machine-id
try {
const id = fs.readFileSync('/etc/machine-id', 'utf8').trim();
if (id && id.length >= 16) return id;
} catch (_) {}
// 2. boot_id
try {
const id = fs.readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim().replace(/-/g, '');
if (id && id.length >= 16) return id;
} catch (_) {}
// 3. MAC 地址
try {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (!iface.internal && iface.mac && iface.mac !== '00:00:00:00:00:00') {
return crypto.createHash('sha256').update(iface.mac).digest('hex').slice(0, 32);
}
}
}
} catch (_) {}
// 4. 随机 UUID 兜底
return crypto.randomUUID().replace(/-/g, '');
}
module.exports = { getBoxId };