@celljs/ta-relay 4.9.1 → 4.11.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.en.md +29 -2
- package/README.md +29 -2
- package/esm/common/index.d.ts +10 -0
- package/esm/common/index.js +10 -0
- package/esm/common/relay-blobs.d.ts +54 -0
- package/esm/common/relay-blobs.js +153 -0
- package/esm/common/relay-card.d.ts +23 -0
- package/esm/common/relay-card.js +30 -0
- package/esm/common/relay-cipher.d.ts +15 -0
- package/esm/common/relay-cipher.js +62 -2
- package/esm/common/relay-content.d.ts +104 -0
- package/esm/common/relay-content.js +316 -0
- package/esm/common/relay-conversation.d.ts +59 -0
- package/esm/common/relay-conversation.js +9 -0
- package/esm/common/relay-files.d.ts +108 -0
- package/esm/common/relay-files.js +170 -0
- package/esm/common/relay-hub-util.d.ts +91 -0
- package/esm/common/relay-hub-util.js +164 -0
- package/esm/common/relay-hub.d.ts +125 -0
- package/esm/common/relay-hub.js +438 -0
- package/esm/common/relay-hubs.d.ts +271 -0
- package/esm/common/relay-hubs.js +844 -0
- package/esm/common/relay-identity.d.ts +46 -0
- package/esm/common/relay-identity.js +126 -0
- package/esm/common/relay-protocol.d.ts +217 -2
- package/esm/common/relay-protocol.js +53 -0
- package/esm/common/relay-room-chat.d.ts +80 -0
- package/esm/common/relay-room-chat.js +152 -0
- package/esm/common/relay-socket.d.ts +8 -0
- package/esm/common/relay-socket.js +33 -4
- package/esm/guest/guest-i18n.d.ts +47 -0
- package/esm/guest/guest-i18n.js +97 -0
- package/esm/guest/guest-style.d.ts +1 -0
- package/esm/guest/guest-style.js +78 -0
- package/esm/guest/guest.d.ts +1 -0
- package/esm/guest/guest.js +371 -0
- package/esm/node/index.d.ts +3 -0
- package/esm/node/index.js +3 -0
- package/esm/node/relay-files-host.d.ts +27 -0
- package/esm/node/relay-files-host.js +130 -0
- package/esm/node/relay-host.d.ts +35 -2
- package/esm/node/relay-host.js +150 -13
- package/esm/node/relay-hub-files.d.ts +12 -0
- package/esm/node/relay-hub-files.js +39 -0
- package/esm/node/relay-server.d.ts +46 -1
- package/esm/node/relay-server.js +227 -1
- package/esm/node/relay-store.d.ts +72 -0
- package/esm/node/relay-store.js +123 -2
- package/esm/node/relay-viewer.d.ts +7 -0
- package/esm/node/relay-viewer.js +33 -0
- package/esm/viewer/viewer-i18n.d.ts +56 -0
- package/esm/viewer/viewer-i18n.js +117 -0
- package/esm/viewer/viewer-markdown.d.ts +7 -0
- package/esm/viewer/viewer-markdown.js +198 -0
- package/esm/viewer/viewer-style.d.ts +1 -0
- package/esm/viewer/viewer-style.js +72 -0
- package/esm/viewer/viewer.d.ts +8 -0
- package/esm/viewer/viewer.js +378 -0
- package/lib/common/index.d.ts +10 -0
- package/lib/common/index.js +10 -0
- package/lib/common/relay-blobs.d.ts +55 -0
- package/lib/common/relay-blobs.js +157 -0
- package/lib/common/relay-card.d.ts +24 -0
- package/lib/common/relay-card.js +33 -0
- package/lib/common/relay-cipher.d.ts +15 -0
- package/lib/common/relay-cipher.js +63 -3
- package/lib/common/relay-content.d.ts +105 -0
- package/lib/common/relay-content.js +320 -0
- package/lib/common/relay-conversation.d.ts +60 -0
- package/lib/common/relay-conversation.js +12 -0
- package/lib/common/relay-files.d.ts +109 -0
- package/lib/common/relay-files.js +174 -0
- package/lib/common/relay-hub-util.d.ts +92 -0
- package/lib/common/relay-hub-util.js +167 -0
- package/lib/common/relay-hub.d.ts +126 -0
- package/lib/common/relay-hub.js +442 -0
- package/lib/common/relay-hubs.d.ts +272 -0
- package/lib/common/relay-hubs.js +848 -0
- package/lib/common/relay-identity.d.ts +47 -0
- package/lib/common/relay-identity.js +129 -0
- package/lib/common/relay-protocol.d.ts +217 -2
- package/lib/common/relay-protocol.js +54 -1
- package/lib/common/relay-room-chat.d.ts +81 -0
- package/lib/common/relay-room-chat.js +156 -0
- package/lib/common/relay-socket.d.ts +8 -0
- package/lib/common/relay-socket.js +32 -3
- package/lib/guest/guest-i18n.d.ts +48 -0
- package/lib/guest/guest-i18n.js +100 -0
- package/lib/guest/guest-style.d.ts +2 -0
- package/lib/guest/guest-style.js +81 -0
- package/lib/guest/guest.d.ts +2 -0
- package/lib/guest/guest.js +373 -0
- package/lib/node/index.d.ts +3 -0
- package/lib/node/index.js +3 -0
- package/lib/node/relay-files-host.d.ts +28 -0
- package/lib/node/relay-files-host.js +134 -0
- package/lib/node/relay-host.d.ts +35 -2
- package/lib/node/relay-host.js +149 -12
- package/lib/node/relay-hub-files.d.ts +13 -0
- package/lib/node/relay-hub-files.js +43 -0
- package/lib/node/relay-server.d.ts +46 -1
- package/lib/node/relay-server.js +226 -0
- package/lib/node/relay-store.d.ts +72 -0
- package/lib/node/relay-store.js +121 -0
- package/lib/node/relay-viewer.d.ts +8 -0
- package/lib/node/relay-viewer.js +32 -0
- package/lib/viewer/viewer-i18n.d.ts +57 -0
- package/lib/viewer/viewer-i18n.js +120 -0
- package/lib/viewer/viewer-markdown.d.ts +8 -0
- package/lib/viewer/viewer-markdown.js +201 -0
- package/lib/viewer/viewer-style.d.ts +2 -0
- package/lib/viewer/viewer-style.js +75 -0
- package/lib/viewer/viewer.d.ts +9 -0
- package/lib/viewer/viewer.js +381 -0
- package/package.json +5 -4
- package/viewer/frame.html +1 -0
- package/viewer/page.html +80 -0
- package/viewer/room.html +87 -0
package/lib/node/relay-host.js
CHANGED
|
@@ -3,34 +3,86 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RelayHost = void 0;
|
|
4
4
|
const relay_bytes_1 = require("../common/relay-bytes");
|
|
5
5
|
const relay_cipher_1 = require("../common/relay-cipher");
|
|
6
|
+
const relay_files_1 = require("../common/relay-files");
|
|
6
7
|
const relay_pairing_1 = require("../common/relay-pairing");
|
|
7
8
|
const relay_protocol_1 = require("../common/relay-protocol");
|
|
9
|
+
const relay_files_host_1 = require("./relay-files-host");
|
|
10
|
+
/** Wrong keys (passwords) for a share before it is refused a while, and for how long. */
|
|
11
|
+
const MAX_WRONG_KEYS = 10;
|
|
12
|
+
const WRONG_KEY_WINDOW_MS = 10 * 60 * 1000;
|
|
8
13
|
const MAX_RECONNECT_DELAY_MS = 30000;
|
|
9
14
|
const HELLO_TIMEOUT_MS = 10000;
|
|
10
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
|
|
17
|
+
* for files (RelayFiles, RelayFilesHost), the file it asks for. What a channel is, it says in its first
|
|
18
|
+
* sealed message; until then nothing of ta serve is opened for it.
|
|
19
|
+
*/
|
|
11
20
|
class RelayChannel {
|
|
12
21
|
constructor(id, host, options) {
|
|
13
22
|
this.id = id;
|
|
14
23
|
this.host = host;
|
|
15
24
|
this.options = options;
|
|
16
25
|
this.early = [];
|
|
26
|
+
/** Agreed in the hello of a viewer of content, set once it proved. */
|
|
27
|
+
this.compress = false;
|
|
17
28
|
this.timer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
|
|
18
29
|
}
|
|
19
30
|
async receive(text) {
|
|
20
|
-
var _a;
|
|
31
|
+
var _a, _b;
|
|
21
32
|
if (!this.cipher) {
|
|
22
33
|
await this.hello(text);
|
|
23
34
|
return;
|
|
24
35
|
}
|
|
36
|
+
if (this.content && this.candidates) {
|
|
37
|
+
// Whose key it is, the proof says: the relay and the others do not learn who looks.
|
|
38
|
+
const content = this.content;
|
|
39
|
+
for (const candidate of this.candidates) {
|
|
40
|
+
const opened = await candidate.open(text).catch(() => undefined);
|
|
41
|
+
if (opened !== undefined) {
|
|
42
|
+
this.cipher = candidate;
|
|
43
|
+
this.candidates = undefined;
|
|
44
|
+
await this.proven(candidate, content, opened);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
this.candidates = undefined;
|
|
49
|
+
this.wrongKey(content);
|
|
50
|
+
this.close();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
25
53
|
let data;
|
|
26
54
|
try {
|
|
27
55
|
data = await this.cipher.open(text);
|
|
28
56
|
}
|
|
29
|
-
catch (
|
|
57
|
+
catch (_c) {
|
|
58
|
+
if (this.content) {
|
|
59
|
+
this.wrongKey(this.content);
|
|
60
|
+
}
|
|
30
61
|
this.close();
|
|
31
62
|
return;
|
|
32
63
|
}
|
|
33
|
-
if (
|
|
64
|
+
if (this.content) {
|
|
65
|
+
await this.proven(this.cipher, this.content, data);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (this.mode === 'share' || this.mode === 'content') {
|
|
69
|
+
// A link hands on what it shares and nothing else: what its holder sends never opens ta serve's socket or files.
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (!this.mode) {
|
|
73
|
+
this.mode = relay_files_1.RelayFilesUtil.isHello(data) ? 'files' : 'rpc';
|
|
74
|
+
if (this.mode === 'files') {
|
|
75
|
+
this.files = new relay_files_host_1.RelayFilesHost({ localUrl: this.options.localUrl, send: frame => this.sendFrame(frame) });
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.connectLocal(this.cipher);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (this.mode === 'files') {
|
|
82
|
+
await ((_a = this.files) === null || _a === void 0 ? void 0 : _a.receive(data));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (((_b = this.local) === null || _b === void 0 ? void 0 : _b.readyState) === WebSocket.OPEN) {
|
|
34
86
|
this.local.send(data);
|
|
35
87
|
}
|
|
36
88
|
else {
|
|
@@ -39,24 +91,38 @@ class RelayChannel {
|
|
|
39
91
|
}
|
|
40
92
|
/** From the relay: the device is gone. */
|
|
41
93
|
closed() {
|
|
42
|
-
var _a;
|
|
94
|
+
var _a, _b;
|
|
43
95
|
clearTimeout(this.timer);
|
|
44
|
-
(
|
|
96
|
+
clearTimeout(this.proofTimer);
|
|
97
|
+
(_a = this.files) === null || _a === void 0 ? void 0 : _a.close();
|
|
98
|
+
(_b = this.local) === null || _b === void 0 ? void 0 : _b.close();
|
|
45
99
|
}
|
|
46
|
-
|
|
100
|
+
/** A frame of the files of the workspace, sealed as the rest. */
|
|
101
|
+
sendFrame(frame) {
|
|
47
102
|
var _a;
|
|
103
|
+
(_a = this.cipher) === null || _a === void 0 ? void 0 : _a.seal(JSON.stringify(frame)).then(sealed => this.host.send({ t: 'data', ch: this.id, d: sealed }));
|
|
104
|
+
}
|
|
105
|
+
async hello(text) {
|
|
106
|
+
var _a, _b, _c;
|
|
48
107
|
let hello;
|
|
49
108
|
try {
|
|
50
109
|
hello = JSON.parse(text);
|
|
51
110
|
}
|
|
52
|
-
catch (
|
|
111
|
+
catch (_d) {
|
|
53
112
|
this.close();
|
|
54
113
|
return;
|
|
55
114
|
}
|
|
56
115
|
const valid = hello.t === 'hello' && hello.v === 1 && typeof hello.nonce === 'string';
|
|
57
116
|
const device = valid ? this.options.store.device(hello.device) : undefined;
|
|
58
117
|
const share = valid && !device && this.options.sharePackage ? this.options.store.share(hello.device) : undefined;
|
|
59
|
-
const
|
|
118
|
+
const content = valid && !device && !share && this.options.shareContent ? this.options.store.content(hello.device) : undefined;
|
|
119
|
+
if (content && this.host.blocked(content.id)) {
|
|
120
|
+
this.answer({ t: 'refused', reason: 'busy' });
|
|
121
|
+
this.close();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const people = (content === null || content === void 0 ? void 0 : content.access) === 'people' ? ((_a = content.people) !== null && _a !== void 0 ? _a : []).map(person => person.key) : undefined;
|
|
125
|
+
const key = (_c = (_b = device === null || device === void 0 ? void 0 : device.key) !== null && _b !== void 0 ? _b : share === null || share === void 0 ? void 0 : share.key) !== null && _c !== void 0 ? _c : ((content === null || content === void 0 ? void 0 : content.access) === 'self' ? undefined : people ? people[0] : content === null || content === void 0 ? void 0 : content.key);
|
|
60
126
|
if (!key) {
|
|
61
127
|
this.answer({ t: 'refused', reason: 'This device is not paired.' });
|
|
62
128
|
this.close();
|
|
@@ -66,13 +132,70 @@ class RelayChannel {
|
|
|
66
132
|
const nonce = relay_bytes_1.RelayBytes.random(16);
|
|
67
133
|
const cipher = await relay_cipher_1.RelayCipher.derive(relay_bytes_1.RelayBytes.fromBase64(key), relay_bytes_1.RelayBytes.fromBase64(hello.nonce), nonce, 'host');
|
|
68
134
|
this.cipher = cipher;
|
|
69
|
-
|
|
135
|
+
if (people) {
|
|
136
|
+
this.candidates = [
|
|
137
|
+
cipher,
|
|
138
|
+
...(await Promise.all(people.slice(1).map(other => relay_cipher_1.RelayCipher.derive(relay_bytes_1.RelayBytes.fromBase64(other), relay_bytes_1.RelayBytes.fromBase64(hello.nonce), nonce, 'host')))),
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
if (share) {
|
|
142
|
+
this.mode = 'share';
|
|
143
|
+
}
|
|
144
|
+
const compress = hello.z === 1 && relay_cipher_1.RelayDeflate.supported();
|
|
145
|
+
if (content) {
|
|
146
|
+
// The viewer proves first: what this side seals would let one with the link but not the password try passwords offline.
|
|
147
|
+
this.content = content;
|
|
148
|
+
this.compress = compress;
|
|
149
|
+
// A viewer that never proves would hold one of the places of the devices.
|
|
150
|
+
this.proofTimer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
|
|
151
|
+
this.answer({ t: 'ready', nonce: relay_bytes_1.RelayBytes.toBase64(nonce), ...(compress ? { z: 1 } : {}) });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const proof = await cipher.seal(relay_protocol_1.RELAY_PROOF);
|
|
155
|
+
// The messages after the proof, both ways: the device sets it once it opened the proof.
|
|
156
|
+
cipher.compress = compress;
|
|
157
|
+
this.answer({ t: 'ready', nonce: relay_bytes_1.RelayBytes.toBase64(nonce), proof, ...(compress ? { z: 1 } : {}) });
|
|
70
158
|
if (share) {
|
|
71
159
|
await this.sendShare(cipher, share);
|
|
72
160
|
}
|
|
161
|
+
}
|
|
162
|
+
/** The first sealed message of a viewer of content: its proof, then the content; a wrong one is counted, and refused. */
|
|
163
|
+
async proven(cipher, content, data) {
|
|
164
|
+
this.content = undefined;
|
|
165
|
+
clearTimeout(this.proofTimer);
|
|
166
|
+
if (data !== relay_protocol_1.RELAY_PROOF) {
|
|
167
|
+
this.wrongKey(content);
|
|
168
|
+
this.close();
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
cipher.compress = this.compress;
|
|
172
|
+
this.mode = 'content';
|
|
173
|
+
const send = async (message) => this.host.send({ t: 'data', ch: this.id, d: await cipher.seal(JSON.stringify(message)) });
|
|
174
|
+
const bytes = this.options.store.content(content.id) ? this.options.store.contentData(content) : undefined;
|
|
175
|
+
if (!bytes) {
|
|
176
|
+
await send({ t: 'error', message: 'This link no longer works: the share was stopped or ran out.' });
|
|
177
|
+
}
|
|
73
178
|
else {
|
|
74
|
-
|
|
179
|
+
await send({
|
|
180
|
+
t: 'content',
|
|
181
|
+
kind: content.kind,
|
|
182
|
+
title: content.title,
|
|
183
|
+
size: bytes.length,
|
|
184
|
+
createdAt: content.createdAt,
|
|
185
|
+
...(content.type ? { type: content.type } : {}),
|
|
186
|
+
});
|
|
187
|
+
for (let at = 0; at < bytes.length; at += relay_protocol_1.RELAY_SHARE_CHUNK_BYTES) {
|
|
188
|
+
await send({ t: 'chunk', d: relay_bytes_1.RelayBytes.toBase64(bytes.subarray(at, at + relay_protocol_1.RELAY_SHARE_CHUNK_BYTES)) });
|
|
189
|
+
}
|
|
190
|
+
await send({ t: 'end' });
|
|
75
191
|
}
|
|
192
|
+
this.close();
|
|
193
|
+
}
|
|
194
|
+
/** The key did not open: told in the clear (the viewer cannot open what is sealed), and counted. */
|
|
195
|
+
wrongKey(content) {
|
|
196
|
+
this.content = undefined;
|
|
197
|
+
this.answer({ t: 'refused', reason: 'key' });
|
|
198
|
+
this.host.wrongKey(content.id);
|
|
76
199
|
}
|
|
77
200
|
/** 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
201
|
async sendShare(cipher, share) {
|
|
@@ -111,6 +234,7 @@ class RelayChannel {
|
|
|
111
234
|
}
|
|
112
235
|
close() {
|
|
113
236
|
clearTimeout(this.timer);
|
|
237
|
+
clearTimeout(this.proofTimer);
|
|
114
238
|
this.host.closeChannel(this.id);
|
|
115
239
|
}
|
|
116
240
|
}
|
|
@@ -125,6 +249,18 @@ class RelayHost {
|
|
|
125
249
|
this.stopped = false;
|
|
126
250
|
this.attempts = 0;
|
|
127
251
|
this.channels = new Map();
|
|
252
|
+
/** Wrong keys of each share of content, of late. */
|
|
253
|
+
this.wrong = new Map();
|
|
254
|
+
}
|
|
255
|
+
/** @internal Whether a share of content had too many wrong keys of late. */
|
|
256
|
+
blocked(contentId, now = Date.now()) {
|
|
257
|
+
const wrong = this.wrong.get(contentId);
|
|
258
|
+
return !!wrong && now - wrong.since < WRONG_KEY_WINDOW_MS && wrong.count >= MAX_WRONG_KEYS;
|
|
259
|
+
}
|
|
260
|
+
/** @internal */
|
|
261
|
+
wrongKey(contentId, now = Date.now()) {
|
|
262
|
+
const wrong = this.wrong.get(contentId);
|
|
263
|
+
this.wrong.set(contentId, wrong && now - wrong.since < WRONG_KEY_WINDOW_MS ? { count: wrong.count + 1, since: wrong.since } : { count: 1, since: now });
|
|
128
264
|
}
|
|
129
265
|
/** Whether the connection to the relay is open. */
|
|
130
266
|
get online() {
|
|
@@ -139,7 +275,8 @@ class RelayHost {
|
|
|
139
275
|
/** relay.json may have changed: connect if it now has devices, or leave if it has none. */
|
|
140
276
|
refresh() {
|
|
141
277
|
const { url, host, devices } = this.options.store.read();
|
|
142
|
-
const shared = !!this.options.sharePackage && this.options.store.shares().length > 0
|
|
278
|
+
const shared = (!!this.options.sharePackage && this.options.store.shares().length > 0) ||
|
|
279
|
+
(!!this.options.shareContent && this.options.store.contents().some(content => content.access !== 'self'));
|
|
143
280
|
const wanted = !this.stopped && url !== undefined && host !== undefined && (devices.length > 0 || shared);
|
|
144
281
|
if (!wanted) {
|
|
145
282
|
this.disconnect();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RelayHubsStore, RelayHubsStoreName } from '../common/relay-hubs';
|
|
2
|
+
/** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
|
|
3
|
+
export declare class RelayHubFiles implements RelayHubsStore {
|
|
4
|
+
readonly agentDir: string;
|
|
5
|
+
constructor(agentDir: string);
|
|
6
|
+
file(name: RelayHubsStoreName): string;
|
|
7
|
+
read<T>(name: RelayHubsStoreName): T | undefined;
|
|
8
|
+
/** Written whole, then renamed over the file, readable by the user alone: it holds keys. */
|
|
9
|
+
write(name: RelayHubsStoreName, value: unknown): void;
|
|
10
|
+
/** The name of a new identity: the user's on this system. */
|
|
11
|
+
static userName(): string;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=relay-hub-files.d.ts.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayHubFiles = void 0;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_os_1 = require("node:os");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
/** What the rooms of a Ta keep, as files of its agent directory readable by the user alone: `identity.json`, `rooms.json`, `friends.json`. */
|
|
8
|
+
class RelayHubFiles {
|
|
9
|
+
constructor(agentDir) {
|
|
10
|
+
this.agentDir = agentDir;
|
|
11
|
+
}
|
|
12
|
+
file(name) {
|
|
13
|
+
return (0, node_path_1.join)(this.agentDir, `${name}.json`);
|
|
14
|
+
}
|
|
15
|
+
read(name) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse((0, node_fs_1.readFileSync)(this.file(name), 'utf8'));
|
|
18
|
+
}
|
|
19
|
+
catch (_a) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Written whole, then renamed over the file, readable by the user alone: it holds keys. */
|
|
24
|
+
write(name, value) {
|
|
25
|
+
const file = this.file(name);
|
|
26
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(file), { recursive: true });
|
|
27
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
28
|
+
(0, node_fs_1.writeFileSync)(temporary, `${JSON.stringify(value, undefined, 2)}\n`, { mode: 0o600 });
|
|
29
|
+
(0, node_fs_1.chmodSync)(temporary, 0o600);
|
|
30
|
+
(0, node_fs_1.renameSync)(temporary, file);
|
|
31
|
+
}
|
|
32
|
+
/** The name of a new identity: the user's on this system. */
|
|
33
|
+
static userName() {
|
|
34
|
+
try {
|
|
35
|
+
return (0, node_os_1.userInfo)().username || 'Ta';
|
|
36
|
+
}
|
|
37
|
+
catch (_a) {
|
|
38
|
+
return 'Ta';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.RelayHubFiles = RelayHubFiles;
|
|
43
|
+
//# sourceMappingURL=relay-hub-files.js.map
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { type IncomingMessage, type Server } from 'node:http';
|
|
1
|
+
import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
2
2
|
import type { Duplex } from 'node:stream';
|
|
3
3
|
import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
|
|
4
|
+
import { type RelayBlobStorage, RelayBlobs } from '../common/relay-blobs';
|
|
5
|
+
import { RelayHub, type RelayHubStorage, type RelayHubWho } from '../common/relay-hub';
|
|
4
6
|
import { RelayRoom } from '../common/relay-room';
|
|
5
7
|
export interface RelayServerOptions {
|
|
6
8
|
host?: string;
|
|
@@ -12,6 +14,37 @@ interface Room {
|
|
|
12
14
|
clients: Map<string, WebSocketConnection>;
|
|
13
15
|
relay: RelayRoom;
|
|
14
16
|
}
|
|
17
|
+
/** A copy of content in memory, deleted when it runs out. */
|
|
18
|
+
declare class MemoryBlobStorage implements RelayBlobStorage {
|
|
19
|
+
protected readonly values: Map<string, unknown>;
|
|
20
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
21
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
22
|
+
put(key: string, value: unknown): Promise<void>;
|
|
23
|
+
deleteAll(): Promise<void>;
|
|
24
|
+
/** No copy is kept: nothing of it need stay in the relay's memory. */
|
|
25
|
+
get empty(): boolean;
|
|
26
|
+
setAlarm(at: number): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
/** A room in memory, as a Durable Object keeps it. */
|
|
29
|
+
declare class MemoryHubStorage implements RelayHubStorage {
|
|
30
|
+
protected readonly values: Map<string, unknown>;
|
|
31
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
32
|
+
put(key: string, value: unknown): Promise<void>;
|
|
33
|
+
delete(key: string): Promise<boolean>;
|
|
34
|
+
list<T>(options: {
|
|
35
|
+
prefix: string;
|
|
36
|
+
}): Promise<Map<string, T>>;
|
|
37
|
+
deleteAll(): Promise<void>;
|
|
38
|
+
/** No room was made, or it was closed. */
|
|
39
|
+
get empty(): boolean;
|
|
40
|
+
}
|
|
41
|
+
interface Hub {
|
|
42
|
+
hub: RelayHub;
|
|
43
|
+
storage: MemoryHubStorage;
|
|
44
|
+
sockets: Map<WebSocketConnection, {
|
|
45
|
+
who?: RelayHubWho;
|
|
46
|
+
}>;
|
|
47
|
+
}
|
|
15
48
|
/**
|
|
16
49
|
* `ta relay`: the relay on a machine of one's own, as the Cloudflare Worker is in the cloud. It forwards,
|
|
17
50
|
* and keeps the hash of each host's secret in memory, for as long as it runs.
|
|
@@ -19,14 +52,26 @@ interface Room {
|
|
|
19
52
|
export declare class RelayServer {
|
|
20
53
|
protected readonly options: RelayServerOptions;
|
|
21
54
|
protected readonly rooms: Map<string, Room>;
|
|
55
|
+
protected readonly blobs: Map<string, {
|
|
56
|
+
blobs: RelayBlobs;
|
|
57
|
+
storage: MemoryBlobStorage;
|
|
58
|
+
pending: number;
|
|
59
|
+
}>;
|
|
60
|
+
protected readonly hubs: Map<string, Hub>;
|
|
22
61
|
protected server?: Server;
|
|
23
62
|
constructor(options?: RelayServerOptions);
|
|
24
63
|
listen(): Promise<string>;
|
|
25
64
|
close(): Promise<void>;
|
|
65
|
+
/** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
|
|
66
|
+
protected blob(id: string, request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
67
|
+
/** The body of a request, or undefined past `max` bytes: the rest is read and dropped. */
|
|
68
|
+
protected static body(request: IncomingMessage, max: number): Promise<Buffer<ArrayBuffer> | undefined>;
|
|
26
69
|
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
27
70
|
/** The first host to come with an id keeps it, for as long as the relay runs. */
|
|
28
71
|
protected openHost(id: string, secret: string, connection: WebSocketConnection): void;
|
|
29
72
|
protected openClient(id: string, connection: WebSocketConnection): void;
|
|
73
|
+
/** A connection to the hub of a room (RelayHub), kept in memory for as long as the relay runs. */
|
|
74
|
+
protected openHub(id: string, connection: WebSocketConnection): void;
|
|
30
75
|
protected createRoom(id: string, secretHash: string): Room;
|
|
31
76
|
protected static same(a: string, b: string): boolean;
|
|
32
77
|
}
|
package/lib/node/relay-server.js
CHANGED
|
@@ -4,9 +4,71 @@ exports.RelayServer = void 0;
|
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const node_http_1 = require("node:http");
|
|
6
6
|
const websocket_connection_1 = require("@celljs/rpc/lib/node/websocket/websocket-connection");
|
|
7
|
+
const relay_blobs_1 = require("../common/relay-blobs");
|
|
8
|
+
const relay_hub_1 = require("../common/relay-hub");
|
|
7
9
|
const relay_protocol_1 = require("../common/relay-protocol");
|
|
8
10
|
const relay_room_1 = require("../common/relay-room");
|
|
11
|
+
const relay_viewer_1 = require("./relay-viewer");
|
|
9
12
|
const WEBSOCKET = { maxPayload: 32 * 1024 * 1024, heartbeat: 30000 };
|
|
13
|
+
/** A copy of content in memory, deleted when it runs out. */
|
|
14
|
+
class MemoryBlobStorage {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.values = new Map();
|
|
17
|
+
}
|
|
18
|
+
async get(key) {
|
|
19
|
+
return this.values.get(key);
|
|
20
|
+
}
|
|
21
|
+
async put(key, value) {
|
|
22
|
+
this.values.set(key, value);
|
|
23
|
+
}
|
|
24
|
+
async deleteAll() {
|
|
25
|
+
this.values.clear();
|
|
26
|
+
clearTimeout(this.timer);
|
|
27
|
+
}
|
|
28
|
+
/** No copy is kept: nothing of it need stay in the relay's memory. */
|
|
29
|
+
get empty() {
|
|
30
|
+
return this.values.size === 0;
|
|
31
|
+
}
|
|
32
|
+
async setAlarm(at) {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
clearTimeout(this.timer);
|
|
35
|
+
// Past the 24 days of a timer, it is found run out on the next read.
|
|
36
|
+
this.timer = setTimeout(() => this.values.clear(), Math.min(Math.max(at - Date.now(), 0), 2 ** 31 - 1));
|
|
37
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** A room in memory, as a Durable Object keeps it. */
|
|
41
|
+
class MemoryHubStorage {
|
|
42
|
+
constructor() {
|
|
43
|
+
this.values = new Map();
|
|
44
|
+
}
|
|
45
|
+
async get(key) {
|
|
46
|
+
const value = this.values.get(key);
|
|
47
|
+
return value === undefined ? undefined : structuredClone(value);
|
|
48
|
+
}
|
|
49
|
+
async put(key, value) {
|
|
50
|
+
this.values.set(key, structuredClone(value));
|
|
51
|
+
}
|
|
52
|
+
async delete(key) {
|
|
53
|
+
return this.values.delete(key);
|
|
54
|
+
}
|
|
55
|
+
async list(options) {
|
|
56
|
+
const found = new Map();
|
|
57
|
+
for (const [key, value] of [...this.values].sort(([a], [b]) => a.localeCompare(b))) {
|
|
58
|
+
if (key.startsWith(options.prefix)) {
|
|
59
|
+
found.set(key, structuredClone(value));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return found;
|
|
63
|
+
}
|
|
64
|
+
async deleteAll() {
|
|
65
|
+
this.values.clear();
|
|
66
|
+
}
|
|
67
|
+
/** No room was made, or it was closed. */
|
|
68
|
+
get empty() {
|
|
69
|
+
return this.values.size === 0;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
10
72
|
const peer = (connection) => ({ send: text => connection.send(text), close: (code, reason) => connection.close(code, reason) });
|
|
11
73
|
/**
|
|
12
74
|
* `ta relay`: the relay on a machine of one's own, as the Cloudflare Worker is in the cloud. It forwards,
|
|
@@ -16,11 +78,37 @@ class RelayServer {
|
|
|
16
78
|
constructor(options = {}) {
|
|
17
79
|
this.options = options;
|
|
18
80
|
this.rooms = new Map();
|
|
81
|
+
this.blobs = new Map();
|
|
82
|
+
this.hubs = new Map();
|
|
19
83
|
}
|
|
20
84
|
listen() {
|
|
21
85
|
const server = (0, node_http_1.createServer)((request, response) => {
|
|
22
86
|
var _a;
|
|
23
87
|
const path = ((_a = request.url) !== null && _a !== void 0 ? _a : '/').split(/[?#]/)[0];
|
|
88
|
+
if (path.startsWith(relay_protocol_1.RELAY_BLOB_PATH)) {
|
|
89
|
+
this.blob(path.slice(relay_protocol_1.RELAY_BLOB_PATH.length), request, response).catch(() => {
|
|
90
|
+
// A request aborted or that failed: an unhandled rejection would end the relay.
|
|
91
|
+
if (!response.headersSent) {
|
|
92
|
+
response.writeHead(400);
|
|
93
|
+
}
|
|
94
|
+
response.end();
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (path === relay_protocol_1.RELAY_ROOM_PATH) {
|
|
99
|
+
response.writeHead(200, { ...relay_protocol_1.RELAY_VIEWER_HEADERS, 'x-frame-options': 'DENY' });
|
|
100
|
+
response.end(relay_viewer_1.RelayViewer.room());
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (path === relay_protocol_1.RELAY_CONTENT_PATH || path === relay_protocol_1.RELAY_FRAME_PATH) {
|
|
104
|
+
const frame = path === relay_protocol_1.RELAY_FRAME_PATH;
|
|
105
|
+
response.writeHead(200, {
|
|
106
|
+
...relay_protocol_1.RELAY_VIEWER_HEADERS,
|
|
107
|
+
...(frame ? { 'content-security-policy': relay_protocol_1.RELAY_FRAME_HEADER_CSP } : { 'x-frame-options': 'DENY' }),
|
|
108
|
+
});
|
|
109
|
+
response.end(frame ? relay_viewer_1.RelayViewer.frame() : relay_viewer_1.RelayViewer.page());
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
24
112
|
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;
|
|
25
113
|
response.writeHead(200, { 'content-type': page ? 'text/html; charset=utf-8' : 'text/plain' });
|
|
26
114
|
response.end(page !== null && page !== void 0 ? page : 'Ta relay\n');
|
|
@@ -38,6 +126,12 @@ class RelayServer {
|
|
|
38
126
|
}
|
|
39
127
|
close() {
|
|
40
128
|
var _a;
|
|
129
|
+
for (const { sockets } of this.hubs.values()) {
|
|
130
|
+
for (const connection of sockets.keys()) {
|
|
131
|
+
connection.close(1001, 'The relay is stopping.');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
this.hubs.clear();
|
|
41
135
|
for (const room of this.rooms.values()) {
|
|
42
136
|
(_a = room.host) === null || _a === void 0 ? void 0 : _a.close(1001, 'The relay is stopping.');
|
|
43
137
|
for (const client of room.clients.values()) {
|
|
@@ -47,6 +141,70 @@ class RelayServer {
|
|
|
47
141
|
this.rooms.clear();
|
|
48
142
|
return new Promise(resolve => (this.server ? this.server.close(() => resolve()) : resolve()));
|
|
49
143
|
}
|
|
144
|
+
/** A copy of content (RelayBlobs), kept in memory for as long as the relay runs. */
|
|
145
|
+
async blob(id, request, response) {
|
|
146
|
+
var _a;
|
|
147
|
+
if (!relay_protocol_1.RELAY_ID.test(id)) {
|
|
148
|
+
response.writeHead(404).end();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const read = await RelayServer.body(request, relay_blobs_1.RelayBlobs.MAX_BYTES);
|
|
152
|
+
if (!read) {
|
|
153
|
+
// Refused before it is all in memory, whatever its content-length said.
|
|
154
|
+
response.writeHead(413, { connection: 'close' }).end('Too large.\n');
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
// Kept only while it holds a copy: requests for ids of no copy leave nothing behind.
|
|
158
|
+
let copy = this.blobs.get(id);
|
|
159
|
+
if (!copy) {
|
|
160
|
+
const storage = new MemoryBlobStorage();
|
|
161
|
+
copy = { blobs: new relay_blobs_1.RelayBlobs(storage), storage, pending: 0 };
|
|
162
|
+
this.blobs.set(id, copy);
|
|
163
|
+
}
|
|
164
|
+
const headers = new Headers();
|
|
165
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
166
|
+
if (typeof value === 'string') {
|
|
167
|
+
headers.set(name, value);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const method = (_a = request.method) !== null && _a !== void 0 ? _a : 'GET';
|
|
171
|
+
const body = read.length && method !== 'GET' && method !== 'HEAD' ? read : undefined;
|
|
172
|
+
copy.pending++;
|
|
173
|
+
let answer;
|
|
174
|
+
try {
|
|
175
|
+
answer = await copy.blobs.handle(new Request(`http://relay${request.url}`, { method, headers, body }));
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
copy.pending--;
|
|
179
|
+
if (copy.pending === 0 && copy.storage.empty && this.blobs.get(id) === copy) {
|
|
180
|
+
this.blobs.delete(id);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
response.writeHead(answer.status, Object.fromEntries(answer.headers));
|
|
184
|
+
response.end(Buffer.from(await answer.arrayBuffer()));
|
|
185
|
+
}
|
|
186
|
+
/** The body of a request, or undefined past `max` bytes: the rest is read and dropped. */
|
|
187
|
+
static body(request, max) {
|
|
188
|
+
return new Promise((resolve, reject) => {
|
|
189
|
+
const chunks = [];
|
|
190
|
+
let size = 0;
|
|
191
|
+
request.on('data', (chunk) => {
|
|
192
|
+
if (size < 0) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
size += chunk.length;
|
|
196
|
+
if (size > max) {
|
|
197
|
+
size = -1;
|
|
198
|
+
chunks.length = 0;
|
|
199
|
+
resolve(undefined);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
chunks.push(chunk);
|
|
203
|
+
});
|
|
204
|
+
request.on('end', () => size >= 0 && resolve(Buffer.concat(chunks)));
|
|
205
|
+
request.on('error', reject);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
50
208
|
upgrade(request, socket, head) {
|
|
51
209
|
var _a, _b;
|
|
52
210
|
const url = new URL((_a = request.url) !== null && _a !== void 0 ? _a : '/', 'http://relay');
|
|
@@ -55,6 +213,15 @@ class RelayServer {
|
|
|
55
213
|
websocket_connection_1.WebSocketConnection.refuse(socket, checked.status, checked.message);
|
|
56
214
|
return;
|
|
57
215
|
}
|
|
216
|
+
if (url.pathname.startsWith(relay_protocol_1.RELAY_HUB_PATH)) {
|
|
217
|
+
const id = url.pathname.slice(relay_protocol_1.RELAY_HUB_PATH.length);
|
|
218
|
+
if (!relay_protocol_1.RELAY_ID.test(id)) {
|
|
219
|
+
websocket_connection_1.WebSocketConnection.refuse(socket, 404, 'Not found.');
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
this.openHub(id, websocket_connection_1.WebSocketConnection.open(socket, checked.key, head, WEBSOCKET));
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
58
225
|
const [path, id] = url.pathname.startsWith(relay_protocol_1.RELAY_HOST_PATH)
|
|
59
226
|
? [relay_protocol_1.RELAY_HOST_PATH, url.pathname.slice(relay_protocol_1.RELAY_HOST_PATH.length)]
|
|
60
227
|
: [relay_protocol_1.RELAY_CONNECT_PATH, url.pathname.slice(relay_protocol_1.RELAY_CONNECT_PATH.length)];
|
|
@@ -110,6 +277,65 @@ class RelayServer {
|
|
|
110
277
|
}
|
|
111
278
|
});
|
|
112
279
|
}
|
|
280
|
+
/** A connection to the hub of a room (RelayHub), kept in memory for as long as the relay runs. */
|
|
281
|
+
openHub(id, connection) {
|
|
282
|
+
let hub = this.hubs.get(id);
|
|
283
|
+
if (!hub) {
|
|
284
|
+
const sockets = new Map();
|
|
285
|
+
const socketOf = (other) => {
|
|
286
|
+
var _a;
|
|
287
|
+
const state = (_a = sockets.get(other)) !== null && _a !== void 0 ? _a : {};
|
|
288
|
+
return {
|
|
289
|
+
key: other,
|
|
290
|
+
get who() {
|
|
291
|
+
return state.who;
|
|
292
|
+
},
|
|
293
|
+
set who(who) {
|
|
294
|
+
state.who = who;
|
|
295
|
+
},
|
|
296
|
+
send: text => other.send(text),
|
|
297
|
+
close: (code, reason) => other.close(code, reason),
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
const storage = new MemoryHubStorage();
|
|
301
|
+
hub = { sockets, storage, hub: new relay_hub_1.RelayHub(storage, () => [...sockets.keys()].map(socketOf)) };
|
|
302
|
+
this.hubs.set(id, hub);
|
|
303
|
+
}
|
|
304
|
+
const current = hub;
|
|
305
|
+
const { sockets, hub: relay } = current;
|
|
306
|
+
sockets.set(connection, {});
|
|
307
|
+
let chain = Promise.resolve();
|
|
308
|
+
const socket = {
|
|
309
|
+
key: connection,
|
|
310
|
+
get who() {
|
|
311
|
+
var _a;
|
|
312
|
+
return (_a = sockets.get(connection)) === null || _a === void 0 ? void 0 : _a.who;
|
|
313
|
+
},
|
|
314
|
+
set who(who) {
|
|
315
|
+
const state = sockets.get(connection);
|
|
316
|
+
if (state) {
|
|
317
|
+
state.who = who;
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
send: text => connection.send(text),
|
|
321
|
+
close: (code, reason) => connection.close(code, reason),
|
|
322
|
+
};
|
|
323
|
+
connection.onMessage(text => {
|
|
324
|
+
chain = chain.then(() => relay.receive(socket, text)).catch(() => undefined);
|
|
325
|
+
});
|
|
326
|
+
connection.onClose(() => {
|
|
327
|
+
void chain.then(() => {
|
|
328
|
+
if (sockets.has(connection)) {
|
|
329
|
+
relay.closed(socket);
|
|
330
|
+
sockets.delete(connection);
|
|
331
|
+
}
|
|
332
|
+
// Connections to ids of no room (refused, or the room closed) leave nothing behind.
|
|
333
|
+
if (sockets.size === 0 && current.storage.empty && this.hubs.get(id) === current) {
|
|
334
|
+
this.hubs.delete(id);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
}
|
|
113
339
|
createRoom(id, secretHash) {
|
|
114
340
|
const room = {
|
|
115
341
|
secretHash,
|