feat: 支持 macOS 设备 GUID 管理与修正 Linux global 路径 (#2099)

* feat(webui): 支持 macOS 设备 GUID 管理与修正 Linux global 路径

    Linux 的 global 目录直接位于数据根目录下(不再经过 nt_qq), 与 macOS 对齐;
    新增 getGlobalDataPath 统一推导, 修正 Registry20/machine-info 在 Linux 下指向错误目录。

    macOS 设备 GUID 实现(逆向自 wrapper.node, 对齐 qq_guid_tool):
    - guid = MD5(machine_id[8] || sn)
    - machineid-info 使用 QQ 私有 TEA 容器(16 轮, 大端), 密钥为 IOPlatformUUID 前 16 字节
    - machine_id 优先读 machineid-info, 失败回退主网卡 MAC + 0000
    - sn 优先 /dev/disk0 序列号, 回退 IOPlatformSerialNumber
    - 新增 GetMacGUIDInfo / GetMacHardwareInfo / SetMacMachineId /
      GetMacInfoBackups / CreateMacInfoBackup / RestoreMacInfoBackup / ResetMacDeviceID
    - 前端 GUID 面板支持 macOS: 显示硬件信息, 编辑 machine_id 并实时预览 GUID, 备份/恢复/删除

* fix(webui): 加固 GUID 管理并补充 Linux global 路径迁移

按评审意见处理:

- Linux 迁移: 仅当 <dataPath>/global 不存在且 <dataPath>/nt_qq/global 存在时
  搬运旧目录, 新目录已存在则保持原样; 逻辑抽到 global-migrate.ts 便于测试
- macOS 主网卡 MAC: 优先 en0, 再回退父级 IOEthernetController 上的
  IOMACAddress, 整条链路判空容错
- 恢复接口防目录穿越: 备份名必须命中白名单且匹配 .bak.<时间戳> 格式,
  写入前校验内容合法性 (Registry20 / machine-info / TEA 容器)
- SN 留空语义统一为"沿用当前生效值", 前后端预览与落库一致
- 前端保存按钮 dirty 判定纳入 SN
- 备份命名加毫秒时间戳与随机后缀, COPYFILE_EXCL 落盘, 杜绝同秒覆盖
- 硬件查询改为异步 execFile 并加进程内缓存, 不再阻塞事件循环

测试: 新增 Linux 迁移 5 例与备份命名/穿越/内容校验用例,
全量 27 文件 256 例通过; tsc --noEmit 与 eslint 通过。
This commit is contained in:
H3CoF6 authored and GitHub committed 2026-10-11 12:22:47 +08:00
1 parent 6797143b04
commit 7471e3ecd6
11 files changed
+1534 -47

No files matched your search

+147 -13
View File
@@ -5,7 +5,18 @@ import { WebUiDataRuntime } from '@/napcat-webui-backend/src/helper/Data';
import { WebUiConfig } from '@/napcat-webui-backend/index';
import { isEmpty } from '@/napcat-webui-backend/src/utils/check';
import { sendError, sendSuccess } from '@/napcat-webui-backend/src/utils/response';
import { Registry20Utils, MachineInfoUtils } from '@/napcat-webui-backend/src/utils/guid';
import {
Registry20Utils,
MachineInfoUtils,
getGlobalDataPath,
MacMachineInfoUtils,
computeMacGuidInfo,
getMacPlatformUuid,
getMacSn,
getMacSerialNumber,
getMacDiskSerial,
getMacPrimaryMac,
} from '@/napcat-webui-backend/src/utils/guid';
import os from 'node:os';
// oidb 新设备验证请求辅助函数
@@ -39,8 +50,8 @@ function oidbRequest (uid: string, body: Record<string, unknown>): Promise<Recor
});
}
// 获取 Registry20 路径的辅助函数
const getRegistryPath = () => {
// 获取 QQ 数据根目录 (dataPath)
const getQQDataPath = (): string => {
// 优先从 WebUiDataRuntime 获取早期设置的 dataPath
let dataPath = WebUiDataRuntime.getQQDataPath();
if (!dataPath) {
@@ -51,20 +62,17 @@ const getRegistryPath = () => {
if (!dataPath) {
throw new Error('QQ data path not available yet');
}
return Registry20Utils.getRegistryPath(dataPath);
return dataPath;
};
// 获取 Registry20 路径的辅助函数
const getRegistryPath = () => {
return Registry20Utils.getRegistryPath(getQQDataPath());
};
// 获取 machine-info 路径的辅助函数 (Linux)
const getMachineInfoPath = () => {
let dataPath = WebUiDataRuntime.getQQDataPath();
if (!dataPath) {
const oneBotContext = WebUiDataRuntime.getOneBotContext();
dataPath = oneBotContext?.core?.dataPath;
}
if (!dataPath) {
throw new Error('QQ data path not available yet');
}
return MachineInfoUtils.getMachineInfoPath(dataPath);
return MachineInfoUtils.getMachineInfoPath(getQQDataPath());
};
// 获取QQ登录二维码
@@ -515,6 +523,132 @@ export const QQResetLinuxDeviceIDHandler: RequestHandler = async (_, res) => {
}
};
// ============================================================
// macOS GUID 管理 (machineid-info / TEA)
// ============================================================
// macOS: 当前生效的 GUID 与来源信息
export const QQGetMacGUIDInfoHandler: RequestHandler = async (_, res) => {
try {
const dataPathGlobal = getGlobalDataPath(getQQDataPath());
const info = await computeMacGuidInfo(dataPathGlobal);
return sendSuccess(res, info);
} catch (e) {
return sendError(res, `Failed to get macOS GUID: ${(e as Error).message}`);
}
};
// macOS: 本机硬件信息 (UUID / SN / 主网卡 MAC)
export const QQGetMacHardwareInfoHandler: RequestHandler = async (_, res) => {
try {
const [mac, platformUuid, serialNumber, diskSerial] = await Promise.all([
getMacPrimaryMac(),
getMacPlatformUuid(),
getMacSerialNumber(),
getMacDiskSerial(),
]);
return sendSuccess(res, {
platformUuid: platformUuid ?? '',
serialNumber: serialNumber ?? '',
diskSerial: diskSerial ?? '',
mac: mac ? mac.toString('hex') : '',
});
} catch (e) {
return sendError(res, `Failed to get macOS hardware info: ${(e as Error).message}`);
}
};
// macOS: 修改 machine_id (写入 TEA 加密的 machineid-info)
export const QQSetMacMachineIdHandler: RequestHandler = async (req, res) => {
const { machineId, sn } = req.body;
if (!machineId || typeof machineId !== 'string' || !/^[0-9a-fA-F]{16}$/.test(machineId)) {
return sendError(res, 'Invalid machine_id, must be 16 hex characters');
}
try {
const dataPathGlobal = getGlobalDataPath(getQQDataPath());
const uuid = await getMacPlatformUuid();
if (!uuid) {
return sendError(res, 'IOPlatformUUID not available, cannot derive TEA key');
}
const key = MacMachineInfoUtils.keyFromUuid(uuid);
const machineIdInfoPath = MacMachineInfoUtils.getMachineIdInfoPath(dataPathGlobal);
// SN 留空 = 沿用当前生效值: 优先 machineid-info 内已有 SN, 其次本机硬件 SN
let snValue: string;
if (typeof sn === 'string' && sn.length > 0) {
snValue = sn;
} else {
const existing = MacMachineInfoUtils.readMachineIdInfo(machineIdInfoPath, key);
snValue = existing?.sn || (await getMacSn());
}
// 自动备份
try {
MacMachineInfoUtils.backup(machineIdInfoPath);
} catch { }
MacMachineInfoUtils.writeMachineIdInfo(machineIdInfoPath, Buffer.from(machineId, 'hex'), snValue, key);
const guid = MacMachineInfoUtils.computeGuid(Buffer.from(machineId, 'hex'), snValue);
return sendSuccess(res, {
message: 'machine_id set successfully',
guid: guid.toString('hex'),
guidUuid: MacMachineInfoUtils.guidToUuidStyle(guid),
});
} catch (e) {
return sendError(res, `Failed to set machine_id: ${(e as Error).message}`);
}
};
// macOS: machineid-info 备份列表
export const QQGetMacInfoBackupsHandler: RequestHandler = async (_, res) => {
try {
const dataPathGlobal = getGlobalDataPath(getQQDataPath());
const backups = MacMachineInfoUtils.getBackups(MacMachineInfoUtils.getMachineIdInfoPath(dataPathGlobal));
return sendSuccess(res, backups);
} catch (e) {
return sendError(res, `Failed to get backups: ${(e as Error).message}`);
}
};
// macOS: 创建 machineid-info 备份
export const QQCreateMacInfoBackupHandler: RequestHandler = async (_, res) => {
try {
const dataPathGlobal = getGlobalDataPath(getQQDataPath());
const backupPath = MacMachineInfoUtils.backup(MacMachineInfoUtils.getMachineIdInfoPath(dataPathGlobal));
return sendSuccess(res, { message: 'Backup created', path: backupPath });
} catch (e) {
return sendError(res, `Failed to backup: ${(e as Error).message}`);
}
};
// macOS: 恢复 machineid-info 备份
export const QQRestoreMacInfoBackupHandler: RequestHandler = async (req, res) => {
const { backupName } = req.body;
if (!backupName) {
return sendError(res, 'Backup name is required');
}
try {
const dataPathGlobal = getGlobalDataPath(getQQDataPath());
MacMachineInfoUtils.restore(MacMachineInfoUtils.getMachineIdInfoPath(dataPathGlobal), backupName);
return sendSuccess(res, { message: 'Restored successfully' });
} catch (e) {
return sendError(res, `Failed to restore: ${(e as Error).message}`);
}
};
// macOS: 重置设备信息 (删除 machineid-info, 回退到 MAC)
export const QQResetMacDeviceIDHandler: RequestHandler = async (_, res) => {
try {
const dataPathGlobal = getGlobalDataPath(getQQDataPath());
const machineIdInfoPath = MacMachineInfoUtils.getMachineIdInfoPath(dataPathGlobal);
// 自动备份
try {
MacMachineInfoUtils.backup(machineIdInfoPath);
} catch { }
MacMachineInfoUtils.delete(machineIdInfoPath);
return sendSuccess(res, { message: 'Device ID reset successfully (machineid-info deleted)' });
} catch (e) {
return sendError(res, `Failed to reset Device ID: ${(e as Error).message}`);
}
};
// ============================================================
// OIDB 新设备 QR 验证
// ============================================================