mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-03-03 09:10:25 +00:00
Add Linux machine-info GUID management
Add end-to-end support for reading/writing Linux machine-info and computing GUIDs. Backend: - Introduce MachineInfoUtils (TS) for machine-info path lookup, ROT13 serialization, read/write/delete, backups, and MD5-based GUID computation. - Add a Python utility (guid.py) for CLI inspection, encode/decode, dump, and GUID computation. - Extend QQLogin API with new handlers: GetPlatformInfo, GetLinuxMAC, SetLinuxMAC, GetLinuxMachineId, ComputeLinuxGUID, GetLinuxMachineInfoBackups, CreateLinuxMachineInfoBackup, RestoreLinuxMachineInfoBackup, ResetLinuxDeviceID. Handlers include automatic backup behavior and error handling. Router: Register new QQLogin routes for platform info and Linux machine-info operations. Frontend: - Enhance guid_manager UI to detect platform and provide Linux-specific workflow (display machine-id, show/edit MAC, preview computed GUID via MD5, backup/restore/delete machine-info, and restart actions). - Add client-side MD5 (crypto-js) usage and new QQManager API methods to call the new backend endpoints. This change enables cross-platform GUID management (Windows and Linux), includes CLI tooling for low-level inspection, and adds frontend workflows for Linux device-id management.
This commit is contained in:
@@ -4,7 +4,8 @@ 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 } from '@/napcat-webui-backend/src/utils/guid';
|
||||
import { Registry20Utils, MachineInfoUtils } from '@/napcat-webui-backend/src/utils/guid';
|
||||
import os from 'node:os';
|
||||
|
||||
// 获取 Registry20 路径的辅助函数
|
||||
const getRegistryPath = () => {
|
||||
@@ -21,6 +22,19 @@ const getRegistryPath = () => {
|
||||
return Registry20Utils.getRegistryPath(dataPath);
|
||||
};
|
||||
|
||||
// 获取 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);
|
||||
};
|
||||
|
||||
// 获取QQ登录二维码
|
||||
export const QQGetQRcodeHandler: RequestHandler = async (_, res) => {
|
||||
// 判断是否已经登录
|
||||
@@ -265,4 +279,137 @@ export const QQRestartNapCatHandler: RequestHandler = async (_, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
// ============================================================
|
||||
// 平台信息 & Linux GUID 管理
|
||||
// ============================================================
|
||||
|
||||
// 获取平台信息
|
||||
export const QQGetPlatformInfoHandler: RequestHandler = async (_, res) => {
|
||||
return sendSuccess(res, { platform: os.platform() });
|
||||
};
|
||||
|
||||
// 获取 Linux MAC 地址 (从 machine-info 文件读取)
|
||||
export const QQGetLinuxMACHandler: RequestHandler = async (_, res) => {
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
const mac = MachineInfoUtils.readMac(machineInfoPath);
|
||||
return sendSuccess(res, { mac });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to get MAC: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 设置 Linux MAC 地址 (写入 machine-info 文件)
|
||||
export const QQSetLinuxMACHandler: RequestHandler = async (req, res) => {
|
||||
const { mac } = req.body;
|
||||
if (!mac || typeof mac !== 'string') {
|
||||
return sendError(res, 'MAC address is required');
|
||||
}
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
// 自动备份
|
||||
try {
|
||||
MachineInfoUtils.backup(machineInfoPath);
|
||||
} catch { }
|
||||
|
||||
MachineInfoUtils.writeMac(machineInfoPath, mac);
|
||||
return sendSuccess(res, { message: 'MAC set successfully' });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to set MAC: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取 Linux machine-id
|
||||
export const QQGetLinuxMachineIdHandler: RequestHandler = async (_, res) => {
|
||||
try {
|
||||
const machineId = MachineInfoUtils.readMachineId();
|
||||
return sendSuccess(res, { machineId });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to read machine-id: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 计算 Linux GUID (用于前端实时预览)
|
||||
export const QQComputeLinuxGUIDHandler: RequestHandler = async (req, res) => {
|
||||
const { mac, machineId } = req.body;
|
||||
try {
|
||||
// 如果没传 machineId,从 /etc/machine-id 读取
|
||||
let mid = machineId;
|
||||
if (!mid || typeof mid !== 'string') {
|
||||
try {
|
||||
mid = MachineInfoUtils.readMachineId();
|
||||
} catch {
|
||||
mid = '';
|
||||
}
|
||||
}
|
||||
// 如果没传 mac,从 machine-info 文件读取
|
||||
let macStr = mac;
|
||||
if (!macStr || typeof macStr !== 'string') {
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
macStr = MachineInfoUtils.readMac(machineInfoPath);
|
||||
} catch {
|
||||
macStr = '';
|
||||
}
|
||||
}
|
||||
const guid = MachineInfoUtils.computeGuid(mid, macStr);
|
||||
return sendSuccess(res, { guid, machineId: mid, mac: macStr });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to compute GUID: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取 Linux machine-info 备份列表
|
||||
export const QQGetLinuxMachineInfoBackupsHandler: RequestHandler = async (_, res) => {
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
const backups = MachineInfoUtils.getBackups(machineInfoPath);
|
||||
return sendSuccess(res, backups);
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to get backups: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 创建 Linux machine-info 备份
|
||||
export const QQCreateLinuxMachineInfoBackupHandler: RequestHandler = async (_, res) => {
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
const backupPath = MachineInfoUtils.backup(machineInfoPath);
|
||||
return sendSuccess(res, { message: 'Backup created', path: backupPath });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to backup: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 恢复 Linux machine-info 备份
|
||||
export const QQRestoreLinuxMachineInfoBackupHandler: RequestHandler = async (req, res) => {
|
||||
const { backupName } = req.body;
|
||||
if (!backupName) {
|
||||
return sendError(res, 'Backup name is required');
|
||||
}
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
MachineInfoUtils.restore(machineInfoPath, backupName);
|
||||
return sendSuccess(res, { message: 'Restored successfully' });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to restore: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 重置 Linux 设备信息 (删除 machine-info)
|
||||
export const QQResetLinuxDeviceIDHandler: RequestHandler = async (_, res) => {
|
||||
try {
|
||||
const machineInfoPath = getMachineInfoPath();
|
||||
// 自动备份
|
||||
try {
|
||||
MachineInfoUtils.backup(machineInfoPath);
|
||||
} catch { }
|
||||
|
||||
MachineInfoUtils.delete(machineInfoPath);
|
||||
return sendSuccess(res, { message: 'Device ID reset successfully (machine-info deleted)' });
|
||||
} catch (e) {
|
||||
return sendError(res, `Failed to reset Device ID: ${(e as Error).message}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user