Skip to content

Commit

Permalink
fix: packet send/recv
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Oct 12, 2024
1 parent bb8b06c commit cdd00d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/common/lru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
25 changes: 18 additions & 7 deletions src/core/apis/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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');
Expand Down
11 changes: 1 addition & 10 deletions src/core/apis/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions src/core/helper/packet.ts
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down

0 comments on commit cdd00d6

Please sign in to comment.