From cdda5f45ee7ab976a6ebae255c62475e2ab84078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 18 Sep 2024 11:19:29 +0800 Subject: [PATCH] refactor: guid fk tx --- src/common/system.ts | 52 -------------------- src/core/index.ts | 12 +++-- src/core/services/NodeIKernelLoginService.ts | 2 + src/shell/napcat.ts | 7 +++ 4 files changed, 18 insertions(+), 55 deletions(-) diff --git a/src/common/system.ts b/src/common/system.ts index bb69edd0..a79a4c72 100644 --- a/src/common/system.ts +++ b/src/common/system.ts @@ -1,12 +1,8 @@ import os from 'node:os'; import path from 'node:path'; -import { networkInterfaces } from 'os'; -import { randomUUID } from 'crypto'; // 缓解Win7设备兼容性问题 let osName: string; -// 设备ID -let machineId: Promise; try { osName = os.hostname(); @@ -14,54 +10,6 @@ try { osName = 'NapCat'; // + crypto.randomUUID().substring(0, 4); } -const invalidMacAddresses = new Set([ - '00:00:00:00:00:00', - 'ff:ff:ff:ff:ff:ff', - 'ac:de:48:00:11:22', -]); - -function validateMacAddress(candidate: string): boolean { - // eslint-disable-next-line no-useless-escape - const tempCandidate = candidate.replace(/\-/g, ':').toLowerCase(); - return !invalidMacAddresses.has(tempCandidate); -} - -export async function getMachineId(): Promise { - if (!machineId) { - machineId = (async () => { - const id = await getMacMachineId(); - return id ?? randomUUID(); // fallback, generate a UUID - })(); - } - - return machineId; -} - -export function getMac(): string { - const ifaces = networkInterfaces(); - for (const name in ifaces) { - const networkInterface = ifaces[name]; - if (networkInterface) { - for (const { mac } of networkInterface) { - if (validateMacAddress(mac)) { - return mac; - } - } - } - } - - throw new Error('Unable to retrieve mac address (unexpected format)'); -} - -async function getMacMachineId(): Promise { - try { - const crypto = await import('crypto'); - const macAddress = getMac(); - return crypto.createHash('sha256').update(macAddress, 'utf8').digest('hex'); - } catch (err) { - return undefined; - } -} const homeDir = os.homedir(); diff --git a/src/core/index.ts b/src/core/index.ts index 2cd311f0..475573ce 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -22,7 +22,7 @@ import { QQBasicInfoWrapper } from '@/common/qq-basic-info'; import { NapCatPathWrapper } from '@/common/path'; import path from 'node:path'; import fs from 'node:fs'; -import { getMachineId, hostname, systemName, systemVersion } from '@/common/system'; +import { hostname, systemName, systemVersion } from '@/common/system'; import { NTEventWrapper } from '@/common/event'; import { DataSource, GroupMember, KickedOffLineInfo, SelfInfo, SelfStatusInfo } from '@/core/entities'; import { NapCatConfigLoader } from '@/core/helper/config'; @@ -247,10 +247,16 @@ export class NapCatCore { } } -export async function genSessionConfig(QQVersionAppid: string, QQVersion: string, selfUin: string, selfUid: string, account_path: string): Promise { +export async function genSessionConfig( + guid:string, + QQVersionAppid: string, + QQVersion: string, + selfUin: string, + selfUid: string, + account_path: string +): Promise { const downloadPath = path.join(account_path, 'NapCat', 'temp'); fs.mkdirSync(downloadPath, { recursive: true }); - const guid: string = await getMachineId();//26702 支持JS获取guid值 在LoginService中获取 TODO mlikiow a //os.platform() let systemPlatform = PlatformType.KWINDOWS; switch (os.platform()) { diff --git a/src/core/services/NodeIKernelLoginService.ts b/src/core/services/NodeIKernelLoginService.ts index d474006f..895ea2aa 100644 --- a/src/core/services/NodeIKernelLoginService.ts +++ b/src/core/services/NodeIKernelLoginService.ts @@ -59,6 +59,8 @@ export interface QuickLoginResult { } export interface NodeIKernelLoginService { + getMachineGuid(): string; + get(): NodeIKernelLoginService; connect(): boolean; diff --git a/src/shell/napcat.ts b/src/shell/napcat.ts index 6ef11355..c238d715 100644 --- a/src/shell/napcat.ts +++ b/src/shell/napcat.ts @@ -52,6 +52,7 @@ export async function NCoreInitShell() { const engine = wrapper.NodeIQQNTWrapperEngine.get(); //const util = wrapper.NodeQQNTWrapperUtil.get(); const loginService = wrapper.NodeIKernelLoginService.get(); + const session = wrapper.NodeIQQNTWrapperSession.create(); // from get dataPath @@ -237,10 +238,16 @@ export async function NCoreInitShell() { let amgomDataPiece = 'eb1fd6ac257461580dc7438eb099f23aae04ca679f4d88f53072dc56e3bb1129'; o3Service.setAmgomDataPiece(basicInfoWrapper.QQVersionAppid, new Uint8Array(Buffer.from(amgomDataPiece, 'hex'))); // AFTER LOGGING IN + //99b15bdb4c984fc69d5aa1feb9aa16xx --> 99b15bdb-4c98-4fc6-9d5a-a1feb9aa16xx + //把guid从左向右转换为guid格式 loginService.getMachineGuid() + let guid = loginService.getMachineGuid(); + guid = guid.slice(0, 8) + '-' + guid.slice(8, 12) + '-' + guid.slice(12, 16) + '-' + guid.slice(16, 20) + '-' + guid.slice(20); + console.log('guid:', guid); // from initSession await new Promise(async (resolve, reject) => { const sessionConfig = await genSessionConfig( + guid, basicInfoWrapper.QQVersionAppid!, basicInfoWrapper.getFullQQVesion(), selfInfo.uin,