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
+39 -1
View File
@@ -106,7 +106,9 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
// SocketClient: 20 core events (connection, messaging, presence, groups, calls, etc.)
// + 20 dynamic listeners (user handlers, plugins)
// + 10 buffer slots for high-load scenarios = 50 total
maxSocketClientListeners: 50
maxSocketClientListeners: 50,
// Unified session telemetry (reduces detection of unofficial clients)
enableUnifiedSession: true
}
export const MEDIA_PATH_MAP: { [T in MediaType]?: string } = {
@@ -189,3 +191,39 @@ export const DEFAULT_CACHE_MAX_KEYS = {
// Re-export retry constants for backwards compatibility
// Actual definitions are in retry-utils.ts to avoid ESM initialization order issues
export { RETRY_BACKOFF_DELAYS, RETRY_JITTER_FACTOR } from '../Utils/retry-utils'
// ============================================
// Time Constants
// ============================================
/**
* Time constants in milliseconds for various timing calculations.
* Used by unified session, rate limiting, and other time-based features.
*
* @example
* ```typescript
* import { TimeMs } from './Defaults'
*
* // Calculate 3 days in milliseconds
* const threeDays = 3 * TimeMs.Day
*
* // Check if 1 week has passed
* if (Date.now() - lastUpdate > TimeMs.Week) {
* // do something
* }
* ```
*/
export const TimeMs = {
/** One second in milliseconds (1,000) */
Second: 1_000,
/** One minute in milliseconds (60,000) */
Minute: 60_000,
/** One hour in milliseconds (3,600,000) */
Hour: 3_600_000,
/** One day in milliseconds (86,400,000) */
Day: 86_400_000,
/** One week in milliseconds (604,800,000) */
Week: 604_800_000,
} as const
export type TimeMsKey = keyof typeof TimeMs