diff --git a/src/common/lru-cache.ts b/src/common/lru-cache.ts index 0e537b6a..39c879f0 100644 --- a/src/common/lru-cache.ts +++ b/src/common/lru-cache.ts @@ -1,6 +1,6 @@ export class LRUCache { private capacity: number; - private cache: Map; + public cache: Map; constructor(capacity: number) { this.capacity = capacity; diff --git a/src/core/packet/client/client.ts b/src/core/packet/client/client.ts index f348bfdb..88147054 100644 --- a/src/core/packet/client/client.ts +++ b/src/core/packet/client/client.ts @@ -59,6 +59,7 @@ export abstract class PacketClient { private async registerCallback(trace_id: string, type: string, callback: (json: RecvPacketData) => Promise): Promise { this.cb.put(createHash('md5').update(trace_id).digest('hex') + type, callback); + console.log(this.cb.cache); } private async sendCommand(cmd: string, data: string, trace_id: string, rsp: boolean = false, timeout: number = 20000, sendcb: (json: RecvPacketData) => void = () => { @@ -67,7 +68,6 @@ export abstract class PacketClient { if (!this.isAvailable) { throw new Error("Packet Service is not available"); } - this.sendCommandImpl(cmd, data, trace_id); if (rsp) { this.registerCallback(trace_id, 'recv', async (json: RecvPacketData) => { clearTimeout(timeoutHandle); @@ -81,6 +81,7 @@ export abstract class PacketClient { resolve(json); } }); + this.sendCommandImpl(cmd, data, trace_id); const timeoutHandle = setTimeout(() => { reject(new Error(`sendCommand timed out after ${timeout} ms for ${cmd} with trace_id ${trace_id}`)); }, timeout); @@ -98,6 +99,7 @@ export abstract class PacketClient { const trace_id = (this.randText(4) + md5 + data).slice(0, data.length / 2); this.sendCommand(cmd, data, trace_id, rsp, 20000, async () => { + console.log('sendPacket:', cmd, data, trace_id); await this.napCatCore.context.session.getMsgService().sendSsoCmdReqByContend(cmd, trace_id); }).then((res) => resolve(res)).catch((e: Error) => reject(e)); }); diff --git a/src/core/packet/client/nativeClient.ts b/src/core/packet/client/nativeClient.ts index 7bfb43ab..c409f443 100644 --- a/src/core/packet/client/nativeClient.ts +++ b/src/core/packet/client/nativeClient.ts @@ -6,15 +6,16 @@ import fs from "fs"; import { PacketClient } from "@/core/packet/client/client"; import { constants } from "node:os"; import { LogWrapper } from "@/common/log"; - +import { LRUCache } from "@/common/lru-cache"; +//0 send 1recv export interface NativePacketExportType { - InitHook?: (recv: string, send: string, callback: (type: number, uin: string, seq: number, cmd: string, hex_data: string) => void) => boolean; + InitHook?: (recv: string, send: string, callback: (type: number, uin: string, cmd: string, seq: number, hex_data: string) => void) => boolean; SendPacket?: (cmd: string, data: string, trace_id: string) => void; } export class NativePacketClient extends PacketClient { static supportedPlatforms = ['win32.x64']; private MoeHooExport: { exports: NativePacketExportType } = { exports: {} }; - + private sendEvent = new LRUCache(500);//seq->trace_id protected constructor(core: NapCatCore) { super(core); } @@ -46,21 +47,37 @@ export class NativePacketClient extends PacketClient { const moehoo_path = path.join(dirname(fileURLToPath(import.meta.url)), './moehoo/MoeHoo.' + platform + '.node'); process.dlopen(this.MoeHooExport, moehoo_path, constants.dlopen.RTLD_LAZY); console.log('MoeHooExport:', this.MoeHooExport); - console.log('recv:', recv, 'send:', ); - this.MoeHooExport.exports.InitHook?.(send, recv, (type: number, uin: string, seq: number, cmd: string, hex_data: string) => { - const callback = this.cb.get(createHash('md5').update(Buffer.from(hex_data, 'hex')).digest('hex') + (type === 0 ? 'send' : 'recv')); - if (callback) { - callback({ seq, cmd, hex_data }); - } else { - this.logger.logError(`Callback not found for hex_data: ${hex_data}`); + console.log('recv:', recv, 'send:',); + this.MoeHooExport.exports.InitHook?.(send, recv, (type: number, uin: string, cmd: string, seq: number, hex_data: string) => { + const trace_id = createHash('md5').update(Buffer.from(hex_data, 'hex')).digest('hex'); + if (type === 0 && this.cb.get(trace_id + 'recv')) { + //此时为send 提取seq + this.sendEvent.put(seq, trace_id); } - console.log('type:', type, 'uin:', uin, 'seq:', seq, 'cmd:', cmd, 'hex_data:', hex_data); + if (type === 1 && this.sendEvent.get(seq)) { + //此时为recv 调用callback + const trace_id = this.sendEvent.get(seq); + const callback = this.cb.get(trace_id + 'recv'); + console.log('callback:', callback, trace_id); + callback?.({ seq, cmd, hex_data }); + } + + // const callback = this.cb.get(createHash('md5').update(Buffer.from(hex_data, 'hex')).digest('hex') + (type === 0 ? 'send' : 'recv')); + // if (callback) { + // callback({ seq, cmd, hex_data }); + // } else { + // this.logger.logError(`Callback not found for hex_data: ${hex_data}`); + // } + console.log('type:', type, 'cmd:', cmd, 'trace_id:', trace_id); }); this.isAvailable = true; } sendCommandImpl(cmd: string, data: string, trace_id: string): void { - this.MoeHooExport.exports.SendPacket?.(cmd, data, crypto.createHash('md5').update(trace_id).digest('hex')); + const trace_id_md5 = createHash('md5').update(trace_id).digest('hex'); + console.log('sendCommandImpl:', cmd, data, trace_id_md5); + this.MoeHooExport.exports.SendPacket?.(cmd, data, trace_id_md5); + this.cb.get(trace_id_md5 + 'send')?.({ seq: 0, cmd, hex_data: '' }); } connect(cb: () => void): Promise {