@decentnetwork/peer 0.1.69 → 0.1.70

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/dist/peer.js +30 -2
  2. package/package.json +1 -1
package/dist/peer.js CHANGED
@@ -82,6 +82,18 @@ const FRIEND_PING_INTERVAL_MS = readEnvInt("DECENT_FRIEND_PING_INTERVAL_MS", 400
82
82
  // slow enough not to burn CPU on idle peers. Tunable via env.
83
83
  const FRIEND_CONNECTION_LOOP_MS = readEnvInt("DECENT_FRIEND_CONNECTION_LOOP_MS", 250);
84
84
  const FRIEND_TIMEOUT_MS = readEnvInt("DECENT_FRIEND_TIMEOUT_MS", 32000);
85
+ // A session whose keys we've PROVEN (decrypted ≥1 real packet from the peer) is
86
+ // NOT torn down at FRIEND_TIMEOUT_MS. A transient all-transport blackout — e.g.
87
+ // the GFW throttling a China<->abroad path for tens of seconds — must not trigger
88
+ // a re-handshake: an ASYMMETRIC re-handshake (one side re-keys while the other
89
+ // keeps its session) is exactly what desyncs the two sessions and produces the
90
+ // connect/disconnect CHURN that breaks the exit. Instead we keep the proven keys
91
+ // and keep probing over every transport; when the path recovers, a single packet
92
+ // resumes the SAME session with no re-handshake. Only after this much longer hard
93
+ // window do we treat the path as truly dead and re-handshake. (Unproven sessions
94
+ // — established but never carried a packet — still tear down at FRIEND_TIMEOUT_MS
95
+ // so a wedged handshake self-heals.)
96
+ const PROVEN_SESSION_HARD_TIMEOUT_MS = readEnvInt("DECENT_PROVEN_SESSION_HARD_TIMEOUT_MS", 180_000);
85
97
  // How long the direct UDP path may go without an inbound packet before we
86
98
  // release a physical-LAN lock (so a peer that left the LAN can recover onto the
87
99
  // public/relay path). 12s ≈ 3 missed 4s keepalives — long enough to never trip
@@ -2539,8 +2551,24 @@ export class Peer {
2539
2551
  // non-established branch below, which re-initiates a fresh
2540
2552
  // connection — turning a permanent wedge into a ~timeout self-heal.
2541
2553
  const lastAlive = session.lastPingRecvMs ?? session.sessionEstablishedAtMs ?? now;
2542
- if (now - lastAlive > FRIEND_TIMEOUT_MS) {
2543
- this.#debugLog(`session timeout for ${friendId} (no ping in ${now - lastAlive}ms; ` +
2554
+ const silentFor = now - lastAlive;
2555
+ // Proven = we've decrypted ≥1 real packet, so the keys are known-good and
2556
+ // a silence is a transport blackout (GFW), not a desync. Give proven
2557
+ // sessions a long grace so a blackout doesn't churn them via re-handshake;
2558
+ // unproven sessions still time out fast to self-heal a wedged handshake.
2559
+ const proven = session.lastPingRecvMs !== undefined;
2560
+ const deadline = proven ? PROVEN_SESSION_HARD_TIMEOUT_MS : FRIEND_TIMEOUT_MS;
2561
+ if (proven && silentFor > FRIEND_TIMEOUT_MS && silentFor <= deadline) {
2562
+ // Blackout grace: keep the proven session and keep probing (the
2563
+ // keepalive + UDP re-punch + relay-keepalive below all still run). Do
2564
+ // NOT delete or re-handshake — that's what desyncs and churns. The peer,
2565
+ // running the same logic, also keeps its half, so when the path recovers
2566
+ // both resume in sync. Status stays as-is; the exit's own health probe
2567
+ // (multi-exit router) handles failover meanwhile.
2568
+ this.#debugVerboseLog(`session blackout-grace ${friendId} (silent ${silentFor}ms, proven — keeping keys, not re-handshaking)`);
2569
+ }
2570
+ else if (silentFor > deadline) {
2571
+ this.#debugLog(`session timeout for ${friendId} (no ping in ${silentFor}ms, proven=${proven}; ` +
2544
2572
  `lastPingRecv=${session.lastPingRecvMs ?? "never"}) — tearing down to re-handshake`);
2545
2573
  this.#friendSessions.delete(friendId);
2546
2574
  this.#setFriendOffline(friendId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decentnetwork/peer",
3
- "version": "0.1.69",
3
+ "version": "0.1.70",
4
4
  "description": "Pure TypeScript port of Elastos Carrier (toxcore-derived) P2P messaging. DHT, onion routing, TCP relay, FlatBuffers app payloads, Express offline relay. Wire-compatible with iOS Beagle and the Carrier C SDK.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",