Overview
The NativePacketHandler class provides low-level access to QQ protocol packets by hooking into NTQQ’s native send/receive functions. This enables developers to inspect, monitor, and analyze raw protocol data flowing through the QQ client.
The NativePacketHandler uses native binary modules and requires platform-specific binaries. Improper use can cause crashes or unexpected behavior.
Architecture
The packet handler is implemented in packages/napcat-core/packet/handler/client.ts:28 and uses the MoeHoo native module to hook packet transmission.
win32.x64 - Windows 64-bit
linux.x64 - Linux 64-bit
linux.arm64 - Linux ARM64
darwin.x64 - macOS Intel
darwin.arm64 - macOS Apple Silicon
Packet Types
Initialization
The packet handler requires version-specific memory offsets defined in packet.json:
Offsets are stored as {version}-{build}-{arch}:
Listening to Packets
Basic Listeners
The handler provides flexible event registration methods:
Listen to All Packets
Listen by Packet Type
Listen by Command
Exact Matching
One-Time Listeners
All listener methods have once variants that auto-remove after first trigger:
Listener Priority
When a packet is received, listeners are triggered in this order:
- Exact match -
exact:type:cmd
- Command match -
cmd:xxx
- Type match -
type:0 or type:1
- Global -
all
From client.ts:166:
Managing Listeners
Removing Listeners
Practical Examples
Monitor Message Flow
Packet Statistics
Debug Specific Protocol Flow
While the current implementation doesn’t support modifying packets, you can log and analyze them:
Common Protocol Commands
Some frequently seen commands:
trpc.msg.olpush.OlPushService.MsgPush - Incoming messages
trpc.msg.msg_svc.MsgService.SendMsg - Outgoing messages
trpc.group.manage.GroupManage.* - Group management
trpc.highway.* - File transfers
trpc.msg.register_proxy.RegisterProxy.* - Status sync
Error Handling
From client.ts:180, listener errors are logged but don’t propagate:
Best Practices
- Always check initialization: Verify
init() returns true before registering listeners
- Clean up listeners: Use the returned unsubscribe functions to prevent memory leaks
- Use specific listeners: Prefer
onCmd() or onExact() over onAll() for better performance
- Handle hex data carefully: Convert to Buffer before processing binary data
- Test across platforms: Native modules behave differently on each OS
Limitations
- Read-only: Cannot modify packets in-flight (current implementation)
- Platform-dependent: Requires correct binary for your OS/arch
- Version-specific: Memory offsets change with each QQ update
- No protobuf parsing: Hex data must be decoded separately