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:
1 parent
6797143b04
commit
7471e3ecd6
11 files changed
+1534
-47
No files matched your search
@@ -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 验证
|
||||
// ============================================================
|
||||
|
||||
@@ -31,6 +31,13 @@ import {
|
||||
QQCreateLinuxMachineInfoBackupHandler,
|
||||
QQRestoreLinuxMachineInfoBackupHandler,
|
||||
QQResetLinuxDeviceIDHandler,
|
||||
QQGetMacGUIDInfoHandler,
|
||||
QQGetMacHardwareInfoHandler,
|
||||
QQSetMacMachineIdHandler,
|
||||
QQGetMacInfoBackupsHandler,
|
||||
QQCreateMacInfoBackupHandler,
|
||||
QQRestoreMacInfoBackupHandler,
|
||||
QQResetMacDeviceIDHandler,
|
||||
} from '@/napcat-webui-backend/src/api/QQLogin';
|
||||
|
||||
const router: Router = Router();
|
||||
@@ -99,4 +106,22 @@ router.post('/RestoreLinuxMachineInfoBackup', QQRestoreLinuxMachineInfoBackupHan
|
||||
// router:重置Linux设备信息
|
||||
router.post('/ResetLinuxDeviceID', QQResetLinuxDeviceIDHandler);
|
||||
|
||||
// ============================================================
|
||||
// macOS GUID 管理
|
||||
// ============================================================
|
||||
// router:获取macOS设备GUID信息
|
||||
router.post('/GetMacGUIDInfo', QQGetMacGUIDInfoHandler);
|
||||
// router:获取macOS硬件信息
|
||||
router.post('/GetMacHardwareInfo', QQGetMacHardwareInfoHandler);
|
||||
// router:设置macOS machine_id
|
||||
router.post('/SetMacMachineId', QQSetMacMachineIdHandler);
|
||||
// router:获取macOS machineid-info备份列表
|
||||
router.post('/GetMacInfoBackups', QQGetMacInfoBackupsHandler);
|
||||
// router:创建macOS machineid-info备份
|
||||
router.post('/CreateMacInfoBackup', QQCreateMacInfoBackupHandler);
|
||||
// router:恢复macOS machineid-info备份
|
||||
router.post('/RestoreMacInfoBackup', QQRestoreMacInfoBackupHandler);
|
||||
// router:重置macOS设备信息
|
||||
router.post('/ResetMacDeviceID', QQResetMacDeviceIDHandler);
|
||||
|
||||
export { router as QQLoginRouter };
|
||||
@@ -2,11 +2,105 @@ import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import crypto from 'node:crypto';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { protectData, unprotectData } from 'napcat-dpapi';
|
||||
|
||||
const GUID_HEADER = Buffer.from([0x00, 0x00, 0x00, 0x14]);
|
||||
const XOR_KEY = 0x10;
|
||||
|
||||
/** 备份文件统一后缀,形如 `<原文件名>.bak.20251010031234567-1a2b3c4d` */
|
||||
const BACKUP_SUFFIX = '.bak.';
|
||||
/** 兼容历史命名(秒级、无随机后缀)以及新命名(毫秒级+随机后缀) */
|
||||
const BACKUP_SUFFIX_PATTERN = /^\.bak\.\d{8,20}(?:-[0-9a-f]{1,32})?$/i;
|
||||
|
||||
/**
|
||||
* 由 QQ 数据根目录推导 global 目录。
|
||||
* Windows: <dataPath>/nt_qq/global
|
||||
* Linux : <dataPath>/global
|
||||
* macOS : <dataPath>/global
|
||||
*/
|
||||
export function getGlobalDataPath (dataPath: string): string {
|
||||
if (os.platform() === 'win32') {
|
||||
return path.join(dataPath, 'nt_qq', 'global');
|
||||
}
|
||||
return path.join(dataPath, 'global');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成毫秒级(UTC)时间戳,避免同一秒内的连续备份互相覆盖。
|
||||
*/
|
||||
function backupTimestamp (): string {
|
||||
const now = new Date();
|
||||
const pad = (value: number, width = 2) => String(value).padStart(width, '0');
|
||||
return [
|
||||
now.getUTCFullYear(),
|
||||
pad(now.getUTCMonth() + 1),
|
||||
pad(now.getUTCDate()),
|
||||
pad(now.getUTCHours()),
|
||||
pad(now.getUTCMinutes()),
|
||||
pad(now.getUTCSeconds()),
|
||||
pad(now.getUTCMilliseconds(), 3),
|
||||
].join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建备份文件。使用 `COPYFILE_EXCL` 落盘,杜绝同秒/连击时的无感知覆盖。
|
||||
*/
|
||||
function createBackupFile (filePath: string, missingMessage: string): string {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error(missingMessage);
|
||||
}
|
||||
const timestamp = backupTimestamp();
|
||||
for (let attempt = 0; attempt < 8; attempt++) {
|
||||
const randomSuffix = crypto.randomBytes(4).toString('hex');
|
||||
const backupPath = `${filePath}${BACKUP_SUFFIX}${timestamp}-${randomSuffix}`;
|
||||
try {
|
||||
fs.copyFileSync(filePath, backupPath, fs.constants.COPYFILE_EXCL);
|
||||
return backupPath;
|
||||
} catch (e) {
|
||||
// 仅在同名冲突时重试,其余错误(权限/磁盘等)直接抛出
|
||||
if ((e as NodeJS.ErrnoException).code !== 'EEXIST') throw e;
|
||||
}
|
||||
}
|
||||
throw new Error('Failed to create backup: too many name collisions');
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并解析备份文件名,返回可安全读取的绝对路径。
|
||||
*
|
||||
* 安全约束(防止目录穿越与任意文件覆盖):
|
||||
* - 必须是纯文件名,不能包含路径分隔符或 `..`;
|
||||
* - 必须匹配 `<原文件名>.bak.<时间戳>[-随机后缀]`;
|
||||
* - 必须存在于调用方提供的备份白名单中(来自 `readdirSync`)。
|
||||
*/
|
||||
export function resolveBackupPath (filePath: string, backupFileName: string, backupList: string[]): string {
|
||||
if (typeof backupFileName !== 'string' || backupFileName.length === 0) {
|
||||
throw new Error('Backup name is required');
|
||||
}
|
||||
if (
|
||||
backupFileName !== path.basename(backupFileName) ||
|
||||
backupFileName.includes('/') ||
|
||||
backupFileName.includes('\\') ||
|
||||
backupFileName.includes('..')
|
||||
) {
|
||||
throw new Error('Invalid backup name');
|
||||
}
|
||||
|
||||
const baseName = path.basename(filePath);
|
||||
if (!backupFileName.startsWith(baseName)) {
|
||||
throw new Error('Invalid backup name');
|
||||
}
|
||||
const suffix = backupFileName.slice(baseName.length);
|
||||
if (!BACKUP_SUFFIX_PATTERN.test(suffix)) {
|
||||
throw new Error('Invalid backup name');
|
||||
}
|
||||
if (!backupList.includes(backupFileName)) {
|
||||
throw new Error('Backup file not found');
|
||||
}
|
||||
|
||||
return path.join(path.dirname(filePath), backupFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unprotects data using Windows DPAPI via napcat-dpapi.
|
||||
*/
|
||||
@@ -25,7 +119,7 @@ function dpapiProtectAndWrite (filePath: string, data: Buffer): void {
|
||||
|
||||
export class Registry20Utils {
|
||||
static getRegistryPath (dataPath: string): string {
|
||||
return path.join(dataPath, 'nt_qq', 'global', 'nt_data', 'msf', 'Registry20');
|
||||
return path.join(getGlobalDataPath(dataPath), 'nt_data', 'msf', 'Registry20');
|
||||
}
|
||||
|
||||
static readGuid (registryPath: string): string {
|
||||
@@ -91,20 +185,14 @@ export class Registry20Utils {
|
||||
}
|
||||
|
||||
static backup (registryPath: string): string {
|
||||
if (!fs.existsSync(registryPath)) {
|
||||
throw new Error('Registry20 does not exist');
|
||||
}
|
||||
const timestamp = new Date().toISOString().replace(/[-:T.]/g, '').slice(0, 14);
|
||||
const backupPath = `${registryPath}.bak.${timestamp}`;
|
||||
fs.copyFileSync(registryPath, backupPath);
|
||||
return backupPath;
|
||||
return createBackupFile(registryPath, 'Registry20 does not exist');
|
||||
}
|
||||
|
||||
static restore (registryPath: string, backupFileName: string): void {
|
||||
const dir = path.dirname(registryPath);
|
||||
const backupPath = path.join(dir, backupFileName);
|
||||
if (!fs.existsSync(backupPath)) {
|
||||
throw new Error('Backup file not found');
|
||||
const backupPath = resolveBackupPath(registryPath, backupFileName, Registry20Utils.getBackups(registryPath));
|
||||
// 内容校验: DPAPI 密文不可能为空
|
||||
if (fs.statSync(backupPath).size === 0) {
|
||||
throw new Error('Invalid backup content');
|
||||
}
|
||||
fs.copyFileSync(backupPath, registryPath);
|
||||
}
|
||||
@@ -147,7 +235,7 @@ export class MachineInfoUtils {
|
||||
* 获取 machine-info 文件路径
|
||||
*/
|
||||
static getMachineInfoPath (dataPath: string): string {
|
||||
return path.join(dataPath, 'nt_qq', 'global', 'nt_data', 'msf', 'machine-info');
|
||||
return path.join(getGlobalDataPath(dataPath), 'nt_data', 'msf', 'machine-info');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,24 +331,20 @@ export class MachineInfoUtils {
|
||||
* 创建备份
|
||||
*/
|
||||
static backup (machineInfoPath: string): string {
|
||||
if (!fs.existsSync(machineInfoPath)) {
|
||||
throw new Error('machine-info file does not exist');
|
||||
}
|
||||
const timestamp = new Date().toISOString().replace(/[-:T.]/g, '').slice(0, 14);
|
||||
const backupPath = `${machineInfoPath}.bak.${timestamp}`;
|
||||
fs.copyFileSync(machineInfoPath, backupPath);
|
||||
return backupPath;
|
||||
return createBackupFile(machineInfoPath, 'machine-info file does not exist');
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复备份
|
||||
*/
|
||||
static restore (machineInfoPath: string, backupFileName: string): void {
|
||||
const dir = path.dirname(machineInfoPath);
|
||||
const backupPath = path.join(dir, backupFileName);
|
||||
if (!fs.existsSync(backupPath)) {
|
||||
throw new Error('Backup file not found');
|
||||
}
|
||||
const backupPath = resolveBackupPath(
|
||||
machineInfoPath,
|
||||
backupFileName,
|
||||
MachineInfoUtils.getBackups(machineInfoPath)
|
||||
);
|
||||
// 内容校验: 必须符合 machine-info 的 be32 长度 + ROT13 结构
|
||||
MachineInfoUtils.readMac(backupPath);
|
||||
fs.copyFileSync(backupPath, machineInfoPath);
|
||||
}
|
||||
|
||||
@@ -273,3 +357,506 @@ export class MachineInfoUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// macOS TEA 容器 / machineid-info 工具类
|
||||
// ============================================================
|
||||
|
||||
const TEA_DELTA = 0x9E3779B9;
|
||||
|
||||
function teaKeyWords (key: Buffer): [number, number, number, number] {
|
||||
const padded = Buffer.concat([key, Buffer.alloc(Math.max(0, 16 - key.length))]).subarray(0, 16);
|
||||
return [
|
||||
padded.readUInt32BE(0),
|
||||
padded.readUInt32BE(4),
|
||||
padded.readUInt32BE(8),
|
||||
padded.readUInt32BE(12),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 单块 TEA 加密 (16 轮, delta 0x9E3779B9, 密钥与数据均按大端处理)
|
||||
* 逆向自 macOS wrapper.node sub_4127C1C
|
||||
*/
|
||||
export function teaEncryptBlock (block: Buffer, key: Buffer): Buffer {
|
||||
if (block.length !== 8) {
|
||||
throw new Error('TEA block must be 8 bytes');
|
||||
}
|
||||
let v0 = block.readUInt32BE(0);
|
||||
let v1 = block.readUInt32BE(4);
|
||||
const [k0, k1, k2, k3] = teaKeyWords(key);
|
||||
let sum = TEA_DELTA;
|
||||
for (let i = 0; i < 16; i++) {
|
||||
v0 = (v0 + ((((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >>> 5) + k1)) >>> 0)) >>> 0;
|
||||
v1 = (v1 + ((((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >>> 5) + k3)) >>> 0)) >>> 0;
|
||||
sum = (sum + TEA_DELTA) >>> 0;
|
||||
}
|
||||
const out = Buffer.alloc(8);
|
||||
out.writeUInt32BE(v0, 0);
|
||||
out.writeUInt32BE(v1, 4);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单块 TEA 解密 -> sub_4127CE5
|
||||
*/
|
||||
export function teaDecryptBlock (block: Buffer, key: Buffer): Buffer {
|
||||
if (block.length !== 8) {
|
||||
throw new Error('TEA block must be 8 bytes');
|
||||
}
|
||||
let v0 = block.readUInt32BE(0);
|
||||
let v1 = block.readUInt32BE(4);
|
||||
const [k0, k1, k2, k3] = teaKeyWords(key);
|
||||
let sum = (TEA_DELTA * 16) >>> 0;
|
||||
for (let i = 0; i < 16; i++) {
|
||||
v1 = (v1 - ((((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >>> 5) + k3)) >>> 0)) >>> 0;
|
||||
v0 = (v0 - ((((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >>> 5) + k1)) >>> 0)) >>> 0;
|
||||
sum = (sum - TEA_DELTA) >>> 0;
|
||||
}
|
||||
const out = Buffer.alloc(8);
|
||||
out.writeUInt32BE(v0, 0);
|
||||
out.writeUInt32BE(v1, 4);
|
||||
return out;
|
||||
}
|
||||
|
||||
function xor8 (a: Buffer, b: Buffer): Buffer {
|
||||
const out = Buffer.alloc(8);
|
||||
for (let i = 0; i < 8; i++) out[i] = a[i]! ^ b[i]!;
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* QQ 加密容器封装 -> sub_414B658
|
||||
* P_i = X_i XOR C_(i-1)
|
||||
* C_i = E(P_i) XOR P_(i-1)
|
||||
* X = [head][v8 random][2 random] + payload + 00*7
|
||||
*/
|
||||
export function containerEncrypt (payload: Buffer, key: Buffer, rng: (n: number) => Buffer = crypto.randomBytes): Buffer {
|
||||
const n = payload.length;
|
||||
// 注意: Python 的 % 结果非负, JS 对负数会返回负数, 这里手动取模
|
||||
const v8 = ((8 - ((n + 10) % 8)) % 8);
|
||||
const head = Buffer.from([(rng(1)[0]! & 0xF8) | v8]);
|
||||
const stream = Buffer.concat([head, rng(v8), rng(2), payload, Buffer.alloc(7)]);
|
||||
const out = Buffer.alloc(stream.length);
|
||||
let pPrev: Buffer = Buffer.alloc(8);
|
||||
let cPrev: Buffer = Buffer.alloc(8);
|
||||
for (let i = 0; i < stream.length; i += 8) {
|
||||
const xI = stream.subarray(i, i + 8);
|
||||
const pI = xor8(xI, cPrev);
|
||||
const cI = xor8(teaEncryptBlock(pI, key), pPrev);
|
||||
cI.copy(out, i);
|
||||
pPrev = pI;
|
||||
cPrev = cI;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* QQ 加密容器解封 -> sub_4128416, 失败返回 null
|
||||
*/
|
||||
export function containerDecrypt (data: Buffer, key: Buffer, checkTail = true): Buffer | null {
|
||||
if (data.length < 16 || data.length % 8 !== 0) return null;
|
||||
const stream = Buffer.alloc(data.length);
|
||||
let pPrev: Buffer = Buffer.alloc(8);
|
||||
let cPrev: Buffer = Buffer.alloc(8);
|
||||
for (let i = 0; i < data.length; i += 8) {
|
||||
const cI = data.subarray(i, i + 8);
|
||||
const pI = i === 0 ? teaDecryptBlock(cI, key) : teaDecryptBlock(xor8(pPrev, cI), key);
|
||||
xor8(pI, cPrev).copy(stream, i);
|
||||
pPrev = pI;
|
||||
cPrev = cI;
|
||||
}
|
||||
const v8 = stream[0]! & 0x07;
|
||||
const n = data.length - v8 - 10;
|
||||
if (n < 0) return null;
|
||||
const start = v8 + 3;
|
||||
const payload = stream.subarray(start, start + n);
|
||||
const tail = stream.subarray(start + n);
|
||||
if (checkTail && tail.some((b) => b !== 0)) return null;
|
||||
return Buffer.from(payload);
|
||||
}
|
||||
|
||||
function buildFields (...chunks: Buffer[]): Buffer {
|
||||
const parts: Buffer[] = [];
|
||||
for (const chunk of chunks) {
|
||||
const len = Buffer.alloc(4);
|
||||
len.writeUInt32BE(chunk.length, 0);
|
||||
parts.push(len, chunk);
|
||||
}
|
||||
return Buffer.concat(parts);
|
||||
}
|
||||
|
||||
function parseFields (plain: Buffer): Buffer[] {
|
||||
const fields: Buffer[] = [];
|
||||
let off = 0;
|
||||
while (off + 4 <= plain.length) {
|
||||
const len = plain.readUInt32BE(off);
|
||||
off += 4;
|
||||
if (off + len > plain.length) {
|
||||
fields.push(plain.subarray(off));
|
||||
break;
|
||||
}
|
||||
fields.push(plain.subarray(off, off + len));
|
||||
off += len;
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
export interface MacMachineInfo {
|
||||
machineId: Buffer;
|
||||
sn: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* macOS machine-info 工具类
|
||||
*
|
||||
* 文件位置 (相对 QQ global 目录):
|
||||
* nt_data/msf/machineid-info —— TEA 容器, 明文 = be32(8)+machine_id[8] + be32(len)+sn
|
||||
* nt_data/msf/machine-info —— 明文 be32(8) + [MAC(6) + 00 00]
|
||||
* nt_data/msf/machine-guid —— 明文 16 字节 guid
|
||||
*/
|
||||
export class MacMachineInfoUtils {
|
||||
static getMsfDir (dataPathGlobal: string): string {
|
||||
return path.join(dataPathGlobal, 'nt_data', 'msf');
|
||||
}
|
||||
|
||||
static getMachineIdInfoPath (dataPathGlobal: string): string {
|
||||
return path.join(MacMachineInfoUtils.getMsfDir(dataPathGlobal), 'machineid-info');
|
||||
}
|
||||
|
||||
static getMachineInfoPath (dataPathGlobal: string): string {
|
||||
return path.join(MacMachineInfoUtils.getMsfDir(dataPathGlobal), 'machine-info');
|
||||
}
|
||||
|
||||
static getMachineGuidPath (dataPathGlobal: string): string {
|
||||
return path.join(MacMachineInfoUtils.getMsfDir(dataPathGlobal), 'machine-guid');
|
||||
}
|
||||
|
||||
/**
|
||||
* TEA 密钥 = IOPlatformUUID 字符串前 16 字节 (不足补 0)
|
||||
*/
|
||||
static keyFromUuid (uuid: string): Buffer {
|
||||
return Buffer.concat([Buffer.from(uuid, 'utf8').subarray(0, 16), Buffer.alloc(16)]).subarray(0, 16);
|
||||
}
|
||||
|
||||
static guidToUuidStyle (guid: Buffer, upper = true): string {
|
||||
const s = guid.toString('hex');
|
||||
const out = `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${s.slice(16, 20)}-${s.slice(20, 32)}`;
|
||||
return upper ? out.toUpperCase() : out;
|
||||
}
|
||||
|
||||
static buildMachineIdInfo (machineId: Buffer, sn: string): Buffer {
|
||||
if (machineId.length !== 8) {
|
||||
throw new Error('machine_id must be 8 bytes');
|
||||
}
|
||||
return buildFields(machineId, Buffer.from(sn, 'utf8'));
|
||||
}
|
||||
|
||||
static parseMachineIdInfo (plain: Buffer): MacMachineInfo {
|
||||
const fields = parseFields(plain);
|
||||
const machineId = fields[0]?.subarray(0, 8) ?? Buffer.alloc(0);
|
||||
const sn = fields[1] ? fields[1].toString('utf8') : '';
|
||||
return { machineId: Buffer.from(machineId), sn };
|
||||
}
|
||||
|
||||
/**
|
||||
* guid = MD5(machine_id[8] || sn) -> sub_414E158
|
||||
*/
|
||||
static computeGuid (machineId: Buffer, sn: string): Buffer {
|
||||
const md5 = crypto.createHash('md5');
|
||||
md5.update(machineId.subarray(0, 8));
|
||||
md5.update(sn, 'utf8');
|
||||
return md5.digest();
|
||||
}
|
||||
|
||||
static readMachineIdInfo (filePath: string, key: Buffer, checkTail = true): MacMachineInfo | null {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const plain = containerDecrypt(fs.readFileSync(filePath), key, checkTail);
|
||||
if (!plain) return null;
|
||||
return MacMachineInfoUtils.parseMachineIdInfo(plain);
|
||||
}
|
||||
|
||||
static writeMachineIdInfo (filePath: string, machineId: Buffer, sn: string, key: Buffer): void {
|
||||
const plain = MacMachineInfoUtils.buildMachineIdInfo(machineId, sn);
|
||||
const encrypted = containerEncrypt(plain, key);
|
||||
const dir = path.dirname(filePath);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
fs.writeFileSync(filePath, encrypted);
|
||||
}
|
||||
|
||||
/**
|
||||
* machine-info 是明文 be32(8) + 8 字节 (MAC(6) + 00 00)
|
||||
*/
|
||||
static readMachineId (filePath: string): Buffer | null {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const data = fs.readFileSync(filePath);
|
||||
if (data.length < 4) return null;
|
||||
const len = data.readUInt32BE(0);
|
||||
if (len < 8 || data.length < 4 + len) return null;
|
||||
return Buffer.from(data.subarray(4, 4 + 8));
|
||||
}
|
||||
|
||||
static readMachineGuid (filePath: string): Buffer | null {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const data = fs.readFileSync(filePath);
|
||||
if (data.length < 16) return null;
|
||||
return Buffer.from(data.subarray(0, 16));
|
||||
}
|
||||
|
||||
static getBackups (filePath: string): string[] {
|
||||
const dir = path.dirname(filePath);
|
||||
const baseName = path.basename(filePath);
|
||||
if (!fs.existsSync(dir)) return [];
|
||||
|
||||
return fs.readdirSync(dir)
|
||||
.filter(f => f.startsWith(`${baseName}.bak.`))
|
||||
.sort()
|
||||
.reverse();
|
||||
}
|
||||
|
||||
static backup (filePath: string): string {
|
||||
return createBackupFile(filePath, 'machineid-info file does not exist');
|
||||
}
|
||||
|
||||
static restore (filePath: string, backupFileName: string): void {
|
||||
const backupPath = resolveBackupPath(filePath, backupFileName, MacMachineInfoUtils.getBackups(filePath));
|
||||
// 内容校验: TEA 容器按 8 字节分组, 至少 16 字节
|
||||
const size = fs.statSync(backupPath).size;
|
||||
if (size < 16 || size % 8 !== 0) {
|
||||
throw new Error('Invalid backup content');
|
||||
}
|
||||
fs.copyFileSync(backupPath, filePath);
|
||||
}
|
||||
|
||||
static delete (filePath: string): void {
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// macOS 本机硬件读取 (与 wrapper.node 的行为保持一致)
|
||||
// ============================================================
|
||||
|
||||
interface HardwareCacheEntry<T> {
|
||||
promise: Promise<T>;
|
||||
}
|
||||
|
||||
const hardwareCache = new Map<string, HardwareCacheEntry<unknown>>();
|
||||
|
||||
/**
|
||||
* 进程内硬件信息缓存。硬件标识在进程生命周期内不会变化,
|
||||
* 缓存 `Promise` 可避免并发请求重复拉起 `ioreg`/`diskutil`。
|
||||
*/
|
||||
function cachedHardware<T> (key: string, factory: () => Promise<T>): Promise<T> {
|
||||
const existing = hardwareCache.get(key);
|
||||
if (existing) return existing.promise as Promise<T>;
|
||||
const entry: HardwareCacheEntry<T> = { promise: factory() };
|
||||
hardwareCache.set(key, entry as HardwareCacheEntry<unknown>);
|
||||
return entry.promise;
|
||||
}
|
||||
|
||||
function runCommand (file: string, args: string[]): Promise<string | null> {
|
||||
return new Promise((resolve) => {
|
||||
execFile(file, args, { timeout: 10000, encoding: 'utf8', maxBuffer: 4 * 1024 * 1024 }, (error, stdout) => {
|
||||
if (error) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
resolve(stdout);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 `ioreg` 输出中解析 `"property" = <value>`。
|
||||
*/
|
||||
function parseIoregProperty (out: string, property: string): string | null {
|
||||
for (const line of out.split('\n')) {
|
||||
if (line.includes(`"${property}"`) && line.includes('=')) {
|
||||
const raw = line.split('=').slice(1).join('=').trim();
|
||||
return raw.replace(/^"|"$/g, '');
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行 `ioreg -rd1 -c <className>` 并缓存结果。
|
||||
*/
|
||||
function ioregClassOutput (className: string): Promise<string | null> {
|
||||
return cachedHardware(`ioreg:${className}`, () => runCommand('ioreg', ['-rd1', '-c', className]));
|
||||
}
|
||||
|
||||
/**
|
||||
* TEA 密钥来源: IOPlatformUUID
|
||||
*/
|
||||
export function getMacPlatformUuid (): Promise<string | null> {
|
||||
return cachedHardware('mac:platformUuid', async () => {
|
||||
const out = await ioregClassOutput('IOPlatformExpertDevice');
|
||||
return out ? parseIoregProperty(out, 'IOPlatformUUID') : null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列号回退: IOPlatformSerialNumber
|
||||
*/
|
||||
export function getMacSerialNumber (): Promise<string | null> {
|
||||
return cachedHardware('mac:serialNumber', async () => {
|
||||
const out = await ioregClassOutput('IOPlatformExpertDevice');
|
||||
return out ? parseIoregProperty(out, 'IOPlatformSerialNumber') : null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 优先 /dev/disk0 的序列号 (对应 wrapper.node sub_414CD1C)
|
||||
*/
|
||||
export function getMacDiskSerial (): Promise<string | null> {
|
||||
return cachedHardware('mac:diskSerial', async () => {
|
||||
const out = await runCommand('diskutil', ['info', '/dev/disk0']);
|
||||
if (!out) return null;
|
||||
for (const line of out.split('\n')) {
|
||||
if (line.includes('Serial Number') || line.includes('设备序列号')) {
|
||||
return line.split(':').slice(1).join(':').trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* macOS 序列号: 优先 /dev/disk0, 失败回退 IOPlatformSerialNumber
|
||||
*/
|
||||
export async function getMacSn (): Promise<string> {
|
||||
return (await getMacDiskSerial()) ?? (await getMacSerialNumber()) ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 ioreg 节点行中提取 `"IOMACAddress" = <...>` 的 6 字节 MAC。
|
||||
*/
|
||||
function parseIomacAddress (line: string): Buffer | null {
|
||||
const blob = line.split('=').slice(1).join('=');
|
||||
const hex = (blob.match(/[0-9a-fA-F]{2}/g) ?? []).join('');
|
||||
if (hex.length >= 12) {
|
||||
return Buffer.from(hex.slice(0, 12), 'hex');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 按 `+-o ` 节点行切分 ioreg 输出。 */
|
||||
function splitIoregNodes (out: string): string[] {
|
||||
const nodes: string[] = [];
|
||||
let current: string[] = [];
|
||||
for (const line of out.split('\n')) {
|
||||
if (line.includes('+-o ')) {
|
||||
if (current.length > 0) nodes.push(current.join('\n'));
|
||||
current = [line];
|
||||
} else if (current.length > 0) {
|
||||
current.push(line);
|
||||
}
|
||||
}
|
||||
if (current.length > 0) nodes.push(current.join('\n'));
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/** 从节点文本中取 IOMACAddress。 */
|
||||
function nodeMac (node: string): Buffer | null {
|
||||
const line = node.split('\n').find((l) => l.includes('"IOMACAddress"'));
|
||||
return line ? parseIomacAddress(line) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主网卡 MAC。
|
||||
*
|
||||
* wrapper.node 取的是 IOEthernetInterface 上(或其父控制器上)的 IOMACAddress。
|
||||
* 真实 ioreg 层级里 IOMACAddress 通常挂在父级控制器(如 en0 对应的
|
||||
* IOEthernetController)上,且同一层级下可能有多个接口,因此这里:
|
||||
* 1. 优先匹配 BSD Name 为 en0 的接口;
|
||||
* 2. 其次匹配第一个 USB/以太网控制器上的接口;
|
||||
* 3. 若接口自身没有该属性,则回到 `ioreg -c IOEthernetInterface` 的整个子树中查找,
|
||||
* 兼容 IOMACAddress 挂在父级控制器的情况。
|
||||
*/
|
||||
export function getMacPrimaryMac (): Promise<Buffer | null> {
|
||||
return cachedHardware('mac:primaryMac', async () => {
|
||||
const ifOut = await ioregClassOutput('IOEthernetInterface');
|
||||
const nodes = ifOut ? splitIoregNodes(ifOut) : [];
|
||||
const en0 = nodes.find((node) => node.includes('"BSD Name" = "en0"'));
|
||||
|
||||
// 1. en0 接口自身的 IOMACAddress
|
||||
if (en0) {
|
||||
const mac = nodeMac(en0);
|
||||
if (mac) return mac;
|
||||
}
|
||||
|
||||
// 2. 父级以太网控制器上的 IOMACAddress(真实机器上多数情况下挂在这里)
|
||||
const controllerOut = await ioregClassOutput('IOEthernetController');
|
||||
if (controllerOut) {
|
||||
for (const node of splitIoregNodes(controllerOut)) {
|
||||
const mac = nodeMac(node);
|
||||
if (mac) return mac;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 任意 IOEthernetInterface 自身的 IOMACAddress
|
||||
for (const node of nodes) {
|
||||
const mac = nodeMac(node);
|
||||
if (mac) return mac;
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
export interface MacGuidInfo {
|
||||
guid: string;
|
||||
guidUuid: string;
|
||||
machineId: string;
|
||||
sn: string;
|
||||
mac: string;
|
||||
platformUuid: string;
|
||||
source: 'machineid-info' | 'mac-fallback';
|
||||
}
|
||||
|
||||
/**
|
||||
* 主流程 (对应 wrapper.node GetMachineGuid4MacOS):
|
||||
* guid = MD5(machine_id[8] || sn)
|
||||
* machine_id 优先从 machineid-info (TEA) 读取, 否则回退成 MAC + 00 00
|
||||
*/
|
||||
export async function computeMacGuidInfo (dataPathGlobal: string, uuidOverride?: string, snOverride?: string): Promise<MacGuidInfo> {
|
||||
const platformUuid = uuidOverride ?? (await getMacPlatformUuid()) ?? '';
|
||||
const key = MacMachineInfoUtils.keyFromUuid(platformUuid);
|
||||
const sn = snOverride ?? (await getMacSn());
|
||||
const machineIdInfoPath = MacMachineInfoUtils.getMachineIdInfoPath(dataPathGlobal);
|
||||
const primaryMac = await getMacPrimaryMac();
|
||||
|
||||
let machineId: Buffer | null = null;
|
||||
let source: MacGuidInfo['source'] = 'machineid-info';
|
||||
let snValue = sn;
|
||||
|
||||
const fromFile = MacMachineInfoUtils.readMachineIdInfo(machineIdInfoPath, key);
|
||||
if (fromFile && fromFile.machineId.length === 8) {
|
||||
machineId = fromFile.machineId;
|
||||
if (!snOverride && fromFile.sn) snValue = fromFile.sn;
|
||||
} else {
|
||||
const mac = primaryMac;
|
||||
if (!mac) {
|
||||
throw new Error('Unable to determine machine_id (no machineid-info and no MAC)');
|
||||
}
|
||||
machineId = Buffer.concat([mac, Buffer.from([0, 0])]);
|
||||
source = 'mac-fallback';
|
||||
}
|
||||
|
||||
const guid = MacMachineInfoUtils.computeGuid(machineId, snValue);
|
||||
return {
|
||||
guid: guid.toString('hex'),
|
||||
guidUuid: MacMachineInfoUtils.guidToUuidStyle(guid),
|
||||
machineId: machineId.toString('hex'),
|
||||
sn: snValue,
|
||||
mac: primaryMac?.toString('hex') ?? '',
|
||||
platformUuid,
|
||||
source,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user