@peer2peer/relay 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +19 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +88 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +23 -0
- package/dist/config.d.ts +266 -0
- package/dist/config.js +326 -0
- package/dist/crypto.d.ts +159 -0
- package/dist/crypto.js +514 -0
- package/dist/identity.d.ts +84 -0
- package/dist/identity.js +386 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +14 -0
- package/dist/links.d.ts +29 -0
- package/dist/links.js +98 -0
- package/dist/log.d.ts +19 -0
- package/dist/log.js +35 -0
- package/dist/members.d.ts +109 -0
- package/dist/members.js +369 -0
- package/dist/metrics.d.ts +13 -0
- package/dist/metrics.js +66 -0
- package/dist/net.d.ts +23 -0
- package/dist/net.js +103 -0
- package/dist/ratelimit.d.ts +16 -0
- package/dist/ratelimit.js +70 -0
- package/dist/server.d.ts +271 -0
- package/dist/server.js +1951 -0
- package/dist/sqlite-store.d.ts +184 -0
- package/dist/sqlite-store.js +1068 -0
- package/dist/store.d.ts +368 -0
- package/dist/store.js +492 -0
- package/package.json +57 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Ix Infrastructure, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @peer2peer/relay
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The open relay for [Peer2Peer](https://github.com/ix-infrastructure/Peer2Peer) v3: a small store-and-forward server for end-to-end encrypted envelopes, public identity documents and short-lived device-link mailboxes. Anyone can run one, and every conforming client works with every conforming relay. It never holds a key, so it can't read what it carries.
|
|
4
|
+
|
|
5
|
+
You need Node.js 22.13 or newer (it uses the built-in `node:sqlite`; there is no database server and no native dependency).
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
RELAY_URL=wss://relay.example.org/v3 \
|
|
9
|
+
OPERATOR_CONTACT=mailto:ops@example.org \
|
|
10
|
+
DB_PATH=/var/lib/p2p-relay/relay.db \
|
|
11
|
+
npx @peer2peer/relay
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
After `npm install -g @peer2peer/relay` the same server runs as `p2p-relay`; `p2p-relay --help` lists the settings. TLS, limits, discovery (`/.well-known/p2p-relay.json`), operations and the privacy promise are covered in [docs/run-a-relay.md](https://github.com/ix-infrastructure/Peer2Peer/blob/main/docs/run-a-relay.md).
|
|
15
|
+
|
|
16
|
+
**Members-only.** By default the relay is open to any valid device. Set `RELAY_ALLOW` (userIds), `RELAY_ALLOW_FILE` and/or `RELAY_ALLOW_CIRCLE` (a circle whose members the relay admits, verifying the owner-signed chain itself) and only those members can authenticate; strangers are refused at auth (`not-member`, close 4001), removals disconnect at once (`SIGHUP` or an edit of the allow file), `RELAY_DENY` shuts out anyone at once, and a member's `p2p invite` code admits the teammate who redeems it. An open circle never admits anyone (the relay fails closed to the allowlist). See [Members-only relays](https://github.com/ix-infrastructure/Peer2Peer/blob/main/docs/run-a-relay.md#members-only-relays).
|
|
17
|
+
|
|
18
|
+
The package also exports the `Relay` class and its stores for embedding a relay in tests or another Node process.
|
|
19
|
+
|
|
20
|
+
Licensed under Apache-2.0.
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
3
|
+
//
|
|
4
|
+
// p2p-relay: run an open Peer2Peer v3 relay. Configuration comes from env vars
|
|
5
|
+
// and/or a JSON file named by RELAY_CONFIG; see docs/run-a-relay.md.
|
|
6
|
+
import { watchFile } from "node:fs";
|
|
7
|
+
import { loadConfig } from "./config.js";
|
|
8
|
+
import { createRelayCrypto } from "./crypto.js";
|
|
9
|
+
import { createLogger } from "./log.js";
|
|
10
|
+
import { Relay } from "./server.js";
|
|
11
|
+
import { MemoryStore } from "./store.js";
|
|
12
|
+
import { SqliteStore } from "./sqlite-store.js";
|
|
13
|
+
async function main() {
|
|
14
|
+
if (process.argv.includes("--help") || process.argv.includes("-h")) {
|
|
15
|
+
process.stdout.write("p2p-relay: open Peer2Peer v3 relay\n\n" +
|
|
16
|
+
"Env: RELAY_URL (wss://host/v3), PORT (8080), HOST (0.0.0.0), DB_PATH (./relay.db or :memory:),\n" +
|
|
17
|
+
" POW_BITS (0), OPERATOR_CONTACT, RELAY_NAME, RELAY_POLICY, TRUST_PROXY (proxy hops, 0),\n" +
|
|
18
|
+
" METRICS_PORT (9464, or off), METRICS_HOST (127.0.0.1), METRICS_TOKEN, LOG_LEVEL, DEBUG_STATS (off),\n" +
|
|
19
|
+
" LIMIT_* (e.g. LIMIT_MAILBOX_ENVELOPES), RELAY_CONFIG (path to a JSON file),\n" +
|
|
20
|
+
" RELAY_PRESET (small: a 2 GB board), SQLITE_WAL_LIMIT_BYTES, SQLITE_JOURNAL_SIZE_LIMIT_BYTES,\n" +
|
|
21
|
+
" SQLITE_CHECKPOINT_INTERVAL_MS\n" +
|
|
22
|
+
"Members-only: RELAY_ALLOW (userIds), RELAY_ALLOW_FILE (one per line, !<userId> denies; SIGHUP reloads),\n" +
|
|
23
|
+
" RELAY_DENY (userIds never admitted),\n" +
|
|
24
|
+
" RELAY_ALLOW_CIRCLE (c_…), RELAY_INVITE_ADMIT_SEC (604800; 0: invites admit nobody)\n");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const crypto = createRelayCrypto();
|
|
28
|
+
const config = loadConfig(crypto);
|
|
29
|
+
const log = createLogger({ level: config.logLevel });
|
|
30
|
+
if (!process.env.RELAY_URL && !process.env.RELAY_CONFIG) {
|
|
31
|
+
log.warn("RELAY_URL is not set; clients will reject this relay unless they dial exactly this URL", { relay: config.relayUrl });
|
|
32
|
+
}
|
|
33
|
+
const store = config.dbPath === ":memory:" ? new MemoryStore() : new SqliteStore(config.dbPath, { ...config.storage, warn: (msg, fields) => log.warn(msg, fields) });
|
|
34
|
+
if (config.preset)
|
|
35
|
+
log.info("relay preset", { preset: config.preset });
|
|
36
|
+
const relay = new Relay({ config, store, crypto, logger: log });
|
|
37
|
+
await relay.listen();
|
|
38
|
+
let stopping = false;
|
|
39
|
+
const stop = (signal) => {
|
|
40
|
+
if (stopping)
|
|
41
|
+
return;
|
|
42
|
+
stopping = true;
|
|
43
|
+
log.info("shutting down", { signal });
|
|
44
|
+
relay.close().then(() => process.exit(0), (e) => {
|
|
45
|
+
log.error("shutdown failed", { error: e.message });
|
|
46
|
+
process.exit(1);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
// Members-only: SIGHUP (and any change to RELAY_ALLOW_FILE) re-reads the allowlist
|
|
50
|
+
// (RELAY_CONFIG's `allow` and the allow file's lines; env vars are fixed for the
|
|
51
|
+
// process). Anyone removed is disconnected at once. A bad file keeps the current list.
|
|
52
|
+
const reload = (why) => {
|
|
53
|
+
try {
|
|
54
|
+
const next = loadConfig(crypto);
|
|
55
|
+
if (next.allowCircle !== config.allowCircle)
|
|
56
|
+
log.warn("RELAY_ALLOW_CIRCLE changes need a restart; kept the current circle");
|
|
57
|
+
// Never fail open: turning members-only off takes a restart.
|
|
58
|
+
if (next.allow === undefined && config.allowCircle === undefined && relay.members.enabled) {
|
|
59
|
+
log.warn("the reloaded config has no allowlist; opening the relay needs a restart, so the current list stays");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
relay.setAllow(next.allow, next.deny ?? []);
|
|
63
|
+
log.info("allowlist reloaded", { on: why, allowlisted: next.allow?.length ?? 0 });
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
log.error("allowlist reload failed; kept the current one", { on: why, error: e.message });
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
process.on("SIGHUP", () => reload("SIGHUP"));
|
|
70
|
+
// The allow file is also polled (works through Docker bind mounts), so an edit takes effect within seconds.
|
|
71
|
+
if (config.allowFile) {
|
|
72
|
+
const file = config.allowFile;
|
|
73
|
+
watchFile(file, { interval: 2000, persistent: false }, (cur, prev) => {
|
|
74
|
+
if (cur.mtimeMs === prev.mtimeMs && cur.size === prev.size)
|
|
75
|
+
return;
|
|
76
|
+
// An empty (or vanished) file is more likely a write in progress than an intent: SIGHUP applies one.
|
|
77
|
+
if (cur.size === 0)
|
|
78
|
+
return log.warn("the allow file is empty or gone; send SIGHUP to apply that", { file });
|
|
79
|
+
reload("allow-file-change");
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
process.on("SIGTERM", () => stop("SIGTERM"));
|
|
83
|
+
process.on("SIGINT", () => stop("SIGINT"));
|
|
84
|
+
}
|
|
85
|
+
main().catch((e) => {
|
|
86
|
+
process.stderr.write(`${JSON.stringify({ level: "error", msg: "relay failed to start", error: e.message })}\n`);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
});
|
package/dist/client.d.ts
ADDED
package/dist/client.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// The one client-side rule that belongs next to the relay (docs/protocol.md
|
|
4
|
+
// §5.2 step 2, review H5): before answering `hello`, a client MUST compare
|
|
5
|
+
// `hello.relay` with the canonical form of the URL it actually dialled, and
|
|
6
|
+
// close the connection when they differ. Otherwise a relay could relay the
|
|
7
|
+
// client's auth signature to a different relay that names itself differently.
|
|
8
|
+
//
|
|
9
|
+
// The relay signs nothing; this helper exists so clients and the relay's own
|
|
10
|
+
// tests share one implementation of the check.
|
|
11
|
+
/** True when `helloRelay` is exactly the canonical form of `dialledUrl`. */
|
|
12
|
+
export function helloMatchesDialled(crypto, dialledUrl, helloRelay) {
|
|
13
|
+
if (typeof helloRelay !== "string")
|
|
14
|
+
return false;
|
|
15
|
+
let dialled;
|
|
16
|
+
try {
|
|
17
|
+
dialled = crypto.canonicalRelayUrl(dialledUrl);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return helloRelay === dialled;
|
|
23
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { RelayCrypto } from "./crypto.js";
|
|
2
|
+
export interface RelayLimits {
|
|
3
|
+
/** Largest WebSocket frame accepted (§5.2: 96 KiB). */
|
|
4
|
+
frameBytes: number;
|
|
5
|
+
/** Largest envelope JWS (§1: 64 KiB). */
|
|
6
|
+
envelopeBytes: number;
|
|
7
|
+
/** Envelopes per mailbox before the oldest is dropped (§5.4). */
|
|
8
|
+
mailboxEnvelopes: number;
|
|
9
|
+
/** Bytes per mailbox before the oldest is dropped (§5.4: 64 MiB). */
|
|
10
|
+
mailboxBytes: number;
|
|
11
|
+
/** How long an un-acked envelope is kept (§5.4: 7 days). */
|
|
12
|
+
retentionSec: number;
|
|
13
|
+
/** Sends per minute per `env.from` (§5.4). */
|
|
14
|
+
sendsPerMinute: number;
|
|
15
|
+
/** Cap-less `contact` limits (§5.4 step 4). */
|
|
16
|
+
contactPerSenderPerHour: number;
|
|
17
|
+
contactPerPairPerHour: number;
|
|
18
|
+
contactPerIpPerHour: number;
|
|
19
|
+
/** Unauthenticated link-put/link-get per source IP (§5.7). */
|
|
20
|
+
linkOpsPerIpPerMinute: number;
|
|
21
|
+
/** Link mailbox lifetime (§5.7: 10 minutes). */
|
|
22
|
+
linkTtlSec: number;
|
|
23
|
+
/** Entries in a link mailbox's `req` slot (§5.7: 4). */
|
|
24
|
+
linkReqEntries: number;
|
|
25
|
+
/** Bytes per link entry (§5.7: 16 KiB). */
|
|
26
|
+
linkEntryBytes: number;
|
|
27
|
+
/** Link mailboxes one device may hold open at once. */
|
|
28
|
+
linkOpenPerDevice: number;
|
|
29
|
+
/** How long non-head identity versions are kept (§5.6: 90 days). */
|
|
30
|
+
identityVersionRetentionSec: number;
|
|
31
|
+
/** Largest `n` for the chain walk (§5.6: 64). */
|
|
32
|
+
chainMaxN: number;
|
|
33
|
+
/**
|
|
34
|
+
* Concurrent WebSocket connections, in total and per source IP. The per-IP
|
|
35
|
+
* value applies to IPv6 /64 buckets; IPv4 addresses, which are routinely
|
|
36
|
+
* shared (carrier NAT, campuses, offices), use `maxConnectionsPerIpv4` (F-5).
|
|
37
|
+
*/
|
|
38
|
+
maxConnections: number;
|
|
39
|
+
maxConnectionsPerIp: number;
|
|
40
|
+
maxConnectionsPerIpv4: number;
|
|
41
|
+
/** HTTP requests per source IP per minute. */
|
|
42
|
+
httpPerIpPerMinute: number;
|
|
43
|
+
/**
|
|
44
|
+
* How long a connection may stay unauthenticated. hello/auth is one round
|
|
45
|
+
* trip; link-put/link-get clients reconnect to keep polling.
|
|
46
|
+
*/
|
|
47
|
+
unauthTimeoutSec: number;
|
|
48
|
+
/** Unauthenticated connections at once (the rest of maxConnections stays free for authenticated ones). */
|
|
49
|
+
maxUnauthConnections: number;
|
|
50
|
+
/** Concurrent connections per userId. */
|
|
51
|
+
maxConnectionsPerIss: number;
|
|
52
|
+
/**
|
|
53
|
+
* Restricted connections (a valid certificate for a device the held document
|
|
54
|
+
* doesn't list; publish-only, §2.7 restore) per userId, kept apart from
|
|
55
|
+
* maxConnectionsPerIss so they can't crowd out listed devices.
|
|
56
|
+
*/
|
|
57
|
+
restrictedPerIss: number;
|
|
58
|
+
/** A restricted connection that hasn't been upgraded (its device listed) is closed after this long. */
|
|
59
|
+
restrictedTimeoutSec: number;
|
|
60
|
+
/** Identity publishes per userId per hour over restricted connections (charged only for a parsable document). */
|
|
61
|
+
restrictedIdentityPerIssPerHour: number;
|
|
62
|
+
/** Frames of any type per connection per minute. */
|
|
63
|
+
framesPerConnPerMinute: number;
|
|
64
|
+
/** Envelope bytes sent per connection, and per source IP (/64 for IPv6), per minute. */
|
|
65
|
+
sendBytesPerConnPerMinute: number;
|
|
66
|
+
sendBytesPerIpPerMinute: number;
|
|
67
|
+
/**
|
|
68
|
+
* Total envelope bytes the relay aims to store. Past it, the oldest
|
|
69
|
+
* envelopes of the largest mailboxes are evicted to make room.
|
|
70
|
+
*/
|
|
71
|
+
storeMaxBytes: number;
|
|
72
|
+
/** Hard ceiling: past it (eviction can't keep up), sends get `undeliverable`. */
|
|
73
|
+
storeHardMaxBytes: number;
|
|
74
|
+
/** Stored bytes per recipient userId; past it, that recipient's oldest are evicted. */
|
|
75
|
+
recipientMaxBytes: number;
|
|
76
|
+
/**
|
|
77
|
+
* Stored bytes per sending IP (/64 for IPv6): the long-horizon disk-fairness
|
|
78
|
+
* share. Past it, that source's OWN oldest envelopes are evicted; nothing is
|
|
79
|
+
* refused, so it isn't visible to a prober.
|
|
80
|
+
*/
|
|
81
|
+
sourceMaxBytes: number;
|
|
82
|
+
/**
|
|
83
|
+
* Bytes accepted from one sending IP per hour, refilling continuously. Past
|
|
84
|
+
* it, sends are rate-limited. Counts silently dropped contacts too and never
|
|
85
|
+
* refunds, so it reveals nothing about recipients.
|
|
86
|
+
*/
|
|
87
|
+
sourceAcceptedBytesPerHour: number;
|
|
88
|
+
/** Unauthenticated connections per source IP. */
|
|
89
|
+
maxUnauthPerIp: number;
|
|
90
|
+
/**
|
|
91
|
+
* New device bindings per source IP per day (dids listed in a held identity
|
|
92
|
+
* document are exempt): IPv6 /64, and IPv4.
|
|
93
|
+
*/
|
|
94
|
+
newDevicesPerIpPerDay: number;
|
|
95
|
+
newDevicesPerIpv4PerDay: number;
|
|
96
|
+
/** New WebSocket connections, and auth attempts, per source IP per minute (auths: IPv6 /64; IPv4 below). */
|
|
97
|
+
connectsPerIpPerMinute: number;
|
|
98
|
+
authsPerIpPerMinute: number;
|
|
99
|
+
authsPerIpv4PerMinute: number;
|
|
100
|
+
/** Device bindings (did -> iss rows) the relay keeps; new devices past it are refused. */
|
|
101
|
+
maxDevices: number;
|
|
102
|
+
/** `revoke` frames per connection per minute, and per userId per hour. */
|
|
103
|
+
revokesPerConnPerMinute: number;
|
|
104
|
+
revokesPerIssPerHour: number;
|
|
105
|
+
/** How long a did -> iss binding is kept after the device was last seen. */
|
|
106
|
+
deviceRetentionSec: number;
|
|
107
|
+
/** Identity publishes per userId, and per source IP, per hour. */
|
|
108
|
+
identityPublishPerIssPerHour: number;
|
|
109
|
+
identityPublishPerIpPerHour: number;
|
|
110
|
+
/** Branch tips (§2.5 forks) and versions kept per userId. */
|
|
111
|
+
identityMaxTips: number;
|
|
112
|
+
identityMaxVersions: number;
|
|
113
|
+
/** A userId whose head expired longer ago than this is tombstoned (documents deleted, allowlist kept). */
|
|
114
|
+
identityExpiredRetentionSec: number;
|
|
115
|
+
/** How long a tombstone keeps the last allowlist (longer than a revoked device's cert can live). */
|
|
116
|
+
identityTombstoneRetentionSec: number;
|
|
117
|
+
/** link-open per userId and per source IP per hour, and open link mailboxes per source IP. */
|
|
118
|
+
linkOpenPerIssPerHour: number;
|
|
119
|
+
linkOpenPerIpPerHour: number;
|
|
120
|
+
linkOpenPerIp: number;
|
|
121
|
+
/** Bytes held across all link mailboxes. */
|
|
122
|
+
linkTotalBytes: number;
|
|
123
|
+
/** P4 short-code invites: the longest TTL a put may ask for (7 days). */
|
|
124
|
+
inviteTtlMaxSec: number;
|
|
125
|
+
/** Bytes per sealed invite blob (32 KiB). */
|
|
126
|
+
inviteBlobBytes: number;
|
|
127
|
+
/** invite-put per userId and per source IP per hour. */
|
|
128
|
+
invitePutPerIssPerHour: number;
|
|
129
|
+
invitePutPerIpPerHour: number;
|
|
130
|
+
/** Invites one userId may hold open at once. */
|
|
131
|
+
inviteOpenPerIss: number;
|
|
132
|
+
/** Bytes held across every invite. */
|
|
133
|
+
inviteTotalBytes: number;
|
|
134
|
+
/** Bytes of open invites per source network (an IPv4 address, or an IPv6 /48). */
|
|
135
|
+
inviteBytesPerIp: number;
|
|
136
|
+
/**
|
|
137
|
+
* The last tenth of inviteTotalBytes is reserved: once past 90%, identities
|
|
138
|
+
* the relay first saw less than this long ago can't put.
|
|
139
|
+
*/
|
|
140
|
+
inviteYoungIdentitySec: number;
|
|
141
|
+
/** Unauthenticated invite-get per source IP per minute, and misses (not-found) per source IP per hour. */
|
|
142
|
+
inviteGetsPerIpPerMinute: number;
|
|
143
|
+
inviteMissesPerIpPerHour: number;
|
|
144
|
+
}
|
|
145
|
+
export declare const DEFAULT_LIMITS: Readonly<RelayLimits>;
|
|
146
|
+
/**
|
|
147
|
+
* SQLite tuning passed to the store. Unset fields keep the store's defaults
|
|
148
|
+
* (sqlite-store.ts: WAL watchdog limit 256 MiB, journal_size_limit 64 MiB,
|
|
149
|
+
* checkpoint every 100 ms).
|
|
150
|
+
*/
|
|
151
|
+
export interface StorageTuning {
|
|
152
|
+
/** A WAL larger than this means checkpoints aren't keeping up (the watchdog then intervenes). */
|
|
153
|
+
walLimitBytes?: number;
|
|
154
|
+
/** After a checkpoint, the WAL file is truncated back to this size. */
|
|
155
|
+
journalSizeLimitBytes?: number;
|
|
156
|
+
/** How often the background thread checkpoints the WAL (10 ms to 10 s). */
|
|
157
|
+
checkpointIntervalMs?: number;
|
|
158
|
+
}
|
|
159
|
+
export type RelayPresetName = "small";
|
|
160
|
+
export interface RelayPreset {
|
|
161
|
+
limits: Partial<RelayLimits>;
|
|
162
|
+
storage: StorageTuning;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Named presets that sit between DEFAULT_LIMITS and the JSON file / env vars.
|
|
166
|
+
* They only lower capacity limits; the protocol sizes (frames, envelopes,
|
|
167
|
+
* retention, link and invite formats) stay the protocol's.
|
|
168
|
+
*
|
|
169
|
+
* `small`: a private team relay (up to about 50 people) on a 2 GB single-board
|
|
170
|
+
* computer such as a Jetson Nano or a Raspberry Pi, with its database on a USB
|
|
171
|
+
* SSD. About 512 MiB of envelopes at most, a few hundred connections, and a
|
|
172
|
+
* smaller write-ahead log. Pair it with NODE_OPTIONS=--max-old-space-size=384
|
|
173
|
+
* and a container memory limit of 768 MiB (docs/run-a-relay-on-any-machine.md).
|
|
174
|
+
* Per-address limits are moderate: still far above what a 50-person team
|
|
175
|
+
* needs, but low enough that one outside address can't take every connection
|
|
176
|
+
* or fill the device table when the relay sits behind a public tunnel. An
|
|
177
|
+
* office behind one NAT address can raise them (docs/run-a-relay-on-any-machine.md).
|
|
178
|
+
*/
|
|
179
|
+
export declare const RELAY_PRESETS: Readonly<Record<RelayPresetName, Readonly<RelayPreset>>>;
|
|
180
|
+
/** Members-only: a redeemed invite admits its redeemer for 7 days unless the list or circle does first. */
|
|
181
|
+
export declare const DEFAULT_INVITE_ADMIT_SEC: number;
|
|
182
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
183
|
+
export interface RelayConfig {
|
|
184
|
+
/** This relay's canonical URL (§5.1), e.g. `wss://relay.example.org/v3`. */
|
|
185
|
+
relayUrl: string;
|
|
186
|
+
host: string;
|
|
187
|
+
port: number;
|
|
188
|
+
/** SQLite file, or `:memory:`. */
|
|
189
|
+
dbPath: string;
|
|
190
|
+
/** `hello.pow`: leading zero bits required on cap-less contacts (0..24). */
|
|
191
|
+
powBits: number;
|
|
192
|
+
limits: RelayLimits;
|
|
193
|
+
/** Shown in /.well-known/p2p-relay.json. */
|
|
194
|
+
name?: string;
|
|
195
|
+
operatorContact?: string;
|
|
196
|
+
policy?: string;
|
|
197
|
+
/**
|
|
198
|
+
* How many reverse proxies you run in front of the relay. 0 ignores
|
|
199
|
+
* X-Forwarded-For; n takes the client IP from the n-th entry from the right.
|
|
200
|
+
*/
|
|
201
|
+
trustProxy: number;
|
|
202
|
+
/** Separate /metrics listener (default 127.0.0.1:9464); null disables it. */
|
|
203
|
+
metricsPort: number | null;
|
|
204
|
+
metricsHost: string;
|
|
205
|
+
/** When set, /metrics is also served on the main port to `Authorization: Bearer <token>`. */
|
|
206
|
+
metricsToken?: string;
|
|
207
|
+
logLevel: LogLevel;
|
|
208
|
+
/** How often expired data is swept. */
|
|
209
|
+
sweepIntervalSec: number;
|
|
210
|
+
/**
|
|
211
|
+
* Serve `/debug/stats` (event-loop lag, memory, internal map sizes) on the
|
|
212
|
+
* metrics listener. Off by default; for the resilience suite and local profiling.
|
|
213
|
+
*/
|
|
214
|
+
debugStats: boolean;
|
|
215
|
+
/** The preset applied under the file and env vars, if any. */
|
|
216
|
+
preset?: RelayPresetName;
|
|
217
|
+
/** SQLite tuning for the store; empty keeps the store's defaults. */
|
|
218
|
+
storage?: StorageTuning;
|
|
219
|
+
/**
|
|
220
|
+
* Members-only mode (docs/run-a-relay.md): only these userIds (and, with
|
|
221
|
+
* `allowCircle`, the circle's current members) may authenticate. Unset with
|
|
222
|
+
* no `allowCircle`: an open relay. RELAY_ALLOW, the `allow` list in the
|
|
223
|
+
* JSON file, and the lines of `allowFile` (RELAY_ALLOW_FILE) together.
|
|
224
|
+
*/
|
|
225
|
+
allow?: string[];
|
|
226
|
+
/** A text file of userIds, one per line (`#` comments); re-read on SIGHUP. */
|
|
227
|
+
allowFile?: string;
|
|
228
|
+
/**
|
|
229
|
+
* Members-only: userIds never admitted, whatever the list, the circle or an
|
|
230
|
+
* invite says (RELAY_DENY, `deny` in the file, `!<userId>` lines of the allow
|
|
231
|
+
* file). How an operator removes one provisional member early.
|
|
232
|
+
*/
|
|
233
|
+
deny?: string[];
|
|
234
|
+
/** RELAY_ALLOW_CIRCLE: also admit the current members of this circle (its chain verified by the relay). */
|
|
235
|
+
allowCircle?: string;
|
|
236
|
+
/**
|
|
237
|
+
* Members-only: how long a userId that redeemed a member's invite code here
|
|
238
|
+
* stays admitted before it is on the list or in the circle (0: invites
|
|
239
|
+
* don't admit anyone).
|
|
240
|
+
*/
|
|
241
|
+
inviteAdmitSec: number;
|
|
242
|
+
}
|
|
243
|
+
/** `contactPerPairPerHour` → `LIMIT_CONTACT_PER_PAIR_PER_HOUR`. */
|
|
244
|
+
export declare function limitEnvName(key: keyof RelayLimits): string;
|
|
245
|
+
/**
|
|
246
|
+
* The text of an allow file: one userId per line, blank lines and `#`
|
|
247
|
+
* comments ignored. A line `!<userId>` denies that userId (never admitted,
|
|
248
|
+
* whatever the list, the circle or an invite says).
|
|
249
|
+
*/
|
|
250
|
+
export declare function parseAllowFile(text: string): {
|
|
251
|
+
allow: string[];
|
|
252
|
+
deny: string[];
|
|
253
|
+
};
|
|
254
|
+
export interface LoadConfigOptions {
|
|
255
|
+
env?: NodeJS.ProcessEnv;
|
|
256
|
+
readFile?: (path: string) => string;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Builds the config from defaults, the RELAY_CONFIG JSON file and env vars
|
|
260
|
+
* (env wins). Throws with a readable message on any bad value.
|
|
261
|
+
*/
|
|
262
|
+
export declare function loadConfig(crypto: RelayCrypto, opts?: LoadConfigOptions): RelayConfig;
|
|
263
|
+
/** A config for tests and embedding: defaults plus overrides, no env or file. */
|
|
264
|
+
export declare function makeConfig(crypto: RelayCrypto, over?: Partial<Omit<RelayConfig, "limits">> & {
|
|
265
|
+
limits?: Partial<RelayLimits>;
|
|
266
|
+
}): RelayConfig;
|