@nora-link/client 1.3.0 → 2.0.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/dist/cli.js +1 -0
- package/dist/client.js +104 -102
- package/package.json +9 -8
package/dist/cli.js
CHANGED
|
@@ -9,6 +9,7 @@ commander_1.program
|
|
|
9
9
|
.name(name)
|
|
10
10
|
.version(version, '-v, --version')
|
|
11
11
|
.usage('[OPTIONS]...')
|
|
12
|
+
.showHelpAfterError()
|
|
12
13
|
.option('-d, --debug', 'output extra debugging', false)
|
|
13
14
|
.option('-n, --non-secure', 'don\'t use secure connection', false)
|
|
14
15
|
.option('-h, --host <hostname>', 'Server host name')
|
package/dist/client.js
CHANGED
|
@@ -12,115 +12,117 @@ const const_1 = require("./const");
|
|
|
12
12
|
const firebase_1 = require("./firebase");
|
|
13
13
|
const node_crypto_1 = require("node:crypto");
|
|
14
14
|
class Client {
|
|
15
|
+
options;
|
|
16
|
+
retryCount = 1;
|
|
17
|
+
clientId;
|
|
15
18
|
constructor(options) {
|
|
16
19
|
this.options = options;
|
|
17
|
-
this.retryCount = 1;
|
|
18
|
-
this.server$ = new rxjs_1.Observable(observer => {
|
|
19
|
-
const subdomains = this.options.tunnels.map(v => `${v.subdomain}|${v.label}`);
|
|
20
|
-
const qs = (0, node_querystring_1.stringify)({ s: subdomains, c: this.clientId });
|
|
21
|
-
const protocol = (this.options.secure ?? true) ? 'wss' : 'ws';
|
|
22
|
-
const headers = {
|
|
23
|
-
authorization: `Bearer ${this.options.apiKey}`,
|
|
24
|
-
...(this.options.agent ? {
|
|
25
|
-
'user-agent': this.options.agent,
|
|
26
|
-
} : {}),
|
|
27
|
-
};
|
|
28
|
-
const hostname = this.options.hostname ?? 'noralink.eu';
|
|
29
|
-
const ws = new ws_1.default(`${protocol}://${hostname}/api/tunnel?${qs}`, {
|
|
30
|
-
headers,
|
|
31
|
-
followRedirects: true,
|
|
32
|
-
});
|
|
33
|
-
ws.on('open', () => {
|
|
34
|
-
observer.next(ws);
|
|
35
|
-
});
|
|
36
|
-
ws.on('error', (err) => {
|
|
37
|
-
observer.error(err);
|
|
38
|
-
});
|
|
39
|
-
ws.on('close', () => observer.complete());
|
|
40
|
-
return () => ws.close();
|
|
41
|
-
}).pipe((0, rxjs_1.share)({ connector: () => new rxjs_1.ReplaySubject(1) }));
|
|
42
|
-
this.data$ = this.server$.pipe((0, rxjs_1.switchMap)(ws => new rxjs_1.Observable(observer => {
|
|
43
|
-
const handler = (msg) => {
|
|
44
|
-
Buffer.isBuffer(msg) && observer.next(msg);
|
|
45
|
-
};
|
|
46
|
-
const errorHandler = (err) => observer.error(err);
|
|
47
|
-
const closeHandler = (code, reason) => {
|
|
48
|
-
observer.error(new Error(`${code} - ${reason.toString()}`));
|
|
49
|
-
};
|
|
50
|
-
ws.on('message', handler);
|
|
51
|
-
ws.once('error', errorHandler);
|
|
52
|
-
ws.once('close', closeHandler);
|
|
53
|
-
return () => {
|
|
54
|
-
ws.off('message', handler);
|
|
55
|
-
ws.off('error', errorHandler);
|
|
56
|
-
ws.off('close', closeHandler);
|
|
57
|
-
ws.close();
|
|
58
|
-
};
|
|
59
|
-
})), (0, rxjs_1.map)(v => {
|
|
60
|
-
if (v.length < 18) {
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
let offset = 0;
|
|
64
|
-
const version = v.readUInt8(offset++);
|
|
65
|
-
if (version !== 1) {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
const id = v.subarray(offset, offset + 16);
|
|
69
|
-
offset += 16;
|
|
70
|
-
const typeLength = v.readUint8(offset++);
|
|
71
|
-
const type = v.subarray(offset, offset + typeLength).toString();
|
|
72
|
-
offset += typeLength;
|
|
73
|
-
const msg = v.subarray(offset);
|
|
74
|
-
return {
|
|
75
|
-
id,
|
|
76
|
-
type,
|
|
77
|
-
msg,
|
|
78
|
-
};
|
|
79
|
-
}), (0, rxjs_1.filter)(v => !!v), (0, rxjs_1.share)());
|
|
80
|
-
this.connectToServer$ = (0, rxjs_1.merge)(this.server$.pipe(
|
|
81
|
-
//wait a bit before deeming `connected`. Server might close the connection for various reasons.
|
|
82
|
-
(0, rxjs_1.delay)(500), (0, rxjs_1.tap)(() => {
|
|
83
|
-
this.retryCount = 1;
|
|
84
|
-
}), (0, rxjs_1.map)(_ => 'connected')), this.data$.pipe((0, rxjs_1.mergeMap)(({ id, msg, type }) => {
|
|
85
|
-
switch (type) {
|
|
86
|
-
case 'http':
|
|
87
|
-
case 'ws':
|
|
88
|
-
return this.httpRequest({
|
|
89
|
-
type,
|
|
90
|
-
options: JSON.parse(msg.toString()),
|
|
91
|
-
data$: this.getStreamData(id),
|
|
92
|
-
send: (type, msg) => this.send(id, type, msg),
|
|
93
|
-
});
|
|
94
|
-
case 'go-idle':
|
|
95
|
-
const { db, dbKey, apiKey, token } = JSON.parse(msg.toString());
|
|
96
|
-
this.idle$.next({ db, dbKey, apiKey, token });
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
return rxjs_1.EMPTY;
|
|
100
|
-
}))).pipe((0, rxjs_1.startWith)('connecting'), (0, rxjs_1.catchError)(err => (0, rxjs_1.concat)((0, rxjs_1.of)('disconnected'), (0, rxjs_1.throwError)(() => err))), (0, rxjs_1.retry)({
|
|
101
|
-
delay: (err) => {
|
|
102
|
-
const delaySeconds = Math.round(Math.min(600, Math.pow(1.8, this.retryCount - 1)));
|
|
103
|
-
this.options.logger?.error(`[nora-link] connection error ${this.retryCount}: ${err}`);
|
|
104
|
-
this.options.logger?.info(`[nora-link] retrying in ${delaySeconds} sec`);
|
|
105
|
-
this.retryCount++;
|
|
106
|
-
return (0, rxjs_1.timer)(delaySeconds * 1000);
|
|
107
|
-
},
|
|
108
|
-
}), (0, rxjs_1.share)());
|
|
109
|
-
this.idle$ = new rxjs_1.BehaviorSubject(null);
|
|
110
|
-
this.handle$ = this.idle$.pipe((0, rxjs_1.switchMap)(idle => idle
|
|
111
|
-
? (0, firebase_1.goIdleAndWaitForSignal)({
|
|
112
|
-
...idle,
|
|
113
|
-
subdomains: this.options.tunnels.map(t => t.subdomain),
|
|
114
|
-
goOutOfIdle: () => this.idle$.next(null),
|
|
115
|
-
logger: this.options.logger,
|
|
116
|
-
})
|
|
117
|
-
: this.connectToServer$));
|
|
118
20
|
if (!options.agent) {
|
|
119
21
|
const { name, version } = require('../package.json');
|
|
120
22
|
options.agent = `${name}@${version}`;
|
|
121
23
|
}
|
|
122
24
|
this.clientId = options.clientId ?? (0, node_crypto_1.randomBytes)(16).toString('base64url');
|
|
123
25
|
}
|
|
26
|
+
server$ = new rxjs_1.Observable(observer => {
|
|
27
|
+
const subdomains = this.options.tunnels.map(v => `${v.subdomain}|${v.label}`);
|
|
28
|
+
const qs = (0, node_querystring_1.stringify)({ s: subdomains, c: this.clientId });
|
|
29
|
+
const protocol = (this.options.secure ?? true) ? 'wss' : 'ws';
|
|
30
|
+
const headers = {
|
|
31
|
+
authorization: `Bearer ${this.options.apiKey}`,
|
|
32
|
+
...(this.options.agent ? {
|
|
33
|
+
'user-agent': this.options.agent,
|
|
34
|
+
} : {}),
|
|
35
|
+
};
|
|
36
|
+
const hostname = this.options.hostname ?? 'noralink.eu';
|
|
37
|
+
const ws = new ws_1.default(`${protocol}://${hostname}/api/tunnel?${qs}`, {
|
|
38
|
+
headers,
|
|
39
|
+
followRedirects: true,
|
|
40
|
+
});
|
|
41
|
+
ws.on('open', () => {
|
|
42
|
+
observer.next(ws);
|
|
43
|
+
});
|
|
44
|
+
ws.on('error', (err) => {
|
|
45
|
+
observer.error(err);
|
|
46
|
+
});
|
|
47
|
+
ws.on('close', () => observer.complete());
|
|
48
|
+
return () => ws.close();
|
|
49
|
+
}).pipe((0, rxjs_1.share)({ connector: () => new rxjs_1.ReplaySubject(1) }));
|
|
50
|
+
data$ = this.server$.pipe((0, rxjs_1.switchMap)(ws => new rxjs_1.Observable(observer => {
|
|
51
|
+
const handler = (msg) => {
|
|
52
|
+
Buffer.isBuffer(msg) && observer.next(msg);
|
|
53
|
+
};
|
|
54
|
+
const errorHandler = (err) => observer.error(err);
|
|
55
|
+
const closeHandler = (code, reason) => {
|
|
56
|
+
observer.error(new Error(`${code} - ${reason.toString()}`));
|
|
57
|
+
};
|
|
58
|
+
ws.on('message', handler);
|
|
59
|
+
ws.once('error', errorHandler);
|
|
60
|
+
ws.once('close', closeHandler);
|
|
61
|
+
return () => {
|
|
62
|
+
ws.off('message', handler);
|
|
63
|
+
ws.off('error', errorHandler);
|
|
64
|
+
ws.off('close', closeHandler);
|
|
65
|
+
ws.close();
|
|
66
|
+
};
|
|
67
|
+
})), (0, rxjs_1.map)(v => {
|
|
68
|
+
if (v.length < 18) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
let offset = 0;
|
|
72
|
+
const version = v.readUInt8(offset++);
|
|
73
|
+
if (version !== 1) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const id = v.subarray(offset, offset + 16);
|
|
77
|
+
offset += 16;
|
|
78
|
+
const typeLength = v.readUint8(offset++);
|
|
79
|
+
const type = v.subarray(offset, offset + typeLength).toString();
|
|
80
|
+
offset += typeLength;
|
|
81
|
+
const msg = v.subarray(offset);
|
|
82
|
+
return {
|
|
83
|
+
id,
|
|
84
|
+
type,
|
|
85
|
+
msg,
|
|
86
|
+
};
|
|
87
|
+
}), (0, rxjs_1.filter)(v => !!v), (0, rxjs_1.share)());
|
|
88
|
+
connectToServer$ = (0, rxjs_1.merge)(this.server$.pipe(
|
|
89
|
+
//wait a bit before deeming `connected`. Server might close the connection for various reasons.
|
|
90
|
+
(0, rxjs_1.delay)(500), (0, rxjs_1.tap)(() => {
|
|
91
|
+
this.retryCount = 1;
|
|
92
|
+
}), (0, rxjs_1.map)(_ => 'connected')), this.data$.pipe((0, rxjs_1.mergeMap)(({ id, msg, type }) => {
|
|
93
|
+
switch (type) {
|
|
94
|
+
case 'http':
|
|
95
|
+
case 'ws':
|
|
96
|
+
return this.httpRequest({
|
|
97
|
+
type,
|
|
98
|
+
options: JSON.parse(msg.toString()),
|
|
99
|
+
data$: this.getStreamData(id),
|
|
100
|
+
send: (type, msg) => this.send(id, type, msg),
|
|
101
|
+
});
|
|
102
|
+
case 'go-idle':
|
|
103
|
+
const { db, dbKey, apiKey, token } = JSON.parse(msg.toString());
|
|
104
|
+
this.idle$.next({ db, dbKey, apiKey, token });
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
return rxjs_1.EMPTY;
|
|
108
|
+
}))).pipe((0, rxjs_1.startWith)('connecting'), (0, rxjs_1.catchError)(err => (0, rxjs_1.concat)((0, rxjs_1.of)('disconnected'), (0, rxjs_1.throwError)(() => err))), (0, rxjs_1.retry)({
|
|
109
|
+
delay: (err) => {
|
|
110
|
+
const delaySeconds = Math.round(Math.min(600, Math.pow(1.8, this.retryCount - 1)));
|
|
111
|
+
this.options.logger?.error(`[nora-link] connection error ${this.retryCount}: ${err}`);
|
|
112
|
+
this.options.logger?.info(`[nora-link] retrying in ${delaySeconds} sec`);
|
|
113
|
+
this.retryCount++;
|
|
114
|
+
return (0, rxjs_1.timer)(delaySeconds * 1000);
|
|
115
|
+
},
|
|
116
|
+
}), (0, rxjs_1.share)());
|
|
117
|
+
idle$ = new rxjs_1.BehaviorSubject(null);
|
|
118
|
+
handle$ = this.idle$.pipe((0, rxjs_1.switchMap)(idle => idle
|
|
119
|
+
? (0, firebase_1.goIdleAndWaitForSignal)({
|
|
120
|
+
...idle,
|
|
121
|
+
subdomains: this.options.tunnels.map(t => t.subdomain),
|
|
122
|
+
goOutOfIdle: () => this.idle$.next(null),
|
|
123
|
+
logger: this.options.logger,
|
|
124
|
+
})
|
|
125
|
+
: this.connectToServer$));
|
|
124
126
|
send(id, type, msg) {
|
|
125
127
|
return this.server$.pipe((0, rxjs_1.first)(), (0, rxjs_1.switchMap)(ws => {
|
|
126
128
|
const typeBuffer = Buffer.from(type);
|
|
@@ -183,7 +185,7 @@ class Client {
|
|
|
183
185
|
].join('\r\n');
|
|
184
186
|
observer.next(send(const_1.MessageTypes.DATA, Buffer.from(head)));
|
|
185
187
|
socket.on('data', data => {
|
|
186
|
-
observer.next(send(const_1.MessageTypes.DATA, data));
|
|
188
|
+
observer.next(send(const_1.MessageTypes.DATA, Buffer.from(data)));
|
|
187
189
|
});
|
|
188
190
|
socket.once('end', () => {
|
|
189
191
|
observer.next(send(const_1.MessageTypes.END));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nora-link/client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "noralink.eu client",
|
|
5
5
|
"bin": {
|
|
6
6
|
"noralink": "dist/cli.js"
|
|
@@ -22,14 +22,15 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/andrei-tatar/nora-link-client#readme",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/
|
|
26
|
-
"
|
|
27
|
-
"
|
|
25
|
+
"@types/node": "^25.6.2",
|
|
26
|
+
"@types/ws": "^8.18.1",
|
|
27
|
+
"tsx": "^4.21.0",
|
|
28
|
+
"typescript": "^6.0.3"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"commander": "^
|
|
31
|
-
"firebase": "^
|
|
32
|
-
"rxjs": "^7.8.
|
|
33
|
-
"ws": "^8.
|
|
31
|
+
"commander": "^14.0.3",
|
|
32
|
+
"firebase": "^12.13.0",
|
|
33
|
+
"rxjs": "^7.8.2",
|
|
34
|
+
"ws": "^8.20.0"
|
|
34
35
|
}
|
|
35
36
|
}
|