@decentnetwork/peer 0.1.58 → 0.1.59

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 +24 -1
  2. package/package.json +1 -1
package/dist/peer.js CHANGED
@@ -90,6 +90,9 @@ const FRIEND_TIMEOUT_MS = readEnvInt("DECENT_FRIEND_TIMEOUT_MS", 32000);
90
90
  const REHS_ACCEPT_COUNT = readEnvInt("DECENT_REHS_ACCEPT_COUNT", 4);
91
91
  const REHS_ACCEPT_MS = readEnvInt("DECENT_REHS_ACCEPT_MS", 2500);
92
92
  const REHS_WINDOW_MS = readEnvInt("DECENT_REHS_WINDOW_MS", 8000);
93
+ // Re-handshake when we keep receiving undecryptable crypto data from a known
94
+ // friend's endpoint (desynced sessions), rate-limited per friend.
95
+ const REINIT_ON_DESYNC_MS = readEnvInt("DECENT_REINIT_ON_DESYNC_MS", 2000);
93
96
  // How long a relay path stays "confirmed" (carrying bulk data on its own, no
94
97
  // tcp-relay fan-out) after the last packet received over it. The relay
95
98
  // keepalive refreshes this every ~4s on a healthy path, so a generous window
@@ -1825,7 +1828,27 @@ export class Peer {
1825
1828
  this.#debugVerboseLog(`unknown messenger packet kind ${kind} from ${friendId}`);
1826
1829
  return;
1827
1830
  }
1828
- this.#debugLog(`crypto data received from ${remote.address}:${remote.port} but no session matched`);
1831
+ // We received crypto data we can't decrypt from a KNOWN friend's endpoint:
1832
+ // our two sessions are desynced (different keys) and NEITHER side will
1833
+ // re-handshake on its own, because each believes it is still established
1834
+ // (the half-open breaker can't help — no handshakes are being exchanged).
1835
+ // Kick a fresh handshake so we re-converge. Rate-limited per friend so a
1836
+ // flood of undecryptable packets doesn't spam cookie requests.
1837
+ if (!this.#remoteIsTcp(remote) && !viaRelay) {
1838
+ for (const [fid, st] of this.#friendSessions.entries()) {
1839
+ const matches = (st.remote?.host === remote.address && st.remote?.port === remote.port) ||
1840
+ (st.endpointCandidates ?? []).some((c) => c.host === remote.address && c.port === remote.port);
1841
+ if (!matches)
1842
+ continue;
1843
+ if (Date.now() - (st.lastReinitMs ?? 0) > REINIT_ON_DESYNC_MS) {
1844
+ st.lastReinitMs = Date.now();
1845
+ this.#debugLog(`no-session-matched from ${remote.address}:${remote.port} (friend ${fid}) — desynced, re-initiating handshake`);
1846
+ void this.#initiateSession(fid).catch(() => undefined);
1847
+ }
1848
+ break;
1849
+ }
1850
+ }
1851
+ this.#debugVerboseLog(`crypto data received from ${remote.address}:${remote.port} but no session matched`);
1829
1852
  return;
1830
1853
  }
1831
1854
  if (packet[0] === NET_PACKET_ONION_DATA_RESPONSE && this.#announceDataKey) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decentnetwork/peer",
3
- "version": "0.1.58",
3
+ "version": "0.1.59",
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",