feat(telemetry): add unified_session telemetry to reduce detection

Implements WhatsApp's unified_session telemetry feature to reduce
detection of unofficial clients. This is an enterprise-grade implementation
inspired by whatsmeow PR #1057 and Baileys PR #2294.

Features:
- UnifiedSessionManager class with circuit breaker protection
- Server time synchronization for accurate session IDs
- Rate limiting to prevent spam (1 minute between sends)
- Prometheus metrics integration (unified_session_sent, errors)
- Structured logging for debugging
- Configurable via SocketConfig.enableUnifiedSession
- Environment variable support (BAILEYS_UNIFIED_SESSION_ENABLED)

Trigger points (matching official WhatsApp Web):
- After successful login (CB:success)
- After successful pairing (CB:iq,,pair-success)
- When sending 'available' presence

Implementation details:
- Session ID algorithm: (now + serverOffset + 3days) % 7days
- Time constants exported from Defaults/index.ts
- Full test coverage (31 tests)

References:
- https://github.com/tulir/whatsmeow/pull/1057
- https://github.com/WhiskeySockets/Baileys/pull/2294
- https://github.com/tulir/whatsmeow/issues/810
This commit is contained in:
Claude
2026-01-24 18:56:08 +00:00
parent a55055e55e
commit 9f17567951
7 changed files with 948 additions and 3 deletions
+23
View File
@@ -229,4 +229,27 @@ export type SocketConfig = {
* WARNING: Setting to 0 disables limit and allows potential memory leaks!
*/
maxSocketClientListeners?: number
// === Unified Session Telemetry ===
/**
* Enable unified_session telemetry to reduce detection of unofficial clients.
*
* When enabled, sends time-based session identifiers that mimic official
* WhatsApp Web client behavior. This may help reduce "Your account may be
* at risk" warnings, though effectiveness is not guaranteed.
*
* Telemetry is sent at specific trigger points:
* - After successful login
* - After successful pairing
* - When sending 'available' presence
*
* Can also be controlled via environment variable:
* BAILEYS_UNIFIED_SESSION_ENABLED=true|false
*
* @default true
* @see https://github.com/tulir/whatsmeow/pull/1057
* @see https://github.com/WhiskeySockets/Baileys/pull/2294
*/
enableUnifiedSession?: boolean
}