fix: packet send/recv

This commit is contained in:
手瓜一十雪 2024-10-12 19:51:29 +08:00
parent bb8b06c044
commit cdd00d665d
4 changed files with 31 additions and 25 deletions

View File

@ -24,7 +24,9 @@ export class LRUCache<K, V> {
} else if (this.cache.size >= this.capacity) {
// If the cache is full, remove the least recently used key (the first one in the map)
const firstKey = this.cache.keys().next().value;
this.cache.delete(firstKey);
if (firstKey !== undefined) {
this.cache.delete(firstKey);
}
}
this.cache.set(key, value);
}

View File

@ -15,7 +15,16 @@ import { NTEventWrapper } from '@/common/event';
import { encodeGroupPoke } from '../proto/Poke';
import { randomUUID } from 'crypto';
import { RequestUtil } from '@/common/request';
interface recvPacket
{
type: string,//仅recv
trace_id_md5?: string,
data: {
seq: number,
hex_data: string,
cmd: string
}
}
export class NTQQGroupApi {
context: InstanceContext;
core: NapCatCore;
@ -39,12 +48,9 @@ export class NTQQGroupApi {
this.context.logger.logDebug(`加载${this.groups.length}个群组缓存完成`);
console.log('pid', process.pid);
// this.session = await frida.attach(process.pid);
// setTimeout(async () => {
// let data = Buffer.from('089601', 'hex').toString('utf-8');//optional int32 a = 1;
// console.log('data', Buffer.from(data).toString('hex'));
// let ret = await this.core.context.session.getMsgService().sendSsoCmdReqByContend("OidbSvcTrpcTcp.0xfe1_2", data);
// console.log('sendSsoCmdReqByContend', ret);
// }, 20000);
setTimeout(async () => {
this.sendPocketRkey();
}, 10000);
}
async getCoreAndBaseInfo(uids: string[]) {
return await this.core.eventWrapper.callNoListenerEvent(
@ -53,6 +59,11 @@ export class NTQQGroupApi {
uids,
);
}
async sendPocketRkey() {
let hex = '08E7A00210CA01221D0A130A05080110CA011206A80602B006011A0208022206080A081408022A006001';
let ret = await this.core.apis.PacketApi.sendPacket('OidbSvcTrpcTcp.0x9067_202', hex, true);
console.log('ret: ', ret);
}
async sendPacketPoke(group: number, peer: number) {
let data = encodeGroupPoke(group, peer);
let hex = Buffer.from(data).toString('hex');

View File

@ -53,16 +53,7 @@ export class NTQQPacketApi {
async sendPacket(cmd: string, data: string, rsp = false) {
// wtfk tx
// 校验失败和异常 可能返回undefined
return new Promise<undefined | {
type: string,//仅recv含有data
trace_id: string,
data: {
trace_id: string,
seq: number,
hex_data: string,
cmd: string
}
}>((resolve, reject) => {
return new Promise((resolve, reject) => {
if (!this.isInit || !this.PacketClient?.isConnected) {
this.core.context.logger.logError('PacketClient is not init');
return undefined;

View File

@ -1,13 +1,15 @@
import { LogWrapper } from "@/common/log";
import { LRUCache } from "@/common/lru-cache";
import WebSocket from "ws";
import { createHash } from "crypto";
export class PacketClient {
private websocket: WebSocket | undefined;
public isConnected: boolean = false;
private reconnectAttempts: number = 0;
private maxReconnectAttempts: number = 5;
private cb = new LRUCache<string, { type: string, callback: any }>(500);
//trace_id-type callback
private cb = new LRUCache<string, any>(500);
constructor(private url: string, public logger: LogWrapper) { }
connect(): Promise<void> {
@ -51,7 +53,7 @@ export class PacketClient {
}
}
async registerCallback(trace_id: string, type: string, callback: any): Promise<void> {
this.cb.put(trace_id, { type: type, callback: callback });
this.cb.put(createHash('md5').update(trace_id).digest('hex') + type, callback);
}
async init(pid: number, recv: string, send: string): Promise<void> {
@ -79,7 +81,6 @@ export class PacketClient {
data: data,
trace_id: trace_id
};
this.websocket.send(JSON.stringify(commandMessage));
if (rsp) {
this.registerCallback(trace_id, 'recv', (json: any) => {
@ -103,10 +104,11 @@ export class PacketClient {
try {
let json = JSON.parse(message.toString());
let trace_id = json.trace_id;
let event = this.cb.get(trace_id);
if (event?.type == 'all' || event?.type == json.type) {
await event?.callback(json.data);
let trace_id_md5 = json.trace_id_md5;
let action = json?.type ?? 'init';
let event = this.cb.get(trace_id_md5 + action);
if (event) {
await event(json.data);
}
//console.log("Received message:", json);
} catch (error) {