whalibmob 5.5.34 → 5.5.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/Client.js +24 -11
  2. package/package.json +1 -1
package/lib/Client.js CHANGED
@@ -117,8 +117,11 @@ class WhalibmobClient extends EventEmitter {
117
117
  this._signal = null;
118
118
  this._devMgr = null;
119
119
  this._sessionDir = opts.sessionDir || process.env.HOME + '/.waSession';
120
- this._pingTimer = null;
121
- this._keepTimer = null;
120
+ this._pingTimer = null;
121
+ this._keepTimer = null;
122
+ this._watchdogTimer = null;
123
+ this._watchdogLastPingSentAt = 0; // timestamp when last keepalive ping was sent
124
+ this._watchdogPongReceived = true; // true = server responded after last ping (or no ping sent yet)
122
125
  this._connected = false;
123
126
  this._reconnecting = false;
124
127
  this._reconnectTry = 0;
@@ -259,6 +262,8 @@ class WhalibmobClient extends EventEmitter {
259
262
 
260
263
  _onOpen(successNode) {
261
264
  this._connected = true;
265
+ this._watchdogLastPingSentAt = 0;
266
+ this._watchdogPongReceived = true;
262
267
  this._startTimers();
263
268
 
264
269
  // ── Feature 1: Parse <success> node ──────────────────────────────────────
@@ -495,6 +500,9 @@ class WhalibmobClient extends EventEmitter {
495
500
 
496
501
  this._keepTimer = setInterval(() => {
497
502
  if (this._socket && this._connected) {
503
+ // Record the time we sent this ping so the watchdog can check for missing pongs.
504
+ this._watchdogLastPingSentAt = Date.now();
505
+ this._watchdogPongReceived = false;
498
506
  this._socket.sendNode(new BinaryNode('iq', {
499
507
  id: this._genMsgId(),
500
508
  to: 's.whatsapp.net',
@@ -504,18 +512,21 @@ class WhalibmobClient extends EventEmitter {
504
512
  }
505
513
  }, KEEPALIVE_INTERVAL);
506
514
 
507
- // Watchdog: if no data arrived from WhatsApp in keepalive window,
508
- // the connection is dead but TCP hasn't emitted 'close' (common with NAT drops).
509
- // Force-destroy the socket so _onTcpClose fires and reconnection kicks in.
515
+ // Watchdog: detects a truly dead TCP connection where the OS never emits 'close'
516
+ // (common with NAT/firewall drops). Logic: if we sent a keepalive ping and the
517
+ // server did NOT reply within keepalive window, the connection is dead.
518
+ // NOTE: idleness alone (bot sleeping between sends) is NOT treated as dead —
519
+ // only an unanswered ping triggers the close, so any delay is safe.
510
520
  const WATCHDOG_INTERVAL = KEEPALIVE_INTERVAL * 2 + 5000;
511
521
  this._watchdogTimer = setInterval(() => {
512
522
  if (!this._socket || !this._connected) return;
513
- const noiseSocket = this._socket;
514
- const lastRx = noiseSocket._lastRxAt || 0;
515
- const silentMs = Date.now() - lastRx;
516
- if (silentMs > WATCHDOG_INTERVAL) {
517
- _whaDbg('[DBG] WATCHDOG: no data for ' + Math.round(silentMs / 1000) + 's — force-closing dead connection');
518
- try { noiseSocket.close(); } catch (_) {}
523
+ if (!this._watchdogPongReceived &&
524
+ this._watchdogLastPingSentAt > 0 &&
525
+ Date.now() - this._watchdogLastPingSentAt > WATCHDOG_INTERVAL) {
526
+ _whaDbg('[DBG] WATCHDOG: keepalive ping sent ' +
527
+ Math.round((Date.now() - this._watchdogLastPingSentAt) / 1000) +
528
+ 's ago with no server response — force-closing dead connection');
529
+ try { this._socket.close(); } catch (_) {}
519
530
  }
520
531
  }, KEEPALIVE_INTERVAL);
521
532
  }
@@ -530,6 +541,8 @@ class WhalibmobClient extends EventEmitter {
530
541
 
531
542
  _onNode(node) {
532
543
  if (!node || !node.description) return;
544
+ // Any node from the server means the connection is alive — reset watchdog state.
545
+ this._watchdogPongReceived = true;
533
546
  const tag = node.description;
534
547
  // Debug: log every node received
535
548
  _whaDbg('[DBG] _onNode tag=' + tag + ' attrs=' + JSON.stringify(node.attrs || {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whalibmob",
3
- "version": "5.5.34",
3
+ "version": "5.5.35",
4
4
  "description": "WhatsApp library for interaction with WhatsApp Mobile API no web",
5
5
  "author": "Kunboruto20",
6
6
  "main": "index.js",