iosignal 9.14.0 → 9.19.0
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 +1 -1
- package/README.md +1 -1
- package/dist/browser/esm/io.d.ts +9 -0
- package/dist/browser/esm/io.js +4 -4
- package/dist/browser/esm/io.js.map +1 -1
- package/dist/browser/iife/io.js +4 -4
- package/dist/browser/iife/io.js.map +1 -1
- package/dist/node/iosignal.cjs +100 -10
- package/dist/node/iosignal.d.cts +25 -0
- package/dist/node/iosignal.d.ts +25 -0
- package/dist/node/iosignal.js +100 -10
- package/dist/types/client/IOCore.d.ts +9 -0
- package/dist/types/common/constants.d.ts +2 -0
- package/package.json +1 -1
package/dist/node/iosignal.cjs
CHANGED
|
@@ -23,7 +23,7 @@ var node_os = require('node:os');
|
|
|
23
23
|
var path = require('path');
|
|
24
24
|
|
|
25
25
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
26
|
-
var version$1 = "9.
|
|
26
|
+
var version$1 = "9.19.0";
|
|
27
27
|
var pkg = {
|
|
28
28
|
version: version$1};
|
|
29
29
|
|
|
@@ -449,6 +449,7 @@ for (let c in ENC_MODE) { ENC_MODE[ENC_MODE[c]] = c; }
|
|
|
449
449
|
* @property {number} TAG_LEN2
|
|
450
450
|
* @property {number} CONNECTION_CHECKER_PERIOD
|
|
451
451
|
* @property {number} CLIENT_PING_PERIOD
|
|
452
|
+
* @property {number} SERVER_HELLO_TIMEOUT
|
|
452
453
|
* @property {number} PROMISE_TIMEOUT
|
|
453
454
|
* @property {number} DID
|
|
454
455
|
* @property {number} CID
|
|
@@ -458,6 +459,9 @@ const SIZE_LIMIT = {
|
|
|
458
459
|
TAG_LEN2: 65535,
|
|
459
460
|
CONNECTION_CHECKER_PERIOD: 3000,
|
|
460
461
|
CLIENT_PING_PERIOD: 40000,
|
|
462
|
+
// From starting a connection to the server's hello (SERVER_TIME_NONCE). A connection that hangs
|
|
463
|
+
// longer is closed, so the client reconnects to its own URL (after a redirect: the original one).
|
|
464
|
+
SERVER_HELLO_TIMEOUT: 15000,
|
|
461
465
|
PROMISE_TIMEOUT: 5000,
|
|
462
466
|
DID: 8,
|
|
463
467
|
CID: 12
|
|
@@ -966,6 +970,7 @@ class IOCore extends EventEmitter {
|
|
|
966
970
|
*/
|
|
967
971
|
close() {
|
|
968
972
|
// console.log('####### IOCOre.js close() called')
|
|
973
|
+
this.clearHelloTimer();
|
|
969
974
|
// If auto-reconnect is disabled, we must stop the keep-alive timer.
|
|
970
975
|
if (this.autoReconnect === false) {
|
|
971
976
|
clearInterval(this.connectionCheckerIntervalID);
|
|
@@ -1060,6 +1065,30 @@ class IOCore extends EventEmitter {
|
|
|
1060
1065
|
this.stateChange('redirecting','redirecting');
|
|
1061
1066
|
this.close();
|
|
1062
1067
|
this.createConnection(url2);
|
|
1068
|
+
this.armHelloTimer();
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/**
|
|
1072
|
+
* Closes a connection that has not got the server's hello in SIZE_LIMIT.SERVER_HELLO_TIMEOUT
|
|
1073
|
+
* (a server that cannot be reached, or a connection that opens but never answers). With
|
|
1074
|
+
* autoReconnect the client then reconnects to this.url, so a failed redirect falls back to the
|
|
1075
|
+
* original server, as the Arduino library does.
|
|
1076
|
+
*/
|
|
1077
|
+
armHelloTimer() {
|
|
1078
|
+
this.clearHelloTimer();
|
|
1079
|
+
if (this._closed || !this.socket) return;
|
|
1080
|
+
const socket = this.socket;
|
|
1081
|
+
this.helloTimerID = setTimeout(() => {
|
|
1082
|
+
this.helloTimerID = null;
|
|
1083
|
+
if (this.socket !== socket) return;
|
|
1084
|
+
this.emit('error', new Error('no reply from the server in ' + SIZE_LIMIT.SERVER_HELLO_TIMEOUT / 1000 + ' s'));
|
|
1085
|
+
this.close();
|
|
1086
|
+
}, SIZE_LIMIT.SERVER_HELLO_TIMEOUT);
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
clearHelloTimer() {
|
|
1090
|
+
if (this.helloTimerID) clearTimeout(this.helloTimerID);
|
|
1091
|
+
this.helloTimerID = null;
|
|
1063
1092
|
}
|
|
1064
1093
|
|
|
1065
1094
|
/**
|
|
@@ -1085,6 +1114,7 @@ class IOCore extends EventEmitter {
|
|
|
1085
1114
|
// The actual connection is created here.
|
|
1086
1115
|
this.createConnection(this.url);
|
|
1087
1116
|
if (this._closed) return; // A connecting listener may have stopped the client.
|
|
1117
|
+
this.armHelloTimer();
|
|
1088
1118
|
|
|
1089
1119
|
// Ensure the keep-alive timer is running.
|
|
1090
1120
|
if (!this.connectionCheckerIntervalID) {
|
|
@@ -1279,6 +1309,7 @@ class IOCore extends EventEmitter {
|
|
|
1279
1309
|
break;
|
|
1280
1310
|
|
|
1281
1311
|
case it.BohoMsg.SERVER_TIME_NONCE: // SERVER_READY
|
|
1312
|
+
this.clearHelloTimer();
|
|
1282
1313
|
buffer.copy(this.serverTimeNonce);
|
|
1283
1314
|
if (!this.stateChange('server_ready', 'server_ready')) break;
|
|
1284
1315
|
if (this.useAuth) {
|
|
@@ -2162,8 +2193,15 @@ class IOCongSocket extends IOCore {
|
|
|
2162
2193
|
|
|
2163
2194
|
createConnection(url) {
|
|
2164
2195
|
// TCP Socket
|
|
2165
|
-
|
|
2166
|
-
|
|
2196
|
+
// 'host:port', 'ip:port' or 'cong://host:port'. 'ip:port' alone is not a URL (a scheme cannot
|
|
2197
|
+
// start with a digit), and 'host:port' parses with 'host:' as its scheme.
|
|
2198
|
+
let urlObj;
|
|
2199
|
+
try {
|
|
2200
|
+
urlObj = new URL(url);
|
|
2201
|
+
} catch {
|
|
2202
|
+
urlObj = null;
|
|
2203
|
+
}
|
|
2204
|
+
if (urlObj?.protocol != "cong:") {
|
|
2167
2205
|
urlObj = new URL('cong://' + url);
|
|
2168
2206
|
}
|
|
2169
2207
|
// URL keeps the brackets of an IPv6 literal ('[::1]'); net expects the bare address.
|
|
@@ -10004,6 +10042,32 @@ class BohoAuth {
|
|
|
10004
10042
|
}
|
|
10005
10043
|
|
|
10006
10044
|
|
|
10045
|
+
/**
|
|
10046
|
+
* Hook for a subclass: decide where a verified login belongs before it is accepted.
|
|
10047
|
+
* Called after the key and role checks, before quotas and the duplicate login check.
|
|
10048
|
+
* Return null to accept the login here, or a message (e.g. SERVER_REDIRECT) that is sent to the
|
|
10049
|
+
* client instead of AUTH_RES before the connection is closed. The default accepts every login.
|
|
10050
|
+
* @param {object} peer the connection
|
|
10051
|
+
* @param {object} authInfo the key provider's entry for the login ID (cid, uid, role, quota, ...)
|
|
10052
|
+
* @param {string} role resolved role
|
|
10053
|
+
* @param {string} id login ID
|
|
10054
|
+
* @returns {Promise<Uint8Array | null> | Uint8Array | null}
|
|
10055
|
+
*/
|
|
10056
|
+
beforeAccept(peer, authInfo, role, id) {
|
|
10057
|
+
return null
|
|
10058
|
+
}
|
|
10059
|
+
|
|
10060
|
+
// An error in the hook accepts the login: placement must never lock clients out.
|
|
10061
|
+
async #beforeAcceptSafe(peer, authInfo, role, id) {
|
|
10062
|
+
try {
|
|
10063
|
+
const result = await this.beforeAccept(peer, authInfo, role, id);
|
|
10064
|
+
return result instanceof Uint8Array && result.byteLength > 0 ? result : null
|
|
10065
|
+
} catch (error) {
|
|
10066
|
+
console.error('BohoAuth.beforeAccept error:', error);
|
|
10067
|
+
return null
|
|
10068
|
+
}
|
|
10069
|
+
}
|
|
10070
|
+
|
|
10007
10071
|
async verify_auth_req(auth_req, peer) {
|
|
10008
10072
|
try {
|
|
10009
10073
|
if (!peer.checkAuthChallenge()) return;
|
|
@@ -10069,6 +10133,16 @@ class BohoAuth {
|
|
|
10069
10133
|
this.send_auth_fail(peer, 'unknown auth.role: ' + authInfo.role);
|
|
10070
10134
|
return
|
|
10071
10135
|
}
|
|
10136
|
+
// A login another server should take (beforeAccept) goes there before it is counted here or
|
|
10137
|
+
// replaces a connection here.
|
|
10138
|
+
const redirect = await this.#beforeAcceptSafe(peer, authInfo, role, id);
|
|
10139
|
+
if (peer._closing) return; // closed while deciding
|
|
10140
|
+
if (redirect) {
|
|
10141
|
+
peer.boho.isAuthorized = false;
|
|
10142
|
+
peer.send(redirect);
|
|
10143
|
+
peer.close();
|
|
10144
|
+
return
|
|
10145
|
+
}
|
|
10072
10146
|
const quotaName = await this.resolveQuota(authInfo, role);
|
|
10073
10147
|
const quota = Object.hasOwn(peer.manager.quotas, quotaName) ? peer.manager.quotas[quotaName] : undefined;
|
|
10074
10148
|
if (!quota) {
|
|
@@ -11470,11 +11544,26 @@ class RedisKeyProvider {
|
|
|
11470
11544
|
|
|
11471
11545
|
//sudoService call
|
|
11472
11546
|
async getAuthIdList() {
|
|
11473
|
-
|
|
11474
|
-
|
|
11475
|
-
|
|
11476
|
-
|
|
11477
|
-
|
|
11547
|
+
const keys = await this.scanKeys(DEVICE_PREFIX + '*');
|
|
11548
|
+
return keys.map(key => key.slice(DEVICE_PREFIX.length))
|
|
11549
|
+
}
|
|
11550
|
+
|
|
11551
|
+
// Keys matching pattern, by SCAN: KEYS blocks Redis until it has walked every key.
|
|
11552
|
+
// Falls back to KEYS for a client without scan, or a redis-service from before 2026-11-17 that refuses it
|
|
11553
|
+
// (RemoteRedisClient answers a refused command with undefined).
|
|
11554
|
+
async scanKeys(pattern) {
|
|
11555
|
+
if (typeof this.redis.scan === 'function') {
|
|
11556
|
+
const keys = new Set(); // SCAN may return a key more than once
|
|
11557
|
+
let cursor = '0';
|
|
11558
|
+
do {
|
|
11559
|
+
const reply = await this.redis.scan(cursor, { MATCH: pattern, COUNT: 1000 });
|
|
11560
|
+
if (!reply || !Array.isArray(reply.keys)) break
|
|
11561
|
+
for (const key of reply.keys) keys.add(key);
|
|
11562
|
+
cursor = String(reply.cursor);
|
|
11563
|
+
if (cursor === '0') return [...keys]
|
|
11564
|
+
} while (true)
|
|
11565
|
+
}
|
|
11566
|
+
return (await this.redis.keys(pattern)) ?? []
|
|
11478
11567
|
}
|
|
11479
11568
|
|
|
11480
11569
|
// add device auth info
|
|
@@ -11692,12 +11781,13 @@ var sudoService = /*#__PURE__*/Object.freeze({
|
|
|
11692
11781
|
*
|
|
11693
11782
|
*/
|
|
11694
11783
|
|
|
11784
|
+
// KEYS stays for older clients; new ones list keys with SCAN (RedisKeyProvider.scanKeys).
|
|
11695
11785
|
const COMMANDS = [
|
|
11696
11786
|
'GET', 'SET', 'HGETALL', 'HGET', 'HSET', 'SADD', 'SISMEMBER',
|
|
11697
|
-
'SMEMBERS', 'EXISTS', 'SREM', 'DEL', 'KEYS', 'SAVE',
|
|
11787
|
+
'SMEMBERS', 'EXISTS', 'SREM', 'DEL', 'KEYS', 'SCAN', 'SAVE',
|
|
11698
11788
|
'LPUSH', 'LTRIM', 'LRANGE', 'LINDEX', 'LSET', 'EXPIRE',
|
|
11699
11789
|
'get', 'set', 'hGetAll', 'hGet', 'hSet', 'sAdd', 'sIsMember',
|
|
11700
|
-
'sMembers', 'exists', 'sRem', 'del', 'keys', 'save',
|
|
11790
|
+
'sMembers', 'exists', 'sRem', 'del', 'keys', 'scan', 'save',
|
|
11701
11791
|
'lPush', 'lTrim', 'lRange', 'lIndex', 'lSet', 'expire'
|
|
11702
11792
|
];
|
|
11703
11793
|
|
package/dist/node/iosignal.d.cts
CHANGED
|
@@ -57,6 +57,7 @@ type SIZE_LIMIT = {
|
|
|
57
57
|
TAG_LEN2: number;
|
|
58
58
|
CONNECTION_CHECKER_PERIOD: number;
|
|
59
59
|
CLIENT_PING_PERIOD: number;
|
|
60
|
+
SERVER_HELLO_TIMEOUT: number;
|
|
60
61
|
PROMISE_TIMEOUT: number;
|
|
61
62
|
DID: number;
|
|
62
63
|
CID: number;
|
|
@@ -66,6 +67,7 @@ declare namespace SIZE_LIMIT {
|
|
|
66
67
|
let TAG_LEN2: number;
|
|
67
68
|
let CONNECTION_CHECKER_PERIOD: number;
|
|
68
69
|
let CLIENT_PING_PERIOD: number;
|
|
70
|
+
let SERVER_HELLO_TIMEOUT: number;
|
|
69
71
|
let PROMISE_TIMEOUT: number;
|
|
70
72
|
let DID: number;
|
|
71
73
|
let CID: number;
|
|
@@ -352,6 +354,15 @@ declare class IOCore extends EventEmitter<string | symbol, any> {
|
|
|
352
354
|
* @param {string} url2 - The new URL to redirect to.
|
|
353
355
|
*/
|
|
354
356
|
redirect(url2: string): void;
|
|
357
|
+
/**
|
|
358
|
+
* Closes a connection that has not got the server's hello in SIZE_LIMIT.SERVER_HELLO_TIMEOUT
|
|
359
|
+
* (a server that cannot be reached, or a connection that opens but never answers). With
|
|
360
|
+
* autoReconnect the client then reconnects to this.url, so a failed redirect falls back to the
|
|
361
|
+
* original server, as the Arduino library does.
|
|
362
|
+
*/
|
|
363
|
+
armHelloTimer(): void;
|
|
364
|
+
helloTimerID: any;
|
|
365
|
+
clearHelloTimer(): void;
|
|
355
366
|
/**
|
|
356
367
|
* Opens the WebSocket connection.
|
|
357
368
|
* @param {string} [url] - Optional URL to connect to. If not provided, uses the instance's URL.
|
|
@@ -1373,6 +1384,7 @@ declare class RedisKeyProvider {
|
|
|
1373
1384
|
redis: any;
|
|
1374
1385
|
getAuth(id: any): Promise<any>;
|
|
1375
1386
|
getAuthIdList(): Promise<any>;
|
|
1387
|
+
scanKeys(pattern: any): Promise<any>;
|
|
1376
1388
|
addAuth(id: any, keyStr: any, cid: string | undefined, role: any, quota: any): Promise<any>;
|
|
1377
1389
|
delAuth(id: any): Promise<any>;
|
|
1378
1390
|
save(id: any): Promise<any>;
|
|
@@ -1513,12 +1525,25 @@ declare class BohoAuth {
|
|
|
1513
1525
|
get keepOldConnection(): boolean;
|
|
1514
1526
|
emitAuthEvent(event: any): void;
|
|
1515
1527
|
send_auth_fail(peer: any, reason: any): void;
|
|
1528
|
+
/**
|
|
1529
|
+
* Hook for a subclass: decide where a verified login belongs before it is accepted.
|
|
1530
|
+
* Called after the key and role checks, before quotas and the duplicate login check.
|
|
1531
|
+
* Return null to accept the login here, or a message (e.g. SERVER_REDIRECT) that is sent to the
|
|
1532
|
+
* client instead of AUTH_RES before the connection is closed. The default accepts every login.
|
|
1533
|
+
* @param {object} peer the connection
|
|
1534
|
+
* @param {object} authInfo the key provider's entry for the login ID (cid, uid, role, quota, ...)
|
|
1535
|
+
* @param {string} role resolved role
|
|
1536
|
+
* @param {string} id login ID
|
|
1537
|
+
* @returns {Promise<Uint8Array | null> | Uint8Array | null}
|
|
1538
|
+
*/
|
|
1539
|
+
beforeAccept(peer: object, authInfo: object, role: string, id: string): Promise<Uint8Array | null> | Uint8Array | null;
|
|
1516
1540
|
verify_auth_req(auth_req: any, peer: any): Promise<any>;
|
|
1517
1541
|
getAuth(id: any): Promise<any>;
|
|
1518
1542
|
getAuthIdList(): Promise<any>;
|
|
1519
1543
|
addAuth(id: any, keyStr: any, cid: any, role: any, quota: any): Promise<any>;
|
|
1520
1544
|
delAuth(id: any): Promise<any>;
|
|
1521
1545
|
save(): Promise<any>;
|
|
1546
|
+
#private;
|
|
1522
1547
|
}
|
|
1523
1548
|
|
|
1524
1549
|
declare const version: string;
|
package/dist/node/iosignal.d.ts
CHANGED
|
@@ -57,6 +57,7 @@ type SIZE_LIMIT = {
|
|
|
57
57
|
TAG_LEN2: number;
|
|
58
58
|
CONNECTION_CHECKER_PERIOD: number;
|
|
59
59
|
CLIENT_PING_PERIOD: number;
|
|
60
|
+
SERVER_HELLO_TIMEOUT: number;
|
|
60
61
|
PROMISE_TIMEOUT: number;
|
|
61
62
|
DID: number;
|
|
62
63
|
CID: number;
|
|
@@ -66,6 +67,7 @@ declare namespace SIZE_LIMIT {
|
|
|
66
67
|
let TAG_LEN2: number;
|
|
67
68
|
let CONNECTION_CHECKER_PERIOD: number;
|
|
68
69
|
let CLIENT_PING_PERIOD: number;
|
|
70
|
+
let SERVER_HELLO_TIMEOUT: number;
|
|
69
71
|
let PROMISE_TIMEOUT: number;
|
|
70
72
|
let DID: number;
|
|
71
73
|
let CID: number;
|
|
@@ -352,6 +354,15 @@ declare class IOCore extends EventEmitter<string | symbol, any> {
|
|
|
352
354
|
* @param {string} url2 - The new URL to redirect to.
|
|
353
355
|
*/
|
|
354
356
|
redirect(url2: string): void;
|
|
357
|
+
/**
|
|
358
|
+
* Closes a connection that has not got the server's hello in SIZE_LIMIT.SERVER_HELLO_TIMEOUT
|
|
359
|
+
* (a server that cannot be reached, or a connection that opens but never answers). With
|
|
360
|
+
* autoReconnect the client then reconnects to this.url, so a failed redirect falls back to the
|
|
361
|
+
* original server, as the Arduino library does.
|
|
362
|
+
*/
|
|
363
|
+
armHelloTimer(): void;
|
|
364
|
+
helloTimerID: any;
|
|
365
|
+
clearHelloTimer(): void;
|
|
355
366
|
/**
|
|
356
367
|
* Opens the WebSocket connection.
|
|
357
368
|
* @param {string} [url] - Optional URL to connect to. If not provided, uses the instance's URL.
|
|
@@ -1373,6 +1384,7 @@ declare class RedisKeyProvider {
|
|
|
1373
1384
|
redis: any;
|
|
1374
1385
|
getAuth(id: any): Promise<any>;
|
|
1375
1386
|
getAuthIdList(): Promise<any>;
|
|
1387
|
+
scanKeys(pattern: any): Promise<any>;
|
|
1376
1388
|
addAuth(id: any, keyStr: any, cid: string | undefined, role: any, quota: any): Promise<any>;
|
|
1377
1389
|
delAuth(id: any): Promise<any>;
|
|
1378
1390
|
save(id: any): Promise<any>;
|
|
@@ -1513,12 +1525,25 @@ declare class BohoAuth {
|
|
|
1513
1525
|
get keepOldConnection(): boolean;
|
|
1514
1526
|
emitAuthEvent(event: any): void;
|
|
1515
1527
|
send_auth_fail(peer: any, reason: any): void;
|
|
1528
|
+
/**
|
|
1529
|
+
* Hook for a subclass: decide where a verified login belongs before it is accepted.
|
|
1530
|
+
* Called after the key and role checks, before quotas and the duplicate login check.
|
|
1531
|
+
* Return null to accept the login here, or a message (e.g. SERVER_REDIRECT) that is sent to the
|
|
1532
|
+
* client instead of AUTH_RES before the connection is closed. The default accepts every login.
|
|
1533
|
+
* @param {object} peer the connection
|
|
1534
|
+
* @param {object} authInfo the key provider's entry for the login ID (cid, uid, role, quota, ...)
|
|
1535
|
+
* @param {string} role resolved role
|
|
1536
|
+
* @param {string} id login ID
|
|
1537
|
+
* @returns {Promise<Uint8Array | null> | Uint8Array | null}
|
|
1538
|
+
*/
|
|
1539
|
+
beforeAccept(peer: object, authInfo: object, role: string, id: string): Promise<Uint8Array | null> | Uint8Array | null;
|
|
1516
1540
|
verify_auth_req(auth_req: any, peer: any): Promise<any>;
|
|
1517
1541
|
getAuth(id: any): Promise<any>;
|
|
1518
1542
|
getAuthIdList(): Promise<any>;
|
|
1519
1543
|
addAuth(id: any, keyStr: any, cid: any, role: any, quota: any): Promise<any>;
|
|
1520
1544
|
delAuth(id: any): Promise<any>;
|
|
1521
1545
|
save(): Promise<any>;
|
|
1546
|
+
#private;
|
|
1522
1547
|
}
|
|
1523
1548
|
|
|
1524
1549
|
declare const version: string;
|
package/dist/node/iosignal.js
CHANGED
|
@@ -20,7 +20,7 @@ import { readFileSync } from 'node:fs';
|
|
|
20
20
|
import { totalmem } from 'node:os';
|
|
21
21
|
import path from 'path';
|
|
22
22
|
|
|
23
|
-
var version$1 = "9.
|
|
23
|
+
var version$1 = "9.19.0";
|
|
24
24
|
var pkg = {
|
|
25
25
|
version: version$1};
|
|
26
26
|
|
|
@@ -446,6 +446,7 @@ for (let c in ENC_MODE) { ENC_MODE[ENC_MODE[c]] = c; }
|
|
|
446
446
|
* @property {number} TAG_LEN2
|
|
447
447
|
* @property {number} CONNECTION_CHECKER_PERIOD
|
|
448
448
|
* @property {number} CLIENT_PING_PERIOD
|
|
449
|
+
* @property {number} SERVER_HELLO_TIMEOUT
|
|
449
450
|
* @property {number} PROMISE_TIMEOUT
|
|
450
451
|
* @property {number} DID
|
|
451
452
|
* @property {number} CID
|
|
@@ -455,6 +456,9 @@ const SIZE_LIMIT = {
|
|
|
455
456
|
TAG_LEN2: 65535,
|
|
456
457
|
CONNECTION_CHECKER_PERIOD: 3000,
|
|
457
458
|
CLIENT_PING_PERIOD: 40000,
|
|
459
|
+
// From starting a connection to the server's hello (SERVER_TIME_NONCE). A connection that hangs
|
|
460
|
+
// longer is closed, so the client reconnects to its own URL (after a redirect: the original one).
|
|
461
|
+
SERVER_HELLO_TIMEOUT: 15000,
|
|
458
462
|
PROMISE_TIMEOUT: 5000,
|
|
459
463
|
DID: 8,
|
|
460
464
|
CID: 12
|
|
@@ -963,6 +967,7 @@ class IOCore extends EventEmitter {
|
|
|
963
967
|
*/
|
|
964
968
|
close() {
|
|
965
969
|
// console.log('####### IOCOre.js close() called')
|
|
970
|
+
this.clearHelloTimer();
|
|
966
971
|
// If auto-reconnect is disabled, we must stop the keep-alive timer.
|
|
967
972
|
if (this.autoReconnect === false) {
|
|
968
973
|
clearInterval(this.connectionCheckerIntervalID);
|
|
@@ -1057,6 +1062,30 @@ class IOCore extends EventEmitter {
|
|
|
1057
1062
|
this.stateChange('redirecting','redirecting');
|
|
1058
1063
|
this.close();
|
|
1059
1064
|
this.createConnection(url2);
|
|
1065
|
+
this.armHelloTimer();
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* Closes a connection that has not got the server's hello in SIZE_LIMIT.SERVER_HELLO_TIMEOUT
|
|
1070
|
+
* (a server that cannot be reached, or a connection that opens but never answers). With
|
|
1071
|
+
* autoReconnect the client then reconnects to this.url, so a failed redirect falls back to the
|
|
1072
|
+
* original server, as the Arduino library does.
|
|
1073
|
+
*/
|
|
1074
|
+
armHelloTimer() {
|
|
1075
|
+
this.clearHelloTimer();
|
|
1076
|
+
if (this._closed || !this.socket) return;
|
|
1077
|
+
const socket = this.socket;
|
|
1078
|
+
this.helloTimerID = setTimeout(() => {
|
|
1079
|
+
this.helloTimerID = null;
|
|
1080
|
+
if (this.socket !== socket) return;
|
|
1081
|
+
this.emit('error', new Error('no reply from the server in ' + SIZE_LIMIT.SERVER_HELLO_TIMEOUT / 1000 + ' s'));
|
|
1082
|
+
this.close();
|
|
1083
|
+
}, SIZE_LIMIT.SERVER_HELLO_TIMEOUT);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
clearHelloTimer() {
|
|
1087
|
+
if (this.helloTimerID) clearTimeout(this.helloTimerID);
|
|
1088
|
+
this.helloTimerID = null;
|
|
1060
1089
|
}
|
|
1061
1090
|
|
|
1062
1091
|
/**
|
|
@@ -1082,6 +1111,7 @@ class IOCore extends EventEmitter {
|
|
|
1082
1111
|
// The actual connection is created here.
|
|
1083
1112
|
this.createConnection(this.url);
|
|
1084
1113
|
if (this._closed) return; // A connecting listener may have stopped the client.
|
|
1114
|
+
this.armHelloTimer();
|
|
1085
1115
|
|
|
1086
1116
|
// Ensure the keep-alive timer is running.
|
|
1087
1117
|
if (!this.connectionCheckerIntervalID) {
|
|
@@ -1276,6 +1306,7 @@ class IOCore extends EventEmitter {
|
|
|
1276
1306
|
break;
|
|
1277
1307
|
|
|
1278
1308
|
case it.BohoMsg.SERVER_TIME_NONCE: // SERVER_READY
|
|
1309
|
+
this.clearHelloTimer();
|
|
1279
1310
|
buffer.copy(this.serverTimeNonce);
|
|
1280
1311
|
if (!this.stateChange('server_ready', 'server_ready')) break;
|
|
1281
1312
|
if (this.useAuth) {
|
|
@@ -2159,8 +2190,15 @@ class IOCongSocket extends IOCore {
|
|
|
2159
2190
|
|
|
2160
2191
|
createConnection(url) {
|
|
2161
2192
|
// TCP Socket
|
|
2162
|
-
|
|
2163
|
-
|
|
2193
|
+
// 'host:port', 'ip:port' or 'cong://host:port'. 'ip:port' alone is not a URL (a scheme cannot
|
|
2194
|
+
// start with a digit), and 'host:port' parses with 'host:' as its scheme.
|
|
2195
|
+
let urlObj;
|
|
2196
|
+
try {
|
|
2197
|
+
urlObj = new URL(url);
|
|
2198
|
+
} catch {
|
|
2199
|
+
urlObj = null;
|
|
2200
|
+
}
|
|
2201
|
+
if (urlObj?.protocol != "cong:") {
|
|
2164
2202
|
urlObj = new URL('cong://' + url);
|
|
2165
2203
|
}
|
|
2166
2204
|
// URL keeps the brackets of an IPv6 literal ('[::1]'); net expects the bare address.
|
|
@@ -10001,6 +10039,32 @@ class BohoAuth {
|
|
|
10001
10039
|
}
|
|
10002
10040
|
|
|
10003
10041
|
|
|
10042
|
+
/**
|
|
10043
|
+
* Hook for a subclass: decide where a verified login belongs before it is accepted.
|
|
10044
|
+
* Called after the key and role checks, before quotas and the duplicate login check.
|
|
10045
|
+
* Return null to accept the login here, or a message (e.g. SERVER_REDIRECT) that is sent to the
|
|
10046
|
+
* client instead of AUTH_RES before the connection is closed. The default accepts every login.
|
|
10047
|
+
* @param {object} peer the connection
|
|
10048
|
+
* @param {object} authInfo the key provider's entry for the login ID (cid, uid, role, quota, ...)
|
|
10049
|
+
* @param {string} role resolved role
|
|
10050
|
+
* @param {string} id login ID
|
|
10051
|
+
* @returns {Promise<Uint8Array | null> | Uint8Array | null}
|
|
10052
|
+
*/
|
|
10053
|
+
beforeAccept(peer, authInfo, role, id) {
|
|
10054
|
+
return null
|
|
10055
|
+
}
|
|
10056
|
+
|
|
10057
|
+
// An error in the hook accepts the login: placement must never lock clients out.
|
|
10058
|
+
async #beforeAcceptSafe(peer, authInfo, role, id) {
|
|
10059
|
+
try {
|
|
10060
|
+
const result = await this.beforeAccept(peer, authInfo, role, id);
|
|
10061
|
+
return result instanceof Uint8Array && result.byteLength > 0 ? result : null
|
|
10062
|
+
} catch (error) {
|
|
10063
|
+
console.error('BohoAuth.beforeAccept error:', error);
|
|
10064
|
+
return null
|
|
10065
|
+
}
|
|
10066
|
+
}
|
|
10067
|
+
|
|
10004
10068
|
async verify_auth_req(auth_req, peer) {
|
|
10005
10069
|
try {
|
|
10006
10070
|
if (!peer.checkAuthChallenge()) return;
|
|
@@ -10066,6 +10130,16 @@ class BohoAuth {
|
|
|
10066
10130
|
this.send_auth_fail(peer, 'unknown auth.role: ' + authInfo.role);
|
|
10067
10131
|
return
|
|
10068
10132
|
}
|
|
10133
|
+
// A login another server should take (beforeAccept) goes there before it is counted here or
|
|
10134
|
+
// replaces a connection here.
|
|
10135
|
+
const redirect = await this.#beforeAcceptSafe(peer, authInfo, role, id);
|
|
10136
|
+
if (peer._closing) return; // closed while deciding
|
|
10137
|
+
if (redirect) {
|
|
10138
|
+
peer.boho.isAuthorized = false;
|
|
10139
|
+
peer.send(redirect);
|
|
10140
|
+
peer.close();
|
|
10141
|
+
return
|
|
10142
|
+
}
|
|
10069
10143
|
const quotaName = await this.resolveQuota(authInfo, role);
|
|
10070
10144
|
const quota = Object.hasOwn(peer.manager.quotas, quotaName) ? peer.manager.quotas[quotaName] : undefined;
|
|
10071
10145
|
if (!quota) {
|
|
@@ -11467,11 +11541,26 @@ class RedisKeyProvider {
|
|
|
11467
11541
|
|
|
11468
11542
|
//sudoService call
|
|
11469
11543
|
async getAuthIdList() {
|
|
11470
|
-
|
|
11471
|
-
|
|
11472
|
-
|
|
11473
|
-
|
|
11474
|
-
|
|
11544
|
+
const keys = await this.scanKeys(DEVICE_PREFIX + '*');
|
|
11545
|
+
return keys.map(key => key.slice(DEVICE_PREFIX.length))
|
|
11546
|
+
}
|
|
11547
|
+
|
|
11548
|
+
// Keys matching pattern, by SCAN: KEYS blocks Redis until it has walked every key.
|
|
11549
|
+
// Falls back to KEYS for a client without scan, or a redis-service from before 2026-11-17 that refuses it
|
|
11550
|
+
// (RemoteRedisClient answers a refused command with undefined).
|
|
11551
|
+
async scanKeys(pattern) {
|
|
11552
|
+
if (typeof this.redis.scan === 'function') {
|
|
11553
|
+
const keys = new Set(); // SCAN may return a key more than once
|
|
11554
|
+
let cursor = '0';
|
|
11555
|
+
do {
|
|
11556
|
+
const reply = await this.redis.scan(cursor, { MATCH: pattern, COUNT: 1000 });
|
|
11557
|
+
if (!reply || !Array.isArray(reply.keys)) break
|
|
11558
|
+
for (const key of reply.keys) keys.add(key);
|
|
11559
|
+
cursor = String(reply.cursor);
|
|
11560
|
+
if (cursor === '0') return [...keys]
|
|
11561
|
+
} while (true)
|
|
11562
|
+
}
|
|
11563
|
+
return (await this.redis.keys(pattern)) ?? []
|
|
11475
11564
|
}
|
|
11476
11565
|
|
|
11477
11566
|
// add device auth info
|
|
@@ -11689,12 +11778,13 @@ var sudoService = /*#__PURE__*/Object.freeze({
|
|
|
11689
11778
|
*
|
|
11690
11779
|
*/
|
|
11691
11780
|
|
|
11781
|
+
// KEYS stays for older clients; new ones list keys with SCAN (RedisKeyProvider.scanKeys).
|
|
11692
11782
|
const COMMANDS = [
|
|
11693
11783
|
'GET', 'SET', 'HGETALL', 'HGET', 'HSET', 'SADD', 'SISMEMBER',
|
|
11694
|
-
'SMEMBERS', 'EXISTS', 'SREM', 'DEL', 'KEYS', 'SAVE',
|
|
11784
|
+
'SMEMBERS', 'EXISTS', 'SREM', 'DEL', 'KEYS', 'SCAN', 'SAVE',
|
|
11695
11785
|
'LPUSH', 'LTRIM', 'LRANGE', 'LINDEX', 'LSET', 'EXPIRE',
|
|
11696
11786
|
'get', 'set', 'hGetAll', 'hGet', 'hSet', 'sAdd', 'sIsMember',
|
|
11697
|
-
'sMembers', 'exists', 'sRem', 'del', 'keys', 'save',
|
|
11787
|
+
'sMembers', 'exists', 'sRem', 'del', 'keys', 'scan', 'save',
|
|
11698
11788
|
'lPush', 'lTrim', 'lRange', 'lIndex', 'lSet', 'expire'
|
|
11699
11789
|
];
|
|
11700
11790
|
|
|
@@ -187,6 +187,15 @@ export class IOCore extends EventEmitter<string | symbol, any> {
|
|
|
187
187
|
* @param {string} url2 - The new URL to redirect to.
|
|
188
188
|
*/
|
|
189
189
|
redirect(url2: string): void;
|
|
190
|
+
/**
|
|
191
|
+
* Closes a connection that has not got the server's hello in SIZE_LIMIT.SERVER_HELLO_TIMEOUT
|
|
192
|
+
* (a server that cannot be reached, or a connection that opens but never answers). With
|
|
193
|
+
* autoReconnect the client then reconnects to this.url, so a failed redirect falls back to the
|
|
194
|
+
* original server, as the Arduino library does.
|
|
195
|
+
*/
|
|
196
|
+
armHelloTimer(): void;
|
|
197
|
+
helloTimerID: any;
|
|
198
|
+
clearHelloTimer(): void;
|
|
190
199
|
/**
|
|
191
200
|
* Opens the WebSocket connection.
|
|
192
201
|
* @param {string} [url] - Optional URL to connect to. If not provided, uses the instance's URL.
|
|
@@ -45,6 +45,7 @@ export type SIZE_LIMIT = {
|
|
|
45
45
|
TAG_LEN2: number;
|
|
46
46
|
CONNECTION_CHECKER_PERIOD: number;
|
|
47
47
|
CLIENT_PING_PERIOD: number;
|
|
48
|
+
SERVER_HELLO_TIMEOUT: number;
|
|
48
49
|
PROMISE_TIMEOUT: number;
|
|
49
50
|
DID: number;
|
|
50
51
|
CID: number;
|
|
@@ -54,6 +55,7 @@ export namespace SIZE_LIMIT {
|
|
|
54
55
|
let TAG_LEN2: number;
|
|
55
56
|
let CONNECTION_CHECKER_PERIOD: number;
|
|
56
57
|
let CLIENT_PING_PERIOD: number;
|
|
58
|
+
let SERVER_HELLO_TIMEOUT: number;
|
|
57
59
|
let PROMISE_TIMEOUT: number;
|
|
58
60
|
let DID: number;
|
|
59
61
|
let CID: number;
|