@celljs/ta-relay 4.19.0 → 4.21.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/esm/common/index.d.ts +1 -0
- package/esm/common/index.js +1 -0
- package/esm/common/relay-account-client.d.ts +4 -0
- package/esm/common/relay-account-client.js +13 -3
- package/esm/common/relay-account.d.ts +17 -2
- package/esm/common/relay-account.js +15 -6
- package/esm/common/relay-accounts.d.ts +2 -0
- package/esm/common/relay-accounts.js +16 -2
- package/esm/common/relay-group-invites.d.ts +6 -2
- package/esm/common/relay-group-invites.js +6 -5
- package/esm/common/relay-hub-charter.d.ts +74 -0
- package/esm/common/relay-hub-charter.js +149 -0
- package/esm/common/relay-hub-epochs.d.ts +43 -0
- package/esm/common/relay-hub-epochs.js +92 -0
- package/esm/common/relay-hub-util.d.ts +39 -7
- package/esm/common/relay-hub-util.js +46 -15
- package/esm/common/relay-hub.d.ts +13 -0
- package/esm/common/relay-hub.js +81 -18
- package/esm/common/relay-hubs-admins.d.ts +27 -0
- package/esm/common/relay-hubs-admins.js +91 -0
- package/esm/common/relay-hubs-connection.d.ts +21 -63
- package/esm/common/relay-hubs-connection.js +103 -87
- package/esm/common/relay-hubs-identity.d.ts +29 -1
- package/esm/common/relay-hubs-identity.js +109 -1
- package/esm/common/relay-hubs-live.d.ts +58 -0
- package/esm/common/relay-hubs-live.js +27 -0
- package/esm/common/relay-hubs-protocol.d.ts +10 -0
- package/esm/common/relay-hubs-util.d.ts +9 -0
- package/esm/common/relay-hubs-util.js +23 -1
- package/esm/common/relay-hubs.d.ts +12 -15
- package/esm/common/relay-hubs.js +60 -57
- package/esm/common/relay-identity.d.ts +7 -0
- package/esm/common/relay-identity.js +8 -0
- package/esm/common/relay-protocol.d.ts +21 -3
- package/esm/common/relay-protocol.js +8 -1
- package/esm/common/relay-room-chat.d.ts +6 -1
- package/esm/common/relay-room-chat.js +1 -0
- package/esm/node/relay-server-accounts.d.ts +1 -1
- package/esm/node/relay-server-accounts.js +1 -1
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/common/relay-account-client.d.ts +4 -0
- package/lib/common/relay-account-client.js +13 -3
- package/lib/common/relay-account.d.ts +17 -2
- package/lib/common/relay-account.js +15 -6
- package/lib/common/relay-accounts.d.ts +2 -0
- package/lib/common/relay-accounts.js +16 -2
- package/lib/common/relay-group-invites.d.ts +6 -2
- package/lib/common/relay-group-invites.js +6 -5
- package/lib/common/relay-hub-charter.d.ts +75 -0
- package/lib/common/relay-hub-charter.js +152 -0
- package/lib/common/relay-hub-epochs.d.ts +44 -0
- package/lib/common/relay-hub-epochs.js +95 -0
- package/lib/common/relay-hub-util.d.ts +39 -7
- package/lib/common/relay-hub-util.js +46 -15
- package/lib/common/relay-hub.d.ts +13 -0
- package/lib/common/relay-hub.js +81 -18
- package/lib/common/relay-hubs-admins.d.ts +28 -0
- package/lib/common/relay-hubs-admins.js +95 -0
- package/lib/common/relay-hubs-connection.d.ts +21 -63
- package/lib/common/relay-hubs-connection.js +109 -94
- package/lib/common/relay-hubs-identity.d.ts +29 -1
- package/lib/common/relay-hubs-identity.js +109 -1
- package/lib/common/relay-hubs-live.d.ts +59 -0
- package/lib/common/relay-hubs-live.js +31 -0
- package/lib/common/relay-hubs-protocol.d.ts +10 -0
- package/lib/common/relay-hubs-util.d.ts +9 -0
- package/lib/common/relay-hubs-util.js +23 -1
- package/lib/common/relay-hubs.d.ts +12 -15
- package/lib/common/relay-hubs.js +66 -63
- package/lib/common/relay-identity.d.ts +7 -0
- package/lib/common/relay-identity.js +8 -0
- package/lib/common/relay-protocol.d.ts +21 -3
- package/lib/common/relay-protocol.js +9 -2
- package/lib/common/relay-room-chat.d.ts +6 -1
- package/lib/common/relay-room-chat.js +1 -0
- package/lib/node/relay-server-accounts.d.ts +1 -1
- package/lib/node/relay-server-accounts.js +1 -1
- package/package.json +3 -3
- package/viewer/join.html +6 -6
- package/viewer/page.html +12 -12
- package/viewer/room.html +15 -15
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type RelayHubCharter } from './relay-hub-charter';
|
|
1
2
|
import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
|
|
2
3
|
/** What a link inviting into a room names (docs/design/ta-link.md 3.3); all of it in the fragment, which a browser does not send. */
|
|
3
4
|
export interface RelayHubLink {
|
|
@@ -5,7 +6,7 @@ export interface RelayHubLink {
|
|
|
5
6
|
room: string;
|
|
6
7
|
/** base64url, 32 bytes: what an ask proves and is sealed with. */
|
|
7
8
|
invite: string;
|
|
8
|
-
/** The identity id of the owner: the answer must be signed by it. */
|
|
9
|
+
/** The identity id of the owner: the answer must be signed by it, or by an admin it named (RelayHubCharterUtil). */
|
|
9
10
|
owner: string;
|
|
10
11
|
title: string;
|
|
11
12
|
/** The owner's name, as they gave it. */
|
|
@@ -28,6 +29,8 @@ export interface RelayHubMember extends RelayPublicIdentity {
|
|
|
28
29
|
export interface RelayHubRosterBody {
|
|
29
30
|
v: number;
|
|
30
31
|
members: RelayHubMember[];
|
|
32
|
+
/** Who the admins are and the rules, signed by the owner (docs/design/ta-link.md 8); none in a room of before. */
|
|
33
|
+
charter?: RelayHubCharter;
|
|
31
34
|
}
|
|
32
35
|
/** What one asking to join says of itself, sealed with the key of the invitation and signed. */
|
|
33
36
|
export interface RelayHubAskBody extends RelayPublicIdentity {
|
|
@@ -39,6 +42,8 @@ export interface RelayHubAskBody extends RelayPublicIdentity {
|
|
|
39
42
|
/** What the owner gives one it admits, sealed for it alone: the key of the room, and what the room is. */
|
|
40
43
|
export interface RelayHubAnswerBody {
|
|
41
44
|
key: string;
|
|
45
|
+
/** The keys of the epochs after the first, by number, those kept (docs/design/ta-link.md 8.7). */
|
|
46
|
+
epochs?: Record<string, string>;
|
|
42
47
|
kind: string;
|
|
43
48
|
title: string;
|
|
44
49
|
view?: string;
|
|
@@ -51,10 +56,16 @@ export interface RelayHubSigned<T> {
|
|
|
51
56
|
at: number;
|
|
52
57
|
b: T;
|
|
53
58
|
}
|
|
54
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* The keys of a room, made from its key `R` once: the names of its state always from `R`; what is sealed, from the key
|
|
61
|
+
* of the epoch it is in (docs/design/ta-link.md 8.7), the latest (`epoch`) for what is written now.
|
|
62
|
+
*/
|
|
55
63
|
export interface RelayHubKeys {
|
|
56
64
|
seal: CryptoKey;
|
|
57
65
|
names: Uint8Array;
|
|
66
|
+
epoch: number;
|
|
67
|
+
/** The seal key of each epoch this Ta knows, by its number; 0 is that of `R`. */
|
|
68
|
+
ring: Map<number, CryptoKey>;
|
|
58
69
|
}
|
|
59
70
|
/** Links, keys, sealing and signatures of rooms (docs/design/ta-link.md 3.2, 3.3). */
|
|
60
71
|
export declare namespace RelayHubUtil {
|
|
@@ -67,7 +78,24 @@ export declare namespace RelayHubUtil {
|
|
|
67
78
|
function parse(text: string): RelayHubLink;
|
|
68
79
|
function randomId(): string;
|
|
69
80
|
function randomKey(): string;
|
|
70
|
-
|
|
81
|
+
/** The keys of a room of key `roomKey` (epoch 0), and of the epochs after it this Ta has (RelayRoomRecord.epochs). */
|
|
82
|
+
function keys(roomKey: string, epochs?: Record<string, string>): Promise<RelayHubKeys>;
|
|
83
|
+
/**
|
|
84
|
+
* Sealed with the key of the latest epoch: as {@link seal} in epoch 0, which a Ta of before reads; `<epoch>.<sealed>`
|
|
85
|
+
* after (base64url has no dot).
|
|
86
|
+
*/
|
|
87
|
+
function sealIn<T>(keys: RelayHubKeys, identity: RelayIdentity, member: string, body: T, at?: number): Promise<string>;
|
|
88
|
+
/** The key a text of {@link sealIn} was sealed with, and the text without its epoch; none for an epoch this Ta lacks. */
|
|
89
|
+
function keyFor(keys: RelayHubKeys, sealed: string): {
|
|
90
|
+
key: CryptoKey;
|
|
91
|
+
sealed: string;
|
|
92
|
+
epoch: number;
|
|
93
|
+
} | undefined;
|
|
94
|
+
/** What {@link sealIn} sealed, checked as {@link open}. */
|
|
95
|
+
function openIn<T>(keys: RelayHubKeys, sealed: string, signOf: (member: string) => string | undefined, options?: {
|
|
96
|
+
now?: number;
|
|
97
|
+
skewMs?: number;
|
|
98
|
+
}): Promise<RelayHubSigned<T> | undefined>;
|
|
71
99
|
/** What the relay knows a key of the state by: HMAC of its name, 22 characters. */
|
|
72
100
|
function keyId(keys: RelayHubKeys, name: string): Promise<string>;
|
|
73
101
|
/** What proves an ask comes from one with the invitation; the relay keeps its {@link RelayKeys.hash}. */
|
|
@@ -88,11 +116,15 @@ export declare namespace RelayHubUtil {
|
|
|
88
116
|
/** Undefined for an ask that does not open with this invitation, or is not signed by the keys it gives. */
|
|
89
117
|
function openAsk(invite: string, sealed: string): Promise<RelayHubAskBody | undefined>;
|
|
90
118
|
/**
|
|
91
|
-
* The
|
|
92
|
-
* the owner; all of it sealed with the invitation the ask came with,
|
|
119
|
+
* The answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by the
|
|
120
|
+
* owner, or by an admin with `proof` that the owner named it; all of it sealed with the invitation the ask came with,
|
|
121
|
+
* so that the relay does not see who answered.
|
|
93
122
|
*/
|
|
94
|
-
function sealAnswer(owner: RelayIdentity, room: string, ask: RelayHubAskBody, body: RelayHubAnswerBody, invite: string
|
|
95
|
-
|
|
123
|
+
function sealAnswer(owner: RelayIdentity, room: string, ask: RelayHubAskBody, body: RelayHubAnswerBody, invite: string, proof?: {
|
|
124
|
+
head: RelayPublicIdentity;
|
|
125
|
+
charter: RelayHubCharter;
|
|
126
|
+
}): Promise<string>;
|
|
127
|
+
/** Undefined unless it is signed by the owner the link names (or an admin it named), and opens for this identity; `owner` is the owner. */
|
|
96
128
|
function openAnswer(identity: RelayIdentity, room: string, member: string, ownerId: string, answer: string, invite: string): Promise<{
|
|
97
129
|
owner: RelayPublicIdentity;
|
|
98
130
|
body: RelayHubAnswerBody;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHubUtil = void 0;
|
|
4
4
|
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
5
6
|
const relay_identity_1 = require("./relay-identity");
|
|
6
7
|
const relay_protocol_1 = require("./relay-protocol");
|
|
7
8
|
/** Links, keys, sealing and signatures of rooms (docs/design/ta-link.md 3.2, 3.3). */
|
|
@@ -65,15 +66,45 @@ var RelayHubUtil;
|
|
|
65
66
|
return relay_bytes_1.RelayBytes.toBase64(relay_bytes_1.RelayBytes.random(32));
|
|
66
67
|
}
|
|
67
68
|
RelayHubUtil.randomKey = randomKey;
|
|
68
|
-
|
|
69
|
+
/** The keys of a room of key `roomKey` (epoch 0), and of the epochs after it this Ta has (RelayRoomRecord.epochs). */
|
|
70
|
+
async function keys(roomKey, epochs = {}) {
|
|
69
71
|
const key = relay_bytes_1.RelayBytes.fromBase64(roomKey);
|
|
70
72
|
const none = new Uint8Array(0);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
const sealOf = async (value) => relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayKeys.hkdf(value, none, 'ta-room/1 seal'));
|
|
74
|
+
const ring = new Map([[0, await sealOf(key)]]);
|
|
75
|
+
for (const [epoch, value] of Object.entries(epochs)) {
|
|
76
|
+
const n = Number(epoch);
|
|
77
|
+
if (Number.isInteger(n) && n > 0) {
|
|
78
|
+
ring.set(n, await sealOf(relay_bytes_1.RelayBytes.fromBase64(value)));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const epoch = Math.max(...ring.keys());
|
|
82
|
+
return { seal: ring.get(epoch), names: await relay_identity_1.RelayKeys.hkdf(key, none, 'ta-room/1 key'), epoch, ring };
|
|
75
83
|
}
|
|
76
84
|
RelayHubUtil.keys = keys;
|
|
85
|
+
/**
|
|
86
|
+
* Sealed with the key of the latest epoch: as {@link seal} in epoch 0, which a Ta of before reads; `<epoch>.<sealed>`
|
|
87
|
+
* after (base64url has no dot).
|
|
88
|
+
*/
|
|
89
|
+
async function sealIn(keys, identity, member, body, at = Date.now()) {
|
|
90
|
+
const sealed = await seal(keys.seal, identity, member, body, at);
|
|
91
|
+
return keys.epoch ? `${keys.epoch}.${sealed}` : sealed;
|
|
92
|
+
}
|
|
93
|
+
RelayHubUtil.sealIn = sealIn;
|
|
94
|
+
/** The key a text of {@link sealIn} was sealed with, and the text without its epoch; none for an epoch this Ta lacks. */
|
|
95
|
+
function keyFor(keys, sealed) {
|
|
96
|
+
const dot = sealed.indexOf('.');
|
|
97
|
+
const epoch = dot > 0 ? Number(sealed.slice(0, dot)) : 0;
|
|
98
|
+
const key = Number.isInteger(epoch) ? keys.ring.get(epoch) : undefined;
|
|
99
|
+
return key && { key, sealed: dot > 0 ? sealed.slice(dot + 1) : sealed, epoch };
|
|
100
|
+
}
|
|
101
|
+
RelayHubUtil.keyFor = keyFor;
|
|
102
|
+
/** What {@link sealIn} sealed, checked as {@link open}. */
|
|
103
|
+
async function openIn(keys, sealed, signOf, options = {}) {
|
|
104
|
+
const found = keyFor(keys, String(sealed));
|
|
105
|
+
return found ? open(found.key, found.sealed, signOf, options) : undefined;
|
|
106
|
+
}
|
|
107
|
+
RelayHubUtil.openIn = openIn;
|
|
77
108
|
/** What the relay knows a key of the state by: HMAC of its name, 22 characters. */
|
|
78
109
|
async function keyId(keys, name) {
|
|
79
110
|
return (await relay_identity_1.RelayKeys.hmac(keys.names, name)).slice(0, 22);
|
|
@@ -150,30 +181,30 @@ var RelayHubUtil;
|
|
|
150
181
|
}
|
|
151
182
|
RelayHubUtil.openAsk = openAsk;
|
|
152
183
|
/**
|
|
153
|
-
* The
|
|
154
|
-
* the owner; all of it sealed with the invitation the ask came with,
|
|
184
|
+
* The answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by the
|
|
185
|
+
* owner, or by an admin with `proof` that the owner named it; all of it sealed with the invitation the ask came with,
|
|
186
|
+
* so that the relay does not see who answered.
|
|
155
187
|
*/
|
|
156
|
-
async function sealAnswer(owner, room, ask, body, invite) {
|
|
188
|
+
async function sealAnswer(owner, room, ask, body, invite, proof) {
|
|
157
189
|
const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(owner, ask.box, room, 'ta-room/1 answer'));
|
|
158
190
|
const d = await relay_identity_1.RelayKeys.seal(key, JSON.stringify(body));
|
|
159
191
|
const signed = `${room}\n${ask.member}\n${d}`;
|
|
160
|
-
const answer = JSON.stringify({ owner: relay_identity_1.RelayIdentityUtil.publicOf(owner), d, s: await relay_identity_1.RelayIdentityUtil.sign(owner, signed) });
|
|
192
|
+
const answer = JSON.stringify({ owner: relay_identity_1.RelayIdentityUtil.publicOf(owner), d, s: await relay_identity_1.RelayIdentityUtil.sign(owner, signed), ...(proof !== null && proof !== void 0 ? proof : {}) });
|
|
161
193
|
return relay_identity_1.RelayKeys.seal(await inviteKey(invite), answer);
|
|
162
194
|
}
|
|
163
195
|
RelayHubUtil.sealAnswer = sealAnswer;
|
|
164
|
-
/** Undefined unless it is signed by the owner the link names, and opens for this identity. */
|
|
196
|
+
/** Undefined unless it is signed by the owner the link names (or an admin it named), and opens for this identity; `owner` is the owner. */
|
|
165
197
|
async function openAnswer(identity, room, member, ownerId, answer, invite) {
|
|
166
198
|
try {
|
|
167
|
-
const { owner, d, s } = JSON.parse(await relay_identity_1.RelayKeys.open(await inviteKey(invite), answer));
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
!(await relay_identity_1.RelayIdentityUtil.verify(owner.sign, `${room}\n${member}\n${d}`, s))) {
|
|
199
|
+
const { owner, d, s, head, charter } = JSON.parse(await relay_identity_1.RelayKeys.open(await inviteKey(invite), answer));
|
|
200
|
+
const named = await relay_hub_charter_1.RelayHubCharterUtil.answeredBy(owner, ownerId, room, { head, charter });
|
|
201
|
+
if (!named || !(await relay_identity_1.RelayIdentityUtil.verify(owner.sign, `${room}\n${member}\n${d}`, s))) {
|
|
171
202
|
return undefined;
|
|
172
203
|
}
|
|
173
204
|
const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, owner.box, room, 'ta-room/1 answer'));
|
|
174
205
|
const body = JSON.parse(await relay_identity_1.RelayKeys.open(key, d));
|
|
175
206
|
relay_bytes_1.RelayBytes.fromBase64(body.key);
|
|
176
|
-
return { owner: { ...relay_identity_1.RelayIdentityUtil.publicOf(
|
|
207
|
+
return { owner: { ...relay_identity_1.RelayIdentityUtil.publicOf(named), name: String(named.name).slice(0, 100) }, body };
|
|
177
208
|
}
|
|
178
209
|
catch (_a) {
|
|
179
210
|
return undefined;
|
|
@@ -47,6 +47,13 @@ interface HubMeta {
|
|
|
47
47
|
hash: string;
|
|
48
48
|
expiresAt: number;
|
|
49
49
|
};
|
|
50
|
+
/** The members the owner made admins (docs/design/ta-link.md 8): they let in, take out and invite, as the owner. */
|
|
51
|
+
admins?: string[];
|
|
52
|
+
/** The invitation of each admin, by its member id: one of each, beside the owner's. */
|
|
53
|
+
invites?: Record<string, {
|
|
54
|
+
hash: string;
|
|
55
|
+
expiresAt: number;
|
|
56
|
+
}>;
|
|
50
57
|
keys: number;
|
|
51
58
|
size: number;
|
|
52
59
|
}
|
|
@@ -103,6 +110,12 @@ export declare class RelayHub {
|
|
|
103
110
|
t: 'admit';
|
|
104
111
|
}>): Promise<void>;
|
|
105
112
|
protected setRoster(socket: RelayHubSocket, roster: RelayHubRoster): Promise<void>;
|
|
113
|
+
/** The keys of a roster sealed for each member (RelayHubRoster.k): undefined for none, null for what is not that. */
|
|
114
|
+
protected static epochKeys(k: unknown): Record<string, string> | undefined | null;
|
|
115
|
+
/** Whether a member lets in, takes out and invites: the owner, or an admin it made. */
|
|
116
|
+
protected static manages(meta: HubMeta, member: string): boolean;
|
|
117
|
+
/** One gone is no admin, its invitation with it. */
|
|
118
|
+
protected unmake(meta: HubMeta, member: string): Promise<void>;
|
|
106
119
|
protected closeRoom(): Promise<void>;
|
|
107
120
|
/** The asks still waiting for the owner, those past their time dropped. */
|
|
108
121
|
protected asks(): Promise<HubAsks>;
|
package/lib/common/relay-hub.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RelayHub = exports.RelayHubCloseCode = void 0;
|
|
4
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
4
5
|
const relay_identity_1 = require("./relay-identity");
|
|
5
6
|
const relay_protocol_1 = require("./relay-protocol");
|
|
6
7
|
/** Close codes of a hub. */
|
|
@@ -129,8 +130,7 @@ class RelayHub {
|
|
|
129
130
|
const wasOnline = this.online(member, socket);
|
|
130
131
|
socket.who = { member };
|
|
131
132
|
await this.touch(meta);
|
|
132
|
-
const
|
|
133
|
-
const asks = owner ? await this.pendingAsks() : undefined;
|
|
133
|
+
const asks = RelayHub.manages(meta, member) ? await this.pendingAsks() : undefined;
|
|
134
134
|
const roster = await this.storage.get('roster');
|
|
135
135
|
const online = [...new Set(this.members().map(other => other.who.member))];
|
|
136
136
|
this.send(socket, { t: 'welcome', owner: meta.owner, online, ...(roster ? { roster } : {}), ...(asks ? { asks } : {}) });
|
|
@@ -154,7 +154,7 @@ class RelayHub {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
async ask(socket, request) {
|
|
157
|
-
var _a;
|
|
157
|
+
var _a, _b, _c;
|
|
158
158
|
const meta = await this.storage.get('meta');
|
|
159
159
|
if (!meta) {
|
|
160
160
|
this.refuse(socket, 'gone', 'No such room.', RelayHubCloseCode.GONE);
|
|
@@ -165,12 +165,13 @@ class RelayHub {
|
|
|
165
165
|
return;
|
|
166
166
|
}
|
|
167
167
|
const now = this.now();
|
|
168
|
-
|
|
168
|
+
const proof = await relay_identity_1.RelayKeys.hash(String(request.proof));
|
|
169
|
+
if (![meta.invite, ...Object.values((_a = meta.invites) !== null && _a !== void 0 ? _a : {})].some(invite => invite && invite.expiresAt > now && invite.hash === proof)) {
|
|
169
170
|
this.refuse(socket, 'invite', 'This invitation no longer works.', RelayHubCloseCode.REFUSED);
|
|
170
171
|
return;
|
|
171
172
|
}
|
|
172
173
|
const asks = await this.asks();
|
|
173
|
-
const members = (
|
|
174
|
+
const members = (_b = (await this.storage.get('members'))) !== null && _b !== void 0 ? _b : {};
|
|
174
175
|
if (asks[request.ask] || members[request.ask]) {
|
|
175
176
|
this.refuse(socket, 'exists', 'Asked already.', RelayHubCloseCode.REFUSED);
|
|
176
177
|
return;
|
|
@@ -183,8 +184,10 @@ class RelayHub {
|
|
|
183
184
|
await this.storage.put('asks', asks);
|
|
184
185
|
socket.who = { ask: request.ask };
|
|
185
186
|
this.send(socket, { t: 'waiting' });
|
|
186
|
-
for (const
|
|
187
|
-
|
|
187
|
+
for (const manager of [meta.owner, ...((_c = meta.admins) !== null && _c !== void 0 ? _c : [])]) {
|
|
188
|
+
for (const socket of this.members(manager)) {
|
|
189
|
+
this.send(socket, { t: 'asked', ask: { ask: request.ask, d: request.d, at: now } });
|
|
190
|
+
}
|
|
188
191
|
}
|
|
189
192
|
}
|
|
190
193
|
async wait(socket, ask, token) {
|
|
@@ -219,9 +222,10 @@ class RelayHub {
|
|
|
219
222
|
socket.close(RelayHubCloseCode.ANSWERED, 'Answered.');
|
|
220
223
|
}
|
|
221
224
|
async fromMember(socket, member, request) {
|
|
222
|
-
var _a;
|
|
225
|
+
var _a, _b;
|
|
226
|
+
var _c, _d;
|
|
223
227
|
const meta = await this.storage.get('meta');
|
|
224
|
-
const members = (
|
|
228
|
+
const members = (_c = (await this.storage.get('members'))) !== null && _c !== void 0 ? _c : {};
|
|
225
229
|
if (!meta || !members[member]) {
|
|
226
230
|
this.refuse(socket, 'gone', 'Not in this room any more.', RelayHubCloseCode.REMOVED);
|
|
227
231
|
return;
|
|
@@ -238,25 +242,44 @@ class RelayHub {
|
|
|
238
242
|
}
|
|
239
243
|
delete members[member];
|
|
240
244
|
await this.storage.put('members', members);
|
|
245
|
+
await this.unmake(meta, member);
|
|
241
246
|
for (const other of this.members(member)) {
|
|
242
247
|
other.close(RelayHubCloseCode.REMOVED, 'Left the room.');
|
|
243
248
|
}
|
|
244
249
|
this.broadcast({ t: 'left', member });
|
|
245
250
|
return;
|
|
246
251
|
}
|
|
247
|
-
if (!owner) {
|
|
252
|
+
if (!RelayHub.manages(meta, member) || ((request.t === 'admins' || request.t === 'close') && !owner)) {
|
|
248
253
|
this.send(socket, { t: 'error', code: 'owner', message: 'Only the owner does that.' });
|
|
249
254
|
return;
|
|
250
255
|
}
|
|
251
256
|
switch (request.t) {
|
|
252
|
-
case '
|
|
257
|
+
case 'admins': {
|
|
258
|
+
const admins = Array.isArray(request.admins) ? request.admins : [];
|
|
259
|
+
meta.admins = [...new Set(admins.map(String))].filter(id => id !== meta.owner && !!members[id]).slice(0, relay_hub_charter_1.RELAY_HUB_MAX_ADMINS);
|
|
260
|
+
for (const id of Object.keys((_d = meta.invites) !== null && _d !== void 0 ? _d : {})) {
|
|
261
|
+
if (!meta.admins.includes(id)) {
|
|
262
|
+
(_a = meta.invites) === null || _a === void 0 ? true : delete _a[id];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
await this.storage.put('meta', meta);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
case 'invite': {
|
|
253
269
|
if (!/^[A-Za-z0-9_-]{43}$/.test(String(request.hash)) || !(request.expiresAt > this.now())) {
|
|
254
270
|
this.send(socket, { t: 'error', code: 'invalid', message: 'An invitation needs a hash and a time.' });
|
|
255
271
|
return;
|
|
256
272
|
}
|
|
257
|
-
|
|
273
|
+
const invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + relay_protocol_1.RELAY_INVITE_MAX_DAYS * DAY_MS) };
|
|
274
|
+
if (owner) {
|
|
275
|
+
meta.invite = invite;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
meta.invites = { ...meta.invites, [member]: invite };
|
|
279
|
+
}
|
|
258
280
|
await this.storage.put('meta', meta);
|
|
259
281
|
return;
|
|
282
|
+
}
|
|
260
283
|
case 'admit':
|
|
261
284
|
return this.admit(socket, members, request);
|
|
262
285
|
case 'refuse': {
|
|
@@ -277,8 +300,13 @@ class RelayHub {
|
|
|
277
300
|
if (request.member === member || !members[request.member]) {
|
|
278
301
|
return;
|
|
279
302
|
}
|
|
303
|
+
if (request.member === meta.owner || (!owner && ((_b = meta.admins) === null || _b === void 0 ? void 0 : _b.includes(request.member)))) {
|
|
304
|
+
this.send(socket, { t: 'error', code: 'owner', message: 'Only the owner does that.' });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
280
307
|
delete members[request.member];
|
|
281
308
|
await this.storage.put('members', members);
|
|
309
|
+
await this.unmake(meta, request.member);
|
|
282
310
|
for (const other of this.members(request.member)) {
|
|
283
311
|
this.refuse(other, 'auth', 'Removed by the owner.', RelayHubCloseCode.REMOVED);
|
|
284
312
|
}
|
|
@@ -301,7 +329,7 @@ class RelayHub {
|
|
|
301
329
|
}
|
|
302
330
|
}
|
|
303
331
|
async set(socket, member, meta, request) {
|
|
304
|
-
var _a, _b;
|
|
332
|
+
var _a, _b, _c;
|
|
305
333
|
const id = Number(request.id);
|
|
306
334
|
const done = (ok, v, error) => this.send(socket, { t: 'done', id, ok, v, ...(error ? { error } : {}) });
|
|
307
335
|
if (typeof request.k !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(request.k) || typeof request.d !== 'string') {
|
|
@@ -315,17 +343,23 @@ class RelayHub {
|
|
|
315
343
|
const key = STATE + request.k;
|
|
316
344
|
const before = await this.storage.get(key);
|
|
317
345
|
const version = (_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0;
|
|
346
|
+
// What a member keeps as its own (its lines, its posts) no other member replaces or clears: the owner alone may.
|
|
347
|
+
if ((before === null || before === void 0 ? void 0 : before.own) && before.by !== member && member !== meta.owner && ((_b = (await this.storage.get('members'))) !== null && _b !== void 0 ? _b : {})[before.by]) {
|
|
348
|
+
done(false, version, 'owned');
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
318
351
|
if (request.v !== undefined && request.v !== version) {
|
|
319
352
|
done(false, version);
|
|
320
353
|
return;
|
|
321
354
|
}
|
|
322
355
|
const keys = meta.keys + (before ? 0 : 1);
|
|
323
|
-
const size = meta.size - ((
|
|
356
|
+
const size = meta.size - ((_c = before === null || before === void 0 ? void 0 : before.d.length) !== null && _c !== void 0 ? _c : 0) + request.d.length;
|
|
324
357
|
if (keys > relay_protocol_1.RELAY_HUB_MAX_KEYS || size > relay_protocol_1.RELAY_HUB_MAX_STATE) {
|
|
325
358
|
done(false, version, 'limit');
|
|
326
359
|
return;
|
|
327
360
|
}
|
|
328
|
-
const
|
|
361
|
+
const own = request.own === true || ((before === null || before === void 0 ? void 0 : before.own) === true && before.by === member);
|
|
362
|
+
const entry = { k: request.k, v: version + 1, d: request.d, by: member, ...(own ? { own: true } : {}) };
|
|
329
363
|
await this.storage.put(key, entry);
|
|
330
364
|
meta.keys = keys;
|
|
331
365
|
meta.size = size;
|
|
@@ -357,12 +391,41 @@ class RelayHub {
|
|
|
357
391
|
async setRoster(socket, roster) {
|
|
358
392
|
var _a;
|
|
359
393
|
const before = await this.storage.get('roster');
|
|
360
|
-
|
|
394
|
+
const k = RelayHub.epochKeys(roster === null || roster === void 0 ? void 0 : roster.k);
|
|
395
|
+
if (!roster || typeof roster.d !== 'string' || roster.d.length > relay_protocol_1.RELAY_HUB_MAX_VALUE || k === null || !(roster.v > ((_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0))) {
|
|
361
396
|
this.send(socket, { t: 'error', code: 'invalid', message: 'A roster needs a newer version.' });
|
|
362
397
|
return;
|
|
363
398
|
}
|
|
364
|
-
|
|
365
|
-
this.
|
|
399
|
+
const kept = { v: roster.v, d: roster.d, ...(k ? { k } : {}) };
|
|
400
|
+
await this.storage.put('roster', kept);
|
|
401
|
+
this.broadcast({ t: 'roster', roster: kept });
|
|
402
|
+
}
|
|
403
|
+
/** The keys of a roster sealed for each member (RelayHubRoster.k): undefined for none, null for what is not that. */
|
|
404
|
+
static epochKeys(k) {
|
|
405
|
+
if (k === undefined) {
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
if (!k || typeof k !== 'object' || Array.isArray(k)) {
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
const entries = Object.entries(k);
|
|
412
|
+
const ok = entries.length <= relay_protocol_1.RELAY_HUB_MAX_MEMBERS &&
|
|
413
|
+
entries.every(([member, value]) => RelayHub.id(member) && typeof value === 'string' && value.length < 4096);
|
|
414
|
+
return ok ? Object.fromEntries(entries) : null;
|
|
415
|
+
}
|
|
416
|
+
/** Whether a member lets in, takes out and invites: the owner, or an admin it made. */
|
|
417
|
+
static manages(meta, member) {
|
|
418
|
+
var _a;
|
|
419
|
+
return member === meta.owner || !!((_a = meta.admins) === null || _a === void 0 ? void 0 : _a.includes(member));
|
|
420
|
+
}
|
|
421
|
+
/** One gone is no admin, its invitation with it. */
|
|
422
|
+
async unmake(meta, member) {
|
|
423
|
+
var _a, _b, _c, _d;
|
|
424
|
+
if (((_a = meta.admins) === null || _a === void 0 ? void 0 : _a.includes(member)) || ((_b = meta.invites) === null || _b === void 0 ? void 0 : _b[member])) {
|
|
425
|
+
meta.admins = (_c = meta.admins) === null || _c === void 0 ? void 0 : _c.filter(id => id !== member);
|
|
426
|
+
(_d = meta.invites) === null || _d === void 0 ? true : delete _d[member];
|
|
427
|
+
await this.storage.put('meta', meta);
|
|
428
|
+
}
|
|
366
429
|
}
|
|
367
430
|
async closeRoom() {
|
|
368
431
|
await this.storage.deleteAll();
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type RelayHubRules } from './relay-hub-charter';
|
|
2
|
+
import { RelayHubsIdentity } from './relay-hubs-identity';
|
|
3
|
+
import type { Live } from './relay-hubs-live';
|
|
4
|
+
import type { RelayRoomInfo } from './relay-hubs-protocol';
|
|
5
|
+
/**
|
|
6
|
+
* The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
|
|
7
|
+
* signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class RelayHubsAdmins extends RelayHubsIdentity {
|
|
10
|
+
/**
|
|
11
|
+
* The owner's choices for a room (docs/design/ta-link.md 8): how many members it lets in at most
|
|
12
|
+
* (RELAY_ROOM_MAX_MEMBERS by default, 2 to RELAY_ROOM_CAPACITY_MAX; those in already stay), its admins (member ids,
|
|
13
|
+
* Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
|
|
14
|
+
*/
|
|
15
|
+
configure(roomId: string, options: {
|
|
16
|
+
members?: number;
|
|
17
|
+
admins?: string[];
|
|
18
|
+
rules?: RelayHubRules;
|
|
19
|
+
}): Promise<RelayRoomInfo>;
|
|
20
|
+
/** The members it lets in at most: the owner's own choice, else the one its charter says. */
|
|
21
|
+
protected capacityOf(live: Live): number;
|
|
22
|
+
protected managed(roomId: string): Live;
|
|
23
|
+
protected owned(roomId: string): Live;
|
|
24
|
+
protected member(roomId: string): Live;
|
|
25
|
+
protected isFriends(live: Live): boolean;
|
|
26
|
+
protected abstract info(live: Live): RelayRoomInfo;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=relay-hubs-admins.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayHubsAdmins = void 0;
|
|
4
|
+
const relay_friends_1 = require("./relay-friends");
|
|
5
|
+
const relay_hub_charter_1 = require("./relay-hub-charter");
|
|
6
|
+
const relay_hubs_identity_1 = require("./relay-hubs-identity");
|
|
7
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
8
|
+
/**
|
|
9
|
+
* The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
|
|
10
|
+
* signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
|
|
11
|
+
*/
|
|
12
|
+
class RelayHubsAdmins extends relay_hubs_identity_1.RelayHubsIdentity {
|
|
13
|
+
/**
|
|
14
|
+
* The owner's choices for a room (docs/design/ta-link.md 8): how many members it lets in at most
|
|
15
|
+
* (RELAY_ROOM_MAX_MEMBERS by default, 2 to RELAY_ROOM_CAPACITY_MAX; those in already stay), its admins (member ids,
|
|
16
|
+
* Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
|
|
17
|
+
*/
|
|
18
|
+
async configure(roomId, options) {
|
|
19
|
+
var _a;
|
|
20
|
+
var _b, _c;
|
|
21
|
+
const live = this.owned(roomId);
|
|
22
|
+
let capacity = live.record.capacity;
|
|
23
|
+
if (options.members !== undefined) {
|
|
24
|
+
const members = Math.round(Number(options.members));
|
|
25
|
+
if (!(members >= 2 && members <= relay_protocol_1.RELAY_ROOM_CAPACITY_MAX)) {
|
|
26
|
+
throw new Error(`A room takes 2 to ${relay_protocol_1.RELAY_ROOM_CAPACITY_MAX} members.`);
|
|
27
|
+
}
|
|
28
|
+
capacity = members === relay_protocol_1.RELAY_ROOM_MAX_MEMBERS ? undefined : members;
|
|
29
|
+
live.record.capacity = capacity;
|
|
30
|
+
if (capacity === undefined) {
|
|
31
|
+
delete live.record.capacity;
|
|
32
|
+
}
|
|
33
|
+
this.save();
|
|
34
|
+
}
|
|
35
|
+
if (options.admins === undefined && options.rules === undefined && (options.members === undefined || !live.charter)) {
|
|
36
|
+
return this.info(live);
|
|
37
|
+
}
|
|
38
|
+
const before = (_b = live.charter) !== null && _b !== void 0 ? _b : { room: live.record.id, v: 0, admins: [], rules: {} };
|
|
39
|
+
const roster = (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : [];
|
|
40
|
+
const admins = options.admins === undefined
|
|
41
|
+
? before.admins
|
|
42
|
+
: [...new Set(options.admins.map(String))]
|
|
43
|
+
.map(id => roster.find(member => member.member === id && !member.owner && !member.guest))
|
|
44
|
+
.filter(member => !!member)
|
|
45
|
+
.slice(0, relay_hub_charter_1.RELAY_HUB_MAX_ADMINS)
|
|
46
|
+
.map(member => ({ member: member.member, id: member.id }));
|
|
47
|
+
const charter = {
|
|
48
|
+
room: live.record.id,
|
|
49
|
+
v: before.v,
|
|
50
|
+
admins,
|
|
51
|
+
rules: options.rules === undefined ? before.rules : relay_hub_charter_1.RelayHubCharterUtil.rules(options.rules),
|
|
52
|
+
...(capacity ? { capacity } : {}),
|
|
53
|
+
};
|
|
54
|
+
await this.ready(live);
|
|
55
|
+
await this.writeRoster(live, same => same, charter);
|
|
56
|
+
this.request(live, { t: 'admins', admins: admins.map(admin => admin.member) });
|
|
57
|
+
this.emit({ type: 'rooms' });
|
|
58
|
+
return this.info(live);
|
|
59
|
+
}
|
|
60
|
+
/** The members it lets in at most: the owner's own choice, else the one its charter says. */
|
|
61
|
+
capacityOf(live) {
|
|
62
|
+
var _a;
|
|
63
|
+
var _b;
|
|
64
|
+
if (this.isFriends(live)) {
|
|
65
|
+
return relay_protocol_1.RELAY_FRIENDS_MAX + 1;
|
|
66
|
+
}
|
|
67
|
+
return (_b = (live.record.own ? live.record.capacity : (_a = live.charter) === null || _a === void 0 ? void 0 : _a.capacity)) !== null && _b !== void 0 ? _b : relay_protocol_1.RELAY_ROOM_MAX_MEMBERS;
|
|
68
|
+
}
|
|
69
|
+
managed(roomId) {
|
|
70
|
+
const live = this.member(roomId);
|
|
71
|
+
if (!this.manages(live)) {
|
|
72
|
+
throw new Error('Only the owner or an admin of the room does that.');
|
|
73
|
+
}
|
|
74
|
+
return live;
|
|
75
|
+
}
|
|
76
|
+
owned(roomId) {
|
|
77
|
+
const live = this.member(roomId);
|
|
78
|
+
if (!live.record.own) {
|
|
79
|
+
throw new Error('Only the owner of the room does that.');
|
|
80
|
+
}
|
|
81
|
+
return live;
|
|
82
|
+
}
|
|
83
|
+
member(roomId) {
|
|
84
|
+
const live = this.live.get(roomId);
|
|
85
|
+
if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
|
|
86
|
+
throw new Error('Not in that room.');
|
|
87
|
+
}
|
|
88
|
+
return live;
|
|
89
|
+
}
|
|
90
|
+
isFriends(live) {
|
|
91
|
+
return live.record.own && live.record.kind === relay_friends_1.RELAY_FRIENDS_KIND;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.RelayHubsAdmins = RelayHubsAdmins;
|
|
95
|
+
//# sourceMappingURL=relay-hubs-admins.js.map
|