fix: 日志乱飞版本

This commit is contained in:
手瓜一十雪 2024-11-05 11:24:36 +08:00
parent 000ef55273
commit f9996a9987
3 changed files with 33 additions and 14 deletions

View File

@ -1,6 +1,6 @@
export class LRUCache<K, V> {
private capacity: number;
private cache: Map<K, V>;
public cache: Map<K, V>;
constructor(capacity: number) {
this.capacity = capacity;

View File

@ -59,6 +59,7 @@ export abstract class PacketClient {
private async registerCallback(trace_id: string, type: string, callback: (json: RecvPacketData) => Promise<void>): Promise<void> {
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));
});

View File

@ -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<number, string>(500);//seq->trace_id
protected constructor(core: NapCatCore) {
super(core);
}
@ -47,20 +48,36 @@ export class NativePacketClient extends PacketClient {
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}`);
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<void> {