iosignal 6.1.2 → 7.0.2
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.
- package/README.ko.md +54 -0
- package/README.md +57 -0
- package/dist/browser/esm/io.d.ts +1 -1
- package/dist/browser/esm/io.js +1 -1
- package/dist/browser/esm/io.js.map +1 -1
- package/dist/browser/iife/io.js +1 -1
- package/dist/browser/iife/io.js.map +1 -1
- package/dist/node/iosignal.cjs +283 -123
- package/dist/node/iosignal.js +283 -123
- package/dist/types/client/IOCore.d.ts +1 -1
- package/package.json +1 -1
package/dist/node/iosignal.js
CHANGED
|
@@ -9,12 +9,13 @@ import require$$7 from 'url';
|
|
|
9
9
|
import require$$0 from 'zlib';
|
|
10
10
|
import require$$0$1 from 'buffer';
|
|
11
11
|
import require$$2 from 'util';
|
|
12
|
+
import { performance } from 'node:perf_hooks';
|
|
12
13
|
import { networkInterfaces } from 'os';
|
|
13
14
|
import fs, { readFileSync } from 'fs';
|
|
14
15
|
import { memoryUsage } from 'process';
|
|
15
16
|
import path from 'path';
|
|
16
17
|
|
|
17
|
-
var version$1 = "
|
|
18
|
+
var version$1 = "7.0.2";
|
|
18
19
|
var pkg = {
|
|
19
20
|
version: version$1};
|
|
20
21
|
|
|
@@ -963,7 +964,7 @@ class IOCore extends EventEmitter {
|
|
|
963
964
|
|
|
964
965
|
|
|
965
966
|
/**
|
|
966
|
-
* Performs common cleanup for the connection. It
|
|
967
|
+
* Performs common cleanup for the connection. It rejects pending RPCs,
|
|
967
968
|
* resets the socket reference, and sets the state to closed.
|
|
968
969
|
* This method is guarded to only run once.
|
|
969
970
|
* If autoReconnect is false, it also clears the keep-alive timer.
|
|
@@ -998,6 +999,12 @@ class IOCore extends EventEmitter {
|
|
|
998
999
|
}
|
|
999
1000
|
this.socket = null;
|
|
1000
1001
|
}
|
|
1002
|
+
const error = new Error('Connection closed');
|
|
1003
|
+
error.code = 'CONNECTION_CLOSED';
|
|
1004
|
+
for (const [, reject, timeoutId] of this.promiseMap.values()) {
|
|
1005
|
+
clearTimeout(timeoutId);
|
|
1006
|
+
reject(error);
|
|
1007
|
+
}
|
|
1001
1008
|
this.promiseMap.clear();
|
|
1002
1009
|
this.cid = '';
|
|
1003
1010
|
if (this.boho) this.boho.isAuthorized = false;
|
|
@@ -1691,11 +1698,12 @@ class IOCore extends EventEmitter {
|
|
|
1691
1698
|
call(target, topic, ...args) {
|
|
1692
1699
|
if (!target || !topic)
|
|
1693
1700
|
return Promise.reject(new Error('request need target and topic)'))
|
|
1701
|
+
this.mid = (this.mid + 1) & 0xffff;
|
|
1694
1702
|
let sigPack;
|
|
1695
1703
|
if (args.length > 0) {
|
|
1696
1704
|
sigPack = $$1.pack(
|
|
1697
1705
|
$$1.MB('#MsgType', '8', IOMsg.CALL),
|
|
1698
|
-
$$1.MB('mid', '16',
|
|
1706
|
+
$$1.MB('mid', '16', this.mid),
|
|
1699
1707
|
$$1.MB('target', target),
|
|
1700
1708
|
$$1.MB('topic', topic),
|
|
1701
1709
|
$$1.MBA(...args)
|
|
@@ -1703,7 +1711,7 @@ class IOCore extends EventEmitter {
|
|
|
1703
1711
|
} else {
|
|
1704
1712
|
sigPack = $$1.pack(
|
|
1705
1713
|
$$1.MB('#MsgType', '8', IOMsg.CALL),
|
|
1706
|
-
$$1.MB('mid', '16',
|
|
1714
|
+
$$1.MB('mid', '16', this.mid),
|
|
1707
1715
|
$$1.MB('target', target),
|
|
1708
1716
|
$$1.MB('topic', topic)
|
|
1709
1717
|
);
|
|
@@ -2055,54 +2063,45 @@ class CongRx extends Transform {
|
|
|
2055
2063
|
|
|
2056
2064
|
|
|
2057
2065
|
parse() {
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
this.buffer = Buffer.alloc(0);
|
|
2085
|
-
}
|
|
2066
|
+
while (this.buffer.byteLength > 0) {
|
|
2067
|
+
const head = this.buffer[0];
|
|
2068
|
+
let headerLen;
|
|
2069
|
+
let payloadSize;
|
|
2070
|
+
|
|
2071
|
+
if (head === CongType.TYPE_LEN1) {
|
|
2072
|
+
headerLen = 2;
|
|
2073
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2074
|
+
payloadSize = this.buffer.readUint8(1);
|
|
2075
|
+
} else if (head === CongType.TYPE_LEN2) {
|
|
2076
|
+
headerLen = 3;
|
|
2077
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2078
|
+
payloadSize = this.buffer.readUint16LE(1);
|
|
2079
|
+
} else if (head === CongType.TYPE_LEN3) {
|
|
2080
|
+
headerLen = 4;
|
|
2081
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2082
|
+
payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536;
|
|
2083
|
+
} else if (head === CongType.TYPE_LEN4) {
|
|
2084
|
+
headerLen = 5;
|
|
2085
|
+
if (this.buffer.byteLength < headerLen) return;
|
|
2086
|
+
payloadSize = this.buffer.readUint32LE(1);
|
|
2087
|
+
} else {
|
|
2088
|
+
this.emit('wrong', this.buffer);
|
|
2089
|
+
this.buffer = Buffer.alloc(0);
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2086
2092
|
|
|
2093
|
+
const frameSize = headerLen + payloadSize;
|
|
2094
|
+
if (this.buffer.byteLength < frameSize) return;
|
|
2087
2095
|
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
this.buffer = this.buffer.subarray(
|
|
2095
|
-
this.parse();
|
|
2096
|
-
} else {
|
|
2097
|
-
// not ready
|
|
2098
|
-
// console.log('+')
|
|
2099
|
-
return
|
|
2096
|
+
this.frames.push(this.buffer.subarray(headerLen, frameSize));
|
|
2097
|
+
if (this.buffer.byteLength === frameSize) {
|
|
2098
|
+
this.buffer = Buffer.alloc(0);
|
|
2099
|
+
return;
|
|
2100
|
+
}
|
|
2101
|
+
// Consume each complete frame without growing the call stack.
|
|
2102
|
+
this.buffer = this.buffer.subarray(frameSize);
|
|
2100
2103
|
}
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
2104
|
}
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
2105
|
}
|
|
2107
2106
|
|
|
2108
2107
|
class IOCongSocket extends IOCore {
|
|
@@ -7330,6 +7329,7 @@ let serverOption = {
|
|
|
7330
7329
|
httpServer: null,
|
|
7331
7330
|
wsPath: null,
|
|
7332
7331
|
timeout: 50000,
|
|
7332
|
+
pingTimeoutGrace: 5000,
|
|
7333
7333
|
showMessage: 'none',
|
|
7334
7334
|
showMetric: 0,
|
|
7335
7335
|
showChannel: 0,
|
|
@@ -7369,6 +7369,13 @@ let serverOption = {
|
|
|
7369
7369
|
limitCounter: 1000
|
|
7370
7370
|
},
|
|
7371
7371
|
|
|
7372
|
+
// Applied to each new Server instance; packet layouts remain unchanged.
|
|
7373
|
+
security: {
|
|
7374
|
+
maxClockSkewMs: 60000,
|
|
7375
|
+
authChallengeMaxAgeMs: 60000,
|
|
7376
|
+
maxReplayEntries: 65536
|
|
7377
|
+
},
|
|
7378
|
+
|
|
7372
7379
|
auth: {
|
|
7373
7380
|
delay_auth_fail: 600
|
|
7374
7381
|
},
|
|
@@ -7392,6 +7399,13 @@ class RemoteCore {
|
|
|
7392
7399
|
socket.openTime = Date.now();
|
|
7393
7400
|
|
|
7394
7401
|
this.boho = new it();
|
|
7402
|
+
this.security = manager.server.security;
|
|
7403
|
+
this._challengeIssuedAt = null;
|
|
7404
|
+
this._challengeUsed = false;
|
|
7405
|
+
this._replayBuckets = new Map();
|
|
7406
|
+
this._replayEntries = 0;
|
|
7407
|
+
this._replayExpiredBefore = 0;
|
|
7408
|
+
this.securityFailure = null;
|
|
7395
7409
|
this.encMode = ENC_MODE.AUTO;
|
|
7396
7410
|
this.channels = new Set(); // subscribed tags
|
|
7397
7411
|
this.retain_signal = new Map();
|
|
@@ -7467,6 +7481,7 @@ class RemoteCore {
|
|
|
7467
7481
|
rxQuotaChecker(message) {
|
|
7468
7482
|
let rxBytes = message.byteLength;
|
|
7469
7483
|
this.manager.rxBytes += rxBytes;
|
|
7484
|
+
if (this.manager.telemetry) { this.manager.telemetry.rxBytes += rxBytes; this.manager.telemetry.rxMessages++; }
|
|
7470
7485
|
|
|
7471
7486
|
if (serverOption.useQuota.signalSize && (rxBytes > this.quota.signalSize)) {
|
|
7472
7487
|
console.log('## quota: size over');
|
|
@@ -7480,15 +7495,92 @@ class RemoteCore {
|
|
|
7480
7495
|
return true;
|
|
7481
7496
|
}
|
|
7482
7497
|
|
|
7498
|
+
issueAuthChallenge() {
|
|
7499
|
+
const packet = this.boho.server_time_nonce();
|
|
7500
|
+
this._challengeIssuedAt = performance.now();
|
|
7501
|
+
return packet;
|
|
7502
|
+
}
|
|
7503
|
+
|
|
7504
|
+
securityMismatch(reason) {
|
|
7505
|
+
if (this._closing || this.state === STATE.CLOSED) return false;
|
|
7506
|
+
this.securityFailure = reason;
|
|
7507
|
+
this.boho.clearAuth();
|
|
7508
|
+
this.isAdmin = false;
|
|
7509
|
+
this._replayBuckets.clear();
|
|
7510
|
+
this._replayEntries = 0;
|
|
7511
|
+
this.close(true);
|
|
7512
|
+
this.manager.attackLogger?.log(`SECURITY #${this.ssid} reason:${reason}`);
|
|
7513
|
+
this.manager.server.emit('security:mismatch', this, reason);
|
|
7514
|
+
return false;
|
|
7515
|
+
}
|
|
7516
|
+
|
|
7517
|
+
checkAuthChallenge() {
|
|
7518
|
+
if (this._closing || this.state === STATE.CLOSED) return false;
|
|
7519
|
+
if (this._challengeUsed) return this.securityMismatch('AUTH_CHALLENGE_REUSED');
|
|
7520
|
+
if (this._challengeIssuedAt === null ||
|
|
7521
|
+
performance.now() - this._challengeIssuedAt > this.security.authChallengeMaxAgeMs) {
|
|
7522
|
+
return this.securityMismatch('AUTH_CHALLENGE_EXPIRED');
|
|
7523
|
+
}
|
|
7524
|
+
return true;
|
|
7525
|
+
}
|
|
7526
|
+
|
|
7527
|
+
// Called only after Boho has authenticated the packet's clock and payload.
|
|
7528
|
+
acceptEncryptedClock(message, receivedAtMs) {
|
|
7529
|
+
const seconds = message.readUInt32LE(5);
|
|
7530
|
+
const milliseconds = message.readUInt16LE(9);
|
|
7531
|
+
const counter = message.readUInt16LE(11);
|
|
7532
|
+
const packetTime = seconds * 1000 + milliseconds;
|
|
7533
|
+
const earliest = receivedAtMs - this.security.maxClockSkewMs;
|
|
7534
|
+
if (milliseconds >= 1000 || Math.abs(receivedAtMs - packetTime) > this.security.maxClockSkewMs ||
|
|
7535
|
+
packetTime < this._replayExpiredBefore) {
|
|
7536
|
+
return this.securityMismatch('TIME_MISMATCH');
|
|
7537
|
+
}
|
|
7538
|
+
// Never reopen an expired timestamp range after the server clock moves back.
|
|
7539
|
+
this._replayExpiredBefore = Math.max(this._replayExpiredBefore, earliest);
|
|
7540
|
+
for (const [second, values] of this._replayBuckets) {
|
|
7541
|
+
if ((second + 1) * 1000 <= this._replayExpiredBefore) {
|
|
7542
|
+
this._replayEntries -= values.size;
|
|
7543
|
+
this._replayBuckets.delete(second);
|
|
7544
|
+
}
|
|
7545
|
+
}
|
|
7546
|
+
let bucket = this._replayBuckets.get(seconds);
|
|
7547
|
+
const value = milliseconds * 65536 + counter;
|
|
7548
|
+
if (bucket?.has(value)) return this.securityMismatch('REPLAY_DETECTED');
|
|
7549
|
+
// Fail closed instead of evicting still-valid replay evidence under load.
|
|
7550
|
+
if (this._replayEntries >= this.security.maxReplayEntries) {
|
|
7551
|
+
return this.securityMismatch('REPLAY_CAPACITY_EXCEEDED');
|
|
7552
|
+
}
|
|
7553
|
+
if (!bucket) this._replayBuckets.set(seconds, bucket = new Set());
|
|
7554
|
+
bucket.add(value);
|
|
7555
|
+
this._replayEntries++;
|
|
7556
|
+
return true;
|
|
7557
|
+
}
|
|
7558
|
+
|
|
7559
|
+
decryptSessionPacket(message, receivedAtMs) {
|
|
7560
|
+
let decoded;
|
|
7561
|
+
try {
|
|
7562
|
+
decoded = this.boho.decrypt_488(message);
|
|
7563
|
+
} catch {
|
|
7564
|
+
this.securityMismatch('INVALID_ENCRYPTED_PACKET');
|
|
7565
|
+
return;
|
|
7566
|
+
}
|
|
7567
|
+
if (!decoded) {
|
|
7568
|
+
this.securityMismatch('INVALID_ENCRYPTED_PACKET');
|
|
7569
|
+
return;
|
|
7570
|
+
}
|
|
7571
|
+
if (this.acceptEncryptedClock(message, receivedAtMs)) return decoded;
|
|
7572
|
+
}
|
|
7573
|
+
|
|
7483
7574
|
onTimeDelayMessage(message, isBinary = true) {
|
|
7575
|
+
const receivedAtMs = Date.now();
|
|
7484
7576
|
setTimeout(() => {
|
|
7485
|
-
this.onSocketMessage(message, isBinary);
|
|
7577
|
+
this.onSocketMessage(message, isBinary, receivedAtMs);
|
|
7486
7578
|
}
|
|
7487
7579
|
, serverOption.debug.delay);
|
|
7488
7580
|
}
|
|
7489
7581
|
|
|
7490
7582
|
// CongSocket or WebSocket
|
|
7491
|
-
onSocketMessage(message, isBinary = true) {
|
|
7583
|
+
onSocketMessage(message, isBinary = true, receivedAtMs = Date.now()) {
|
|
7492
7584
|
|
|
7493
7585
|
if (this._closing || this.state === STATE.CLOSED) return;
|
|
7494
7586
|
|
|
@@ -7504,12 +7596,7 @@ class RemoteCore {
|
|
|
7504
7596
|
|
|
7505
7597
|
if (msgType === it.BohoMsg.ENC_488) {
|
|
7506
7598
|
|
|
7507
|
-
|
|
7508
|
-
decoded = this.boho.decrypt_488(message);
|
|
7509
|
-
} catch (err) {
|
|
7510
|
-
console.log('-- E488 DEC_FAIL', err);
|
|
7511
|
-
return
|
|
7512
|
-
}
|
|
7599
|
+
decoded = this.decryptSessionPacket(message, receivedAtMs);
|
|
7513
7600
|
|
|
7514
7601
|
if (decoded) {
|
|
7515
7602
|
msgType = decoded[0];
|
|
@@ -7519,12 +7606,7 @@ class RemoteCore {
|
|
|
7519
7606
|
}
|
|
7520
7607
|
|
|
7521
7608
|
} else if (msgType === it.BohoMsg.ENC_E2E) { // symetric E2EE Signal
|
|
7522
|
-
|
|
7523
|
-
decoded = this.boho.decrypt_488(message);
|
|
7524
|
-
} catch (err) {
|
|
7525
|
-
// console.log('-- E2E DEC_FAIL', err)
|
|
7526
|
-
return
|
|
7527
|
-
}
|
|
7609
|
+
decoded = this.decryptSessionPacket(message, receivedAtMs);
|
|
7528
7610
|
// console.log('e2e unpack:', decoded )
|
|
7529
7611
|
if (decoded) {
|
|
7530
7612
|
msgType = decoded[0];
|
|
@@ -7669,13 +7751,17 @@ class RemoteCore {
|
|
|
7669
7751
|
// client's auth requst
|
|
7670
7752
|
case it.BohoMsg.AUTH_REQ:
|
|
7671
7753
|
if (!this.manager.authManager) return
|
|
7672
|
-
//
|
|
7673
|
-
if (this.state === STATE.AUTH_REQ)
|
|
7754
|
+
// A second request while verification is pending must not restart auth.
|
|
7755
|
+
if (this.state === STATE.AUTH_REQ) {
|
|
7756
|
+
this.securityMismatch('AUTH_IN_PROGRESS');
|
|
7757
|
+
return;
|
|
7758
|
+
}
|
|
7674
7759
|
if (![STATE.SERVER_READY, STATE.AUTH_RES, STATE.AUTH_FAIL, STATE.CID_RES].includes(this.state)) {
|
|
7675
7760
|
console.log('protocol error. must called auth_req after server_ready');
|
|
7676
7761
|
this.close();
|
|
7677
7762
|
return;
|
|
7678
7763
|
}
|
|
7764
|
+
if (!this.checkAuthChallenge()) return;
|
|
7679
7765
|
this.setState(STATE.AUTH_REQ);
|
|
7680
7766
|
//async
|
|
7681
7767
|
this.manager.authManager.verify_auth_req(message, this)
|
|
@@ -7867,6 +7953,10 @@ const decoder$2 = new TextDecoder();
|
|
|
7867
7953
|
class Remote extends RemoteCore {
|
|
7868
7954
|
constructor(socket, req, manager) {
|
|
7869
7955
|
super(socket, manager);
|
|
7956
|
+
this.txQueuePeakBytes = 0;
|
|
7957
|
+
this.txWriteFalseCount = 0;
|
|
7958
|
+
this.txBlockedSince = null;
|
|
7959
|
+
this.txDrainMaxMs = 0;
|
|
7870
7960
|
this.socketType = socket.socketType;
|
|
7871
7961
|
|
|
7872
7962
|
if (this.socketType === 'websocket') {
|
|
@@ -7903,6 +7993,11 @@ class Remote extends RemoteCore {
|
|
|
7903
7993
|
this.manager.removeRemote(this);
|
|
7904
7994
|
};
|
|
7905
7995
|
} else { // TCP else
|
|
7996
|
+
socket.on('drain', () => {
|
|
7997
|
+
if (this.txBlockedSince === null) return;
|
|
7998
|
+
this.txDrainMaxMs = Math.max(this.txDrainMaxMs, performance.now() - this.txBlockedSince);
|
|
7999
|
+
this.txBlockedSince = null;
|
|
8000
|
+
});
|
|
7906
8001
|
this.congRx = new CongRx();
|
|
7907
8002
|
socket.on('data', data => { this.congRx.write(data); });
|
|
7908
8003
|
|
|
@@ -7971,6 +8066,7 @@ class Remote extends RemoteCore {
|
|
|
7971
8066
|
// any type of received messages: message, ping, pong
|
|
7972
8067
|
receiveMonitor() {
|
|
7973
8068
|
this.socket.rxCounter++;
|
|
8069
|
+
if (this.socket.isAlive === false) this.manager.heartbeatPending.delete(this);
|
|
7974
8070
|
this.socket.isAlive = true;
|
|
7975
8071
|
}
|
|
7976
8072
|
|
|
@@ -8034,10 +8130,16 @@ class Remote extends RemoteCore {
|
|
|
8034
8130
|
}
|
|
8035
8131
|
}
|
|
8036
8132
|
|
|
8133
|
+
recordTxQueue() {
|
|
8134
|
+
const queued = this.socket.bufferedAmount ?? this.socket.writableLength ?? 0;
|
|
8135
|
+
if (queued > this.txQueuePeakBytes) this.txQueuePeakBytes = queued;
|
|
8136
|
+
}
|
|
8137
|
+
|
|
8037
8138
|
// node.js server side remote.
|
|
8038
8139
|
send(message, isBinary) {
|
|
8039
8140
|
// console.log(`<-S [${IOMsg[ message[0]]}]`)
|
|
8040
8141
|
this.manager.txBytes += message.byteLength;
|
|
8142
|
+
if (this.manager.telemetry) { this.manager.telemetry.txBytes += message.byteLength; this.manager.telemetry.txMessages++; }
|
|
8041
8143
|
this.socket.txCounter++;
|
|
8042
8144
|
if (this.socketType === 'websocket') {
|
|
8043
8145
|
if (this.socket.readyState === 1) {
|
|
@@ -8046,6 +8148,7 @@ class Remote extends RemoteCore {
|
|
|
8046
8148
|
} else {
|
|
8047
8149
|
this.socket.send(message);
|
|
8048
8150
|
}
|
|
8151
|
+
this.recordTxQueue();
|
|
8049
8152
|
} else {
|
|
8050
8153
|
console.log('server(ws)Remote.send() called. not open state. current readyState:', this.socket.readyState );
|
|
8051
8154
|
this.close(true);
|
|
@@ -8053,7 +8156,12 @@ class Remote extends RemoteCore {
|
|
|
8053
8156
|
|
|
8054
8157
|
} else { //CongSocket
|
|
8055
8158
|
if (this.socket.readyState == 'open') {
|
|
8056
|
-
this.socket.write(pack(message));
|
|
8159
|
+
const writable = this.socket.write(pack(message));
|
|
8160
|
+
this.recordTxQueue();
|
|
8161
|
+
if (!writable) {
|
|
8162
|
+
this.txWriteFalseCount++;
|
|
8163
|
+
if (this.txBlockedSince === null) this.txBlockedSince = performance.now();
|
|
8164
|
+
}
|
|
8057
8165
|
} else {
|
|
8058
8166
|
console.log('server(cong)Remote.send() called. not open state. current readyState:', this.socket.readyState );
|
|
8059
8167
|
this.close(true);
|
|
@@ -8257,6 +8365,12 @@ class Manager {
|
|
|
8257
8365
|
|
|
8258
8366
|
constructor(server, authManager) {
|
|
8259
8367
|
this.server = server;
|
|
8368
|
+
this.pingPeriod = serverOption.timeout;
|
|
8369
|
+
this.pingGrace = serverOption.pingTimeoutGrace;
|
|
8370
|
+
if (!Number.isFinite(this.pingPeriod) || this.pingPeriod <= 0 ||
|
|
8371
|
+
!Number.isFinite(this.pingGrace) || this.pingGrace < 0) {
|
|
8372
|
+
throw new RangeError('Invalid heartbeat period or grace');
|
|
8373
|
+
}
|
|
8260
8374
|
this.txBytes = 0;
|
|
8261
8375
|
this.rxBytes = 0;
|
|
8262
8376
|
|
|
@@ -8283,25 +8397,20 @@ class Manager {
|
|
|
8283
8397
|
this.metrics = new Metrics(this);
|
|
8284
8398
|
this.lastSSID = 0;
|
|
8285
8399
|
|
|
8286
|
-
|
|
8287
|
-
|
|
8288
|
-
|
|
8289
|
-
|
|
8290
|
-
|
|
8291
|
-
|
|
8292
|
-
|
|
8293
|
-
|
|
8294
|
-
|
|
8295
|
-
|
|
8296
|
-
|
|
8297
|
-
|
|
8298
|
-
|
|
8299
|
-
|
|
8300
|
-
remo.ping();
|
|
8301
|
-
}
|
|
8302
|
-
|
|
8303
|
-
});
|
|
8304
|
-
}, serverOption.timeout);
|
|
8400
|
+
const groups = Math.min(50, Math.max(1, Math.floor(this.pingPeriod / 1000)));
|
|
8401
|
+
this.pingGroups = Array.from({ length: groups }, () => new Set());
|
|
8402
|
+
this.nextPingGroup = 0;
|
|
8403
|
+
this.pingGroupCursor = 0;
|
|
8404
|
+
// Same timeout for all entries: insertion order is also deadline order.
|
|
8405
|
+
this.heartbeatPending = new Map();
|
|
8406
|
+
this.pingTickMs = this.pingPeriod / groups;
|
|
8407
|
+
this.heartbeatStopped = false;
|
|
8408
|
+
const tick = () => {
|
|
8409
|
+
if (this.heartbeatStopped) return;
|
|
8410
|
+
this.heartbeatTick();
|
|
8411
|
+
if (!this.heartbeatStopped) this.pingIntervalID = setTimeout(tick, this.pingTickMs);
|
|
8412
|
+
};
|
|
8413
|
+
this.pingIntervalID = setTimeout(tick, this.pingTickMs);
|
|
8305
8414
|
|
|
8306
8415
|
if (serverOption.showMetric) {
|
|
8307
8416
|
this.monitIntervalID = setInterval((e) => {
|
|
@@ -8313,11 +8422,38 @@ class Manager {
|
|
|
8313
8422
|
}
|
|
8314
8423
|
|
|
8315
8424
|
|
|
8425
|
+
heartbeatTick() {
|
|
8426
|
+
const now = performance.now();
|
|
8427
|
+
// Process only expired requests, not every connected peer.
|
|
8428
|
+
for (const [remote, deadline] of this.heartbeatPending) {
|
|
8429
|
+
if (deadline > now) break;
|
|
8430
|
+
this.heartbeatPending.delete(remote);
|
|
8431
|
+
if (!remote._closing && remote.socket.isAlive === false) remote.close(true);
|
|
8432
|
+
}
|
|
8433
|
+
// The single timer schedules its next tick after this work; missed groups are not replayed.
|
|
8434
|
+
const group = this.pingGroups[this.pingGroupCursor];
|
|
8435
|
+
this.pingGroupCursor = (this.pingGroupCursor + 1) % this.pingGroups.length;
|
|
8436
|
+
for (const remote of group) {
|
|
8437
|
+
if (remote._closing || this.heartbeatPending.has(remote)) continue;
|
|
8438
|
+
remote.socket.isAlive = false;
|
|
8439
|
+
this.heartbeatPending.set(remote, performance.now() + this.pingPeriod + this.pingGrace);
|
|
8440
|
+
try {
|
|
8441
|
+
remote.ping();
|
|
8442
|
+
} catch {
|
|
8443
|
+
this.heartbeatPending.delete(remote);
|
|
8444
|
+
remote.close(true);
|
|
8445
|
+
}
|
|
8446
|
+
}
|
|
8447
|
+
}
|
|
8448
|
+
|
|
8316
8449
|
addRemote(socket, req) {
|
|
8317
8450
|
socket.isAlive = true;
|
|
8318
8451
|
let remote = new Remote(socket, req, this);
|
|
8319
8452
|
this.remotes.add(remote);
|
|
8320
|
-
remote.
|
|
8453
|
+
remote.pingGroup = this.nextPingGroup;
|
|
8454
|
+
this.pingGroups[remote.pingGroup].add(remote);
|
|
8455
|
+
this.nextPingGroup = (this.nextPingGroup + 1) % this.pingGroups.length;
|
|
8456
|
+
remote.send(remote.issueAuthChallenge());
|
|
8321
8457
|
remote.send( Buffer.from([ IOMsg.SERVER_READY]) );
|
|
8322
8458
|
remote.setState(STATE.SERVER_READY);
|
|
8323
8459
|
this.lastSSID = remote.ssid;
|
|
@@ -8330,6 +8466,8 @@ class Manager {
|
|
|
8330
8466
|
removeRemote(remote) {
|
|
8331
8467
|
|
|
8332
8468
|
remote._closing = true;
|
|
8469
|
+
this.pingGroups[remote.pingGroup]?.delete(remote);
|
|
8470
|
+
this.heartbeatPending.delete(remote);
|
|
8333
8471
|
remote.setState(STATE.CLOSED);
|
|
8334
8472
|
|
|
8335
8473
|
let remoteInfo = `- IP:${remote.ip} #${remote.ssid} cid:${remote.cid} ${remote?.socket.socketType === 'websocket' ? "WS" : "CS"} `;
|
|
@@ -8508,6 +8646,7 @@ class Manager {
|
|
|
8508
8646
|
|
|
8509
8647
|
}
|
|
8510
8648
|
|
|
8649
|
+
if (this.telemetry) this.telemetry.publishes++;
|
|
8511
8650
|
let sentCounter = 0;
|
|
8512
8651
|
if (this.channel_map.has(tag)) {
|
|
8513
8652
|
// console.log('raw channel_map matched tag:', tag)
|
|
@@ -8519,6 +8658,7 @@ class Manager {
|
|
|
8519
8658
|
} else {
|
|
8520
8659
|
limit = remotes.size;
|
|
8521
8660
|
}
|
|
8661
|
+
if (this.telemetry) this.telemetry.quotaExcluded += remotes.size - limit;
|
|
8522
8662
|
let peers = remotes.values();
|
|
8523
8663
|
while (sentCounter < limit) {
|
|
8524
8664
|
let c = peers.next().value;
|
|
@@ -8538,6 +8678,7 @@ class Manager {
|
|
|
8538
8678
|
// } else {
|
|
8539
8679
|
// // console.log(`pub >> [${tag}] sent: ${sentCounter} [no quota limit] total: ${remotes.size} subscribers. ` )
|
|
8540
8680
|
// }
|
|
8681
|
+
if (this.telemetry) this.telemetry.deliveryAttempts += sentCounter;
|
|
8541
8682
|
return ['ok', sentCounter]
|
|
8542
8683
|
}
|
|
8543
8684
|
}
|
|
@@ -8671,7 +8812,10 @@ class Manager {
|
|
|
8671
8812
|
|
|
8672
8813
|
close() {
|
|
8673
8814
|
this.metrics.close();
|
|
8674
|
-
|
|
8815
|
+
this.heartbeatStopped = true;
|
|
8816
|
+
clearTimeout(this.pingIntervalID);
|
|
8817
|
+
this.heartbeatPending.clear();
|
|
8818
|
+
for (const group of this.pingGroups) group.clear();
|
|
8675
8819
|
if (this.monitIntervalID) {
|
|
8676
8820
|
clearInterval(this.monitIntervalID);
|
|
8677
8821
|
}
|
|
@@ -8699,11 +8843,25 @@ class Server extends require$$0$3 {
|
|
|
8699
8843
|
|
|
8700
8844
|
constructor(options, authManager) {
|
|
8701
8845
|
super();
|
|
8846
|
+
const security = { ...serverOption.security, ...options.security };
|
|
8847
|
+
for (const name of ['maxClockSkewMs', 'authChallengeMaxAgeMs', 'maxReplayEntries']) {
|
|
8848
|
+
if (!Number.isSafeInteger(security[name]) || security[name] <= 0) {
|
|
8849
|
+
throw new RangeError(`security.${name} must be a positive safe integer`);
|
|
8850
|
+
}
|
|
8851
|
+
}
|
|
8852
|
+
this.security = Object.freeze(security);
|
|
8702
8853
|
this.serviceNames = new Set();
|
|
8703
8854
|
this.wss = {};
|
|
8704
8855
|
this.port = null;
|
|
8705
8856
|
this.congPort = null;
|
|
8706
8857
|
|
|
8858
|
+
if (options.pingTimeoutGrace !== undefined) {
|
|
8859
|
+
if (!Number.isFinite(options.pingTimeoutGrace) || options.pingTimeoutGrace < 0) {
|
|
8860
|
+
throw new RangeError('pingTimeoutGrace must be a finite non-negative number');
|
|
8861
|
+
}
|
|
8862
|
+
serverOption.pingTimeoutGrace = options.pingTimeoutGrace;
|
|
8863
|
+
}
|
|
8864
|
+
|
|
8707
8865
|
if (options.timeout) {
|
|
8708
8866
|
let pingT = parseInt(options.timeout);
|
|
8709
8867
|
if (pingT && pingT >= 1000) serverOption.timeout = pingT;
|
|
@@ -8837,44 +8995,36 @@ class Server extends require$$0$3 {
|
|
|
8837
8995
|
throw new Error(`Service ${service} : no commands or !Array.`)
|
|
8838
8996
|
}
|
|
8839
8997
|
|
|
8840
|
-
|
|
8841
|
-
|
|
8842
|
-
|
|
8843
|
-
|
|
8844
|
-
|
|
8845
|
-
|
|
8846
|
-
|
|
8847
|
-
|
|
8848
|
-
|
|
8849
|
-
|
|
8850
|
-
|
|
8851
|
-
|
|
8852
|
-
|
|
8853
|
-
|
|
8854
|
-
|
|
8855
|
-
|
|
8856
|
-
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
8998
|
+
const handleError = (remote, req, error) => {
|
|
8999
|
+
console.error(`Unhandled Service Error in service [${service}]:`, error);
|
|
9000
|
+
if (remote._closing) return;
|
|
9001
|
+
// A disconnected transport can also fail while reporting the service error.
|
|
9002
|
+
try {
|
|
9003
|
+
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
9004
|
+
} catch (responseError) {
|
|
9005
|
+
console.error(`Service error response failed [${service}]:`, responseError);
|
|
9006
|
+
}
|
|
9007
|
+
};
|
|
9008
|
+
const useCall = typeof service_module.call === 'function';
|
|
9009
|
+
this.on(service, (remote, req) => {
|
|
9010
|
+
try {
|
|
9011
|
+
if (!service_module.checkPermission(remote, req)) {
|
|
9012
|
+
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.");
|
|
9013
|
+
return;
|
|
8857
9014
|
}
|
|
8858
|
-
|
|
8859
|
-
|
|
8860
|
-
|
|
8861
|
-
this.on(service, (remote, req) => {
|
|
8862
|
-
try {
|
|
8863
|
-
if (!service_module.checkPermission(remote, req)) {
|
|
8864
|
-
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.");
|
|
8865
|
-
return
|
|
8866
|
-
}
|
|
8867
|
-
if( service_module.commands.includes( req.topic )){
|
|
8868
|
-
service_module[req.topic](remote, req);
|
|
8869
|
-
}else {
|
|
8870
|
-
remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
|
|
8871
|
-
}
|
|
8872
|
-
} catch (error) {
|
|
8873
|
-
console.error(`Unhandled Service Error in service [${service}]:`, error);
|
|
8874
|
-
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
9015
|
+
if (!service_module.commands.includes(req.topic)) {
|
|
9016
|
+
remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
|
|
9017
|
+
return;
|
|
8875
9018
|
}
|
|
8876
|
-
|
|
8877
|
-
|
|
9019
|
+
// Keep synchronous invocation and the service receiver; also observe async failures.
|
|
9020
|
+
const result = useCall
|
|
9021
|
+
? service_module.call(remote, req)
|
|
9022
|
+
: service_module[req.topic](remote, req);
|
|
9023
|
+
Promise.resolve(result).catch(error => handleError(remote, req, error));
|
|
9024
|
+
} catch (error) {
|
|
9025
|
+
handleError(remote, req, error);
|
|
9026
|
+
}
|
|
9027
|
+
});
|
|
8878
9028
|
this.serviceNames.add(service);
|
|
8879
9029
|
// console.log(`[Service attached: ${service} ] accept commands: ${service_module.commands}`)
|
|
8880
9030
|
return this
|
|
@@ -8960,6 +9110,12 @@ class BohoAuth {
|
|
|
8960
9110
|
|
|
8961
9111
|
async verify_auth_req(auth_req, peer) {
|
|
8962
9112
|
try {
|
|
9113
|
+
if (!peer.checkAuthChallenge()) return;
|
|
9114
|
+
if (!(auth_req instanceof Uint8Array) || auth_req.length !== it.MetaSize.AUTH_REQ ||
|
|
9115
|
+
auth_req[0] !== it.BohoMsg.AUTH_REQ) {
|
|
9116
|
+
peer.securityMismatch('INVALID_AUTH_PACKET');
|
|
9117
|
+
return;
|
|
9118
|
+
}
|
|
8963
9119
|
//1. unpack
|
|
8964
9120
|
let infoPack = $$1.unpack(auth_req, it.Meta.AUTH_REQ);
|
|
8965
9121
|
if (!infoPack) {
|
|
@@ -8976,7 +9132,7 @@ class BohoAuth {
|
|
|
8976
9132
|
|
|
8977
9133
|
//2. get key of id from DB
|
|
8978
9134
|
let authInfo = await this.keyProvider.getAuth(id);
|
|
8979
|
-
if (peer.
|
|
9135
|
+
if (!peer.checkAuthChallenge()) return;
|
|
8980
9136
|
if (!authInfo) {
|
|
8981
9137
|
this.send_auth_fail(peer, 'NO ID:' + id);
|
|
8982
9138
|
return
|
|
@@ -9006,6 +9162,10 @@ class BohoAuth {
|
|
|
9006
9162
|
return
|
|
9007
9163
|
}
|
|
9008
9164
|
|
|
9165
|
+
// A valid proof consumes this challenge even if account policy rejects it.
|
|
9166
|
+
// Reauthentication requires a new connection, never a replay-state reset.
|
|
9167
|
+
peer._challengeUsed = true;
|
|
9168
|
+
|
|
9009
9169
|
//4. get info
|
|
9010
9170
|
|
|
9011
9171
|
//5. check duplicate login.
|
|
@@ -152,7 +152,7 @@ export class IOCore {
|
|
|
152
152
|
*/
|
|
153
153
|
private _closed;
|
|
154
154
|
/**
|
|
155
|
-
* Performs common cleanup for the connection. It
|
|
155
|
+
* Performs common cleanup for the connection. It rejects pending RPCs,
|
|
156
156
|
* resets the socket reference, and sets the state to closed.
|
|
157
157
|
* This method is guarded to only run once.
|
|
158
158
|
* If autoReconnect is false, it also clears the keep-alive timer.
|