@celljs/ta-relay 4.8.0 → 4.9.1
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.en.md +8 -3
- package/README.md +8 -3
- package/esm/common/index.d.ts +2 -0
- package/esm/common/index.js +2 -0
- package/esm/common/qr-code.d.ts +42 -0
- package/esm/common/qr-code.js +366 -0
- package/esm/common/relay-protocol.d.ts +42 -1
- package/esm/common/relay-protocol.js +26 -0
- package/esm/common/relay-room.d.ts +1 -0
- package/esm/common/relay-room.js +13 -2
- package/esm/common/relay-share.d.ts +31 -0
- package/esm/common/relay-share.js +117 -0
- package/esm/common/relay-socket.d.ts +18 -1
- package/esm/common/relay-socket.js +43 -5
- package/esm/node/relay-host.d.ts +18 -3
- package/esm/node/relay-host.js +82 -8
- package/esm/node/relay-server.js +10 -6
- package/esm/node/relay-store.d.ts +29 -1
- package/esm/node/relay-store.js +54 -2
- package/lib/common/index.d.ts +2 -0
- package/lib/common/index.js +2 -0
- package/lib/common/qr-code.d.ts +43 -0
- package/lib/common/qr-code.js +370 -0
- package/lib/common/relay-protocol.d.ts +42 -1
- package/lib/common/relay-protocol.js +27 -1
- package/lib/common/relay-room.d.ts +1 -0
- package/lib/common/relay-room.js +12 -1
- package/lib/common/relay-share.d.ts +32 -0
- package/lib/common/relay-share.js +120 -0
- package/lib/common/relay-socket.d.ts +18 -1
- package/lib/common/relay-socket.js +42 -4
- package/lib/node/relay-host.d.ts +18 -3
- package/lib/node/relay-host.js +81 -7
- package/lib/node/relay-server.js +9 -5
- package/lib/node/relay-store.d.ts +29 -1
- package/lib/node/relay-store.js +54 -2
- package/package.json +4 -3
- package/esm/common/index.js.map +0 -1
- package/esm/common/relay-bytes.js.map +0 -1
- package/esm/common/relay-cipher.js.map +0 -1
- package/esm/common/relay-pairing.js.map +0 -1
- package/esm/common/relay-protocol.js.map +0 -1
- package/esm/common/relay-room.js.map +0 -1
- package/esm/common/relay-socket.js.map +0 -1
- package/esm/node/index.js.map +0 -1
- package/esm/node/relay-host.js.map +0 -1
- package/esm/node/relay-server.js.map +0 -1
- package/esm/node/relay-store.js.map +0 -1
- package/lib/common/index.d.ts.map +0 -1
- package/lib/common/index.js.map +0 -1
- package/lib/common/relay-bytes.d.ts.map +0 -1
- package/lib/common/relay-bytes.js.map +0 -1
- package/lib/common/relay-cipher.d.ts.map +0 -1
- package/lib/common/relay-cipher.js.map +0 -1
- package/lib/common/relay-cipher.spec.d.ts +0 -2
- package/lib/common/relay-cipher.spec.d.ts.map +0 -1
- package/lib/common/relay-cipher.spec.js +0 -43
- package/lib/common/relay-cipher.spec.js.map +0 -1
- package/lib/common/relay-pairing.d.ts.map +0 -1
- package/lib/common/relay-pairing.js.map +0 -1
- package/lib/common/relay-pairing.spec.d.ts +0 -2
- package/lib/common/relay-pairing.spec.d.ts.map +0 -1
- package/lib/common/relay-pairing.spec.js +0 -25
- package/lib/common/relay-pairing.spec.js.map +0 -1
- package/lib/common/relay-protocol.d.ts.map +0 -1
- package/lib/common/relay-protocol.js.map +0 -1
- package/lib/common/relay-room.d.ts.map +0 -1
- package/lib/common/relay-room.js.map +0 -1
- package/lib/common/relay-socket.d.ts.map +0 -1
- package/lib/common/relay-socket.js.map +0 -1
- package/lib/node/index.d.ts.map +0 -1
- package/lib/node/index.js.map +0 -1
- package/lib/node/relay-host.d.ts.map +0 -1
- package/lib/node/relay-host.js.map +0 -1
- package/lib/node/relay-server.d.ts.map +0 -1
- package/lib/node/relay-server.js.map +0 -1
- package/lib/node/relay-store.d.ts.map +0 -1
- package/lib/node/relay-store.js.map +0 -1
- package/lib/node/relay.spec.d.ts +0 -2
- package/lib/node/relay.spec.d.ts.map +0 -1
- package/lib/node/relay.spec.js +0 -141
- package/lib/node/relay.spec.js.map +0 -1
- package/src/common/index.ts +0 -6
- package/src/common/relay-bytes.ts +0 -37
- package/src/common/relay-cipher.spec.ts +0 -45
- package/src/common/relay-cipher.ts +0 -74
- package/src/common/relay-pairing.spec.ts +0 -26
- package/src/common/relay-pairing.ts +0 -46
- package/src/common/relay-protocol.ts +0 -50
- package/src/common/relay-room.ts +0 -79
- package/src/common/relay-socket.ts +0 -106
- package/src/node/index.ts +0 -4
- package/src/node/relay-host.ts +0 -230
- package/src/node/relay-server.ts +0 -143
- package/src/node/relay-store.ts +0 -84
- package/src/node/relay.spec.ts +0 -151
package/lib/common/relay-room.js
CHANGED
|
@@ -26,6 +26,11 @@ class RelayRoom {
|
|
|
26
26
|
return undefined;
|
|
27
27
|
}
|
|
28
28
|
fromClient(channel, text) {
|
|
29
|
+
var _a;
|
|
30
|
+
if (text === relay_protocol_1.RELAY_PING) {
|
|
31
|
+
(_a = this.peers.client(channel)) === null || _a === void 0 ? void 0 : _a.send(relay_protocol_1.RELAY_PONG);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
29
34
|
const host = this.peers.host();
|
|
30
35
|
if (host) {
|
|
31
36
|
this.toHost(host, { t: 'data', ch: channel, d: text });
|
|
@@ -39,11 +44,16 @@ class RelayRoom {
|
|
|
39
44
|
}
|
|
40
45
|
/** What the host sends that is not a message of the protocol is dropped. */
|
|
41
46
|
fromHost(text) {
|
|
47
|
+
var _a;
|
|
48
|
+
if (text === relay_protocol_1.RELAY_PING) {
|
|
49
|
+
(_a = this.peers.host()) === null || _a === void 0 ? void 0 : _a.send(relay_protocol_1.RELAY_PONG);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
42
52
|
let message;
|
|
43
53
|
try {
|
|
44
54
|
message = JSON.parse(text);
|
|
45
55
|
}
|
|
46
|
-
catch (
|
|
56
|
+
catch (_b) {
|
|
47
57
|
return;
|
|
48
58
|
}
|
|
49
59
|
const client = typeof message.ch === 'string' ? this.peers.client(message.ch) : undefined;
|
|
@@ -54,6 +64,7 @@ class RelayRoom {
|
|
|
54
64
|
client === null || client === void 0 ? void 0 : client.close(relay_protocol_1.RelayCloseCode.CLOSED_BY_HOST, 'Closed by the host.');
|
|
55
65
|
}
|
|
56
66
|
}
|
|
67
|
+
/** The host went, or connected again (its channels went with the connection before): its devices are told to come again. */
|
|
57
68
|
hostClosed() {
|
|
58
69
|
for (const client of this.peers.clients()) {
|
|
59
70
|
client.close(relay_protocol_1.RelayCloseCode.HOST_OFFLINE, 'The host went offline.');
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** What another computer needs to take a package from the host that shared it, through the relay. */
|
|
2
|
+
export interface RelayShareLink {
|
|
3
|
+
relay: string;
|
|
4
|
+
host: string;
|
|
5
|
+
/** The id of the share, as a device's on the relay. */
|
|
6
|
+
share: string;
|
|
7
|
+
/** base64url, 32 bytes. */
|
|
8
|
+
key: string;
|
|
9
|
+
/** The name of its package, to show before it is fetched. */
|
|
10
|
+
name?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface RelayShareOptions {
|
|
13
|
+
maxBytes?: number;
|
|
14
|
+
/** For all of it. Default 60 seconds. */
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
}
|
|
17
|
+
export declare namespace RelayShareUtil {
|
|
18
|
+
/** `https://<relay>/share#v=1&h=<host>&s=<share>&k=<key>&n=<name>`: the key in the fragment, which is not sent to the relay. */
|
|
19
|
+
function link(share: RelayShareLink): string;
|
|
20
|
+
function is(text: string): boolean;
|
|
21
|
+
/** Throws on what is not a link of {@link link}. */
|
|
22
|
+
function parse(text: string): RelayShareLink;
|
|
23
|
+
/**
|
|
24
|
+
* The package of a link, from the computer that shared it, end to end through the relay: that computer
|
|
25
|
+
* must be running Ta. Rejects past the size limit, the time limit, or when the share is gone.
|
|
26
|
+
*/
|
|
27
|
+
function fetch(text: string, options?: RelayShareOptions): Promise<{
|
|
28
|
+
name: string;
|
|
29
|
+
data: Uint8Array;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=relay-share.d.ts.map
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayShareUtil = void 0;
|
|
4
|
+
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
6
|
+
const relay_socket_1 = require("./relay-socket");
|
|
7
|
+
var RelayShareUtil;
|
|
8
|
+
(function (RelayShareUtil) {
|
|
9
|
+
/** `https://<relay>/share#v=1&h=<host>&s=<share>&k=<key>&n=<name>`: the key in the fragment, which is not sent to the relay. */
|
|
10
|
+
function link(share) {
|
|
11
|
+
const fragment = new URLSearchParams({ v: '1', h: share.host, s: share.share, k: share.key });
|
|
12
|
+
if (share.name) {
|
|
13
|
+
fragment.set('n', share.name);
|
|
14
|
+
}
|
|
15
|
+
return `${share.relay}${relay_protocol_1.RELAY_SHARE_PATH}#${fragment}`;
|
|
16
|
+
}
|
|
17
|
+
RelayShareUtil.link = link;
|
|
18
|
+
function is(text) {
|
|
19
|
+
try {
|
|
20
|
+
parse(text);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
catch (_a) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
RelayShareUtil.is = is;
|
|
28
|
+
/** Throws on what is not a link of {@link link}. */
|
|
29
|
+
function parse(text) {
|
|
30
|
+
var _a;
|
|
31
|
+
const url = new URL(text.trim());
|
|
32
|
+
const fragment = new URLSearchParams(url.hash.slice(1));
|
|
33
|
+
const [host, share, key] = ['h', 's', 'k'].map(name => { var _a; return (_a = fragment.get(name)) !== null && _a !== void 0 ? _a : ''; });
|
|
34
|
+
if (!/^https?:$/.test(url.protocol) ||
|
|
35
|
+
url.pathname !== relay_protocol_1.RELAY_SHARE_PATH ||
|
|
36
|
+
fragment.get('v') !== '1' ||
|
|
37
|
+
!relay_protocol_1.RELAY_ID.test(host) ||
|
|
38
|
+
!relay_protocol_1.RELAY_ID.test(share) ||
|
|
39
|
+
!/^[A-Za-z0-9_-]{43}$/.test(key)) {
|
|
40
|
+
throw new Error('Not a link of an extension of Ta.');
|
|
41
|
+
}
|
|
42
|
+
return { relay: url.origin, host, share, key, name: (_a = fragment.get('n')) !== null && _a !== void 0 ? _a : undefined };
|
|
43
|
+
}
|
|
44
|
+
RelayShareUtil.parse = parse;
|
|
45
|
+
/**
|
|
46
|
+
* The package of a link, from the computer that shared it, end to end through the relay: that computer
|
|
47
|
+
* must be running Ta. Rejects past the size limit, the time limit, or when the share is gone.
|
|
48
|
+
*/
|
|
49
|
+
function fetch(text, options = {}) {
|
|
50
|
+
var _a;
|
|
51
|
+
const share = parse(text);
|
|
52
|
+
const max = (_a = options.maxBytes) !== null && _a !== void 0 ? _a : relay_protocol_1.RELAY_SHARE_MAX_BYTES;
|
|
53
|
+
const pairing = { relay: share.relay, host: share.host, device: share.share, key: share.key };
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
var _a;
|
|
56
|
+
const socket = new relay_socket_1.RelaySocket(pairing);
|
|
57
|
+
const chunks = [];
|
|
58
|
+
let received = 0;
|
|
59
|
+
let expected;
|
|
60
|
+
let done = false;
|
|
61
|
+
const finish = (error, result) => {
|
|
62
|
+
if (done) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
done = true;
|
|
66
|
+
clearTimeout(timer);
|
|
67
|
+
socket.onclose = null;
|
|
68
|
+
socket.close();
|
|
69
|
+
if (error) {
|
|
70
|
+
reject(error);
|
|
71
|
+
}
|
|
72
|
+
else if (result) {
|
|
73
|
+
resolve(result);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const timer = setTimeout(() => finish(new Error('The computer that shared it took too long to send it.')), (_a = options.timeoutMs) !== null && _a !== void 0 ? _a : 60000);
|
|
77
|
+
socket.onmessage = ({ data }) => {
|
|
78
|
+
let message;
|
|
79
|
+
try {
|
|
80
|
+
message = JSON.parse(data);
|
|
81
|
+
}
|
|
82
|
+
catch (_a) {
|
|
83
|
+
finish(new Error('The computer that shared it sent what is not a package.'));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (message.t === 'error') {
|
|
87
|
+
finish(new Error(message.message));
|
|
88
|
+
}
|
|
89
|
+
else if (message.t === 'package') {
|
|
90
|
+
if (!(message.size > 0) || message.size > max) {
|
|
91
|
+
finish(new Error(`The package is larger than ${Math.round(max / 1024 / 1024)} MB.`));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
expected = { name: String(message.name), size: message.size };
|
|
95
|
+
}
|
|
96
|
+
else if (message.t === 'chunk' && expected) {
|
|
97
|
+
const bytes = relay_bytes_1.RelayBytes.fromBase64(message.d);
|
|
98
|
+
received += bytes.length;
|
|
99
|
+
if (received > expected.size) {
|
|
100
|
+
finish(new Error('The package is larger than it said.'));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
chunks.push(bytes);
|
|
104
|
+
}
|
|
105
|
+
else if (message.t === 'end' && expected) {
|
|
106
|
+
if (received !== expected.size) {
|
|
107
|
+
finish(new Error('The package came short.'));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
finish(undefined, { name: expected.name, data: relay_bytes_1.RelayBytes.concat(...chunks) });
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
socket.onclose = ({ code }) => finish(new Error(code === relay_socket_1.RelaySocket.REFUSED
|
|
114
|
+
? 'This link no longer works: the share was stopped or ran out.'
|
|
115
|
+
: 'The computer that shared it is not running Ta, or cannot be reached.'));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
RelayShareUtil.fetch = fetch;
|
|
119
|
+
})(RelayShareUtil || (exports.RelayShareUtil = RelayShareUtil = {}));
|
|
120
|
+
//# sourceMappingURL=relay-share.js.map
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { RelayCipher } from './relay-cipher';
|
|
2
2
|
import { type RelayPairing } from './relay-pairing';
|
|
3
|
+
export interface RelaySocketOptions {
|
|
4
|
+
/** Default RELAY_PING_MS. */
|
|
5
|
+
pingMs?: number;
|
|
6
|
+
/** Default 15 seconds. */
|
|
7
|
+
helloMs?: number;
|
|
8
|
+
}
|
|
3
9
|
/**
|
|
4
10
|
* A WebSocket to ta serve through a relay, for a paired device: it says hello, checks that the host has
|
|
5
11
|
* the device's key, and then seals and opens every message. It behaves as much like a WebSocket as
|
|
@@ -27,13 +33,24 @@ export declare class RelaySocket {
|
|
|
27
33
|
protected cipher?: RelayCipher;
|
|
28
34
|
protected readonly socket: WebSocket;
|
|
29
35
|
protected readonly nonce: Uint8Array<ArrayBufferLike>;
|
|
30
|
-
|
|
36
|
+
protected readonly helloTimer: ReturnType<typeof setTimeout>;
|
|
37
|
+
protected pingTimer?: ReturnType<typeof setInterval>;
|
|
38
|
+
/** Whether the relay answers pings: one of before does not, and is not given up for that. */
|
|
39
|
+
protected ponged: boolean;
|
|
40
|
+
protected awaitingPong: boolean;
|
|
41
|
+
/** Messages are opened one after another, in order, and a close waits for them: a host may send several at once, then close. */
|
|
42
|
+
protected receiving: Promise<void>;
|
|
43
|
+
constructor(pairing: RelayPairing, options?: RelaySocketOptions);
|
|
31
44
|
get readyState(): number;
|
|
32
45
|
/** Why it closed, once it has. */
|
|
33
46
|
get closeCode(): number | undefined;
|
|
34
47
|
send(text: string): void;
|
|
35
48
|
close(code?: number, reason?: string): void;
|
|
36
49
|
protected receive(text: string): Promise<void>;
|
|
50
|
+
/** The relay stopped answering: the connection is gone without a word, and closing it would wait for one. */
|
|
51
|
+
protected ping(): void;
|
|
52
|
+
/** Closed at once, rather than once the close is answered. */
|
|
53
|
+
protected drop(code: number, reason: string): void;
|
|
37
54
|
protected finish(code: number, reason: string): void;
|
|
38
55
|
}
|
|
39
56
|
//# sourceMappingURL=relay-socket.d.ts.map
|
|
@@ -9,13 +9,16 @@ const CONNECTING = 0;
|
|
|
9
9
|
const OPEN = 1;
|
|
10
10
|
const CLOSING = 2;
|
|
11
11
|
const CLOSED = 3;
|
|
12
|
+
/** How long the host has to answer the hello: the relay may still take a host that is gone for there. */
|
|
13
|
+
const HELLO_MS = 15000;
|
|
12
14
|
/**
|
|
13
15
|
* A WebSocket to ta serve through a relay, for a paired device: it says hello, checks that the host has
|
|
14
16
|
* the device's key, and then seals and opens every message. It behaves as much like a WebSocket as
|
|
15
17
|
* TaConnection needs: `readyState`, `send`, `close` and the four handlers.
|
|
16
18
|
*/
|
|
17
19
|
class RelaySocket {
|
|
18
|
-
constructor(pairing) {
|
|
20
|
+
constructor(pairing, options = {}) {
|
|
21
|
+
var _a;
|
|
19
22
|
this.pairing = pairing;
|
|
20
23
|
this.onopen = null;
|
|
21
24
|
this.onmessage = null;
|
|
@@ -23,14 +26,30 @@ class RelaySocket {
|
|
|
23
26
|
this.onerror = null;
|
|
24
27
|
this.state = CONNECTING;
|
|
25
28
|
this.nonce = relay_bytes_1.RelayBytes.random(16);
|
|
29
|
+
/** Whether the relay answers pings: one of before does not, and is not given up for that. */
|
|
30
|
+
this.ponged = false;
|
|
31
|
+
this.awaitingPong = false;
|
|
32
|
+
/** Messages are opened one after another, in order, and a close waits for them: a host may send several at once, then close. */
|
|
33
|
+
this.receiving = Promise.resolve();
|
|
26
34
|
this.socket = new WebSocket(relay_pairing_1.RelayPairingUtil.websocketUrl(pairing.relay, relay_protocol_1.RELAY_CONNECT_PATH + pairing.host));
|
|
35
|
+
this.helloTimer = setTimeout(() => this.drop(relay_protocol_1.RelayCloseCode.TIMEOUT, 'The host did not answer.'), (_a = options.helloMs) !== null && _a !== void 0 ? _a : HELLO_MS);
|
|
27
36
|
this.socket.onopen = () => {
|
|
37
|
+
var _a;
|
|
28
38
|
const hello = { t: 'hello', v: 1, device: pairing.device, nonce: relay_bytes_1.RelayBytes.toBase64(this.nonce) };
|
|
29
39
|
this.socket.send(JSON.stringify(hello));
|
|
40
|
+
this.pingTimer = setInterval(() => this.ping(), (_a = options.pingMs) !== null && _a !== void 0 ? _a : relay_protocol_1.RELAY_PING_MS);
|
|
41
|
+
};
|
|
42
|
+
this.socket.onmessage = message => {
|
|
43
|
+
const text = String(message.data);
|
|
44
|
+
if (text === relay_protocol_1.RELAY_PONG) {
|
|
45
|
+
this.ponged = true;
|
|
46
|
+
this.awaitingPong = false;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
this.receiving = this.receiving.then(() => this.receive(text));
|
|
30
50
|
};
|
|
31
|
-
this.socket.onmessage = message => void this.receive(String(message.data));
|
|
32
51
|
this.socket.onerror = event => { var _a; return (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, event); };
|
|
33
|
-
this.socket.onclose = event => this.finish(event.code, event.reason);
|
|
52
|
+
this.socket.onclose = event => void this.receiving.then(() => this.finish(event.code, event.reason));
|
|
34
53
|
}
|
|
35
54
|
get readyState() {
|
|
36
55
|
return this.state;
|
|
@@ -74,19 +93,38 @@ class RelaySocket {
|
|
|
74
93
|
throw new Error('The host did not show the key.');
|
|
75
94
|
}
|
|
76
95
|
this.cipher = cipher;
|
|
96
|
+
clearTimeout(this.helloTimer);
|
|
77
97
|
this.state = OPEN;
|
|
78
98
|
(_b = this.onopen) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
79
99
|
}
|
|
80
100
|
catch (_d) {
|
|
81
101
|
// A host without the key is not this device's host, whoever answered.
|
|
82
|
-
this.
|
|
102
|
+
this.drop(RelaySocket.REFUSED, 'The host refused this device.');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** The relay stopped answering: the connection is gone without a word, and closing it would wait for one. */
|
|
106
|
+
ping() {
|
|
107
|
+
if (this.awaitingPong && this.ponged) {
|
|
108
|
+
this.drop(relay_protocol_1.RelayCloseCode.TIMEOUT, 'The relay did not answer.');
|
|
109
|
+
return;
|
|
83
110
|
}
|
|
111
|
+
this.awaitingPong = true;
|
|
112
|
+
if (this.socket.readyState === OPEN) {
|
|
113
|
+
this.socket.send(relay_protocol_1.RELAY_PING);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Closed at once, rather than once the close is answered. */
|
|
117
|
+
drop(code, reason) {
|
|
118
|
+
this.close(code, reason);
|
|
119
|
+
this.finish(code, reason);
|
|
84
120
|
}
|
|
85
121
|
finish(code, reason) {
|
|
86
122
|
var _a;
|
|
87
123
|
if (this.state === CLOSED) {
|
|
88
124
|
return;
|
|
89
125
|
}
|
|
126
|
+
clearTimeout(this.helloTimer);
|
|
127
|
+
clearInterval(this.pingTimer);
|
|
90
128
|
this.state = CLOSED;
|
|
91
129
|
this.code = code;
|
|
92
130
|
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this, { code, reason });
|
package/lib/node/relay-host.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { RelayCipher } from '../common/relay-cipher';
|
|
2
|
-
import { type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol';
|
|
3
|
-
import type { RelayStore } from './relay-store';
|
|
2
|
+
import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol';
|
|
3
|
+
import type { RelayShare, RelayStore } from './relay-store';
|
|
4
4
|
export interface RelayHostOptions {
|
|
5
5
|
store: RelayStore;
|
|
6
6
|
/** The WebSocket of this ta serve, with its token: every device gets a connection to it of its own. */
|
|
7
7
|
localUrl: string;
|
|
8
8
|
/** First wait before reconnecting to the relay; it doubles up to 30 seconds. */
|
|
9
9
|
reconnectDelayMs?: number;
|
|
10
|
+
/** Between the pings of the heartbeat. Default RELAY_PING_MS. */
|
|
11
|
+
pingMs?: number;
|
|
12
|
+
/** The bytes of a package handed on (a share of the store), for one who has its link; without it, shares are refused. */
|
|
13
|
+
sharePackage?(share: RelayShare): Promise<Uint8Array>;
|
|
10
14
|
}
|
|
11
15
|
/** A device on a channel: the handshake, then its connection to ta serve. */
|
|
12
16
|
declare class RelayChannel {
|
|
@@ -22,6 +26,8 @@ declare class RelayChannel {
|
|
|
22
26
|
/** From the relay: the device is gone. */
|
|
23
27
|
closed(): void;
|
|
24
28
|
protected hello(text: string): Promise<void>;
|
|
29
|
+
/** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
|
|
30
|
+
protected sendShare(cipher: RelayCipher, share: RelayShare): Promise<void>;
|
|
25
31
|
protected connectLocal(cipher: RelayCipher): void;
|
|
26
32
|
protected answer(welcome: RelayWelcome): void;
|
|
27
33
|
protected close(): void;
|
|
@@ -39,6 +45,7 @@ export declare class RelayHost {
|
|
|
39
45
|
protected stopped: boolean;
|
|
40
46
|
protected attempts: number;
|
|
41
47
|
protected timer?: ReturnType<typeof setTimeout>;
|
|
48
|
+
protected pingTimer?: ReturnType<typeof setInterval>;
|
|
42
49
|
protected readonly channels: Map<string, RelayChannel>;
|
|
43
50
|
constructor(options: RelayHostOptions);
|
|
44
51
|
/** Whether the connection to the relay is open. */
|
|
@@ -49,10 +56,18 @@ export declare class RelayHost {
|
|
|
49
56
|
refresh(): void;
|
|
50
57
|
stop(): void;
|
|
51
58
|
/** @internal */
|
|
52
|
-
send(message: RelayHostMessage): void;
|
|
59
|
+
send(message: RelayHostMessage | typeof RELAY_PING): void;
|
|
53
60
|
/** @internal */
|
|
54
61
|
closeChannel(id: string): void;
|
|
55
62
|
protected connect(target: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* The heartbeat of the connection to the relay. Without it a connection gone without a word (through
|
|
65
|
+
* a proxy, a network that changed, a computer that slept) would look open for good, its devices told
|
|
66
|
+
* the host is offline until ta serve started again. It is made again when the relay stops answering,
|
|
67
|
+
* or when the timer was late by much (the computer slept). A relay of before, which never answers, is
|
|
68
|
+
* not given up for that.
|
|
69
|
+
*/
|
|
70
|
+
protected beat(socket: WebSocket): void;
|
|
56
71
|
protected receive(text: string): void;
|
|
57
72
|
protected scheduleReconnect(): void;
|
|
58
73
|
protected disconnect(): void;
|
package/lib/node/relay-host.js
CHANGED
|
@@ -44,26 +44,54 @@ class RelayChannel {
|
|
|
44
44
|
(_a = this.local) === null || _a === void 0 ? void 0 : _a.close();
|
|
45
45
|
}
|
|
46
46
|
async hello(text) {
|
|
47
|
+
var _a;
|
|
47
48
|
let hello;
|
|
48
49
|
try {
|
|
49
50
|
hello = JSON.parse(text);
|
|
50
51
|
}
|
|
51
|
-
catch (
|
|
52
|
+
catch (_b) {
|
|
52
53
|
this.close();
|
|
53
54
|
return;
|
|
54
55
|
}
|
|
55
|
-
const
|
|
56
|
-
|
|
56
|
+
const valid = hello.t === 'hello' && hello.v === 1 && typeof hello.nonce === 'string';
|
|
57
|
+
const device = valid ? this.options.store.device(hello.device) : undefined;
|
|
58
|
+
const share = valid && !device && this.options.sharePackage ? this.options.store.share(hello.device) : undefined;
|
|
59
|
+
const key = (_a = device === null || device === void 0 ? void 0 : device.key) !== null && _a !== void 0 ? _a : share === null || share === void 0 ? void 0 : share.key;
|
|
60
|
+
if (!key) {
|
|
57
61
|
this.answer({ t: 'refused', reason: 'This device is not paired.' });
|
|
58
62
|
this.close();
|
|
59
63
|
return;
|
|
60
64
|
}
|
|
61
65
|
clearTimeout(this.timer);
|
|
62
66
|
const nonce = relay_bytes_1.RelayBytes.random(16);
|
|
63
|
-
const cipher = await relay_cipher_1.RelayCipher.derive(relay_bytes_1.RelayBytes.fromBase64(
|
|
67
|
+
const cipher = await relay_cipher_1.RelayCipher.derive(relay_bytes_1.RelayBytes.fromBase64(key), relay_bytes_1.RelayBytes.fromBase64(hello.nonce), nonce, 'host');
|
|
64
68
|
this.cipher = cipher;
|
|
65
69
|
this.answer({ t: 'ready', nonce: relay_bytes_1.RelayBytes.toBase64(nonce), proof: await cipher.seal(relay_protocol_1.RELAY_PROOF) });
|
|
66
|
-
|
|
70
|
+
if (share) {
|
|
71
|
+
await this.sendShare(cipher, share);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
this.connectLocal(cipher);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
|
|
78
|
+
async sendShare(cipher, share) {
|
|
79
|
+
const send = async (message) => this.host.send({ t: 'data', ch: this.id, d: await cipher.seal(JSON.stringify(message)) });
|
|
80
|
+
try {
|
|
81
|
+
const data = await this.options.sharePackage(share);
|
|
82
|
+
if (data.length > relay_protocol_1.RELAY_SHARE_MAX_BYTES) {
|
|
83
|
+
throw new Error(`${share.name} is larger than ${relay_protocol_1.RELAY_SHARE_MAX_BYTES / 1024 / 1024} MB, too large to hand on by a link.`);
|
|
84
|
+
}
|
|
85
|
+
await send({ t: 'package', name: share.name, size: data.length });
|
|
86
|
+
for (let at = 0; at < data.length; at += relay_protocol_1.RELAY_SHARE_CHUNK_BYTES) {
|
|
87
|
+
await send({ t: 'chunk', d: relay_bytes_1.RelayBytes.toBase64(data.subarray(at, at + relay_protocol_1.RELAY_SHARE_CHUNK_BYTES)) });
|
|
88
|
+
}
|
|
89
|
+
await send({ t: 'end' });
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
await send({ t: 'error', message: error instanceof Error ? error.message : String(error) });
|
|
93
|
+
}
|
|
94
|
+
this.close();
|
|
67
95
|
}
|
|
68
96
|
connectLocal(cipher) {
|
|
69
97
|
const local = new WebSocket(this.options.localUrl);
|
|
@@ -111,7 +139,8 @@ class RelayHost {
|
|
|
111
139
|
/** relay.json may have changed: connect if it now has devices, or leave if it has none. */
|
|
112
140
|
refresh() {
|
|
113
141
|
const { url, host, devices } = this.options.store.read();
|
|
114
|
-
const
|
|
142
|
+
const shared = !!this.options.sharePackage && this.options.store.shares().length > 0;
|
|
143
|
+
const wanted = !this.stopped && url !== undefined && host !== undefined && (devices.length > 0 || shared);
|
|
115
144
|
if (!wanted) {
|
|
116
145
|
this.disconnect();
|
|
117
146
|
return;
|
|
@@ -131,7 +160,7 @@ class RelayHost {
|
|
|
131
160
|
send(message) {
|
|
132
161
|
var _a;
|
|
133
162
|
if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
134
|
-
this.socket.send(JSON.stringify(message));
|
|
163
|
+
this.socket.send(typeof message === 'string' ? message : JSON.stringify(message));
|
|
135
164
|
}
|
|
136
165
|
}
|
|
137
166
|
/** @internal */
|
|
@@ -149,6 +178,7 @@ class RelayHost {
|
|
|
149
178
|
this.target = target;
|
|
150
179
|
socket.onopen = () => {
|
|
151
180
|
this.attempts = 0;
|
|
181
|
+
this.beat(socket);
|
|
152
182
|
};
|
|
153
183
|
socket.onmessage = message => this.receive(String(message.data));
|
|
154
184
|
socket.onclose = event => {
|
|
@@ -156,6 +186,7 @@ class RelayHost {
|
|
|
156
186
|
return;
|
|
157
187
|
}
|
|
158
188
|
this.socket = undefined;
|
|
189
|
+
clearInterval(this.pingTimer);
|
|
159
190
|
this.closeChannels();
|
|
160
191
|
// Another host has this id: trying again would not change that.
|
|
161
192
|
if (event.code !== relay_protocol_1.RelayCloseCode.REFUSED && event.code !== relay_protocol_1.RelayCloseCode.REPLACED) {
|
|
@@ -165,8 +196,50 @@ class RelayHost {
|
|
|
165
196
|
// A failed connection also closes it.
|
|
166
197
|
socket.onerror = () => undefined;
|
|
167
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* The heartbeat of the connection to the relay. Without it a connection gone without a word (through
|
|
201
|
+
* a proxy, a network that changed, a computer that slept) would look open for good, its devices told
|
|
202
|
+
* the host is offline until ta serve started again. It is made again when the relay stops answering,
|
|
203
|
+
* or when the timer was late by much (the computer slept). A relay of before, which never answers, is
|
|
204
|
+
* not given up for that.
|
|
205
|
+
*/
|
|
206
|
+
beat(socket) {
|
|
207
|
+
var _a, _b;
|
|
208
|
+
var _c;
|
|
209
|
+
const every = (_c = this.options.pingMs) !== null && _c !== void 0 ? _c : relay_protocol_1.RELAY_PING_MS;
|
|
210
|
+
let last = Date.now();
|
|
211
|
+
let ponged = false;
|
|
212
|
+
let awaiting = false;
|
|
213
|
+
const pongs = (message) => {
|
|
214
|
+
if (message.data === relay_protocol_1.RELAY_PONG) {
|
|
215
|
+
ponged = true;
|
|
216
|
+
awaiting = false;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
socket.addEventListener('message', pongs);
|
|
220
|
+
clearInterval(this.pingTimer);
|
|
221
|
+
this.pingTimer = setInterval(() => {
|
|
222
|
+
const now = Date.now();
|
|
223
|
+
const slept = now - last > every * 2;
|
|
224
|
+
last = now;
|
|
225
|
+
if (this.socket !== socket) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (slept || (awaiting && ponged)) {
|
|
229
|
+
this.disconnect();
|
|
230
|
+
this.refresh();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
awaiting = true;
|
|
234
|
+
this.send(relay_protocol_1.RELAY_PING);
|
|
235
|
+
}, every);
|
|
236
|
+
(_b = (_a = this.pingTimer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
237
|
+
}
|
|
168
238
|
receive(text) {
|
|
169
239
|
var _a, _b;
|
|
240
|
+
if (text === relay_protocol_1.RELAY_PONG) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
170
243
|
let message;
|
|
171
244
|
try {
|
|
172
245
|
message = JSON.parse(text);
|
|
@@ -196,6 +269,7 @@ class RelayHost {
|
|
|
196
269
|
}
|
|
197
270
|
disconnect() {
|
|
198
271
|
clearTimeout(this.timer);
|
|
272
|
+
clearInterval(this.pingTimer);
|
|
199
273
|
const socket = this.socket;
|
|
200
274
|
this.socket = undefined;
|
|
201
275
|
this.closeChannels();
|
package/lib/node/relay-server.js
CHANGED
|
@@ -20,9 +20,10 @@ class RelayServer {
|
|
|
20
20
|
listen() {
|
|
21
21
|
const server = (0, node_http_1.createServer)((request, response) => {
|
|
22
22
|
var _a;
|
|
23
|
-
const
|
|
23
|
+
const path = ((_a = request.url) !== null && _a !== void 0 ? _a : '/').split(/[?#]/)[0];
|
|
24
|
+
const page = path === relay_protocol_1.RELAY_PAIR_PATH ? relay_protocol_1.RELAY_PAIR_PAGE : path === relay_protocol_1.RELAY_SHARE_PATH ? relay_protocol_1.RELAY_SHARE_PAGE : undefined;
|
|
24
25
|
response.writeHead(200, { 'content-type': page ? 'text/html; charset=utf-8' : 'text/plain' });
|
|
25
|
-
response.end(page ?
|
|
26
|
+
response.end(page !== null && page !== void 0 ? page : 'Ta relay\n');
|
|
26
27
|
});
|
|
27
28
|
server.on('upgrade', (request, socket, head) => this.upgrade(request, socket, head));
|
|
28
29
|
this.server = server;
|
|
@@ -72,14 +73,17 @@ class RelayServer {
|
|
|
72
73
|
/** The first host to come with an id keeps it, for as long as the relay runs. */
|
|
73
74
|
openHost(id, secret, connection) {
|
|
74
75
|
var _a;
|
|
75
|
-
var _b;
|
|
76
76
|
const hash = (0, node_crypto_1.createHash)('sha256').update(secret).digest('base64url');
|
|
77
|
-
const room = (
|
|
77
|
+
const room = (_a = this.rooms.get(id)) !== null && _a !== void 0 ? _a : this.createRoom(id, hash);
|
|
78
78
|
if (secret.length < 32 || !RelayServer.same(room.secretHash, hash)) {
|
|
79
79
|
connection.close(relay_protocol_1.RelayCloseCode.REFUSED, 'Refused.');
|
|
80
80
|
return;
|
|
81
81
|
}
|
|
82
|
-
(
|
|
82
|
+
if (room.host) {
|
|
83
|
+
// The devices came over the connection before, whose channels went with it.
|
|
84
|
+
room.host.close(relay_protocol_1.RelayCloseCode.REPLACED, 'The host connected again.');
|
|
85
|
+
room.relay.hostClosed();
|
|
86
|
+
}
|
|
83
87
|
room.host = connection;
|
|
84
88
|
connection.onMessage(text => room.relay.fromHost(text));
|
|
85
89
|
connection.onClose(() => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { RelayPairing } from '../common/relay-pairing';
|
|
2
|
+
import type { RelayShareLink } from '../common/relay-share';
|
|
2
3
|
export interface RelayDevice {
|
|
3
4
|
id: string;
|
|
4
5
|
name: string;
|
|
@@ -6,6 +7,17 @@ export interface RelayDevice {
|
|
|
6
7
|
key: string;
|
|
7
8
|
createdAt: number;
|
|
8
9
|
}
|
|
10
|
+
/** A package handed on by a link: whoever has the link takes it from this ta serve while the share lasts. */
|
|
11
|
+
export interface RelayShare {
|
|
12
|
+
id: string;
|
|
13
|
+
/** base64url, 32 bytes. */
|
|
14
|
+
key: string;
|
|
15
|
+
/** The directory of the package. */
|
|
16
|
+
root: string;
|
|
17
|
+
name: string;
|
|
18
|
+
createdAt: number;
|
|
19
|
+
expiresAt: number;
|
|
20
|
+
}
|
|
9
21
|
/** relay.json in the agent directory. Without devices, ta serve does not connect to the relay. */
|
|
10
22
|
export interface RelayConfig {
|
|
11
23
|
/** The relay, without a trailing slash. */
|
|
@@ -16,6 +28,8 @@ export interface RelayConfig {
|
|
|
16
28
|
secret: string;
|
|
17
29
|
};
|
|
18
30
|
devices: RelayDevice[];
|
|
31
|
+
/** Packages handed on; those past their time are dropped when it is written. */
|
|
32
|
+
shares?: RelayShare[];
|
|
19
33
|
}
|
|
20
34
|
/** The devices paired with this ta serve. Read again on every use: `ta pair` changes it while ta serve runs. */
|
|
21
35
|
export declare class RelayStore {
|
|
@@ -25,12 +39,26 @@ export declare class RelayStore {
|
|
|
25
39
|
get file(): string;
|
|
26
40
|
read(): RelayConfig;
|
|
27
41
|
device(deviceId: string): RelayDevice | undefined;
|
|
42
|
+
/** A share still on, by its id. */
|
|
43
|
+
share(shareId: string, now?: number): RelayShare | undefined;
|
|
44
|
+
/** The shares still on. */
|
|
45
|
+
shares(now?: number): RelayShare[];
|
|
46
|
+
/**
|
|
47
|
+
* A link of the package in `root`: the one of before while it lasts, else a new one for RELAY_SHARE_DAYS,
|
|
48
|
+
* on the relay of the devices (the default one when there is none), which this ta serve then connects to.
|
|
49
|
+
*/
|
|
50
|
+
shareRoot(root: string, name: string, now?: number): {
|
|
51
|
+
link: RelayShareLink;
|
|
52
|
+
share: RelayShare;
|
|
53
|
+
};
|
|
54
|
+
/** Stops the shares of a package: their links no longer work. False when there were none. */
|
|
55
|
+
unshareRoot(root: string): boolean;
|
|
28
56
|
/** A new device for this relay; another relay than before unpairs the devices of the old one. */
|
|
29
57
|
pair(relay: string, deviceName: string): RelayPairing;
|
|
30
58
|
/** False when there was no such device. */
|
|
31
59
|
remove(deviceId: string): boolean;
|
|
32
60
|
/** What the relay keeps of the host's secret. */
|
|
33
61
|
static secretHash(secret: string): string;
|
|
34
|
-
|
|
62
|
+
write(config: RelayConfig): void;
|
|
35
63
|
}
|
|
36
64
|
//# sourceMappingURL=relay-store.d.ts.map
|