@celljs/ta-share 4.24.1 → 4.26.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 +1 -1
- package/README.md +1 -1
- package/extension/index.mjs +343 -0
- package/host/index.mjs +1973 -7
- package/package.json +6 -5
- package/web/index.js +1102 -25
package/host/index.mjs
CHANGED
|
@@ -2,9 +2,1584 @@
|
|
|
2
2
|
import { readFile, stat } from "node:fs/promises";
|
|
3
3
|
import { extname, isAbsolute, resolve } from "node:path";
|
|
4
4
|
|
|
5
|
+
// ../ta-relay/esm/common/relay-bytes.js
|
|
6
|
+
var RelayBytes;
|
|
7
|
+
(function(RelayBytes2) {
|
|
8
|
+
function random(length) {
|
|
9
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(length));
|
|
10
|
+
}
|
|
11
|
+
RelayBytes2.random = random;
|
|
12
|
+
function toBase64(bytes) {
|
|
13
|
+
let binary = "";
|
|
14
|
+
for (const byte of bytes) {
|
|
15
|
+
binary += String.fromCharCode(byte);
|
|
16
|
+
}
|
|
17
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
18
|
+
}
|
|
19
|
+
RelayBytes2.toBase64 = toBase64;
|
|
20
|
+
function fromBase64(text) {
|
|
21
|
+
if (!/^[A-Za-z0-9_-]*$/.test(text)) {
|
|
22
|
+
throw new Error("Not base64url.");
|
|
23
|
+
}
|
|
24
|
+
const binary = atob(text.replace(/-/g, "+").replace(/_/g, "/"));
|
|
25
|
+
const bytes = new Uint8Array(binary.length);
|
|
26
|
+
for (let i = 0;i < binary.length; i++) {
|
|
27
|
+
bytes[i] = binary.charCodeAt(i);
|
|
28
|
+
}
|
|
29
|
+
return bytes;
|
|
30
|
+
}
|
|
31
|
+
RelayBytes2.fromBase64 = fromBase64;
|
|
32
|
+
function concat(...parts) {
|
|
33
|
+
const bytes = new Uint8Array(parts.reduce((length, part) => length + part.length, 0));
|
|
34
|
+
let offset = 0;
|
|
35
|
+
for (const part of parts) {
|
|
36
|
+
bytes.set(part, offset);
|
|
37
|
+
offset += part.length;
|
|
38
|
+
}
|
|
39
|
+
return bytes;
|
|
40
|
+
}
|
|
41
|
+
RelayBytes2.concat = concat;
|
|
42
|
+
})(RelayBytes || (RelayBytes = {}));
|
|
43
|
+
|
|
44
|
+
// ../ta-relay/esm/common/relay-feedback-board-types.js
|
|
45
|
+
var RELAY_FEEDBACK_BOARD_POLICY = {
|
|
46
|
+
halfLifeDays: 30,
|
|
47
|
+
archiveDoneDays: 7,
|
|
48
|
+
archiveIdleDays: 90,
|
|
49
|
+
votesDaily: 200,
|
|
50
|
+
commentCooldownSeconds: 60,
|
|
51
|
+
commentsDaily: 30,
|
|
52
|
+
commentNewHours: 24,
|
|
53
|
+
breakerHourly: 50,
|
|
54
|
+
sourceCap: 2
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// ../ta-relay/esm/common/relay-feedback-score.js
|
|
58
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
59
|
+
var HOUR_MS = 60 * 60 * 1000;
|
|
60
|
+
var PLUS_WEIGHT = 2;
|
|
61
|
+
var UP_WEIGHT = 1;
|
|
62
|
+
var BURST_TIMES = 10;
|
|
63
|
+
var BURST_MIN = 20;
|
|
64
|
+
var Z = 1.96;
|
|
65
|
+
var RelayFeedbackScore;
|
|
66
|
+
(function(RelayFeedbackScore2) {
|
|
67
|
+
function trust(since, now, spammer = false) {
|
|
68
|
+
if (spammer || now - since < DAY_MS) {
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
return now - since < 7 * DAY_MS ? 0.5 : 1;
|
|
72
|
+
}
|
|
73
|
+
RelayFeedbackScore2.trust = trust;
|
|
74
|
+
function decay(age, halfLifeDays) {
|
|
75
|
+
return 2 ** (-Math.max(0, age) / (halfLifeDays * DAY_MS));
|
|
76
|
+
}
|
|
77
|
+
RelayFeedbackScore2.decay = decay;
|
|
78
|
+
function counted(votes2, spammers, cap = 2) {
|
|
79
|
+
var _a;
|
|
80
|
+
const seen = new Map;
|
|
81
|
+
const kept = [];
|
|
82
|
+
for (const vote of votes2) {
|
|
83
|
+
if (vote.h || spammers.has(vote.a)) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const key = `${vote.src}:${vote.day}`;
|
|
87
|
+
const n = ((_a = seen.get(key)) !== null && _a !== undefined ? _a : 0) + 1;
|
|
88
|
+
seen.set(key, n);
|
|
89
|
+
if (!cap || n <= cap) {
|
|
90
|
+
kept.push(vote);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return kept;
|
|
94
|
+
}
|
|
95
|
+
RelayFeedbackScore2.counted = counted;
|
|
96
|
+
function tally(votes2, spammers, now, halfLifeDays, cap = 2) {
|
|
97
|
+
var _a, _b, _c;
|
|
98
|
+
const counted2 = RelayFeedbackScore2.counted(votes2, spammers, cap);
|
|
99
|
+
let support = 0;
|
|
100
|
+
let against = 0;
|
|
101
|
+
for (const vote of counted2) {
|
|
102
|
+
if (vote.p) {
|
|
103
|
+
support += PLUS_WEIGHT * vote.w * decay(now - ((_a = vote.pAt) !== null && _a !== undefined ? _a : now), halfLifeDays);
|
|
104
|
+
}
|
|
105
|
+
if (vote.s === "up") {
|
|
106
|
+
support += UP_WEIGHT * vote.w * decay(now - ((_b = vote.sAt) !== null && _b !== undefined ? _b : now), halfLifeDays);
|
|
107
|
+
} else if (vote.s === "down") {
|
|
108
|
+
against += vote.w * decay(now - ((_c = vote.sAt) !== null && _c !== undefined ? _c : now), halfLifeDays);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
plus: counted2.filter((vote) => vote.p).length,
|
|
113
|
+
up: counted2.filter((vote) => vote.s === "up").length,
|
|
114
|
+
down: counted2.filter((vote) => vote.s === "down").length,
|
|
115
|
+
held: votes2.filter((vote) => vote.h).length,
|
|
116
|
+
support,
|
|
117
|
+
against,
|
|
118
|
+
at: now
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
RelayFeedbackScore2.tally = tally;
|
|
122
|
+
function wilson(support, against) {
|
|
123
|
+
const n = support + against;
|
|
124
|
+
if (n <= 0) {
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
const p = support / n;
|
|
128
|
+
const z2 = Z * Z;
|
|
129
|
+
return (p + z2 / (2 * n) - Z * Math.sqrt(p * (1 - p) / n + z2 / (4 * n * n))) / (1 + z2 / n);
|
|
130
|
+
}
|
|
131
|
+
RelayFeedbackScore2.wilson = wilson;
|
|
132
|
+
function votes(given, now, halfLifeDays) {
|
|
133
|
+
var _a, _b, _c;
|
|
134
|
+
const fade = decay(now - ((_a = given.scoredAt) !== null && _a !== undefined ? _a : now), halfLifeDays);
|
|
135
|
+
const support = ((_b = given.support) !== null && _b !== undefined ? _b : 0) * fade;
|
|
136
|
+
const against = ((_c = given.against) !== null && _c !== undefined ? _c : 0) * fade;
|
|
137
|
+
return Math.log2(1 + support) * wilson(support, against);
|
|
138
|
+
}
|
|
139
|
+
RelayFeedbackScore2.votes = votes;
|
|
140
|
+
function burst(times, now) {
|
|
141
|
+
const hour = times.filter((at) => now - at < HOUR_MS).length + 1;
|
|
142
|
+
const week = times.filter((at) => now - at >= HOUR_MS && now - at < 7 * DAY_MS).length;
|
|
143
|
+
return hour >= BURST_MIN && hour > BURST_TIMES * week / (7 * 24 - 1);
|
|
144
|
+
}
|
|
145
|
+
RelayFeedbackScore2.burst = burst;
|
|
146
|
+
function tokens(text) {
|
|
147
|
+
var _a;
|
|
148
|
+
const out = new Set;
|
|
149
|
+
for (const run of (_a = text.toLowerCase().match(/[\p{L}\p{N}]+/gu)) !== null && _a !== undefined ? _a : []) {
|
|
150
|
+
if (/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u.test(run)) {
|
|
151
|
+
for (let i = 0;i < run.length - 1; i++) {
|
|
152
|
+
out.add(run.slice(i, i + 2));
|
|
153
|
+
}
|
|
154
|
+
if (run.length === 1) {
|
|
155
|
+
out.add(run);
|
|
156
|
+
}
|
|
157
|
+
} else if (run.length > 1) {
|
|
158
|
+
out.add(run);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
RelayFeedbackScore2.tokens = tokens;
|
|
164
|
+
function match(query, text) {
|
|
165
|
+
if (!query.size) {
|
|
166
|
+
return 0;
|
|
167
|
+
}
|
|
168
|
+
let hit = 0;
|
|
169
|
+
for (const token of query) {
|
|
170
|
+
if (text.has(token)) {
|
|
171
|
+
hit++;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return hit / query.size;
|
|
175
|
+
}
|
|
176
|
+
RelayFeedbackScore2.match = match;
|
|
177
|
+
})(RelayFeedbackScore || (RelayFeedbackScore = {}));
|
|
178
|
+
// ../ta-relay/esm/common/relay-feedback-types.js
|
|
179
|
+
var RELAY_FEEDBACK_PACKAGE_PATH = "/v1/feedback/package";
|
|
180
|
+
var RELAY_FEEDBACK_INBOX_PACKAGE_PATH = "/v1/feedback/inbox/package";
|
|
181
|
+
var RELAY_FEEDBACK_CATALOG_PATH = "/v1/feedback/catalog";
|
|
182
|
+
var RELAY_FEEDBACK_CATALOG_PACKAGE_PATH = "/v1/feedback/catalog/package";
|
|
183
|
+
var RELAY_FEEDBACK_KINDS = ["bug", "wish"];
|
|
184
|
+
var RELAY_FEEDBACK_STATUSES = ["new", "planned", "doing", "done", "declined"];
|
|
185
|
+
var RELAY_FEEDBACK_CLOSED = ["done", "declined"];
|
|
186
|
+
var RELAY_FEEDBACK_LIMITS = {
|
|
187
|
+
title: 80,
|
|
188
|
+
body: 4000,
|
|
189
|
+
reply: 2000,
|
|
190
|
+
images: 3,
|
|
191
|
+
image: 700000,
|
|
192
|
+
package: 512 * 1024,
|
|
193
|
+
request: 3200000,
|
|
194
|
+
catalog: 100,
|
|
195
|
+
kept: 2000
|
|
196
|
+
};
|
|
197
|
+
var RELAY_FEEDBACK_IMAGE = /^data:(image\/(?:jpeg|png|webp));base64,([A-Za-z0-9+/]+={0,2})$/;
|
|
198
|
+
var RELAY_FEEDBACK_MODES = ["confirm", "auto", "human"];
|
|
199
|
+
var RELAY_FEEDBACK_POLICY = { on: true, cooldownMinutes: 10, daily: 5, open: 10, newDays: 7, imageDays: 30 };
|
|
200
|
+
var RELAY_FEEDBACK_PACE = { mode: "confirm", daily: 3, parallel: 2, tokens: 2000000, from: 9, to: 21, rush: ["security"] };
|
|
201
|
+
var RelayFeedbackPolicyUtil;
|
|
202
|
+
(function(RelayFeedbackPolicyUtil2) {
|
|
203
|
+
const count = (value, fallback, min, max) => {
|
|
204
|
+
const n = Math.round(Number(value));
|
|
205
|
+
return Number.isFinite(n) && value !== null && value !== "" ? Math.min(max, Math.max(min, n)) : fallback;
|
|
206
|
+
};
|
|
207
|
+
const mode = (value) => RELAY_FEEDBACK_MODES.find((each) => each === value);
|
|
208
|
+
function words(value, most = 20, max = 40) {
|
|
209
|
+
const list = Array.isArray(value) ? value : typeof value === "string" ? value.split(/[,,、\n]/) : [];
|
|
210
|
+
return [
|
|
211
|
+
...new Set(list.map((each) => String(each !== null && each !== undefined ? each : "").trim().slice(0, max)).filter(Boolean))
|
|
212
|
+
].slice(0, most);
|
|
213
|
+
}
|
|
214
|
+
RelayFeedbackPolicyUtil2.words = words;
|
|
215
|
+
function env(given) {
|
|
216
|
+
const env2 = given !== null && given !== undefined ? given : {};
|
|
217
|
+
const short = (value, max) => String(value).trim().slice(0, max);
|
|
218
|
+
return {
|
|
219
|
+
...env2.version ? { version: short(env2.version, 40) } : {},
|
|
220
|
+
...env2.os ? { os: short(env2.os, 60) } : {},
|
|
221
|
+
...env2.language ? { language: short(env2.language, 20) } : {}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
RelayFeedbackPolicyUtil2.env = env;
|
|
225
|
+
function input(text) {
|
|
226
|
+
var _a, _b;
|
|
227
|
+
let given;
|
|
228
|
+
try {
|
|
229
|
+
given = JSON.parse(text);
|
|
230
|
+
} catch (_c) {
|
|
231
|
+
return { error: "Not a feedback." };
|
|
232
|
+
}
|
|
233
|
+
const kind = RELAY_FEEDBACK_KINDS.find((each) => each === (given === null || given === undefined ? undefined : given.kind));
|
|
234
|
+
const title = String((_a = given === null || given === undefined ? undefined : given.title) !== null && _a !== undefined ? _a : "").trim().slice(0, RELAY_FEEDBACK_LIMITS.title);
|
|
235
|
+
const body = String((_b = given === null || given === undefined ? undefined : given.body) !== null && _b !== undefined ? _b : "").trim().slice(0, RELAY_FEEDBACK_LIMITS.body);
|
|
236
|
+
const images = Array.isArray(given === null || given === undefined ? undefined : given.images) ? given.images : [];
|
|
237
|
+
if (!kind || !title) {
|
|
238
|
+
return { error: "Say what it is about, in a title." };
|
|
239
|
+
}
|
|
240
|
+
if (images.length > RELAY_FEEDBACK_LIMITS.images || images.some((url) => typeof url !== "string" || url.length > RELAY_FEEDBACK_LIMITS.image || !RELAY_FEEDBACK_IMAGE.test(url))) {
|
|
241
|
+
return { error: `At most ${RELAY_FEEDBACK_LIMITS.images} pictures, each JPEG, PNG or WebP of about 500 KB.` };
|
|
242
|
+
}
|
|
243
|
+
return { kind, title, body, images, ...given.env ? { env: given.env } : {}, ...given.package ? { package: given.package } : {} };
|
|
244
|
+
}
|
|
245
|
+
RelayFeedbackPolicyUtil2.input = input;
|
|
246
|
+
function packageInfo(given, size) {
|
|
247
|
+
const info = given !== null && given !== undefined ? given : {};
|
|
248
|
+
const short = (value, max) => value === undefined || value === null ? "" : String(value).trim().slice(0, max);
|
|
249
|
+
const name = short(info.name, 214);
|
|
250
|
+
if (!name) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const title = short(info.title, 80);
|
|
254
|
+
const summary = short(info.summary, 400);
|
|
255
|
+
const version = short(info.version, 40);
|
|
256
|
+
const files = Math.max(0, Math.min(5000, Math.round(Number(info.files)) || 0));
|
|
257
|
+
return { name, ...title ? { title } : {}, ...summary ? { summary } : {}, ...version ? { version } : {}, size, files };
|
|
258
|
+
}
|
|
259
|
+
RelayFeedbackPolicyUtil2.packageInfo = packageInfo;
|
|
260
|
+
function pace(given, base = RELAY_FEEDBACK_PACE) {
|
|
261
|
+
var _a;
|
|
262
|
+
return {
|
|
263
|
+
mode: (_a = mode(given === null || given === undefined ? undefined : given.mode)) !== null && _a !== undefined ? _a : base.mode,
|
|
264
|
+
daily: count(given === null || given === undefined ? undefined : given.daily, base.daily, 0, 100),
|
|
265
|
+
parallel: count(given === null || given === undefined ? undefined : given.parallel, base.parallel, 1, 20),
|
|
266
|
+
tokens: count(given === null || given === undefined ? undefined : given.tokens, base.tokens, 0, 1e9),
|
|
267
|
+
from: count(given === null || given === undefined ? undefined : given.from, base.from, 0, 23),
|
|
268
|
+
to: count(given === null || given === undefined ? undefined : given.to, base.to, 1, 24),
|
|
269
|
+
rush: (given === null || given === undefined ? undefined : given.rush) === undefined ? base.rush : words(given.rush, 10)
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
RelayFeedbackPolicyUtil2.pace = pace;
|
|
273
|
+
function rule(given, id) {
|
|
274
|
+
var _a, _b, _c, _d;
|
|
275
|
+
const when = (_a = given === null || given === undefined ? undefined : given.when) !== null && _a !== undefined ? _a : {};
|
|
276
|
+
const then = (_b = given === null || given === undefined ? undefined : given.does) !== null && _b !== undefined ? _b : {};
|
|
277
|
+
const days = (value) => value === undefined || value === null || value === "" ? undefined : count(value, 1, 1, 365);
|
|
278
|
+
const cleanWhen = {
|
|
279
|
+
...RELAY_FEEDBACK_KINDS.includes(when.kind) ? { kind: when.kind } : {},
|
|
280
|
+
...words(when.words).length ? { words: words(when.words) } : {},
|
|
281
|
+
...typeof when.images === "boolean" ? { images: when.images } : {},
|
|
282
|
+
...days(when.newAccount) ? { newAccount: days(when.newAccount) } : {},
|
|
283
|
+
...days(when.idleDays) ? { idleDays: days(when.idleDays) } : {},
|
|
284
|
+
...words(when.tag, 1)[0] ? { tag: words(when.tag, 1)[0] } : {},
|
|
285
|
+
...typeof when.package === "boolean" ? { package: when.package } : {},
|
|
286
|
+
...when.plus !== undefined && when.plus !== null && when.plus !== "" ? { plus: count(when.plus, 1, 1, 1e5) } : {}
|
|
287
|
+
};
|
|
288
|
+
const reply = String((_c = then.reply) !== null && _c !== undefined ? _c : "").trim().slice(0, RELAY_FEEDBACK_LIMITS.reply);
|
|
289
|
+
const cleanThen = {
|
|
290
|
+
...words(then.tags, 10).length ? { tags: words(then.tags, 10) } : {},
|
|
291
|
+
...mode(then.mode) ? { mode: mode(then.mode) } : {},
|
|
292
|
+
...RELAY_FEEDBACK_STATUSES.includes(then.status) ? { status: then.status } : {},
|
|
293
|
+
...reply ? { reply } : {},
|
|
294
|
+
...then.notify === true ? { notify: true } : {},
|
|
295
|
+
...then.public === true ? { public: true } : {}
|
|
296
|
+
};
|
|
297
|
+
if (!Object.keys(cleanThen).length) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const safeId = String((_d = given === null || given === undefined ? undefined : given.id) !== null && _d !== undefined ? _d : "").replace(/[^\w-]/g, "").slice(0, 24) || id;
|
|
301
|
+
return { id: safeId, on: (given === null || given === undefined ? undefined : given.on) !== false, when: cleanWhen, does: cleanThen, ...(given === null || given === undefined ? undefined : given.stop) ? { stop: true } : {} };
|
|
302
|
+
}
|
|
303
|
+
RelayFeedbackPolicyUtil2.rule = rule;
|
|
304
|
+
function board(given, base = RELAY_FEEDBACK_BOARD_POLICY) {
|
|
305
|
+
return {
|
|
306
|
+
halfLifeDays: count(given === null || given === undefined ? undefined : given.halfLifeDays, base.halfLifeDays, 1, 365),
|
|
307
|
+
archiveDoneDays: count(given === null || given === undefined ? undefined : given.archiveDoneDays, base.archiveDoneDays, 0, 365),
|
|
308
|
+
archiveIdleDays: count(given === null || given === undefined ? undefined : given.archiveIdleDays, base.archiveIdleDays, 0, 365),
|
|
309
|
+
votesDaily: count(given === null || given === undefined ? undefined : given.votesDaily, base.votesDaily, 1, 1e4),
|
|
310
|
+
commentCooldownSeconds: count(given === null || given === undefined ? undefined : given.commentCooldownSeconds, base.commentCooldownSeconds, 0, 3600),
|
|
311
|
+
commentsDaily: count(given === null || given === undefined ? undefined : given.commentsDaily, base.commentsDaily, 1, 500),
|
|
312
|
+
commentNewHours: count(given === null || given === undefined ? undefined : given.commentNewHours, base.commentNewHours, 0, 720),
|
|
313
|
+
breakerHourly: count(given === null || given === undefined ? undefined : given.breakerHourly, base.breakerHourly, 0, 1e4),
|
|
314
|
+
sourceCap: count(given === null || given === undefined ? undefined : given.sourceCap, base.sourceCap, 0, 1000)
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
RelayFeedbackPolicyUtil2.board = board;
|
|
318
|
+
function slow(given) {
|
|
319
|
+
const seconds = (given === null || given === undefined ? undefined : given.seconds) ? count(given.seconds, 60, 10, 6 * 3600) : undefined;
|
|
320
|
+
const until = (given === null || given === undefined ? undefined : given.until) ? Math.round(Number(given.until)) || undefined : undefined;
|
|
321
|
+
return seconds || (given === null || given === undefined ? undefined : given.only) ? { ...seconds ? { seconds } : {}, ...(given === null || given === undefined ? undefined : given.only) ? { only: true } : {}, ...until ? { until } : {} } : undefined;
|
|
322
|
+
}
|
|
323
|
+
RelayFeedbackPolicyUtil2.slow = slow;
|
|
324
|
+
function of(given, base = RELAY_FEEDBACK_POLICY) {
|
|
325
|
+
return {
|
|
326
|
+
on: typeof (given === null || given === undefined ? undefined : given.on) === "boolean" ? given.on : base.on,
|
|
327
|
+
cooldownMinutes: count(given === null || given === undefined ? undefined : given.cooldownMinutes, base.cooldownMinutes, 0, 24 * 60),
|
|
328
|
+
daily: count(given === null || given === undefined ? undefined : given.daily, base.daily, 1, 100),
|
|
329
|
+
open: count(given === null || given === undefined ? undefined : given.open, base.open, 1, 100),
|
|
330
|
+
newDays: count(given === null || given === undefined ? undefined : given.newDays, base.newDays, 0, 90),
|
|
331
|
+
imageDays: count(given === null || given === undefined ? undefined : given.imageDays, base.imageDays, 0, 90)
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
RelayFeedbackPolicyUtil2.of = of;
|
|
335
|
+
})(RelayFeedbackPolicyUtil || (RelayFeedbackPolicyUtil = {}));
|
|
336
|
+
|
|
337
|
+
// ../ta-relay/esm/common/relay-feedback-rules.js
|
|
338
|
+
var DAY_MS2 = 24 * 60 * 60 * 1000;
|
|
339
|
+
var KIND_SCORE = { bug: 1, wish: 0.5 };
|
|
340
|
+
var WAIT_SCORE = 2;
|
|
341
|
+
var WAIT_FULL_DAYS = 14;
|
|
342
|
+
var RelayFeedbackRuleUtil;
|
|
343
|
+
(function(RelayFeedbackRuleUtil2) {
|
|
344
|
+
function matches(item, when, now) {
|
|
345
|
+
var _a, _b;
|
|
346
|
+
var _c, _d;
|
|
347
|
+
if (when.kind && item.kind !== when.kind) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
if ((_a = when.words) === null || _a === undefined ? undefined : _a.length) {
|
|
351
|
+
const text = `${item.title}
|
|
352
|
+
${item.body}`.toLowerCase();
|
|
353
|
+
if (!when.words.some((word) => text.includes(word.toLowerCase()))) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (when.images !== undefined && ((_c = item.shots) !== null && _c !== undefined ? _c : item.images) > 0 !== when.images) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
if (when.newAccount && !(item.since !== undefined && item.at - item.since < when.newAccount * DAY_MS2)) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
if (when.idleDays && (RELAY_FEEDBACK_CLOSED.includes(item.status) || now - item.updatedAt < when.idleDays * DAY_MS2)) {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
if (when.package !== undefined && !!item.package !== when.package) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
if (when.plus && ((_d = item.plus) !== null && _d !== undefined ? _d : 0) < when.plus) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
return !when.tag || !!((_b = item.tags) === null || _b === undefined ? undefined : _b.includes(when.tag));
|
|
373
|
+
}
|
|
374
|
+
RelayFeedbackRuleUtil2.matches = matches;
|
|
375
|
+
function queue(items, pace, now, halfLifeDays = 30) {
|
|
376
|
+
const score = (item) => RelayFeedbackScore.votes(item, now, halfLifeDays) + KIND_SCORE[item.kind] + Math.min((now - item.at) / (WAIT_FULL_DAYS * DAY_MS2), 1) * WAIT_SCORE;
|
|
377
|
+
const rush = (item) => {
|
|
378
|
+
var _a;
|
|
379
|
+
return ((_a = item.tags) !== null && _a !== undefined ? _a : []).some((tag) => pace.rush.includes(tag));
|
|
380
|
+
};
|
|
381
|
+
return items.filter((item) => {
|
|
382
|
+
var _a;
|
|
383
|
+
return (item.status === "new" || item.status === "planned") && !item.taken && !item.archived && !item.held && ((_a = item.mode) !== null && _a !== undefined ? _a : pace.mode) !== "human";
|
|
384
|
+
}).sort((a, b) => Number(rush(b)) - Number(rush(a)) || score(b) - score(a) || a.at - b.at || a.n - b.n).map((item) => item.id);
|
|
385
|
+
}
|
|
386
|
+
RelayFeedbackRuleUtil2.queue = queue;
|
|
387
|
+
function does(item, then, now) {
|
|
388
|
+
var _a, _b;
|
|
389
|
+
const did = [];
|
|
390
|
+
const tags = ((_a = then.tags) !== null && _a !== undefined ? _a : []).filter((tag) => {
|
|
391
|
+
var _a2;
|
|
392
|
+
return !((_a2 = item.tags) === null || _a2 === undefined ? undefined : _a2.includes(tag));
|
|
393
|
+
});
|
|
394
|
+
if (tags.length) {
|
|
395
|
+
item.tags = [...(_b = item.tags) !== null && _b !== undefined ? _b : [], ...tags].slice(0, 10);
|
|
396
|
+
did.push(...tags.map((tag) => `tag:${tag}`));
|
|
397
|
+
}
|
|
398
|
+
if (then.mode && item.mode !== then.mode) {
|
|
399
|
+
item.mode = then.mode;
|
|
400
|
+
did.push(`mode:${then.mode}`);
|
|
401
|
+
}
|
|
402
|
+
const status = then.status && item.status !== then.status ? then.status : undefined;
|
|
403
|
+
if (status) {
|
|
404
|
+
did.push(`status:${status}`);
|
|
405
|
+
}
|
|
406
|
+
const replyAt = then.reply ? reply(item, then.reply.split("{n}").join(String(item.n)).split("{title}").join(item.title), now) : undefined;
|
|
407
|
+
if (replyAt) {
|
|
408
|
+
did.push("reply");
|
|
409
|
+
}
|
|
410
|
+
if (then.notify) {
|
|
411
|
+
item.flagged = now;
|
|
412
|
+
did.push("notify");
|
|
413
|
+
}
|
|
414
|
+
if (then.public && !item.public) {
|
|
415
|
+
item.public = true;
|
|
416
|
+
did.push("public:on");
|
|
417
|
+
}
|
|
418
|
+
return { did, ...replyAt ? { replyAt } : {}, ...status ? { status } : {} };
|
|
419
|
+
}
|
|
420
|
+
RelayFeedbackRuleUtil2.does = does;
|
|
421
|
+
function reply(item, text, now) {
|
|
422
|
+
var _a;
|
|
423
|
+
var _b;
|
|
424
|
+
const at = Math.max(now, ((_b = (_a = item.replies[item.replies.length - 1]) === null || _a === undefined ? undefined : _a.at) !== null && _b !== undefined ? _b : 0) + 1);
|
|
425
|
+
item.replies = [...item.replies, { at, text }].slice(-50);
|
|
426
|
+
item.updatedAt = at;
|
|
427
|
+
return at;
|
|
428
|
+
}
|
|
429
|
+
RelayFeedbackRuleUtil2.reply = reply;
|
|
430
|
+
function before(item) {
|
|
431
|
+
return {
|
|
432
|
+
status: item.status,
|
|
433
|
+
...item.tags ? { tags: [...item.tags] } : {},
|
|
434
|
+
...item.mode ? { mode: item.mode } : {},
|
|
435
|
+
...item.taken ? { taken: item.taken } : {},
|
|
436
|
+
...item.flagged ? { flagged: item.flagged } : {},
|
|
437
|
+
...item.public ? { public: true } : {},
|
|
438
|
+
...item.archived ? { archived: item.archived } : {},
|
|
439
|
+
...item.held ? { held: item.held } : {}
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
RelayFeedbackRuleUtil2.before = before;
|
|
443
|
+
})(RelayFeedbackRuleUtil || (RelayFeedbackRuleUtil = {}));
|
|
444
|
+
|
|
445
|
+
// ../ta-relay/esm/common/relay-feedback-board.js
|
|
446
|
+
var DAY_MS3 = 24 * 60 * 60 * 1000;
|
|
447
|
+
var HOUR_MS2 = 60 * 60 * 1000;
|
|
448
|
+
|
|
449
|
+
// ../ta-relay/esm/common/relay-feedback-shelf.js
|
|
450
|
+
var PATHS = new Set([
|
|
451
|
+
RELAY_FEEDBACK_PACKAGE_PATH,
|
|
452
|
+
RELAY_FEEDBACK_INBOX_PACKAGE_PATH,
|
|
453
|
+
RELAY_FEEDBACK_CATALOG_PATH,
|
|
454
|
+
RELAY_FEEDBACK_CATALOG_PACKAGE_PATH
|
|
455
|
+
]);
|
|
456
|
+
|
|
457
|
+
// ../ta-relay/esm/common/relay-feedback.js
|
|
458
|
+
var DAY_MS4 = 24 * 60 * 60 * 1000;
|
|
459
|
+
var HOUR_MS3 = 60 * 60 * 1000;
|
|
460
|
+
|
|
461
|
+
// ../ta-relay/esm/common/relay-moderation-policy.js
|
|
462
|
+
var RELAY_OUTWARD_FEATURES = ["share", "rooms", "moments", "nearby"];
|
|
463
|
+
var RELAY_MODERATION_POLICY = {
|
|
464
|
+
auto: false,
|
|
465
|
+
people: 3,
|
|
466
|
+
severe: 2,
|
|
467
|
+
hostAfter: 3,
|
|
468
|
+
limitDays: 7,
|
|
469
|
+
needsAccount: [],
|
|
470
|
+
banCovers: ["share", "rooms", "moments", "nearby"],
|
|
471
|
+
newDays: 7,
|
|
472
|
+
newDaily: 5
|
|
473
|
+
};
|
|
474
|
+
var RELAY_BIND_PATH = "/v1/bind";
|
|
475
|
+
var RELAY_STANDING_PATH = "/v1/standing";
|
|
476
|
+
var RELAY_APPEAL_PATH = "/v1/appeal";
|
|
477
|
+
var RELAY_TICKET_PATH = "/v1/ticket";
|
|
478
|
+
var RELAY_QUOTA_PATH = "/v1/quota";
|
|
479
|
+
var RELAY_TICKET_MS = 60 * 60 * 1000;
|
|
480
|
+
var RelayModerationPolicyUtil;
|
|
481
|
+
(function(RelayModerationPolicyUtil2) {
|
|
482
|
+
function count(value, fallback, min, max = 100) {
|
|
483
|
+
const n = Math.round(Number(value));
|
|
484
|
+
return Number.isFinite(n) && n >= min ? Math.min(n, max) : fallback;
|
|
485
|
+
}
|
|
486
|
+
function features(value, fallback) {
|
|
487
|
+
return Array.isArray(value) ? RELAY_OUTWARD_FEATURES.filter((feature) => value.includes(feature)) : fallback;
|
|
488
|
+
}
|
|
489
|
+
function of(given) {
|
|
490
|
+
const base = RELAY_MODERATION_POLICY;
|
|
491
|
+
return {
|
|
492
|
+
auto: (given === null || given === undefined ? undefined : given.auto) === true,
|
|
493
|
+
people: count(given === null || given === undefined ? undefined : given.people, base.people, 1),
|
|
494
|
+
severe: count(given === null || given === undefined ? undefined : given.severe, base.severe, 1),
|
|
495
|
+
hostAfter: count(given === null || given === undefined ? undefined : given.hostAfter, base.hostAfter, 0),
|
|
496
|
+
limitDays: count(given === null || given === undefined ? undefined : given.limitDays, base.limitDays, 1, 30),
|
|
497
|
+
needsAccount: features(given === null || given === undefined ? undefined : given.needsAccount, base.needsAccount),
|
|
498
|
+
banCovers: features(given === null || given === undefined ? undefined : given.banCovers, base.banCovers),
|
|
499
|
+
newDays: count(given === null || given === undefined ? undefined : given.newDays, base.newDays, 0, 90),
|
|
500
|
+
newDaily: count(given === null || given === undefined ? undefined : given.newDaily, base.newDaily, 1, 1000)
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
RelayModerationPolicyUtil2.of = of;
|
|
504
|
+
function active(sanction, now) {
|
|
505
|
+
return !!sanction && (sanction.until === undefined || sanction.until > now);
|
|
506
|
+
}
|
|
507
|
+
RelayModerationPolicyUtil2.active = active;
|
|
508
|
+
function takes(sanction, feature, policy, now) {
|
|
509
|
+
if (!active(sanction, now)) {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
return sanction.level === "limit" ? feature === "share" : policy.banCovers.includes(feature);
|
|
513
|
+
}
|
|
514
|
+
RelayModerationPolicyUtil2.takes = takes;
|
|
515
|
+
})(RelayModerationPolicyUtil || (RelayModerationPolicyUtil = {}));
|
|
516
|
+
|
|
517
|
+
// ../ta-relay/esm/common/relay-moderation-tickets.js
|
|
518
|
+
var encoder = new TextEncoder;
|
|
519
|
+
|
|
520
|
+
// ../ta-relay/esm/common/relay-moderation-trust.js
|
|
521
|
+
var DAY_MS5 = 24 * 60 * 60 * 1000;
|
|
522
|
+
|
|
523
|
+
// ../ta-relay/esm/common/relay-moderation-types.js
|
|
524
|
+
var RELAY_MODERATION_PATH = "/v1/moderation";
|
|
525
|
+
var RELAY_REPORT_REASONS = ["phishing", "malware", "sexual", "illegal", "harassment", "other"];
|
|
526
|
+
|
|
527
|
+
// ../ta-relay/esm/common/relay-protocol.js
|
|
528
|
+
var RELAY_CONNECT_PATH = "/v1/connect/";
|
|
529
|
+
var RELAY_PAIR_PATH = "/pair";
|
|
530
|
+
var RELAY_CONTENT_PATH = "/s";
|
|
531
|
+
var RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
532
|
+
var RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
|
|
533
|
+
var RELAY_BLOB_PATH = "/v1/blob/";
|
|
534
|
+
var OPEN_IN_TA = `<p><a id="open" style="display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600">Open in Ta · 在 Ta 中打开</a></p>
|
|
535
|
+
<script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script>`;
|
|
536
|
+
var RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
|
|
537
|
+
<body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
|
|
538
|
+
<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>
|
|
539
|
+
<p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p>${OPEN_IN_TA}</body>`;
|
|
540
|
+
var RELAY_SHARE_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
|
|
541
|
+
<body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
|
|
542
|
+
<h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>
|
|
543
|
+
<p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p>${OPEN_IN_TA}</body>`;
|
|
544
|
+
var RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
|
|
545
|
+
var RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
|
|
546
|
+
var RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
547
|
+
var RELAY_ID = /^[A-Za-z0-9_-]{16,64}$/;
|
|
548
|
+
var RelayCloseCode;
|
|
549
|
+
(function(RelayCloseCode2) {
|
|
550
|
+
RelayCloseCode2[RelayCloseCode2["HOST_OFFLINE"] = 4404] = "HOST_OFFLINE";
|
|
551
|
+
RelayCloseCode2[RelayCloseCode2["REFUSED"] = 4401] = "REFUSED";
|
|
552
|
+
RelayCloseCode2[RelayCloseCode2["REPLACED"] = 4409] = "REPLACED";
|
|
553
|
+
RelayCloseCode2[RelayCloseCode2["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
|
|
554
|
+
RelayCloseCode2[RelayCloseCode2["FULL"] = 4429] = "FULL";
|
|
555
|
+
RelayCloseCode2[RelayCloseCode2["TIMEOUT"] = 4408] = "TIMEOUT";
|
|
556
|
+
RelayCloseCode2[RelayCloseCode2["BLOCKED"] = 4451] = "BLOCKED";
|
|
557
|
+
RelayCloseCode2[RelayCloseCode2["SIGN_IN"] = 4452] = "SIGN_IN";
|
|
558
|
+
})(RelayCloseCode || (RelayCloseCode = {}));
|
|
559
|
+
var RELAY_PING = "ping";
|
|
560
|
+
var RELAY_PONG = "pong";
|
|
561
|
+
var RELAY_PING_MS = 25000;
|
|
562
|
+
var RELAY_PROOF = "ta-relay ready";
|
|
563
|
+
var RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
564
|
+
var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
565
|
+
var RELAY_PICTURE_MAX_CHARS = 16 * 1024;
|
|
566
|
+
var RELAY_HUB_LOG_CHARS = 4 * 1024 * 1024;
|
|
567
|
+
|
|
568
|
+
// ../ta-relay/esm/common/relay-identity.js
|
|
569
|
+
var encoder2 = new TextEncoder;
|
|
570
|
+
var decoder = new TextDecoder;
|
|
571
|
+
var subtle = () => globalThis.crypto.subtle;
|
|
572
|
+
var SIGN = { name: "ECDSA", namedCurve: "P-256" };
|
|
573
|
+
var BOX = { name: "ECDH", namedCurve: "P-256" };
|
|
574
|
+
var SIGNATURE = { name: "ECDSA", hash: "SHA-256" };
|
|
575
|
+
var RelayKeys;
|
|
576
|
+
(function(RelayKeys2) {
|
|
577
|
+
async function hkdf(key, salt, info) {
|
|
578
|
+
const secret = await subtle().importKey("raw", key, "HKDF", false, ["deriveBits"]);
|
|
579
|
+
const bits = await subtle().deriveBits({ name: "HKDF", hash: "SHA-256", salt, info: encoder2.encode(info) }, secret, 256);
|
|
580
|
+
return new Uint8Array(bits);
|
|
581
|
+
}
|
|
582
|
+
RelayKeys2.hkdf = hkdf;
|
|
583
|
+
async function sha256(bytes) {
|
|
584
|
+
const data = typeof bytes === "string" ? encoder2.encode(bytes) : bytes;
|
|
585
|
+
return new Uint8Array(await subtle().digest("SHA-256", data));
|
|
586
|
+
}
|
|
587
|
+
RelayKeys2.sha256 = sha256;
|
|
588
|
+
async function hash(text) {
|
|
589
|
+
return RelayBytes.toBase64(await sha256(text));
|
|
590
|
+
}
|
|
591
|
+
RelayKeys2.hash = hash;
|
|
592
|
+
async function hmac(key, text) {
|
|
593
|
+
const imported = await subtle().importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
594
|
+
return RelayBytes.toBase64(new Uint8Array(await subtle().sign("HMAC", imported, encoder2.encode(text))));
|
|
595
|
+
}
|
|
596
|
+
RelayKeys2.hmac = hmac;
|
|
597
|
+
function aesKey(key) {
|
|
598
|
+
return subtle().importKey("raw", key, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
599
|
+
}
|
|
600
|
+
RelayKeys2.aesKey = aesKey;
|
|
601
|
+
async function seal(key, text) {
|
|
602
|
+
const iv = RelayBytes.random(12);
|
|
603
|
+
const sealed = await subtle().encrypt({ name: "AES-GCM", iv }, key, encoder2.encode(text));
|
|
604
|
+
return RelayBytes.toBase64(RelayBytes.concat(iv, new Uint8Array(sealed)));
|
|
605
|
+
}
|
|
606
|
+
RelayKeys2.seal = seal;
|
|
607
|
+
async function open(key, sealed) {
|
|
608
|
+
const bytes = RelayBytes.fromBase64(sealed);
|
|
609
|
+
const opened = await subtle().decrypt({ name: "AES-GCM", iv: bytes.subarray(0, 12) }, key, bytes.subarray(12));
|
|
610
|
+
return decoder.decode(opened);
|
|
611
|
+
}
|
|
612
|
+
RelayKeys2.open = open;
|
|
613
|
+
})(RelayKeys || (RelayKeys = {}));
|
|
614
|
+
var RelayIdentityUtil;
|
|
615
|
+
(function(RelayIdentityUtil2) {
|
|
616
|
+
async function create(name, now = Date.now()) {
|
|
617
|
+
const sign2 = await subtle().generateKey(SIGN, true, ["sign", "verify"]);
|
|
618
|
+
const box = await subtle().generateKey(BOX, true, ["deriveBits"]);
|
|
619
|
+
const raw = async (key) => RelayBytes.toBase64(new Uint8Array(await subtle().exportKey("raw", key)));
|
|
620
|
+
const [signPublic, boxPublic] = await Promise.all([raw(sign2.publicKey), raw(box.publicKey)]);
|
|
621
|
+
return {
|
|
622
|
+
id: await id(signPublic, boxPublic),
|
|
623
|
+
name,
|
|
624
|
+
sign: signPublic,
|
|
625
|
+
box: boxPublic,
|
|
626
|
+
signKey: await subtle().exportKey("jwk", sign2.privateKey),
|
|
627
|
+
boxKey: await subtle().exportKey("jwk", box.privateKey),
|
|
628
|
+
createdAt: now
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
RelayIdentityUtil2.create = create;
|
|
632
|
+
async function id(sign2, box) {
|
|
633
|
+
const digest = await RelayKeys.sha256(RelayBytes.concat(RelayBytes.fromBase64(sign2), RelayBytes.fromBase64(box)));
|
|
634
|
+
return RelayBytes.toBase64(digest.subarray(0, 16));
|
|
635
|
+
}
|
|
636
|
+
RelayIdentityUtil2.id = id;
|
|
637
|
+
async function check(identity) {
|
|
638
|
+
try {
|
|
639
|
+
if (identity.id !== await id(identity.sign, identity.box)) {
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
await Promise.all([importSign(identity.sign), importBox(identity.box)]);
|
|
643
|
+
return true;
|
|
644
|
+
} catch (_a) {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
RelayIdentityUtil2.check = check;
|
|
649
|
+
function publicOf(identity) {
|
|
650
|
+
return withLook({ id: identity.id, name: identity.name, sign: identity.sign, box: identity.box }, identity.look);
|
|
651
|
+
}
|
|
652
|
+
RelayIdentityUtil2.publicOf = publicOf;
|
|
653
|
+
function isLook(look) {
|
|
654
|
+
return typeof look === "string" && /^[a-z][a-z0-9-]{0,31}$/.test(look);
|
|
655
|
+
}
|
|
656
|
+
RelayIdentityUtil2.isLook = isLook;
|
|
657
|
+
function isPicture(picture) {
|
|
658
|
+
return typeof picture === "string" && picture.length <= RELAY_PICTURE_MAX_CHARS && /^data:image\/(png|jpeg|webp|svg\+xml);base64,[A-Za-z0-9+/]+={0,2}$/.test(picture);
|
|
659
|
+
}
|
|
660
|
+
RelayIdentityUtil2.isPicture = isPicture;
|
|
661
|
+
function withLook(value, look) {
|
|
662
|
+
const { look: _, ...rest } = value;
|
|
663
|
+
return isLook(look) ? { ...rest, look } : rest;
|
|
664
|
+
}
|
|
665
|
+
RelayIdentityUtil2.withLook = withLook;
|
|
666
|
+
async function sign(identity, text) {
|
|
667
|
+
const key = await subtle().importKey("jwk", identity.signKey, SIGN, false, ["sign"]);
|
|
668
|
+
return RelayBytes.toBase64(new Uint8Array(await subtle().sign(SIGNATURE, key, encoder2.encode(text))));
|
|
669
|
+
}
|
|
670
|
+
RelayIdentityUtil2.sign = sign;
|
|
671
|
+
async function verify(signPublic, text, signature) {
|
|
672
|
+
try {
|
|
673
|
+
return await subtle().verify(SIGNATURE, await importSign(signPublic), RelayBytes.fromBase64(signature), encoder2.encode(text));
|
|
674
|
+
} catch (_a) {
|
|
675
|
+
return false;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
RelayIdentityUtil2.verify = verify;
|
|
679
|
+
async function agree(identity, otherBox, salt, info) {
|
|
680
|
+
const own = await subtle().importKey("jwk", identity.boxKey, BOX, false, ["deriveBits"]);
|
|
681
|
+
const shared = await subtle().deriveBits({ name: "ECDH", public: await importBox(otherBox) }, own, 256);
|
|
682
|
+
return RelayKeys.hkdf(new Uint8Array(shared), encoder2.encode(salt), info);
|
|
683
|
+
}
|
|
684
|
+
RelayIdentityUtil2.agree = agree;
|
|
685
|
+
function importSign(raw) {
|
|
686
|
+
return subtle().importKey("raw", RelayBytes.fromBase64(raw), SIGN, false, ["verify"]);
|
|
687
|
+
}
|
|
688
|
+
function importBox(raw) {
|
|
689
|
+
return subtle().importKey("raw", RelayBytes.fromBase64(raw), BOX, false, []);
|
|
690
|
+
}
|
|
691
|
+
})(RelayIdentityUtil || (RelayIdentityUtil = {}));
|
|
692
|
+
|
|
693
|
+
// ../ta-relay/esm/common/relay-cipher.js
|
|
694
|
+
var encoder3 = new TextEncoder;
|
|
695
|
+
var decoder2 = new TextDecoder;
|
|
696
|
+
var IV_LENGTH = 12;
|
|
697
|
+
var PLAIN = 0;
|
|
698
|
+
var DEFLATED = 1;
|
|
699
|
+
var DEFLATE_MIN_BYTES = 1024;
|
|
700
|
+
|
|
701
|
+
class RelayCipher {
|
|
702
|
+
constructor(sendKey, receiveKey) {
|
|
703
|
+
this.sendKey = sendKey;
|
|
704
|
+
this.receiveKey = receiveKey;
|
|
705
|
+
this.compress = false;
|
|
706
|
+
this.sent = 0;
|
|
707
|
+
this.received = 0;
|
|
708
|
+
this.sealing = Promise.resolve();
|
|
709
|
+
this.opening = Promise.resolve();
|
|
710
|
+
}
|
|
711
|
+
static async derive(deviceKey, clientNonce, hostNonce, side) {
|
|
712
|
+
const subtle2 = globalThis.crypto.subtle;
|
|
713
|
+
const secret = await subtle2.importKey("raw", deviceKey, "HKDF", false, ["deriveKey"]);
|
|
714
|
+
const salt = RelayBytes.concat(clientNonce, hostNonce);
|
|
715
|
+
const key = (direction) => subtle2.deriveKey({ name: "HKDF", hash: "SHA-256", salt, info: encoder3.encode(`ta-relay/1 ${direction}`) }, secret, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]);
|
|
716
|
+
const [toHost, toClient] = await Promise.all([key("client-to-host"), key("host-to-client")]);
|
|
717
|
+
return side === "client" ? new RelayCipher(toHost, toClient) : new RelayCipher(toClient, toHost);
|
|
718
|
+
}
|
|
719
|
+
seal(text) {
|
|
720
|
+
const iv = RelayCipher.iv(this.sent++);
|
|
721
|
+
const sealed = this.sealing.then(async () => {
|
|
722
|
+
const data = await globalThis.crypto.subtle.encrypt({ name: "AES-GCM", iv }, this.sendKey, await this.pack(text));
|
|
723
|
+
return RelayBytes.toBase64(new Uint8Array(data));
|
|
724
|
+
});
|
|
725
|
+
this.sealing = sealed.catch(() => {
|
|
726
|
+
return;
|
|
727
|
+
});
|
|
728
|
+
return sealed;
|
|
729
|
+
}
|
|
730
|
+
open(frame) {
|
|
731
|
+
const iv = RelayCipher.iv(this.received++);
|
|
732
|
+
const opened = this.opening.then(async () => {
|
|
733
|
+
const data = await globalThis.crypto.subtle.decrypt({ name: "AES-GCM", iv }, this.receiveKey, RelayBytes.fromBase64(frame));
|
|
734
|
+
return this.unpack(new Uint8Array(data));
|
|
735
|
+
});
|
|
736
|
+
this.opening = opened.catch(() => {
|
|
737
|
+
return;
|
|
738
|
+
});
|
|
739
|
+
return opened;
|
|
740
|
+
}
|
|
741
|
+
async pack(text) {
|
|
742
|
+
const bytes = encoder3.encode(text);
|
|
743
|
+
if (!this.compress) {
|
|
744
|
+
return bytes;
|
|
745
|
+
}
|
|
746
|
+
const deflated = bytes.length >= DEFLATE_MIN_BYTES ? await RelayDeflate.compress(bytes) : undefined;
|
|
747
|
+
return deflated && deflated.length < bytes.length ? RelayBytes.concat(Uint8Array.of(DEFLATED), deflated) : RelayBytes.concat(Uint8Array.of(PLAIN), bytes);
|
|
748
|
+
}
|
|
749
|
+
async unpack(bytes) {
|
|
750
|
+
if (!this.compress) {
|
|
751
|
+
return decoder2.decode(bytes);
|
|
752
|
+
}
|
|
753
|
+
const body = bytes.subarray(1);
|
|
754
|
+
return decoder2.decode(bytes[0] === DEFLATED ? await RelayDeflate.decompress(body) : body);
|
|
755
|
+
}
|
|
756
|
+
static iv(counter) {
|
|
757
|
+
const iv = new Uint8Array(IV_LENGTH);
|
|
758
|
+
new DataView(iv.buffer).setUint32(IV_LENGTH - 8, Math.floor(counter / 2 ** 32));
|
|
759
|
+
new DataView(iv.buffer).setUint32(IV_LENGTH - 4, counter >>> 0);
|
|
760
|
+
return iv;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
var RelayDeflate;
|
|
764
|
+
(function(RelayDeflate2) {
|
|
765
|
+
let support;
|
|
766
|
+
function supported() {
|
|
767
|
+
if (support === undefined) {
|
|
768
|
+
try {
|
|
769
|
+
new CompressionStream("deflate-raw");
|
|
770
|
+
new DecompressionStream("deflate-raw");
|
|
771
|
+
support = true;
|
|
772
|
+
} catch (_a) {
|
|
773
|
+
support = false;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return support;
|
|
777
|
+
}
|
|
778
|
+
RelayDeflate2.supported = supported;
|
|
779
|
+
function compress(bytes) {
|
|
780
|
+
return through(bytes, new CompressionStream("deflate-raw"));
|
|
781
|
+
}
|
|
782
|
+
RelayDeflate2.compress = compress;
|
|
783
|
+
function decompress(bytes) {
|
|
784
|
+
return through(bytes, new DecompressionStream("deflate-raw"));
|
|
785
|
+
}
|
|
786
|
+
RelayDeflate2.decompress = decompress;
|
|
787
|
+
async function through(bytes, stream) {
|
|
788
|
+
const piped = new Blob([bytes]).stream().pipeThrough(stream);
|
|
789
|
+
return new Uint8Array(await new Response(piped).arrayBuffer());
|
|
790
|
+
}
|
|
791
|
+
})(RelayDeflate || (RelayDeflate = {}));
|
|
792
|
+
|
|
793
|
+
// ../ta-relay/esm/common/relay-pairing.js
|
|
794
|
+
var RelayPairingUtil;
|
|
795
|
+
(function(RelayPairingUtil2) {
|
|
796
|
+
function link(pairing) {
|
|
797
|
+
const fragment = new URLSearchParams({ v: "1", h: pairing.host, d: pairing.device, k: pairing.key });
|
|
798
|
+
if (pairing.name) {
|
|
799
|
+
fragment.set("n", pairing.name);
|
|
800
|
+
}
|
|
801
|
+
return `${pairing.relay}${RELAY_PAIR_PATH}#${fragment}`;
|
|
802
|
+
}
|
|
803
|
+
RelayPairingUtil2.link = link;
|
|
804
|
+
function parse(link2) {
|
|
805
|
+
var _a;
|
|
806
|
+
const url = new URL(link2.trim());
|
|
807
|
+
const fragment = new URLSearchParams(url.hash.slice(1));
|
|
808
|
+
const [host, device, key] = ["h", "d", "k"].map((name) => {
|
|
809
|
+
var _a2;
|
|
810
|
+
return (_a2 = fragment.get(name)) !== null && _a2 !== undefined ? _a2 : "";
|
|
811
|
+
});
|
|
812
|
+
if (!/^https?:$/.test(url.protocol) || url.pathname !== RELAY_PAIR_PATH || fragment.get("v") !== "1" || !RELAY_ID.test(host) || !RELAY_ID.test(device) || !/^[A-Za-z0-9_-]{43}$/.test(key)) {
|
|
813
|
+
throw new Error("Not a pairing link of Ta.");
|
|
814
|
+
}
|
|
815
|
+
return { relay: url.origin, host, device, key, name: (_a = fragment.get("n")) !== null && _a !== undefined ? _a : undefined };
|
|
816
|
+
}
|
|
817
|
+
RelayPairingUtil2.parse = parse;
|
|
818
|
+
function websocketUrl(relay, path) {
|
|
819
|
+
return relay.replace(/^http/, "ws") + path;
|
|
820
|
+
}
|
|
821
|
+
RelayPairingUtil2.websocketUrl = websocketUrl;
|
|
822
|
+
})(RelayPairingUtil || (RelayPairingUtil = {}));
|
|
823
|
+
|
|
824
|
+
// ../ta-relay/esm/common/relay-socket.js
|
|
825
|
+
var CONNECTING = 0;
|
|
826
|
+
var OPEN = 1;
|
|
827
|
+
var CLOSING = 2;
|
|
828
|
+
var CLOSED = 3;
|
|
829
|
+
var HELLO_MS = 15000;
|
|
830
|
+
|
|
831
|
+
class RelaySocket {
|
|
832
|
+
constructor(pairing, options = {}) {
|
|
833
|
+
var _a;
|
|
834
|
+
this.pairing = pairing;
|
|
835
|
+
this.options = options;
|
|
836
|
+
this.onopen = null;
|
|
837
|
+
this.onmessage = null;
|
|
838
|
+
this.onclose = null;
|
|
839
|
+
this.onerror = null;
|
|
840
|
+
this.state = CONNECTING;
|
|
841
|
+
this.nonce = RelayBytes.random(16);
|
|
842
|
+
this.ponged = false;
|
|
843
|
+
this.awaitingPong = false;
|
|
844
|
+
this.receiving = Promise.resolve();
|
|
845
|
+
this.socket = new WebSocket(RelayPairingUtil.websocketUrl(pairing.relay, RELAY_CONNECT_PATH + pairing.host));
|
|
846
|
+
this.helloTimer = setTimeout(() => this.drop(RelayCloseCode.TIMEOUT, "The host did not answer."), (_a = options.helloMs) !== null && _a !== undefined ? _a : HELLO_MS);
|
|
847
|
+
this.socket.onopen = () => {
|
|
848
|
+
var _a2;
|
|
849
|
+
const hello = {
|
|
850
|
+
t: "hello",
|
|
851
|
+
v: 1,
|
|
852
|
+
device: pairing.device,
|
|
853
|
+
nonce: RelayBytes.toBase64(this.nonce),
|
|
854
|
+
...RelayDeflate.supported() ? { z: 1 } : {}
|
|
855
|
+
};
|
|
856
|
+
this.socket.send(JSON.stringify(hello));
|
|
857
|
+
this.pingTimer = setInterval(() => this.ping(), (_a2 = options.pingMs) !== null && _a2 !== undefined ? _a2 : RELAY_PING_MS);
|
|
858
|
+
};
|
|
859
|
+
this.socket.onmessage = (message) => {
|
|
860
|
+
const text = String(message.data);
|
|
861
|
+
if (text === RELAY_PONG) {
|
|
862
|
+
this.ponged = true;
|
|
863
|
+
this.awaitingPong = false;
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
this.receiving = this.receiving.then(() => this.receive(text));
|
|
867
|
+
};
|
|
868
|
+
this.socket.onerror = (event) => {
|
|
869
|
+
var _a2;
|
|
870
|
+
return (_a2 = this.onerror) === null || _a2 === undefined ? undefined : _a2.call(this, event);
|
|
871
|
+
};
|
|
872
|
+
this.socket.onclose = (event) => void this.receiving.then(() => this.finish(event.code, event.reason));
|
|
873
|
+
}
|
|
874
|
+
get readyState() {
|
|
875
|
+
return this.state;
|
|
876
|
+
}
|
|
877
|
+
get closeCode() {
|
|
878
|
+
return this.code;
|
|
879
|
+
}
|
|
880
|
+
send(text) {
|
|
881
|
+
if (this.state !== OPEN || !this.cipher) {
|
|
882
|
+
throw new Error("The connection is not open.");
|
|
883
|
+
}
|
|
884
|
+
this.cipher.seal(text).then((frame) => this.socket.readyState === OPEN && this.socket.send(frame));
|
|
885
|
+
}
|
|
886
|
+
close(code = 1000, reason = "") {
|
|
887
|
+
if (this.state === CLOSED) {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
this.state = CLOSING;
|
|
891
|
+
this.socket.close(code, reason);
|
|
892
|
+
}
|
|
893
|
+
async receive(text) {
|
|
894
|
+
var _a, _b;
|
|
895
|
+
if (this.cipher) {
|
|
896
|
+
if (this.options.proveFirst && text.startsWith("{")) {
|
|
897
|
+
this.drop(RelaySocket.REFUSED, RelaySocket.refusal(text));
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
try {
|
|
901
|
+
const data = await this.cipher.open(text);
|
|
902
|
+
(_a = this.onmessage) === null || _a === undefined || _a.call(this, { data });
|
|
903
|
+
} catch (_c) {
|
|
904
|
+
this.close(4400, "A message did not open.");
|
|
905
|
+
}
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
try {
|
|
909
|
+
const welcome = JSON.parse(text);
|
|
910
|
+
if (welcome.t !== "ready") {
|
|
911
|
+
this.drop(RelaySocket.REFUSED, this.options.proveFirst ? RelaySocket.refusal(text) : "The host refused this device.");
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
const cipher = await RelayCipher.derive(RelayBytes.fromBase64(this.pairing.key), this.nonce, RelayBytes.fromBase64(welcome.nonce), "client");
|
|
915
|
+
if (welcome.proof === undefined && this.options.proveFirst) {
|
|
916
|
+
this.socket.send(await cipher.seal(RELAY_PROOF));
|
|
917
|
+
} else if (welcome.proof === undefined || await cipher.open(welcome.proof) !== RELAY_PROOF) {
|
|
918
|
+
throw new Error("The host did not show the key.");
|
|
919
|
+
}
|
|
920
|
+
cipher.compress = welcome.z === 1 && RelayDeflate.supported();
|
|
921
|
+
this.cipher = cipher;
|
|
922
|
+
clearTimeout(this.helloTimer);
|
|
923
|
+
this.state = OPEN;
|
|
924
|
+
(_b = this.onopen) === null || _b === undefined || _b.call(this);
|
|
925
|
+
} catch (_d) {
|
|
926
|
+
this.drop(RelaySocket.REFUSED, "The host refused this device.");
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
static refusal(text) {
|
|
930
|
+
try {
|
|
931
|
+
const reason = JSON.parse(text).reason;
|
|
932
|
+
return reason === "key" || reason === "busy" ? reason : "gone";
|
|
933
|
+
} catch (_a) {
|
|
934
|
+
return "gone";
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
ping() {
|
|
938
|
+
if (this.awaitingPong && this.ponged) {
|
|
939
|
+
this.drop(RelayCloseCode.TIMEOUT, "The relay did not answer.");
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
this.awaitingPong = true;
|
|
943
|
+
if (this.socket.readyState === OPEN) {
|
|
944
|
+
this.socket.send(RELAY_PING);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
drop(code, reason) {
|
|
948
|
+
this.close(code, reason);
|
|
949
|
+
this.finish(code, reason);
|
|
950
|
+
}
|
|
951
|
+
finish(code, reason) {
|
|
952
|
+
var _a;
|
|
953
|
+
if (this.state === CLOSED) {
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
clearTimeout(this.helloTimer);
|
|
957
|
+
clearInterval(this.pingTimer);
|
|
958
|
+
this.state = CLOSED;
|
|
959
|
+
this.code = code;
|
|
960
|
+
(_a = this.onclose) === null || _a === undefined || _a.call(this, { code, reason });
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
RelaySocket.CONNECTING = CONNECTING;
|
|
964
|
+
RelaySocket.OPEN = OPEN;
|
|
965
|
+
RelaySocket.CLOSING = CLOSING;
|
|
966
|
+
RelaySocket.CLOSED = CLOSED;
|
|
967
|
+
RelaySocket.REFUSED = 4403;
|
|
968
|
+
|
|
969
|
+
// ../ta-relay/esm/common/relay-content.js
|
|
970
|
+
var encoder4 = new TextEncoder;
|
|
971
|
+
var decoder3 = new TextDecoder;
|
|
972
|
+
|
|
973
|
+
class RelayContentError extends Error {
|
|
974
|
+
constructor(reason, message) {
|
|
975
|
+
super(message);
|
|
976
|
+
this.reason = reason;
|
|
977
|
+
this.name = "RelayContentError";
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
var PBKDF2_ITERATIONS = 600000;
|
|
981
|
+
var BLOCKED_MESSAGE = "The operator of the relay blocked this link after a report.";
|
|
982
|
+
var PLAIN_KINDS = new Set(["ta-conversation", "file"]);
|
|
983
|
+
var KIND = /^[a-z0-9][a-z0-9-]{0,31}$/;
|
|
984
|
+
var RelayContentUtil;
|
|
985
|
+
(function(RelayContentUtil2) {
|
|
986
|
+
function link(content) {
|
|
987
|
+
var _a, _b;
|
|
988
|
+
const fragment = new URLSearchParams({ v: "1", h: content.host, s: content.share });
|
|
989
|
+
if (content.access === "self") {
|
|
990
|
+
fragment.set("m", "self");
|
|
991
|
+
} else if (content.access === "people") {
|
|
992
|
+
fragment.set("m", "people");
|
|
993
|
+
fragment.set("o", (_a = content.owner) !== null && _a !== undefined ? _a : "");
|
|
994
|
+
} else if (content.key) {
|
|
995
|
+
fragment.set("k", content.key);
|
|
996
|
+
}
|
|
997
|
+
if (content.access === "password") {
|
|
998
|
+
fragment.set("p", "1");
|
|
999
|
+
}
|
|
1000
|
+
if (content.offline && content.access !== "self" && content.access !== "people") {
|
|
1001
|
+
fragment.set("o", "1");
|
|
1002
|
+
}
|
|
1003
|
+
if (content.kind && !PLAIN_KINDS.has(content.kind) && KIND.test(content.kind)) {
|
|
1004
|
+
fragment.set("c", content.kind);
|
|
1005
|
+
}
|
|
1006
|
+
return `${(_b = content.page) !== null && _b !== undefined ? _b : content.relay}${RELAY_CONTENT_PATH}#${fragment}`;
|
|
1007
|
+
}
|
|
1008
|
+
RelayContentUtil2.link = link;
|
|
1009
|
+
function is(text) {
|
|
1010
|
+
try {
|
|
1011
|
+
parse(text);
|
|
1012
|
+
return true;
|
|
1013
|
+
} catch (_a) {
|
|
1014
|
+
return false;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
RelayContentUtil2.is = is;
|
|
1018
|
+
function parse(text) {
|
|
1019
|
+
var _a, _b;
|
|
1020
|
+
const url = new URL(text.trim());
|
|
1021
|
+
const fragment = new URLSearchParams(url.hash.slice(1));
|
|
1022
|
+
const [host, share, key] = ["h", "s", "k"].map((name) => {
|
|
1023
|
+
var _a2;
|
|
1024
|
+
return (_a2 = fragment.get(name)) !== null && _a2 !== undefined ? _a2 : "";
|
|
1025
|
+
});
|
|
1026
|
+
const self = fragment.get("m") === "self";
|
|
1027
|
+
const people = fragment.get("m") === "people";
|
|
1028
|
+
const owner = (_a = fragment.get("o")) !== null && _a !== undefined ? _a : "";
|
|
1029
|
+
if (!/^https?:$/.test(url.protocol) || url.pathname !== RELAY_CONTENT_PATH || fragment.get("v") !== "1" || !RELAY_ID.test(host) || !RELAY_ID.test(share) || (people ? !/^[A-Za-z0-9_-]{22}$/.test(owner) : !self && !/^[A-Za-z0-9_-]{43}$/.test(key))) {
|
|
1030
|
+
throw new RelayContentError("invalid", "Not a link of something shared from Ta.");
|
|
1031
|
+
}
|
|
1032
|
+
const kind = (_b = fragment.get("c")) !== null && _b !== undefined ? _b : "";
|
|
1033
|
+
if (people) {
|
|
1034
|
+
return { relay: url.origin, host, share, access: "people", owner, offline: false, ...KIND.test(kind) ? { kind } : {} };
|
|
1035
|
+
}
|
|
1036
|
+
return {
|
|
1037
|
+
relay: url.origin,
|
|
1038
|
+
host,
|
|
1039
|
+
share,
|
|
1040
|
+
key: self ? undefined : key,
|
|
1041
|
+
access: self ? "self" : fragment.get("p") === "1" ? "password" : "link",
|
|
1042
|
+
offline: !self && fragment.get("o") === "1",
|
|
1043
|
+
...KIND.test(kind) ? { kind } : {}
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
RelayContentUtil2.parse = parse;
|
|
1047
|
+
async function contentKey(linkKey, share, password) {
|
|
1048
|
+
if (password === undefined) {
|
|
1049
|
+
return linkKey;
|
|
1050
|
+
}
|
|
1051
|
+
const subtle2 = globalThis.crypto.subtle;
|
|
1052
|
+
const secret = await subtle2.importKey("raw", encoder4.encode(password.normalize("NFC")), "PBKDF2", false, ["deriveBits"]);
|
|
1053
|
+
const stretched = await subtle2.deriveBits({ name: "PBKDF2", hash: "SHA-256", salt: encoder4.encode(`ta-share:${share}`), iterations: PBKDF2_ITERATIONS }, secret, 256);
|
|
1054
|
+
return hkdf(linkKey, new Uint8Array(stretched), "ta-share/1 password");
|
|
1055
|
+
}
|
|
1056
|
+
RelayContentUtil2.contentKey = contentKey;
|
|
1057
|
+
function peopleKey(identity, otherBox, share) {
|
|
1058
|
+
return RelayIdentityUtil.agree(identity, otherBox, share, "ta-share/1 people");
|
|
1059
|
+
}
|
|
1060
|
+
RelayContentUtil2.peopleKey = peopleKey;
|
|
1061
|
+
async function authToken(key) {
|
|
1062
|
+
return RelayBytes.toBase64(await hkdf(key, new Uint8Array(0), "ta-share/1 auth"));
|
|
1063
|
+
}
|
|
1064
|
+
RelayContentUtil2.authToken = authToken;
|
|
1065
|
+
async function sha256(text) {
|
|
1066
|
+
return RelayBytes.toBase64(new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", encoder4.encode(text))));
|
|
1067
|
+
}
|
|
1068
|
+
RelayContentUtil2.sha256 = sha256;
|
|
1069
|
+
async function seal(key, item) {
|
|
1070
|
+
const iv = RelayBytes.random(12);
|
|
1071
|
+
const sealed = await globalThis.crypto.subtle.encrypt({ name: "AES-GCM", iv }, await blobKey(key), encode(item));
|
|
1072
|
+
return RelayBytes.concat(iv, new Uint8Array(sealed));
|
|
1073
|
+
}
|
|
1074
|
+
RelayContentUtil2.seal = seal;
|
|
1075
|
+
async function open(key, bytes) {
|
|
1076
|
+
const opened = await globalThis.crypto.subtle.decrypt({ name: "AES-GCM", iv: bytes.subarray(0, 12) }, await blobKey(key), bytes.subarray(12));
|
|
1077
|
+
return decode(new Uint8Array(opened));
|
|
1078
|
+
}
|
|
1079
|
+
RelayContentUtil2.open = open;
|
|
1080
|
+
function encode(item) {
|
|
1081
|
+
const { data, ...header } = item;
|
|
1082
|
+
const head = encoder4.encode(JSON.stringify(header));
|
|
1083
|
+
const length = new Uint8Array(4);
|
|
1084
|
+
new DataView(length.buffer).setUint32(0, head.length);
|
|
1085
|
+
return RelayBytes.concat(length, head, data);
|
|
1086
|
+
}
|
|
1087
|
+
RelayContentUtil2.encode = encode;
|
|
1088
|
+
function decode(bytes) {
|
|
1089
|
+
const length = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0);
|
|
1090
|
+
const header = JSON.parse(decoder3.decode(bytes.subarray(4, 4 + length)));
|
|
1091
|
+
return {
|
|
1092
|
+
kind: String(header.kind),
|
|
1093
|
+
title: String(header.title),
|
|
1094
|
+
createdAt: Number(header.createdAt),
|
|
1095
|
+
type: header.type,
|
|
1096
|
+
data: bytes.slice(4 + length)
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
RelayContentUtil2.decode = decode;
|
|
1100
|
+
async function fetch2(text, options = {}) {
|
|
1101
|
+
const link2 = typeof text === "string" ? parse(text) : text;
|
|
1102
|
+
if (link2.access === "people") {
|
|
1103
|
+
if (!options.people) {
|
|
1104
|
+
throw new RelayContentError("people", "Only the Ta friends it was shared with open it, in their own Ta.");
|
|
1105
|
+
}
|
|
1106
|
+
return fromHost(link2, await peopleKey(options.people.identity, options.people.ownerBox, link2.share), options);
|
|
1107
|
+
}
|
|
1108
|
+
if (link2.access === "self" || !link2.key) {
|
|
1109
|
+
throw new RelayContentError("self", "Only the one who shared it opens it, in their own Ta.");
|
|
1110
|
+
}
|
|
1111
|
+
if (link2.access === "password" && !options.password) {
|
|
1112
|
+
throw new RelayContentError("password", "It is shared with a password.");
|
|
1113
|
+
}
|
|
1114
|
+
const key = await contentKey(RelayBytes.fromBase64(link2.key), link2.share, link2.access === "password" ? options.password : undefined);
|
|
1115
|
+
if (link2.offline) {
|
|
1116
|
+
const copy = await fromRelay(link2, key);
|
|
1117
|
+
if (copy) {
|
|
1118
|
+
return copy;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
return fromHost(link2, key, options);
|
|
1122
|
+
}
|
|
1123
|
+
RelayContentUtil2.fetch = fetch2;
|
|
1124
|
+
async function fromRelay(link2, key) {
|
|
1125
|
+
let response;
|
|
1126
|
+
try {
|
|
1127
|
+
response = await globalThis.fetch(`${link2.relay}${RELAY_BLOB_PATH}${link2.share}`, { headers: { "x-ta-auth": await authToken(key) } });
|
|
1128
|
+
} catch (_a) {
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
if (response.status === 403) {
|
|
1132
|
+
throw link2.access === "password" ? new RelayContentError("wrong", "The password is wrong.") : new RelayContentError("gone", "This link no longer works.");
|
|
1133
|
+
}
|
|
1134
|
+
if (response.status === 410 || response.status === 451) {
|
|
1135
|
+
throw new RelayContentError("gone", BLOCKED_MESSAGE);
|
|
1136
|
+
}
|
|
1137
|
+
if (response.status === 429) {
|
|
1138
|
+
throw new RelayContentError("busy", "Too many wrong passwords: try again in a few minutes.");
|
|
1139
|
+
}
|
|
1140
|
+
if (!response.ok) {
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
try {
|
|
1144
|
+
return await open(key, new Uint8Array(await response.arrayBuffer()));
|
|
1145
|
+
} catch (_b) {
|
|
1146
|
+
throw new RelayContentError("wrong", "The password is wrong.");
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
function fromHost(link2, key, options) {
|
|
1150
|
+
var _a;
|
|
1151
|
+
const max = (_a = options.maxBytes) !== null && _a !== undefined ? _a : RELAY_CONTENT_MAX_BYTES;
|
|
1152
|
+
const pairing = { relay: link2.relay, host: link2.host, device: link2.share, key: RelayBytes.toBase64(key) };
|
|
1153
|
+
return new Promise((resolve, reject) => {
|
|
1154
|
+
var _a2;
|
|
1155
|
+
const socket = new RelaySocket(pairing, { proveFirst: true });
|
|
1156
|
+
const chunks = [];
|
|
1157
|
+
let received = 0;
|
|
1158
|
+
let header;
|
|
1159
|
+
let done = false;
|
|
1160
|
+
const finish = (error, item) => {
|
|
1161
|
+
if (done) {
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
done = true;
|
|
1165
|
+
clearTimeout(timer);
|
|
1166
|
+
socket.onclose = null;
|
|
1167
|
+
socket.close();
|
|
1168
|
+
if (item) {
|
|
1169
|
+
resolve(item);
|
|
1170
|
+
} else {
|
|
1171
|
+
reject(error);
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
const timer = setTimeout(() => finish(new RelayContentError("timeout", "The computer that shared it took too long to send it.")), (_a2 = options.timeoutMs) !== null && _a2 !== undefined ? _a2 : 60000);
|
|
1175
|
+
socket.onmessage = ({ data }) => {
|
|
1176
|
+
let message;
|
|
1177
|
+
try {
|
|
1178
|
+
message = JSON.parse(data);
|
|
1179
|
+
} catch (_a3) {
|
|
1180
|
+
finish(new RelayContentError("invalid", "The computer that shared it sent what is not content."));
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
if (message.t === "error") {
|
|
1184
|
+
finish(new RelayContentError("gone", message.message));
|
|
1185
|
+
} else if (message.t === "content") {
|
|
1186
|
+
if (!(message.size >= 0) || message.size > max) {
|
|
1187
|
+
finish(new RelayContentError("large", `It is larger than ${Math.round(max / 1024 / 1024)} MB.`));
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
header = {
|
|
1191
|
+
kind: String(message.kind),
|
|
1192
|
+
title: String(message.title),
|
|
1193
|
+
createdAt: Number(message.createdAt),
|
|
1194
|
+
type: message.type,
|
|
1195
|
+
size: message.size
|
|
1196
|
+
};
|
|
1197
|
+
} else if (message.t === "chunk" && header) {
|
|
1198
|
+
const bytes = RelayBytes.fromBase64(message.d);
|
|
1199
|
+
received += bytes.length;
|
|
1200
|
+
if (received > header.size) {
|
|
1201
|
+
finish(new RelayContentError("invalid", "It is larger than it said."));
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
chunks.push(bytes);
|
|
1205
|
+
} else if (message.t === "end" && header) {
|
|
1206
|
+
if (received !== header.size) {
|
|
1207
|
+
finish(new RelayContentError("invalid", "It came short."));
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
const { size: _size, ...rest } = header;
|
|
1211
|
+
finish(undefined, { ...rest, data: RelayBytes.concat(...chunks) });
|
|
1212
|
+
}
|
|
1213
|
+
};
|
|
1214
|
+
socket.onclose = ({ code, reason }) => finish(closed(code, reason, link2));
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
function closed(code, reason, link2) {
|
|
1218
|
+
if (code === RelaySocket.REFUSED) {
|
|
1219
|
+
if (reason === "key") {
|
|
1220
|
+
return link2.access === "password" ? new RelayContentError("wrong", "The password is wrong.") : new RelayContentError("gone", "This link no longer works.");
|
|
1221
|
+
}
|
|
1222
|
+
if (reason === "busy") {
|
|
1223
|
+
return new RelayContentError("busy", "Too many wrong passwords: try again in a few minutes.");
|
|
1224
|
+
}
|
|
1225
|
+
return new RelayContentError("gone", "This link no longer works: the share was stopped or ran out.");
|
|
1226
|
+
}
|
|
1227
|
+
if (code === RelayCloseCode.BLOCKED) {
|
|
1228
|
+
return new RelayContentError("gone", BLOCKED_MESSAGE);
|
|
1229
|
+
}
|
|
1230
|
+
if (code === RelayCloseCode.SIGN_IN) {
|
|
1231
|
+
return new RelayContentError("gone", "The one who shared it has not signed in to an account, which this relay needs of public links.");
|
|
1232
|
+
}
|
|
1233
|
+
return code === RelayCloseCode.HOST_OFFLINE || code === RelayCloseCode.TIMEOUT ? new RelayContentError("offline", "The computer that shared it is not running Ta now. Try again later.") : new RelayContentError("offline", "The computer that shared it cannot be reached now. Try again later.");
|
|
1234
|
+
}
|
|
1235
|
+
async function blobKey(key) {
|
|
1236
|
+
const bytes = await hkdf(key, new Uint8Array(0), "ta-share/1 blob");
|
|
1237
|
+
return globalThis.crypto.subtle.importKey("raw", bytes, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
1238
|
+
}
|
|
1239
|
+
async function hkdf(key, salt, info) {
|
|
1240
|
+
const subtle2 = globalThis.crypto.subtle;
|
|
1241
|
+
const secret = await subtle2.importKey("raw", key, "HKDF", false, ["deriveBits"]);
|
|
1242
|
+
const bits = await subtle2.deriveBits({ name: "HKDF", hash: "SHA-256", salt, info: encoder4.encode(info) }, secret, 256);
|
|
1243
|
+
return new Uint8Array(bits);
|
|
1244
|
+
}
|
|
1245
|
+
})(RelayContentUtil || (RelayContentUtil = {}));
|
|
1246
|
+
var RelayContentHandoff;
|
|
1247
|
+
(function(RelayContentHandoff2) {
|
|
1248
|
+
const KEY = "ta.shared";
|
|
1249
|
+
function keep(share, item, password) {
|
|
1250
|
+
const { data, ...header } = item;
|
|
1251
|
+
const store = (kept) => {
|
|
1252
|
+
var _a;
|
|
1253
|
+
return (_a = globalThis.sessionStorage) === null || _a === undefined ? undefined : _a.setItem(KEY, JSON.stringify(kept));
|
|
1254
|
+
};
|
|
1255
|
+
try {
|
|
1256
|
+
store({ share, ...password === undefined ? {} : { password }, item: { ...header, data: RelayBytes.toBase64(data) } });
|
|
1257
|
+
} catch (_a) {
|
|
1258
|
+
try {
|
|
1259
|
+
store({ share, ...password === undefined ? {} : { password } });
|
|
1260
|
+
} catch (_b) {}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
RelayContentHandoff2.keep = keep;
|
|
1264
|
+
function take(share) {
|
|
1265
|
+
var _a;
|
|
1266
|
+
var _b;
|
|
1267
|
+
try {
|
|
1268
|
+
const kept = JSON.parse((_b = (_a = globalThis.sessionStorage) === null || _a === undefined ? undefined : _a.getItem(KEY)) !== null && _b !== undefined ? _b : "null");
|
|
1269
|
+
if ((kept === null || kept === undefined ? undefined : kept.share) !== share) {
|
|
1270
|
+
return {};
|
|
1271
|
+
}
|
|
1272
|
+
const item = kept.item && { ...kept.item, data: RelayBytes.fromBase64(kept.item.data) };
|
|
1273
|
+
return { ...item ? { item } : {}, ...kept.password === undefined ? {} : { password: kept.password } };
|
|
1274
|
+
} catch (_c) {
|
|
1275
|
+
return {};
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
RelayContentHandoff2.take = take;
|
|
1279
|
+
})(RelayContentHandoff || (RelayContentHandoff = {}));
|
|
1280
|
+
|
|
1281
|
+
// ../ta-relay/esm/common/relay-report.js
|
|
1282
|
+
var MAX_NOTE = 500;
|
|
1283
|
+
var RelayReportUtil;
|
|
1284
|
+
(function(RelayReportUtil2) {
|
|
1285
|
+
function parse(value) {
|
|
1286
|
+
const given = value;
|
|
1287
|
+
if (!given || typeof given.host !== "string" || typeof given.share !== "string" || !RELAY_ID.test(given.host) || !RELAY_ID.test(given.share) || !RELAY_REPORT_REASONS.includes(given.reason)) {
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
const note = typeof given.note === "string" ? given.note.trim().slice(0, MAX_NOTE) : "";
|
|
1291
|
+
const link = linkOf(given.link, given.host, given.share);
|
|
1292
|
+
return { host: given.host, share: given.share, reason: given.reason, ...note ? { note } : {}, ...link ? { link } : {} };
|
|
1293
|
+
}
|
|
1294
|
+
RelayReportUtil2.parse = parse;
|
|
1295
|
+
function linkOf(value, host, share) {
|
|
1296
|
+
if (typeof value !== "string" || value.length > 2048) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
try {
|
|
1300
|
+
const link = RelayContentUtil.parse(value);
|
|
1301
|
+
return link.host === host && link.share === share && link.access !== "self" && link.access !== "people" ? value.trim() : undefined;
|
|
1302
|
+
} catch (_a) {
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
RelayReportUtil2.linkOf = linkOf;
|
|
1307
|
+
})(RelayReportUtil || (RelayReportUtil = {}));
|
|
1308
|
+
|
|
1309
|
+
// ../ta-relay/esm/common/relay-moderation.js
|
|
1310
|
+
var HOUR_MS4 = 60 * 60 * 1000;
|
|
1311
|
+
var DAY_MS6 = 24 * HOUR_MS4;
|
|
1312
|
+
|
|
1313
|
+
// ../ta-relay/esm/common/relay-moderation-client.js
|
|
1314
|
+
async function call(url, token, init) {
|
|
1315
|
+
var _a, _b;
|
|
1316
|
+
const response = await fetch(url, {
|
|
1317
|
+
...init,
|
|
1318
|
+
headers: { authorization: `Bearer ${token}`, ...(init === null || init === undefined ? undefined : init.body) ? { "content-type": "application/json" } : {} }
|
|
1319
|
+
});
|
|
1320
|
+
const body = await response.json().catch(() => {
|
|
1321
|
+
return;
|
|
1322
|
+
});
|
|
1323
|
+
if (response.status === 401) {
|
|
1324
|
+
throw new Error((_a = body === null || body === undefined ? undefined : body.error) !== null && _a !== undefined ? _a : "The token is wrong.");
|
|
1325
|
+
}
|
|
1326
|
+
if (response.status === 404) {
|
|
1327
|
+
throw new Error("This relay does not answer it.");
|
|
1328
|
+
}
|
|
1329
|
+
if (response.status === 429) {
|
|
1330
|
+
throw new Error("429: past the quota of today.");
|
|
1331
|
+
}
|
|
1332
|
+
if (!response.ok || !body) {
|
|
1333
|
+
throw new Error((_b = body === null || body === undefined ? undefined : body.error) !== null && _b !== undefined ? _b : `The relay answered ${response.status}.`);
|
|
1334
|
+
}
|
|
1335
|
+
return body;
|
|
1336
|
+
}
|
|
1337
|
+
var base = (relay) => relay.replace(/\/+$/, "");
|
|
1338
|
+
var RelayModerationClient;
|
|
1339
|
+
(function(RelayModerationClient2) {
|
|
1340
|
+
function read(relay, token) {
|
|
1341
|
+
return call(`${base(relay)}${RELAY_MODERATION_PATH}`, token);
|
|
1342
|
+
}
|
|
1343
|
+
RelayModerationClient2.read = read;
|
|
1344
|
+
function apply(relay, token, action) {
|
|
1345
|
+
return call(`${base(relay)}${RELAY_MODERATION_PATH}`, token, { method: "POST", body: JSON.stringify(action) });
|
|
1346
|
+
}
|
|
1347
|
+
RelayModerationClient2.apply = apply;
|
|
1348
|
+
})(RelayModerationClient || (RelayModerationClient = {}));
|
|
1349
|
+
var RelayStandingClient;
|
|
1350
|
+
(function(RelayStandingClient2) {
|
|
1351
|
+
function bind(relay, token, host) {
|
|
1352
|
+
return call(`${base(relay)}${RELAY_BIND_PATH}`, token, { method: "POST", body: JSON.stringify({ host: host.id, secret: host.secret }) });
|
|
1353
|
+
}
|
|
1354
|
+
RelayStandingClient2.bind = bind;
|
|
1355
|
+
function standing(relay, token) {
|
|
1356
|
+
return call(`${base(relay)}${RELAY_STANDING_PATH}`, token);
|
|
1357
|
+
}
|
|
1358
|
+
RelayStandingClient2.standing = standing;
|
|
1359
|
+
function ticket(relay, token) {
|
|
1360
|
+
return call(`${base(relay)}${RELAY_TICKET_PATH}`, token, { method: "POST", body: "{}" });
|
|
1361
|
+
}
|
|
1362
|
+
RelayStandingClient2.ticket = ticket;
|
|
1363
|
+
function quota(relay, token) {
|
|
1364
|
+
return call(`${base(relay)}${RELAY_QUOTA_PATH}`, token, { method: "POST", body: "{}" });
|
|
1365
|
+
}
|
|
1366
|
+
RelayStandingClient2.quota = quota;
|
|
1367
|
+
function appeal(relay, token, text) {
|
|
1368
|
+
return call(`${base(relay)}${RELAY_APPEAL_PATH}`, token, { method: "POST", body: JSON.stringify({ text }) });
|
|
1369
|
+
}
|
|
1370
|
+
RelayStandingClient2.appeal = appeal;
|
|
1371
|
+
})(RelayStandingClient || (RelayStandingClient = {}));
|
|
1372
|
+
|
|
1373
|
+
// ../ta-protocol/esm/common/language.js
|
|
1374
|
+
var TaLanguageUtil;
|
|
1375
|
+
(function(TaLanguageUtil2) {
|
|
1376
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
1377
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
1378
|
+
function of(tag) {
|
|
1379
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
1380
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
1381
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
1382
|
+
}
|
|
1383
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
1384
|
+
}
|
|
1385
|
+
TaLanguageUtil2.of = of;
|
|
1386
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
1387
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
1388
|
+
}
|
|
1389
|
+
TaLanguageUtil2.server = server;
|
|
1390
|
+
function pick(language, words) {
|
|
1391
|
+
return words[of(language)];
|
|
1392
|
+
}
|
|
1393
|
+
TaLanguageUtil2.pick = pick;
|
|
1394
|
+
function say(words) {
|
|
1395
|
+
return words[server()];
|
|
1396
|
+
}
|
|
1397
|
+
TaLanguageUtil2.say = say;
|
|
1398
|
+
function chinese(language) {
|
|
1399
|
+
return of(language).startsWith("zh");
|
|
1400
|
+
}
|
|
1401
|
+
TaLanguageUtil2.chinese = chinese;
|
|
1402
|
+
function cjk(language) {
|
|
1403
|
+
return of(language) !== "en-US";
|
|
1404
|
+
}
|
|
1405
|
+
TaLanguageUtil2.cjk = cjk;
|
|
1406
|
+
function list(language, items) {
|
|
1407
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
1408
|
+
}
|
|
1409
|
+
TaLanguageUtil2.list = list;
|
|
1410
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
1411
|
+
|
|
1412
|
+
// web-src/server-texts.ts
|
|
1413
|
+
var REASONS = {
|
|
1414
|
+
"zh-CN": { phishing: "钓鱼或诈骗", malware: "病毒或恶意软件", sexual: "色情内容", illegal: "违法内容", harassment: "骚扰或侵权", other: "其他" },
|
|
1415
|
+
"zh-TW": { phishing: "釣魚或詐騙", malware: "病毒或惡意軟體", sexual: "色情內容", illegal: "違法內容", harassment: "騷擾或侵權", other: "其他" },
|
|
1416
|
+
"en-US": {
|
|
1417
|
+
phishing: "Phishing or scam",
|
|
1418
|
+
malware: "Virus or malware",
|
|
1419
|
+
sexual: "Sexual content",
|
|
1420
|
+
illegal: "Illegal content",
|
|
1421
|
+
harassment: "Harassment or infringement",
|
|
1422
|
+
other: "Other"
|
|
1423
|
+
},
|
|
1424
|
+
"ja-JP": {
|
|
1425
|
+
phishing: "フィッシング・詐欺",
|
|
1426
|
+
malware: "ウイルス・マルウェア",
|
|
1427
|
+
sexual: "性的なコンテンツ",
|
|
1428
|
+
illegal: "違法なコンテンツ",
|
|
1429
|
+
harassment: "嫌がらせ・権利侵害",
|
|
1430
|
+
other: "その他"
|
|
1431
|
+
}
|
|
1432
|
+
};
|
|
1433
|
+
var ShareServerTexts;
|
|
1434
|
+
((ShareServerTexts) => {
|
|
1435
|
+
function language(env = process.env) {
|
|
1436
|
+
return TaLanguageUtil.of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || "");
|
|
1437
|
+
}
|
|
1438
|
+
ShareServerTexts.language = language;
|
|
1439
|
+
function access(access2, lang) {
|
|
1440
|
+
return TaLanguageUtil.pick(lang, {
|
|
1441
|
+
"zh-CN": access2 === "password" ? "知道口令的人" : access2 === "self" ? "只有你自己" : "拿到链接的任何人",
|
|
1442
|
+
"zh-TW": access2 === "password" ? "知道密碼的人" : access2 === "self" ? "只有你自己" : "拿到連結的任何人",
|
|
1443
|
+
"en-US": access2 === "password" ? "whoever has the password" : access2 === "self" ? "only you" : "anyone with the link",
|
|
1444
|
+
"ja-JP": access2 === "password" ? "パスワードを持っている人" : access2 === "self" ? "あなただけ" : "リンクを持つ誰でも"
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
function days(days2, lang) {
|
|
1448
|
+
return TaLanguageUtil.pick(lang, {
|
|
1449
|
+
"zh-CN": days2 > 0 ? `${days2} 天内有效` : "停止分享前一直有效",
|
|
1450
|
+
"zh-TW": days2 > 0 ? `${days2} 天內有效` : "停止分享前一直有效",
|
|
1451
|
+
"en-US": days2 > 0 ? `for ${days2} day${days2 === 1 ? "" : "s"}` : "until you stop it",
|
|
1452
|
+
"ja-JP": days2 > 0 ? `${days2}日間有効` : "停止するまで有効"
|
|
1453
|
+
});
|
|
1454
|
+
}
|
|
1455
|
+
function confirm(what, options) {
|
|
1456
|
+
const lang = language();
|
|
1457
|
+
const offlineText = options.offline ? TaLanguageUtil.pick(lang, {
|
|
1458
|
+
"zh-CN": "另在中继上存一份加密副本,电脑关机也能看。",
|
|
1459
|
+
"zh-TW": "另在中繼上存一份加密副本,電腦關機也能看。",
|
|
1460
|
+
"en-US": "A sealed copy is kept on the relay, to open while this computer is off.",
|
|
1461
|
+
"ja-JP": "リレーに暗号化されたコピーを保存し、コンピュータがオフでも開くことができます。"
|
|
1462
|
+
}) : TaLanguageUtil.pick(lang, {
|
|
1463
|
+
"zh-CN": "只在这台电脑运行 Ta 时能打开。",
|
|
1464
|
+
"zh-TW": "只在這台電腦執行 Ta 時能開啟。",
|
|
1465
|
+
"en-US": "It opens while this computer runs Ta.",
|
|
1466
|
+
"ja-JP": "このコンピュータで Ta を実行している間に開きます。"
|
|
1467
|
+
});
|
|
1468
|
+
return TaLanguageUtil.pick(lang, {
|
|
1469
|
+
"zh-CN": [
|
|
1470
|
+
`分享“${what}”?`,
|
|
1471
|
+
`${access(options.access, lang)}可以查看,${days(options.days, lang)}。${offlineText}看起来像密钥的内容会被隐去。可以在“发现 → 我的分享”里随时停止。`
|
|
1472
|
+
],
|
|
1473
|
+
"zh-TW": [
|
|
1474
|
+
`分享「${what}」?`,
|
|
1475
|
+
`${access(options.access, lang)}可以檢視,${days(options.days, lang)}。${offlineText}看起來像金鑰的內容會被隱去。可以在「發現 → 我的分享」裡隨時停止。`
|
|
1476
|
+
],
|
|
1477
|
+
"en-US": [
|
|
1478
|
+
`Share ${what}?`,
|
|
1479
|
+
`${access(options.access, lang)} can see it, ${days(options.days, lang)}. ${offlineText} What looks like a secret is hidden. Stop it any time in Discover → My shares.`
|
|
1480
|
+
],
|
|
1481
|
+
"ja-JP": [
|
|
1482
|
+
`${what}を共有しますか?`,
|
|
1483
|
+
`${access(options.access, lang)}はこれを見ることができます。${days(options.days, lang)}。${offlineText}秘密に見える内容は隠されます。見つける → 私の共有でいつでも停止できます。`
|
|
1484
|
+
]
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
ShareServerTexts.confirm = confirm;
|
|
1488
|
+
function publicConfirm(port, minutes) {
|
|
1489
|
+
const hours = minutes % 60 === 0 ? minutes / 60 : undefined;
|
|
1490
|
+
return TaLanguageUtil.say({
|
|
1491
|
+
"zh-CN": [
|
|
1492
|
+
`公开本机服务 localhost:${port}?`,
|
|
1493
|
+
`拿到链接的任何人都能访问这个服务,${hours ? `${hours} 小时` : `${minutes} 分钟`}后自动停止,关掉 Ta 也会停止。链接经 Cloudflare 转发,域名是随机的 trycloudflare.com。只公开你愿意给别人看的服务;公司网络通常禁止这样做。`
|
|
1494
|
+
],
|
|
1495
|
+
"zh-TW": [
|
|
1496
|
+
`公開本機服務 localhost:${port}?`,
|
|
1497
|
+
`拿到連結的任何人都能存取這個服務,${hours ? `${hours} 小時` : `${minutes} 分鐘`}後自動停止,關掉 Ta 也會停止。連結經 Cloudflare 轉發,網域是隨機的 trycloudflare.com。只公開你願意給別人看的服務;公司網路通常禁止這樣做。`
|
|
1498
|
+
],
|
|
1499
|
+
"en-US": [
|
|
1500
|
+
`Make localhost:${port} public?`,
|
|
1501
|
+
`Anyone with the link can reach this service, for ${hours ? `${hours} hour${hours === 1 ? "" : "s"}` : `${minutes} minutes`} or until Ta stops. It goes through Cloudflare, on a random domain of trycloudflare.com. Make public only what you would show anyone; company networks usually forbid it.`
|
|
1502
|
+
],
|
|
1503
|
+
"ja-JP": [
|
|
1504
|
+
`localhost:${port} を公開しますか?`,
|
|
1505
|
+
`リンクを持つ誰でもこのサービスにアクセスできます。${hours ? `${hours} 時間` : `${minutes} 分`}後、または Ta を閉じると停止します。Cloudflare 経由で、ランダムな trycloudflare.com のドメインになります。誰に見られてもよいものだけを公開してください。会社のネットワークでは通常禁止されています。`
|
|
1506
|
+
]
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1509
|
+
ShareServerTexts.publicConfirm = publicConfirm;
|
|
1510
|
+
function reportsTitle() {
|
|
1511
|
+
return TaLanguageUtil.say({ "zh-CN": "分享被举报", "zh-TW": "分享被檢舉", "en-US": "A share was reported", "ja-JP": "共有が報告されました" });
|
|
1512
|
+
}
|
|
1513
|
+
ShareServerTexts.reportsTitle = reportsTitle;
|
|
1514
|
+
function reported(reason, count, note) {
|
|
1515
|
+
const lang = language();
|
|
1516
|
+
const why = TaLanguageUtil.pick(lang, REASONS)[reason] ?? reason;
|
|
1517
|
+
const times = TaLanguageUtil.pick(lang, {
|
|
1518
|
+
"zh-CN": count > 1 ? `(${count} 次)` : "",
|
|
1519
|
+
"zh-TW": count > 1 ? `(${count} 次)` : "",
|
|
1520
|
+
"en-US": count > 1 ? ` (${count} times)` : "",
|
|
1521
|
+
"ja-JP": count > 1 ? `(${count} 回)` : ""
|
|
1522
|
+
});
|
|
1523
|
+
return `${why}${times}${note ? `:${note.slice(0, 120)}` : ""}`;
|
|
1524
|
+
}
|
|
1525
|
+
ShareServerTexts.reported = reported;
|
|
1526
|
+
function logged(entry) {
|
|
1527
|
+
const [kind] = entry.target.split(":");
|
|
1528
|
+
const what = TaLanguageUtil.say({
|
|
1529
|
+
"zh-CN": kind === "account" ? "一个账号" : kind === "host" ? "一台电脑" : "一条链接",
|
|
1530
|
+
"zh-TW": kind === "account" ? "一個帳號" : kind === "host" ? "一台電腦" : "一條連結",
|
|
1531
|
+
"en-US": kind === "account" ? "an account" : kind === "host" ? "a computer" : "a link",
|
|
1532
|
+
"ja-JP": kind === "account" ? "アカウント" : kind === "host" ? "コンピュータ" : "リンク"
|
|
1533
|
+
});
|
|
1534
|
+
if (entry.action === "appeal") {
|
|
1535
|
+
return TaLanguageUtil.say({
|
|
1536
|
+
"zh-CN": `收到申诉:${entry.reason ?? ""}`,
|
|
1537
|
+
"zh-TW": `收到申訴:${entry.reason ?? ""}`,
|
|
1538
|
+
"en-US": `An appeal: ${entry.reason ?? ""}`,
|
|
1539
|
+
"ja-JP": `異議申し立て:${entry.reason ?? ""}`
|
|
1540
|
+
});
|
|
1541
|
+
}
|
|
1542
|
+
const done = TaLanguageUtil.say({
|
|
1543
|
+
"zh-CN": entry.action === "limit" ? "限制了公开分享:" : "封禁了",
|
|
1544
|
+
"zh-TW": entry.action === "limit" ? "限制了公開分享:" : "封鎖了",
|
|
1545
|
+
"en-US": entry.action === "limit" ? "limited the public links of" : "blocked",
|
|
1546
|
+
"ja-JP": entry.action === "limit" ? "公開共有を制限しました:" : "ブロックしました:"
|
|
1547
|
+
});
|
|
1548
|
+
return TaLanguageUtil.say({
|
|
1549
|
+
"zh-CN": `自动规则${done}${what},误判可在举报处理里撤销。`,
|
|
1550
|
+
"zh-TW": `自動規則${done}${what},誤判可在檢舉處理裡撤銷。`,
|
|
1551
|
+
"en-US": `The rules ${done} ${what}; undo it in Reports if it was wrong.`,
|
|
1552
|
+
"ja-JP": `自動ルールが${what}を${done}誤りなら「報告の対応」で取り消せます。`
|
|
1553
|
+
});
|
|
1554
|
+
}
|
|
1555
|
+
ShareServerTexts.logged = logged;
|
|
1556
|
+
function password() {
|
|
1557
|
+
return TaLanguageUtil.say({
|
|
1558
|
+
"zh-CN": "设置查看口令",
|
|
1559
|
+
"zh-TW": "設定檢視密碼",
|
|
1560
|
+
"en-US": "A password to open it",
|
|
1561
|
+
"ja-JP": "パスワード"
|
|
1562
|
+
});
|
|
1563
|
+
}
|
|
1564
|
+
ShareServerTexts.password = password;
|
|
1565
|
+
function conversation() {
|
|
1566
|
+
return TaLanguageUtil.say({
|
|
1567
|
+
"zh-CN": "这段对话",
|
|
1568
|
+
"zh-TW": "這段對話",
|
|
1569
|
+
"en-US": "this conversation",
|
|
1570
|
+
"ja-JP": "この会話"
|
|
1571
|
+
});
|
|
1572
|
+
}
|
|
1573
|
+
ShareServerTexts.conversation = conversation;
|
|
1574
|
+
})(ShareServerTexts ||= {});
|
|
1575
|
+
|
|
5
1576
|
// web-src/share-protocol.ts
|
|
1577
|
+
var NAME = "@celljs/ta-share";
|
|
1578
|
+
var REPORTS_VIEW = "@celljs/ta-share/reports";
|
|
1579
|
+
var TUNNELS_KEY = "tunnels.enabled";
|
|
6
1580
|
var CONVERSATION_KIND = "ta-conversation";
|
|
7
1581
|
var FILE_KIND = "file";
|
|
1582
|
+
var MAX_FILE_BYTES = 8 * 1024 * 1024;
|
|
8
1583
|
var ShareLink;
|
|
9
1584
|
((ShareLink) => {
|
|
10
1585
|
const LINK = /^https?:\/\/[^/\s#?]+\/s#v=1&\S+$/i;
|
|
@@ -22,6 +1597,352 @@ var ShareLink;
|
|
|
22
1597
|
ShareLink.needsPassword = needsPassword;
|
|
23
1598
|
})(ShareLink ||= {});
|
|
24
1599
|
|
|
1600
|
+
// web-src/share-moderation.ts
|
|
1601
|
+
var OPERATOR_LOOK_MS = 15 * 60 * 1000;
|
|
1602
|
+
var TOLD_KEY = "operator.told";
|
|
1603
|
+
var TOLD_LOG_KEY = "operator.toldLog";
|
|
1604
|
+
|
|
1605
|
+
class ShareModeration {
|
|
1606
|
+
host;
|
|
1607
|
+
timer;
|
|
1608
|
+
constructor(host) {
|
|
1609
|
+
this.host = host;
|
|
1610
|
+
}
|
|
1611
|
+
start() {
|
|
1612
|
+
this.timer = setInterval(() => void this.look().catch(() => {
|
|
1613
|
+
return;
|
|
1614
|
+
}), OPERATOR_LOOK_MS);
|
|
1615
|
+
this.timer.unref?.();
|
|
1616
|
+
setTimeout(() => void this.look().catch(() => {
|
|
1617
|
+
return;
|
|
1618
|
+
}), 30000).unref?.();
|
|
1619
|
+
}
|
|
1620
|
+
dispose() {
|
|
1621
|
+
clearInterval(this.timer);
|
|
1622
|
+
}
|
|
1623
|
+
async relay() {
|
|
1624
|
+
const service = await this.host.relays.serviceFor("share");
|
|
1625
|
+
if (!service.url) {
|
|
1626
|
+
throw new Error("Links of content go through no relay of Ta here.");
|
|
1627
|
+
}
|
|
1628
|
+
return service.url.replace(/\/+$/, "");
|
|
1629
|
+
}
|
|
1630
|
+
static id(relay) {
|
|
1631
|
+
return `relay-operator:${relay}`;
|
|
1632
|
+
}
|
|
1633
|
+
async token(relay) {
|
|
1634
|
+
if (!this.host.credentials.available()) {
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
return (await this.host.credentials.get(ShareModeration.id(relay)))?.password;
|
|
1638
|
+
}
|
|
1639
|
+
async state() {
|
|
1640
|
+
const relay = await this.relay();
|
|
1641
|
+
const token = await this.token(relay);
|
|
1642
|
+
if (!token) {
|
|
1643
|
+
return { relay, operator: false };
|
|
1644
|
+
}
|
|
1645
|
+
try {
|
|
1646
|
+
return { relay, operator: true, ...await RelayModerationClient.read(relay, token) };
|
|
1647
|
+
} catch (error) {
|
|
1648
|
+
return { relay, operator: true, error: error.message };
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
async setToken(token) {
|
|
1652
|
+
const relay = await this.relay();
|
|
1653
|
+
if (token === null) {
|
|
1654
|
+
await this.host.credentials.remove(ShareModeration.id(relay));
|
|
1655
|
+
return this.state();
|
|
1656
|
+
}
|
|
1657
|
+
const given = String(token ?? "").trim();
|
|
1658
|
+
if (!given) {
|
|
1659
|
+
throw new Error("Give the token of the operator of the relay.");
|
|
1660
|
+
}
|
|
1661
|
+
if (!this.host.credentials.available()) {
|
|
1662
|
+
throw new Error("The vault of Ta is off: turn on Secrets to keep the token.");
|
|
1663
|
+
}
|
|
1664
|
+
await RelayModerationClient.read(relay, given);
|
|
1665
|
+
await this.host.credentials.set(ShareModeration.id(relay), { password: given }, { feature: "relay", name: new URL(relay).host });
|
|
1666
|
+
const state = await this.state();
|
|
1667
|
+
await this.host.setExtensionData(NAME, TOLD_KEY, ShareModeration.toldOf(state.reports ?? []));
|
|
1668
|
+
await this.host.setExtensionData(NAME, TOLD_LOG_KEY, state.log?.[0]?.at ?? 0);
|
|
1669
|
+
return state;
|
|
1670
|
+
}
|
|
1671
|
+
async act(action) {
|
|
1672
|
+
const relay = await this.relay();
|
|
1673
|
+
const token = await this.token(relay);
|
|
1674
|
+
if (!token) {
|
|
1675
|
+
throw new Error("This Ta keeps no token of the operator of the relay.");
|
|
1676
|
+
}
|
|
1677
|
+
return { relay, operator: true, ...await RelayModerationClient.apply(relay, token, action) };
|
|
1678
|
+
}
|
|
1679
|
+
async look() {
|
|
1680
|
+
const relay = await this.relay();
|
|
1681
|
+
const token = await this.token(relay);
|
|
1682
|
+
if (!token) {
|
|
1683
|
+
return [];
|
|
1684
|
+
}
|
|
1685
|
+
const state = await RelayModerationClient.read(relay, token);
|
|
1686
|
+
const told = await this.host.getExtensionData(NAME, TOLD_KEY) ?? {};
|
|
1687
|
+
const fresh = ShareModeration.fresh(state.reports, told, state.blocks.shares, state.blocks.hosts);
|
|
1688
|
+
for (const report of fresh) {
|
|
1689
|
+
await this.host.callPackageHost("@celljs/ta-notify", "notify", [
|
|
1690
|
+
{
|
|
1691
|
+
from: NAME,
|
|
1692
|
+
id: `report:${report.share}:${report.last}`,
|
|
1693
|
+
level: "ask",
|
|
1694
|
+
title: ShareServerTexts.reportsTitle(),
|
|
1695
|
+
text: ShareServerTexts.reported(report.reason, report.count, report.note),
|
|
1696
|
+
view: REPORTS_VIEW
|
|
1697
|
+
}
|
|
1698
|
+
]).catch(() => {
|
|
1699
|
+
return;
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
if (fresh.length) {
|
|
1703
|
+
await this.host.setExtensionData(NAME, TOLD_KEY, ShareModeration.toldOf(state.reports));
|
|
1704
|
+
}
|
|
1705
|
+
const toldLog = Number(await this.host.getExtensionData(NAME, TOLD_LOG_KEY)) || 0;
|
|
1706
|
+
for (const entry of ShareModeration.news(state.log, toldLog)) {
|
|
1707
|
+
await this.host.callPackageHost("@celljs/ta-notify", "notify", [
|
|
1708
|
+
{
|
|
1709
|
+
from: NAME,
|
|
1710
|
+
id: `log:${entry.at}:${entry.target}`,
|
|
1711
|
+
level: "ask",
|
|
1712
|
+
title: ShareServerTexts.reportsTitle(),
|
|
1713
|
+
text: ShareServerTexts.logged(entry),
|
|
1714
|
+
view: REPORTS_VIEW
|
|
1715
|
+
}
|
|
1716
|
+
]).catch(() => {
|
|
1717
|
+
return;
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
if ((state.log[0]?.at ?? 0) > toldLog) {
|
|
1721
|
+
await this.host.setExtensionData(NAME, TOLD_LOG_KEY, state.log[0].at);
|
|
1722
|
+
}
|
|
1723
|
+
return fresh;
|
|
1724
|
+
}
|
|
1725
|
+
static fresh(reports, told, shares, hosts) {
|
|
1726
|
+
return reports.filter((report) => RELAY_REPORT_REASONS.includes(report.reason) && report.last > (told[report.share] ?? 0) && !shares.includes(report.share) && !hosts.includes(report.host));
|
|
1727
|
+
}
|
|
1728
|
+
static news(log, since) {
|
|
1729
|
+
return log.filter((entry) => entry.at > since && (entry.by === "rule" || entry.action === "appeal")).slice(0, 10);
|
|
1730
|
+
}
|
|
1731
|
+
static toldOf(reports) {
|
|
1732
|
+
return Object.fromEntries(reports.map((report) => [report.share, report.last]));
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
// web-src/share-tunnels.ts
|
|
1737
|
+
import { spawn } from "node:child_process";
|
|
1738
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
1739
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
1740
|
+
import { delimiter, join } from "node:path";
|
|
1741
|
+
var CLOUDFLARED_VERSION = "2026.11.1";
|
|
1742
|
+
var CLOUDFLARED_FILES = {
|
|
1743
|
+
"darwin-arm64": { file: "cloudflared-darwin-arm64.tgz", sha256: "1cd7041838e5b0943378763ca86ddc0af896d1727875f36b29f5cc35eb2c4df7", tgz: true },
|
|
1744
|
+
"darwin-x64": { file: "cloudflared-darwin-amd64.tgz", sha256: "52686df40889be19803c368470bd46d5228737462f10a40380bedb6af8ed49a2", tgz: true },
|
|
1745
|
+
"linux-x64": { file: "cloudflared-linux-amd64", sha256: "97bb3b02d23a5980fc9c15a1a270e692da1923f103f920d5b16d4931d5ab55e7" },
|
|
1746
|
+
"linux-arm64": { file: "cloudflared-linux-arm64", sha256: "b155cec3e48e489cfb8e5c602769fb8c771083018d8881d68b4d18a791567c5a" },
|
|
1747
|
+
"win32-x64": { file: "cloudflared-windows-amd64.exe", sha256: "341deda3db5ec498a0482254d6c38c0b7cf01a30c37bb5dd95d2216d08955e35" }
|
|
1748
|
+
};
|
|
1749
|
+
var TUNNEL_MINUTES = 60;
|
|
1750
|
+
var TUNNEL_MAX_MINUTES = 8 * 60;
|
|
1751
|
+
var TUNNEL_MAX = 3;
|
|
1752
|
+
var URL_PATTERN = /https:\/\/[a-z0-9-]+\.trycloudflare\.com/;
|
|
1753
|
+
var START_MS = 45000;
|
|
1754
|
+
|
|
1755
|
+
class ShareTunnels {
|
|
1756
|
+
options;
|
|
1757
|
+
tunnels = new Map;
|
|
1758
|
+
fetching;
|
|
1759
|
+
constructor(options) {
|
|
1760
|
+
this.options = options;
|
|
1761
|
+
}
|
|
1762
|
+
get env() {
|
|
1763
|
+
return this.options.env ?? process.env;
|
|
1764
|
+
}
|
|
1765
|
+
now() {
|
|
1766
|
+
return (this.options.now ?? Date.now)();
|
|
1767
|
+
}
|
|
1768
|
+
get allowed() {
|
|
1769
|
+
return !/^(off|0|false|no)$/i.test(String(this.env.TA_PUBLIC_TUNNELS ?? "").trim());
|
|
1770
|
+
}
|
|
1771
|
+
async state() {
|
|
1772
|
+
return { allowed: this.allowed, enabled: this.allowed && await this.options.enabled(), tunnels: this.list() };
|
|
1773
|
+
}
|
|
1774
|
+
async setEnabled(enabled) {
|
|
1775
|
+
if (enabled && !this.allowed) {
|
|
1776
|
+
throw new Error(ShareTunnels.FORBIDDEN);
|
|
1777
|
+
}
|
|
1778
|
+
await this.options.setEnabled(!!enabled);
|
|
1779
|
+
if (!enabled) {
|
|
1780
|
+
this.stopAll();
|
|
1781
|
+
}
|
|
1782
|
+
return this.state();
|
|
1783
|
+
}
|
|
1784
|
+
list() {
|
|
1785
|
+
return [...this.tunnels.values()].map(({ child: _child, timer: _timer, ...info }) => info);
|
|
1786
|
+
}
|
|
1787
|
+
async open(port, minutes = TUNNEL_MINUTES, title) {
|
|
1788
|
+
if (!this.allowed) {
|
|
1789
|
+
throw new Error(ShareTunnels.FORBIDDEN);
|
|
1790
|
+
}
|
|
1791
|
+
if (!await this.options.enabled()) {
|
|
1792
|
+
throw new Error("Public links of services of this computer are off: the user turns them on in Discover → My shares.");
|
|
1793
|
+
}
|
|
1794
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
1795
|
+
throw new Error("Give the port of a service of this computer, 1 to 65535.");
|
|
1796
|
+
}
|
|
1797
|
+
if (port === this.servePort()) {
|
|
1798
|
+
throw new Error("That is the port of Ta itself, which is never made public: reach it from a phone through Ta instead.");
|
|
1799
|
+
}
|
|
1800
|
+
const same = [...this.tunnels.values()].find((tunnel2) => tunnel2.port === port);
|
|
1801
|
+
if (same) {
|
|
1802
|
+
return this.list().find((tunnel2) => tunnel2.id === same.id);
|
|
1803
|
+
}
|
|
1804
|
+
if (this.tunnels.size >= TUNNEL_MAX) {
|
|
1805
|
+
throw new Error(`At most ${TUNNEL_MAX} public links are open at once: stop one in Discover → My shares.`);
|
|
1806
|
+
}
|
|
1807
|
+
const kept = Math.min(Math.max(Math.round(Number(minutes) || TUNNEL_MINUTES), 1), TUNNEL_MAX_MINUTES);
|
|
1808
|
+
const { command, args } = this.options.command ? await this.options.command(port) : { command: await this.cloudflared(), args: ShareTunnels.args(port) };
|
|
1809
|
+
const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"], env: { ...this.env, TUNNEL_METRICS: "" }, windowsHide: true });
|
|
1810
|
+
const url = await new Promise((resolve, reject) => {
|
|
1811
|
+
let seen = "";
|
|
1812
|
+
const timer2 = setTimeout(() => done(new Error("Cloudflare gave no link in time: the network may not reach it.")), START_MS);
|
|
1813
|
+
const done = (error, found) => {
|
|
1814
|
+
clearTimeout(timer2);
|
|
1815
|
+
child.stdout?.off("data", read);
|
|
1816
|
+
child.stderr?.off("data", read);
|
|
1817
|
+
child.off("exit", exited);
|
|
1818
|
+
child.off("error", failed);
|
|
1819
|
+
if (found) {
|
|
1820
|
+
resolve(found);
|
|
1821
|
+
} else {
|
|
1822
|
+
child.kill();
|
|
1823
|
+
reject(error);
|
|
1824
|
+
}
|
|
1825
|
+
};
|
|
1826
|
+
const read = (data) => {
|
|
1827
|
+
seen = (seen + String(data)).slice(-8000);
|
|
1828
|
+
const found = URL_PATTERN.exec(seen)?.[0];
|
|
1829
|
+
if (found) {
|
|
1830
|
+
done(undefined, found);
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
const exited = (code) => done(new Error(`cloudflared stopped (${code ?? "killed"}): ${seen.trim().split(`
|
|
1834
|
+
`).slice(-3).join(" ")}`));
|
|
1835
|
+
const failed = (error) => done(error);
|
|
1836
|
+
child.stdout?.on("data", read);
|
|
1837
|
+
child.stderr?.on("data", read);
|
|
1838
|
+
child.once("exit", exited);
|
|
1839
|
+
child.once("error", failed);
|
|
1840
|
+
});
|
|
1841
|
+
const now = this.now();
|
|
1842
|
+
const id = randomBytes(9).toString("base64url");
|
|
1843
|
+
const timer = setTimeout(() => this.stop(id), kept * 60000);
|
|
1844
|
+
timer.unref?.();
|
|
1845
|
+
const tunnel = {
|
|
1846
|
+
id,
|
|
1847
|
+
port,
|
|
1848
|
+
url,
|
|
1849
|
+
title: String(title ?? "").trim().slice(0, 80) || `localhost:${port}`,
|
|
1850
|
+
createdAt: now,
|
|
1851
|
+
expiresAt: now + kept * 60000,
|
|
1852
|
+
child,
|
|
1853
|
+
timer
|
|
1854
|
+
};
|
|
1855
|
+
child.stdout?.resume();
|
|
1856
|
+
child.stderr?.resume();
|
|
1857
|
+
child.once("exit", () => this.forget(id));
|
|
1858
|
+
this.tunnels.set(id, tunnel);
|
|
1859
|
+
return this.list().find((each) => each.id === id);
|
|
1860
|
+
}
|
|
1861
|
+
stop(id) {
|
|
1862
|
+
const tunnel = this.tunnels.get(String(id));
|
|
1863
|
+
if (!tunnel) {
|
|
1864
|
+
return false;
|
|
1865
|
+
}
|
|
1866
|
+
this.forget(tunnel.id);
|
|
1867
|
+
tunnel.child.kill();
|
|
1868
|
+
return true;
|
|
1869
|
+
}
|
|
1870
|
+
stopAll() {
|
|
1871
|
+
for (const id of [...this.tunnels.keys()]) {
|
|
1872
|
+
this.stop(id);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
forget(id) {
|
|
1876
|
+
const tunnel = this.tunnels.get(id);
|
|
1877
|
+
if (tunnel) {
|
|
1878
|
+
clearTimeout(tunnel.timer);
|
|
1879
|
+
this.tunnels.delete(id);
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
servePort() {
|
|
1883
|
+
try {
|
|
1884
|
+
const port = JSON.parse(readFileSync(join(this.options.agentDir, "serve-state.json"), "utf8")).port;
|
|
1885
|
+
return typeof port === "number" ? port : undefined;
|
|
1886
|
+
} catch {
|
|
1887
|
+
return;
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
static args(port) {
|
|
1891
|
+
return ["tunnel", "--no-autoupdate", "--url", `http://localhost:${port}`];
|
|
1892
|
+
}
|
|
1893
|
+
static FORBIDDEN = "Public links of services of this computer are turned off for this computer (TA_PUBLIC_TUNNELS=off).";
|
|
1894
|
+
cloudflared() {
|
|
1895
|
+
const name = process.platform === "win32" ? "cloudflared.exe" : "cloudflared";
|
|
1896
|
+
for (const dir of String(this.env.PATH ?? "").split(delimiter)) {
|
|
1897
|
+
if (dir && existsSync(join(dir, name))) {
|
|
1898
|
+
return Promise.resolve(join(dir, name));
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
const kept = join(this.options.agentDir, "bin", `${name.replace(/\.exe$/, "")}-${CLOUDFLARED_VERSION}${name.endsWith(".exe") ? ".exe" : ""}`);
|
|
1902
|
+
if (existsSync(kept)) {
|
|
1903
|
+
return Promise.resolve(kept);
|
|
1904
|
+
}
|
|
1905
|
+
this.fetching ??= ShareTunnels.download(kept).finally(() => {
|
|
1906
|
+
this.fetching = undefined;
|
|
1907
|
+
});
|
|
1908
|
+
return this.fetching;
|
|
1909
|
+
}
|
|
1910
|
+
static async download(to, platform = `${process.platform}-${process.arch}`) {
|
|
1911
|
+
const release = CLOUDFLARED_FILES[platform];
|
|
1912
|
+
if (!release) {
|
|
1913
|
+
throw new Error(`There is no cloudflared for ${platform}: install it and put it on the PATH.`);
|
|
1914
|
+
}
|
|
1915
|
+
const response = await fetch(`https://github.com/cloudflare/cloudflared/releases/download/${CLOUDFLARED_VERSION}/${release.file}`);
|
|
1916
|
+
if (!response.ok) {
|
|
1917
|
+
throw new Error(`cloudflared could not be fetched (${response.status}).`);
|
|
1918
|
+
}
|
|
1919
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
1920
|
+
if (createHash("sha256").update(bytes).digest("hex") !== release.sha256) {
|
|
1921
|
+
throw new Error("cloudflared fetched is not the release Ta knows (its SHA-256 differs): nothing was kept.");
|
|
1922
|
+
}
|
|
1923
|
+
const dir = join(to, "..");
|
|
1924
|
+
mkdirSync(dir, { recursive: true, mode: 448 });
|
|
1925
|
+
const temp = `${to}.${randomBytes(4).toString("hex")}`;
|
|
1926
|
+
if (release.tgz) {
|
|
1927
|
+
const work = `${temp}.d`;
|
|
1928
|
+
mkdirSync(work, { recursive: true });
|
|
1929
|
+
writeFileSync(join(work, "release.tgz"), bytes);
|
|
1930
|
+
await new Promise((resolve, reject) => {
|
|
1931
|
+
const tar = spawn("tar", ["-xzf", "release.tgz", "cloudflared"], { cwd: work, stdio: "ignore" });
|
|
1932
|
+
tar.once("error", reject);
|
|
1933
|
+
tar.once("exit", (code) => code === 0 ? resolve() : reject(new Error(`tar could not unpack cloudflared (${code}).`)));
|
|
1934
|
+
});
|
|
1935
|
+
renameSync(join(work, "cloudflared"), temp);
|
|
1936
|
+
rmSync(work, { recursive: true, force: true });
|
|
1937
|
+
} else {
|
|
1938
|
+
writeFileSync(temp, bytes);
|
|
1939
|
+
}
|
|
1940
|
+
chmodSync(temp, 493);
|
|
1941
|
+
renameSync(temp, to);
|
|
1942
|
+
return to;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
|
|
25
1946
|
// ../ta-protocol/esm/common/redactor.js
|
|
26
1947
|
var REDACTED = "[redacted]";
|
|
27
1948
|
var WHOLE = [
|
|
@@ -847,8 +2768,8 @@ var ConversationImport;
|
|
|
847
2768
|
})(ConversationImport ||= {});
|
|
848
2769
|
|
|
849
2770
|
// web-src/share-service.ts
|
|
850
|
-
var
|
|
851
|
-
var
|
|
2771
|
+
var encoder5 = new TextEncoder;
|
|
2772
|
+
var decoder4 = new TextDecoder;
|
|
852
2773
|
var PICTURE_TYPES = {
|
|
853
2774
|
png: "image/png",
|
|
854
2775
|
jpg: "image/jpeg",
|
|
@@ -862,10 +2783,51 @@ var FAILURES = new Set(["password", "wrong", "busy", "gone", "offline", "self",
|
|
|
862
2783
|
|
|
863
2784
|
class ShareService {
|
|
864
2785
|
host;
|
|
865
|
-
|
|
2786
|
+
tunnels;
|
|
2787
|
+
moderation;
|
|
2788
|
+
constructor(host, tunnels) {
|
|
866
2789
|
this.host = host;
|
|
2790
|
+
this.tunnels = tunnels ?? new ShareTunnels({
|
|
2791
|
+
agentDir: host.agentDir,
|
|
2792
|
+
enabled: async () => await host.getExtensionData(NAME, TUNNELS_KEY) === true,
|
|
2793
|
+
setEnabled: (enabled) => host.setExtensionData(NAME, TUNNELS_KEY, enabled)
|
|
2794
|
+
});
|
|
2795
|
+
this.moderation = new ShareModeration(host);
|
|
2796
|
+
this.moderation.start();
|
|
2797
|
+
}
|
|
2798
|
+
dispose() {
|
|
2799
|
+
this.tunnels.stopAll();
|
|
2800
|
+
this.moderation.dispose();
|
|
2801
|
+
}
|
|
2802
|
+
operatorState() {
|
|
2803
|
+
return this.moderation.state();
|
|
2804
|
+
}
|
|
2805
|
+
setOperatorToken(token) {
|
|
2806
|
+
return this.moderation.setToken(token === null ? null : String(token));
|
|
2807
|
+
}
|
|
2808
|
+
moderate(action) {
|
|
2809
|
+
return this.moderation.act(action);
|
|
2810
|
+
}
|
|
2811
|
+
async standing() {
|
|
2812
|
+
return await this.host.shares.standing?.().catch(() => {
|
|
2813
|
+
return;
|
|
2814
|
+
}) ?? null;
|
|
2815
|
+
}
|
|
2816
|
+
async appeal(text) {
|
|
2817
|
+
if (!this.host.shares.appeal) {
|
|
2818
|
+
throw new Error("This Ta cannot appeal yet: update it.");
|
|
2819
|
+
}
|
|
2820
|
+
return await this.host.shares.appeal(String(text ?? "").trim()) ?? null;
|
|
2821
|
+
}
|
|
2822
|
+
tunnelState() {
|
|
2823
|
+
return this.tunnels.state();
|
|
2824
|
+
}
|
|
2825
|
+
setTunnelsEnabled(enabled) {
|
|
2826
|
+
return this.tunnels.setEnabled(enabled === true);
|
|
2827
|
+
}
|
|
2828
|
+
async stopTunnel(id) {
|
|
2829
|
+
return this.tunnels.stop(String(id));
|
|
867
2830
|
}
|
|
868
|
-
dispose() {}
|
|
869
2831
|
async preview(sessionFile, options = {}) {
|
|
870
2832
|
const { conversation, stats } = await this.snapshot(sessionFile, options, false);
|
|
871
2833
|
const calls = conversation.messages.flatMap((message) => message.role === "assistant" ? message.content.filter((part) => part.type === "toolCall") : []);
|
|
@@ -889,13 +2851,17 @@ class ShareService {
|
|
|
889
2851
|
kind: CONVERSATION_KIND,
|
|
890
2852
|
title: conversation.title,
|
|
891
2853
|
type: "application/json",
|
|
892
|
-
data:
|
|
2854
|
+
data: encoder5.encode(JSON.stringify(conversation)),
|
|
893
2855
|
...ShareService.input(options)
|
|
894
2856
|
});
|
|
895
2857
|
return { share, stats };
|
|
896
2858
|
}
|
|
897
2859
|
async shareFile(title, type, data, options) {
|
|
898
|
-
|
|
2860
|
+
const bytes = ShareService.fromBase64(data);
|
|
2861
|
+
if (bytes.length > MAX_FILE_BYTES) {
|
|
2862
|
+
throw new Error(`The file is larger than ${MAX_FILE_BYTES / 1024 / 1024} MB, too large to share by a link.`);
|
|
2863
|
+
}
|
|
2864
|
+
return { share: await this.shareBytes(title, type, bytes, options) };
|
|
899
2865
|
}
|
|
900
2866
|
shareBytes(title, type, data, options) {
|
|
901
2867
|
return this.host.shares.create({
|
|
@@ -931,7 +2897,7 @@ class ShareService {
|
|
|
931
2897
|
if (opened.content.kind !== CONVERSATION_KIND) {
|
|
932
2898
|
throw new Error("This link shares a file, not a conversation.");
|
|
933
2899
|
}
|
|
934
|
-
const conversation = JSON.parse(
|
|
2900
|
+
const conversation = JSON.parse(decoder4.decode(opened.content.data));
|
|
935
2901
|
if (conversation?.v !== 1 || !Array.isArray(conversation.messages)) {
|
|
936
2902
|
throw new Error("Not a conversation shared from Ta.");
|
|
937
2903
|
}
|