@uuaid/pillar 2.7.2 → 2.8.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/HOSTING-LIMITS.md +95 -0
- package/README.md +251 -1
- package/bin/carrier.mjs +97 -1
- package/bin/pillar-directory.mjs +75 -100
- package/bin/pillar-seed.mjs +127 -11
- package/bin/pillar.mjs +11 -4
- package/package.json +1 -1
- package/src/mailbox/mailbox.mjs +12 -0
- package/src/net/carrier-server.mjs +191 -17
- package/src/net/discovery.mjs +72 -0
- package/src/net/refusals.mjs +15 -0
- package/src/net/seed-registry.mjs +334 -35
- package/src/net/seed-tier.mjs +35 -1
- package/src/net/tunnel-client.mjs +456 -0
- package/src/net/tunnel-relay.mjs +776 -0
- package/src/net/tunnel.mjs +288 -0
- package/src/notify-store.mjs +4 -1
- package/src/pillar.mjs +541 -9
- package/src/registry/directory-flags.mjs +189 -0
- package/src/registry/stats.mjs +1 -1
- package/src/vnext/continuity-cli.mjs +399 -0
- package/src/vnext/continuity.mjs +1801 -0
- package/src/vnext/init-cli.mjs +23 -7
- package/src/vnext/json-dir-mailbox.mjs +6 -0
- package/src/vnext/jsonl-mailbox.mjs +177 -23
- package/src/vnext/knock-cli.mjs +18 -0
- package/src/vnext/retired-import.mjs +556 -0
- package/src/vnext/verify-cli.mjs +1 -1
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
*/
|
|
65
65
|
import { createServer } from "node:http";
|
|
66
66
|
import { clientIpOf, DEFAULT_TRUSTED_PROXIES } from "./client-ip.mjs";
|
|
67
|
-
import { createHash } from "node:crypto";
|
|
67
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
68
68
|
import { requireBetterSqlite3 } from "../sqlite-loader.mjs";
|
|
69
69
|
import { open as openEnvelope, envelopeSha as canonicalEnvelopeSha } from "./envelope.mjs";
|
|
70
70
|
import { Keychain, keyDerivesUuaid, isStrictEd25519PublicKey } from "../identity/keychain.mjs";
|
|
@@ -74,8 +74,71 @@ import { verifyDelegationDoc, delegationCoversKind } from "./delegation.mjs";
|
|
|
74
74
|
import { withCarrierFix } from "./refusals.mjs"; // additive `fix` on refusals; reason/error/status unchanged
|
|
75
75
|
import { carrierReceiptSigningString, carrierReceiptShapeOk, carrierUuaidFromKey } from "./carrier-receipt.mjs";
|
|
76
76
|
import { rekeyToSenderUnique } from "../sqlite-rekey.mjs";
|
|
77
|
+
import { CHALLENGE_RE, makeServedProof } from "./tunnel.mjs";
|
|
77
78
|
|
|
78
79
|
const MAX_WAIT_S = 30;
|
|
80
|
+
/** Most rows in one inbox page. */
|
|
81
|
+
export const INBOX_PAGE_ROWS = 200;
|
|
82
|
+
/**
|
|
83
|
+
* Most stored-body bytes in one inbox page (2.8.0, RedTeam 82902d): a page
|
|
84
|
+
* ends before the row that would pass this, always with whole envelopes. It
|
|
85
|
+
* is the largest envelope the carrier accepts, so a page always holds at
|
|
86
|
+
* least one; a row larger than this (stored by an older build) is served
|
|
87
|
+
* alone. `more: true` says rows remain after the cut.
|
|
88
|
+
*/
|
|
89
|
+
export const INBOX_PAGE_BYTES = 2 * 1024 * 1024 + 8192;
|
|
90
|
+
/** Bytes a page row costs beyond its body (seq, JSON framing), for reservations. */
|
|
91
|
+
const INBOX_ROW_OVERHEAD = 64;
|
|
92
|
+
/** A page's JSON is held about twice while it is serialized and sent; reserve for both. */
|
|
93
|
+
const INBOX_RESERVE_FACTOR = 2;
|
|
94
|
+
// Pages whose reservation is at most this are "small" (chat-sized mail);
|
|
95
|
+
// large pages leave 1/INBOX_SMALL_SLICE of the budget to them, so small
|
|
96
|
+
// mail is still served while large pages hold the rest.
|
|
97
|
+
const INBOX_SMALL_PAGE_COST = 256 * 1024;
|
|
98
|
+
const INBOX_SMALL_SLICE = 8;
|
|
99
|
+
// One client address holds at most 1/INBOX_ADDRESS_SHARE of the budget (C7-2):
|
|
100
|
+
// past that, its reads get 503 seed-busy at once, and other addresses still
|
|
101
|
+
// find room. An address that holds no page may always take one.
|
|
102
|
+
export const INBOX_ADDRESS_SHARE = 4;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Bytes of inbox pages a seed holds for clients at once (2.8.0, RedTeam
|
|
106
|
+
* 82902d). A page is reserved before it is read or built. The reservation is
|
|
107
|
+
* returned when the page has left this process: when its HTTP answer
|
|
108
|
+
* finishes, or, for a request the seed's own tunnel client made with a
|
|
109
|
+
* ticket from ticket(), when the tunnel client calls end(ticket) after it has
|
|
110
|
+
* sent the page to the relay or dropped the stream. Until then the page sits
|
|
111
|
+
* in the loopback socket and the tunnel client's queue, inside this process
|
|
112
|
+
* and its kernel buffers, so finishing the local answer is not enough.
|
|
113
|
+
*/
|
|
114
|
+
export class ResponseBudget {
|
|
115
|
+
constructor(limitBytes) {
|
|
116
|
+
this.limit = limitBytes;
|
|
117
|
+
this.used = 0;
|
|
118
|
+
this._tickets = new Map();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Tunnel client: a one-use ticket for one local request. */
|
|
122
|
+
ticket() {
|
|
123
|
+
const t = randomBytes(16).toString("hex");
|
|
124
|
+
this._tickets.set(t, null);
|
|
125
|
+
return t;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Carrier: run `release` when `ticket` ends. False when the ticket is not a live one. */
|
|
129
|
+
holdUntil(ticket, release) {
|
|
130
|
+
if (typeof ticket !== "string" || !this._tickets.has(ticket) || this._tickets.get(ticket) !== null) return false;
|
|
131
|
+
this._tickets.set(ticket, release);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Tunnel client: the stream is over (sent or dropped); return what it held. */
|
|
136
|
+
end(ticket) {
|
|
137
|
+
const release = this._tickets.get(ticket);
|
|
138
|
+
this._tickets.delete(ticket);
|
|
139
|
+
if (typeof release === "function") release();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
79
142
|
const MAX_BODY = 512 * 1024; // 512 KiB per envelope (community tier)
|
|
80
143
|
const MAX_BODY_STREAM = 2 * 1024 * 1024 + 8192; // absolute read cap: max tier body + grant/header slack
|
|
81
144
|
const MAX_SKEW_MS = 5 * 60 * 1000;
|
|
@@ -162,7 +225,7 @@ const ENVELOPES_COLUMNS = `
|
|
|
162
225
|
const ENVELOPES_INDEX = "create index if not exists idx_envelopes_recipient on envelopes(recipient, seq);";
|
|
163
226
|
|
|
164
227
|
export class CarrierServer {
|
|
165
|
-
constructor({ dbPath = "carrier.db", ttlDays = 14, cryptoInventory, trustedTierIssuers = [], keychain = null, publicUrl = null, secureDelete = true, receiptKeychain = null, communityInboxWaits = COMMUNITY_CONCURRENT_WAITS, trustedProxies = DEFAULT_TRUSTED_PROXIES } = {}) {
|
|
228
|
+
constructor({ dbPath = "carrier.db", ttlDays = 14, cryptoInventory, trustedTierIssuers = [], keychain = null, publicUrl = null, secureDelete = true, receiptKeychain = null, communityInboxWaits = COMMUNITY_CONCURRENT_WAITS, trustedProxies = DEFAULT_TRUSTED_PROXIES, relayOrigin = null, tunnelRelay = null, responseBudget = null } = {}) {
|
|
166
229
|
// Every row's retain_until is created_at + ttlDays, so the TTL must be a
|
|
167
230
|
// real positive duration (a NaN here used to surface only as a vacuum crash).
|
|
168
231
|
if (typeof ttlDays !== "number" || !Number.isFinite(ttlDays) || ttlDays <= 0) {
|
|
@@ -259,6 +322,21 @@ export class CarrierServer {
|
|
|
259
322
|
// does not expose /v1/seed-info.
|
|
260
323
|
this.keychain = keychain;
|
|
261
324
|
this.publicUrl = publicUrl ? String(publicUrl).replace(/\/+$/, "") : null;
|
|
325
|
+
// 2.8.0 egress seed: the relay origin this seed is served through, declared
|
|
326
|
+
// inside its signed /v1/seed-info. Null for a seed with its own public URL.
|
|
327
|
+
this.relayOrigin = relayOrigin ? String(relayOrigin) : null;
|
|
328
|
+
// 2.8.0 relay side: a TunnelRelay (./tunnel-relay.mjs) that serves
|
|
329
|
+
// /v1/tunnel and /e/<localId>/... Null (the default): both stay 404.
|
|
330
|
+
this.tunnelRelay = tunnelRelay ?? null;
|
|
331
|
+
// 2.8.0 (RedTeam 82902d): an optional ResponseBudget (any { limit, used }
|
|
332
|
+
// works; tickets need the class) every inbox page is reserved against
|
|
333
|
+
// BEFORE it is read from the store and built; a seed served through a
|
|
334
|
+
// relay sets one, so the pages it holds while clients read them stay
|
|
335
|
+
// bounded. Null: unlimited, as before.
|
|
336
|
+
this.responseBudget = responseBudget ?? null;
|
|
337
|
+
// Response-budget bytes reserved per client address, for the addresses
|
|
338
|
+
// that hold some (C7-2, INBOX_ADDRESS_SHARE).
|
|
339
|
+
this.reservedByAddress = new Map();
|
|
262
340
|
// Receipt identity: the carrier uuaid is DERIVED from the receipt key
|
|
263
341
|
// (uuaid:foundation:agent:<localId>), never read from the keychain file,
|
|
264
342
|
// so it is always the uuaid a client's pin check derives.
|
|
@@ -358,11 +436,15 @@ export class CarrierServer {
|
|
|
358
436
|
* net/seed-tier.mjs). What the signature does prove is the thing enrollment
|
|
359
437
|
* actually needs — that whoever controls this URL also controls this key.
|
|
360
438
|
*/
|
|
361
|
-
seedInfo() {
|
|
439
|
+
seedInfo({ challenge } = {}) {
|
|
362
440
|
if (!this.keychain?._identity || !this.publicUrl) return null;
|
|
363
441
|
const doc = {
|
|
364
442
|
v: 1,
|
|
365
443
|
url: this.publicUrl,
|
|
444
|
+
// 2.8.0: a relayed seed declares its relay, and echoes a registry's
|
|
445
|
+
// one-use challenge, both under its signature (RedTeam c3ff4d).
|
|
446
|
+
...(this.relayOrigin ? { relay: this.relayOrigin } : {}),
|
|
447
|
+
...(challenge ? { challenge } : {}),
|
|
366
448
|
uuaid: this.keychain._identity.uuaid,
|
|
367
449
|
publicKey: this.keychain._identity.publicKeyHex,
|
|
368
450
|
startedAt: new Date(this.startedAt).toISOString(),
|
|
@@ -394,10 +476,20 @@ export class CarrierServer {
|
|
|
394
476
|
try { grant = JSON.parse(Buffer.from(String(hdr), "base64").toString("utf-8")); }
|
|
395
477
|
catch (_e) { return fallback; }
|
|
396
478
|
if (grant?.uuaid !== sender) return fallback; // grant must name the claimed sender
|
|
479
|
+
const now = Date.now();
|
|
397
480
|
const cached = this._grantCache.get(sender);
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
481
|
+
let warm = Boolean(cached && cached.sig === grant.sig && now - cached.at < 60_000);
|
|
482
|
+
// A cached decision never outlives the grant it came from: at the grant's
|
|
483
|
+
// expiry the sender is verified again and falls to community (2.8.0,
|
|
484
|
+
// RedTeam 22f6c3 Q1: relayed seeds' grants can last minutes, not days).
|
|
485
|
+
if (warm && !(now < cached.expiresAt)) warm = false; // @rule tier.grant-cache-until-expiry
|
|
486
|
+
if (warm) return cached.resolved;
|
|
487
|
+
const v = verifyTierGrant(grant, this.trustedTierIssuers, now);
|
|
488
|
+
// verifyTierGrant still accepts a grant at the very millisecond it
|
|
489
|
+
// expires; here the sender falls to community at now >= expiry.
|
|
490
|
+
let granted = v.ok;
|
|
491
|
+
if (granted && !(now < Date.parse(grant.expires))) granted = false; // @rule tier.grant-expiry-exclusive
|
|
492
|
+
const resolved = granted
|
|
401
493
|
? {
|
|
402
494
|
tier: grant.tier,
|
|
403
495
|
envelopesPerMin: grant.envelopesPerMin,
|
|
@@ -410,7 +502,9 @@ export class CarrierServer {
|
|
|
410
502
|
}
|
|
411
503
|
: fallback;
|
|
412
504
|
if (this._grantCache.size > 4096) this._grantCache.clear(); // bounded
|
|
413
|
-
|
|
505
|
+
// NaN (an unparseable expiry) is never `now <` it, so such an entry is never reused.
|
|
506
|
+
const expiresAt = granted ? Date.parse(grant.expires) : Infinity;
|
|
507
|
+
this._grantCache.set(sender, { sig: grant.sig, at: now, resolved, expiresAt });
|
|
414
508
|
return resolved;
|
|
415
509
|
}
|
|
416
510
|
|
|
@@ -685,6 +779,13 @@ export class CarrierServer {
|
|
|
685
779
|
return { ok: true };
|
|
686
780
|
}
|
|
687
781
|
|
|
782
|
+
/** Reserve (n > 0) or return (n < 0) response-budget bytes for client address `ip`. */
|
|
783
|
+
_reserve(ip, n) {
|
|
784
|
+
this.responseBudget.used += n;
|
|
785
|
+
const left = (this.reservedByAddress.get(ip) ?? 0) + n;
|
|
786
|
+
if (left > 0) this.reservedByAddress.set(ip, left); else this.reservedByAddress.delete(ip);
|
|
787
|
+
}
|
|
788
|
+
|
|
688
789
|
async _handleInbox(req, res, uuaid, url) {
|
|
689
790
|
const since = Number(url.searchParams.get("since") ?? 0);
|
|
690
791
|
const wait = Math.min(Number(url.searchParams.get("wait") ?? 0), MAX_WAIT_S);
|
|
@@ -704,27 +805,91 @@ export class CarrierServer {
|
|
|
704
805
|
}
|
|
705
806
|
if (wait > 0) this.activeWaits++;
|
|
706
807
|
|
|
707
|
-
|
|
808
|
+
// A page is sized from body lengths first (no body is read), cut at
|
|
809
|
+
// INBOX_PAGE_BYTES with whole envelopes, reserved against the response
|
|
810
|
+
// budget, and only then read and built (RedTeam 82902d).
|
|
811
|
+
const sizeRows = () => {
|
|
708
812
|
// closed → the DB is gone; a long-poll timer armed before close() can
|
|
709
813
|
// fire afterwards. Never touch a closed connection.
|
|
710
814
|
if (this._closed) return [];
|
|
711
815
|
return this.db.prepare(
|
|
712
|
-
|
|
816
|
+
// octet_length: UTF-8 bytes. length() would count characters and
|
|
817
|
+
// undercount a non-ASCII body (SQLite >= 3.43; better-sqlite3 ships 3.53).
|
|
818
|
+
`select seq, octet_length(body) as n from envelopes where recipient = ? and seq > ? order by seq asc limit ${INBOX_PAGE_ROWS + 1}`
|
|
713
819
|
).all(uuaid, since);
|
|
714
820
|
};
|
|
715
821
|
|
|
822
|
+
let reserved = 0;
|
|
823
|
+
const ip = this._clientIp(req);
|
|
716
824
|
try {
|
|
717
|
-
let
|
|
825
|
+
let sized = sizeRows();
|
|
718
826
|
const deadline = Date.now() + wait * 1000;
|
|
719
|
-
while (
|
|
827
|
+
while (sized.length === 0 && Date.now() < deadline && !this._closed) {
|
|
720
828
|
await this._sleepOrWake(uuaid, 400);
|
|
721
|
-
|
|
829
|
+
sized = sizeRows();
|
|
722
830
|
}
|
|
831
|
+
// Whole envelopes up to the byte limit and, with a response budget, up
|
|
832
|
+
// to what the free budget holds; the first row always, so a page is
|
|
833
|
+
// never empty while mail waits (a single row may pass either limit:
|
|
834
|
+
// one envelope, alone, when nothing else is reserved).
|
|
835
|
+
const budget = this.responseBudget;
|
|
836
|
+
const cost = (b, n) => (b + n * INBOX_ROW_OVERHEAD) * INBOX_RESERVE_FACTOR;
|
|
837
|
+
const free = budget ? budget.limit - budget.used : Infinity;
|
|
838
|
+
let smallSlice = 0;
|
|
839
|
+
if (budget) smallSlice = Math.floor(budget.limit / INBOX_SMALL_SLICE); // @rule inbox.small-slice
|
|
840
|
+
// What this client address may still reserve: its share, less what its
|
|
841
|
+
// other pages hold; no limit while it holds none (C7-2).
|
|
842
|
+
const mine = this.reservedByAddress.get(ip) ?? 0;
|
|
843
|
+
let addressRoom = Infinity;
|
|
844
|
+
if (mine > 0) addressRoom = Math.floor(budget.limit / INBOX_ADDRESS_SHARE) - mine; // @rule inbox.address-share
|
|
845
|
+
const roomFor = (c) => Math.min(c <= INBOX_SMALL_PAGE_COST ? free : free - smallSlice, addressRoom);
|
|
846
|
+
let take = 0;
|
|
847
|
+
let bytes = 0;
|
|
848
|
+
while (take < sized.length && take < INBOX_PAGE_ROWS) {
|
|
849
|
+
const next = bytes + sized[take].n;
|
|
850
|
+
if (take > 0 && next > INBOX_PAGE_BYTES) break; // @rule inbox.page-bytes
|
|
851
|
+
const c = cost(next, take + 1);
|
|
852
|
+
if (take > 0 && c > roomFor(c)) break; // @rule inbox.page-fits-budget
|
|
853
|
+
bytes = next;
|
|
854
|
+
take++;
|
|
855
|
+
}
|
|
856
|
+
const more = take < sized.length;
|
|
857
|
+
if (take > 0 && budget) {
|
|
858
|
+
// Nobody left to send to (the client went during a long-poll): build nothing.
|
|
859
|
+
if (res.destroyed) return;
|
|
860
|
+
const need = cost(bytes, take);
|
|
861
|
+
// Refused only while other pages hold the budget; an idle budget
|
|
862
|
+
// always serves one page, so no mail is unreadable for good.
|
|
863
|
+
if (budget.used > 0 && need > roomFor(need)) return this._json(res, 503, { error: "seed-busy" }); // @rule inbox.reserve-first
|
|
864
|
+
this._reserve(ip, need);
|
|
865
|
+
reserved = need;
|
|
866
|
+
const release = () => { if (reserved) { this._reserve(ip, -reserved); reserved = 0; } };
|
|
867
|
+
// The seed's own tunnel client returns it once the page has gone to
|
|
868
|
+
// the relay; any other client, when its answer finishes.
|
|
869
|
+
let held = false;
|
|
870
|
+
held = budget.holdUntil?.(req.headers["x-pillar-budget-ticket"], release) === true; // @rule inbox.hold-until-sent
|
|
871
|
+
// Node 22 emits finish also when the client goes mid-page; close is
|
|
872
|
+
// the fallback. A ticket's page is returned by the tunnel client,
|
|
873
|
+
// which ends the ticket when the stream ends, resets or the tunnel goes.
|
|
874
|
+
if (!held) { res.once("finish", release); res.once("close", release); }
|
|
875
|
+
}
|
|
876
|
+
const rows = take === 0 ? [] : this.db.prepare(
|
|
877
|
+
"select seq, body from envelopes where recipient = ? and seq >= ? and seq <= ? order by seq asc"
|
|
878
|
+
).all(uuaid, sized[0].seq, sized[take - 1].seq);
|
|
723
879
|
if (rows.length) this._bump("served", rows.length);
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
880
|
+
const envelopes = rows.map(r => ({ seq: r.seq, envelope: JSON.parse(r.body) }));
|
|
881
|
+
// 2.8.0 served proof (RedTeam 085b71 C4): a seed asked with a one-use
|
|
882
|
+
// challenge signs the digest of this exact page, on this authenticated
|
|
883
|
+
// read, so a relay cannot answer a registry's round trip by itself.
|
|
884
|
+
const challenge = url.searchParams.get("challenge");
|
|
885
|
+
const proof = this.keychain?._identity && challenge !== null && CHALLENGE_RE.test(challenge) && Number.isSafeInteger(since)
|
|
886
|
+
? makeServedProof(this.keychain, { inbox: uuaid, since, challenge, envelopes })
|
|
887
|
+
: undefined;
|
|
888
|
+
this._json(res, 200, { envelopes, now: Date.now(), ...(more ? { more: true } : {}), ...(proof ? { proof } : {}) });
|
|
889
|
+
} catch (e) {
|
|
890
|
+
// Nothing was sent: give the reservation back before the error answer.
|
|
891
|
+
if (reserved && this.responseBudget) { this._reserve(ip, -reserved); reserved = 0; }
|
|
892
|
+
throw e;
|
|
728
893
|
} finally {
|
|
729
894
|
if (wait > 0) this.activeWaits--;
|
|
730
895
|
}
|
|
@@ -741,6 +906,9 @@ export class CarrierServer {
|
|
|
741
906
|
this.server = createServer(async (req, res) => {
|
|
742
907
|
try {
|
|
743
908
|
const url = new URL(req.url, `http://${req.headers.host ?? "localhost"}`);
|
|
909
|
+
// 2.8.0 relay routes, only when this carrier accepts tunnels.
|
|
910
|
+
if (this.tunnelRelay && url.pathname.startsWith("/e/")) return await this.tunnelRelay.handleRequest(req, res, url);
|
|
911
|
+
if (this.tunnelRelay && url.pathname === "/v1/tunnel") return this._json(res, 426, { error: "upgrade-required" });
|
|
744
912
|
if (req.method === "POST" && url.pathname === "/v1/envelopes") {
|
|
745
913
|
const raw = await this._readBody(req);
|
|
746
914
|
let envelope;
|
|
@@ -756,7 +924,11 @@ export class CarrierServer {
|
|
|
756
924
|
return await this._handleInbox(req, res, uuaid, url);
|
|
757
925
|
}
|
|
758
926
|
if (req.method === "GET" && url.pathname === "/v1/seed-info") {
|
|
759
|
-
const
|
|
927
|
+
const challenge = url.searchParams.get("challenge");
|
|
928
|
+
if (challenge !== null && this.keychain?._identity && this.publicUrl && !CHALLENGE_RE.test(challenge)) {
|
|
929
|
+
return this._json(res, 400, { error: "bad-challenge" });
|
|
930
|
+
}
|
|
931
|
+
const info = this.seedInfo({ challenge: challenge ?? undefined });
|
|
760
932
|
// A relay with no seed identity is not a seed and must not look
|
|
761
933
|
// like one to the registry.
|
|
762
934
|
if (!info) return this._json(res, 404, { error: "not-a-seed" });
|
|
@@ -773,6 +945,7 @@ export class CarrierServer {
|
|
|
773
945
|
this._json(res, 500, { error: "internal-error" });
|
|
774
946
|
}
|
|
775
947
|
});
|
|
948
|
+
if (this.tunnelRelay) this.server.on("upgrade", (req, socket, head) => this.tunnelRelay.handleUpgrade(req, socket, head));
|
|
776
949
|
return new Promise((resolve) => {
|
|
777
950
|
if (port !== null && typeof port === "object") this.server.listen(port, () => resolve(this.server.address()));
|
|
778
951
|
else this.server.listen(port, host, () => resolve(this.server.address()));
|
|
@@ -781,6 +954,7 @@ export class CarrierServer {
|
|
|
781
954
|
|
|
782
955
|
async close() {
|
|
783
956
|
this._closed = true;
|
|
957
|
+
this.tunnelRelay?.close();
|
|
784
958
|
for (const set of this._inboxWakers.values()) for (const wake of [...set]) wake();
|
|
785
959
|
if (this.server) {
|
|
786
960
|
// close() must not wait for in-flight connections: a client long-polling
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which carriers a client takes from a directory on its own (`pillar init
|
|
3
|
+
* --directory`, `--refresh`, `--force --directory`).
|
|
4
|
+
*
|
|
5
|
+
* 2.8.0 (RedTeam 22f6c3 Q2): no relayed seed URL (`<relay origin>/e/<id>`,
|
|
6
|
+
* src/net/tunnel.mjs) is configured automatically, whether the directory
|
|
7
|
+
* marks it with `relay` metadata or not, and whatever relays it pins. Inbox
|
|
8
|
+
* auth headers sent through a relay can be replayed for 5 minutes, so a
|
|
9
|
+
* client reaches a relayed seed only when its operator names it explicitly
|
|
10
|
+
* (`pillar init --carriers <url>`). Automatic discovery of relayed seeds
|
|
11
|
+
* waits for versioned, audience-bound, single-use inbox auth. Direct carrier
|
|
12
|
+
* and seed entries are taken exactly as before.
|
|
13
|
+
*/
|
|
14
|
+
import { readFileSync } from "node:fs";
|
|
15
|
+
import { isRelayedSeedUrl } from "./tunnel.mjs";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} d a directory document ({ carrier, seeds: [{ url, relay? }] })
|
|
19
|
+
* @returns {{ carriers: string[], skippedRelayed: number }}
|
|
20
|
+
* carriers in directory order: the bootstrap carrier, then each seed URL.
|
|
21
|
+
*/
|
|
22
|
+
export function directoryCarriers(d) {
|
|
23
|
+
const carriers = [];
|
|
24
|
+
let skippedRelayed = 0;
|
|
25
|
+
let carrier = d?.carrier;
|
|
26
|
+
if (carrier && isRelayedSeedUrl(carrier)) { carrier = null; skippedRelayed++; } // @rule discovery.carrier-not-relayed
|
|
27
|
+
if (carrier) carriers.push(carrier);
|
|
28
|
+
for (const s of Array.isArray(d?.seeds) ? d.seeds : []) {
|
|
29
|
+
let url = s?.url;
|
|
30
|
+
if (url && s?.relay !== undefined) { url = null; skippedRelayed++; } // @rule discovery.seed-relay-metadata
|
|
31
|
+
if (url && isRelayedSeedUrl(url)) { url = null; skippedRelayed++; } // @rule discovery.seed-relayed-shape
|
|
32
|
+
if (url) carriers.push(url);
|
|
33
|
+
}
|
|
34
|
+
return { carriers, skippedRelayed };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The carrier list alone, in the shape init-cli has always used. */
|
|
38
|
+
export function carriersFromDirectory(d) {
|
|
39
|
+
return directoryCarriers(d).carriers;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Read a home's config.json back after a write and check its carriers are
|
|
44
|
+
* exactly `expected` (RedTeam 995f4c Q2: when `pillar init --refresh` is used
|
|
45
|
+
* to clean relayed URLs out of a home, what is on disk is the evidence, not
|
|
46
|
+
* what was meant to be written).
|
|
47
|
+
* @returns {{ ok: true, carriers: string[] } | { ok: false, reason: string, carriers?: string[] }}
|
|
48
|
+
*/
|
|
49
|
+
export function readBackCarriers(cfgPath, expected) {
|
|
50
|
+
let carriers;
|
|
51
|
+
try {
|
|
52
|
+
carriers = JSON.parse(readFileSync(cfgPath, "utf-8"))?.carriers;
|
|
53
|
+
} catch (e) {
|
|
54
|
+
return { ok: false, reason: `cannot read it back (${e.code ?? "not JSON"})` };
|
|
55
|
+
}
|
|
56
|
+
if (!Array.isArray(carriers)) return { ok: false, reason: "no carriers list in it" };
|
|
57
|
+
// Element by element, so a relayed URL left on disk (or any other drift) fails.
|
|
58
|
+
let same = true;
|
|
59
|
+
if (carriers.length !== expected.length || carriers.some((c, i) => c !== expected[i])) same = false; // @rule discovery.readback-equal
|
|
60
|
+
if (!same) return { ok: false, reason: "its carriers differ from what was written", carriers };
|
|
61
|
+
return { ok: true, carriers };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The relayed seed URLs a home's config.json lists now ([] if none or unreadable). */
|
|
65
|
+
export function relayedCarriersIn(cfgPath) {
|
|
66
|
+
try {
|
|
67
|
+
const carriers = JSON.parse(readFileSync(cfgPath, "utf-8"))?.carriers;
|
|
68
|
+
return Array.isArray(carriers) ? carriers.filter((c) => isRelayedSeedUrl(c)) : [];
|
|
69
|
+
} catch (_e) {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/net/refusals.mjs
CHANGED
|
@@ -66,6 +66,16 @@ export const REFUSAL_FIXES = Object.freeze({
|
|
|
66
66
|
"not-found": "use POST /v1/envelopes or GET /v1/inbox/<uuaid>",
|
|
67
67
|
"not-a-seed": "do not list this carrier as a seed; it runs without a seed identity",
|
|
68
68
|
"internal-error": "retry later; if it repeats, tell the carrier operator",
|
|
69
|
+
// Seed over egress (2.8.0): a registry's challenge, and a tunnel relay's /v1/tunnel and /e/*
|
|
70
|
+
"bad-challenge": "send challenge as 64 lowercase hex characters (32 random bytes), or leave it out",
|
|
71
|
+
"upgrade-required": "open /v1/tunnel as a WebSocket, as pillar-seed --relay does",
|
|
72
|
+
"seed-offline": "retry later or use another carrier; this seed's tunnel to the relay is down",
|
|
73
|
+
"tunnel-busy": "back off: retry after 5 s with jitter, doubling to 60 s, or use another carrier",
|
|
74
|
+
"seed-busy": "back off: retry after 5 s with jitter, doubling to 60 s; this seed holds as many inbox pages as its response budget allows",
|
|
75
|
+
"seed-timeout": "retry, or use another carrier; the seed behind this relay did not answer in time",
|
|
76
|
+
"seed-bad-answer": "retry, or use another carrier; the seed behind this relay answered badly",
|
|
77
|
+
"seed-reset": "retry, or use another carrier; the seed behind this relay dropped the request",
|
|
78
|
+
"seed-answer-too-large": "ask with a smaller page, or use another carrier; the seed's answer passed the relay's size cap",
|
|
69
79
|
// Client side: no server gave a code
|
|
70
80
|
"carrier-unreachable": "check the carrier URLs and your network, then retry",
|
|
71
81
|
"carrier-bad-response": "retry, or use another carrier; the carrier or its proxy answered without a reason",
|
|
@@ -75,6 +85,11 @@ export const REFUSAL_FIXES = Object.freeze({
|
|
|
75
85
|
"group-v2-use-v2-send": "send to this pillar-group/2 group with pillar group send <group>; v1 group sends (send-group, sendGroup) do not reach it",
|
|
76
86
|
"p2p-not-installed": "install the optional libp2p transport with npm i @uuaid/pillar-p2p, or omit --libp2p to run in carrier-only mode",
|
|
77
87
|
"sqlite-not-installed": "install or rebuild optional dependency better-sqlite3 with npm i better-sqlite3",
|
|
88
|
+
"continuity-store-unreadable": "run pillar continuity repair (add --home DIR if not the default); it moves the file aside and starts an empty store, then re-confirm or re-pin your contacts",
|
|
89
|
+
"continuity-holds-unreadable": "restore <home>/continuity/holds.json from a backup; it lists keys that were retired or forked, and removing it would let sends reach them again",
|
|
90
|
+
"continuity-held": "ask the contact over another channel (not pillar) for its new uuaid and lineage head, compare them with pillar continuity show, then run pillar continuity confirm with the values the contact gave you",
|
|
91
|
+
"continuity-suspended": "the evidence conflicts, so nothing is sent; get the contact's genuine lineage log over another channel and run pillar continuity recover (pillar continuity show says why)",
|
|
92
|
+
"redirect-blocked:direct-redirect": "the key retired: send with pillar send or Pillar.send, which route to the new key, or call sendDirect for the new key with that key's own peer id and address",
|
|
78
93
|
});
|
|
79
94
|
|
|
80
95
|
/**
|