Overview
NapCat provides low-level packet monitoring through theNativePacketHandler, allowing you to intercept and analyze QQ protocol packets. This is useful for debugging, protocol analysis, and implementing advanced features.
NativePacketHandler
Fromclient.ts:28-227, the NativePacketHandler provides a native hook into the QQ protocol layer.
Initialization
The packet handler is initialized during framework startup:Hook Modes
NapCat supports two hook modes:- Standard Mode (
o3HookMode = 0): Default hook implementation - O3 Hook Mode (
o3HookMode = 1): Alternative hooking method for specific environments
napcat.ts:63:
Packet Types
Packets are classified by direction:Packet Data Structure
Listening to Packets
Listen to All Packets
Listen by Direction
Fromclient.ts:89-103:
Listen to Specific Commands
Fromclient.ts:105-112:
One-Time Listeners
All listener methods have one-time variants:Parsing Packet Data
Packet data is provided in hexadecimal format. Use protobuf to decode:Example: Database Passphrase
Fromnapcat.ts:67-83:
Example: Message Monitoring
Common Protocol Commands
Here are some commonly monitored commands:Advanced Packet Filtering
Filter by Multiple Commands
Conditional Packet Processing
Packet Statistics
Removing Listeners
Fromclient.ts:149-161:
Packet Capture Example
Complete example for debugging:Performance Considerations
- Filter Early: Use specific
onCmdoronExactlisteners instead ofonAll - Limit Logging: Only log essential information
- Use Sampling: For high-frequency packets, sample instead of capturing all
- Clean Up: Remove listeners when no longer needed
- Avoid Heavy Processing: Keep packet handlers lightweight
Sampling Example
Debugging Protocol Issues
Capture Specific Operation
Integration with Plugins
Access packet handler from plugin context:The packet handler is available through
ctx.core.context.packetHandler in plugin contexts.Best Practices
- Always check packet type before processing (send vs receive)
- Handle parsing errors gracefully with try-catch blocks
- Remove listeners when they’re no longer needed
- Use specific listeners (onCmd, onExact) over broad ones (onAll)
- Log selectively to avoid performance impact
- Document packet formats when you discover new ones
- Test in development before deploying to production
Next Steps
Plugin Development
Build custom plugins
Deployment
Deploy to production
