nini-relay 0.4.1__tar.gz
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.
- nini_relay-0.4.1/LICENSE +21 -0
- nini_relay-0.4.1/PKG-INFO +40 -0
- nini_relay-0.4.1/README.md +310 -0
- nini_relay-0.4.1/README.pypi.md +20 -0
- nini_relay-0.4.1/pyproject.toml +34 -0
nini_relay-0.4.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nini contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: nini-relay
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: Stateless public relay for NAT traversal (skeleton)
|
|
5
|
+
Project-URL: Homepage, https://nini.flygpt.cc
|
|
6
|
+
Project-URL: Repository, https://github.com/xusai2014/Nini
|
|
7
|
+
Author-email: Jerry xu <jerryxu521@gmail.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Requires-Dist: fastapi>=0.115
|
|
16
|
+
Requires-Dist: redis>=5
|
|
17
|
+
Requires-Dist: uvicorn[standard]>=0.32
|
|
18
|
+
Requires-Dist: websockets>=13
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# nini-relay
|
|
22
|
+
|
|
23
|
+
A stateless public relay for NAT traversal. Two endpoints that cannot reach each
|
|
24
|
+
other pair up through it, and the relay only ever forwards ciphertext — it holds
|
|
25
|
+
no session state and needs only Redis for connection affinity.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install nini-relay
|
|
29
|
+
nini-relay --port 8787 # see the repo docs for the Redis/URL setup
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- Self-host it if you do not want to use `relay.flygpt.cc`.
|
|
33
|
+
- Payloads are end-to-end encrypted by the daemon and clients; the relay routes
|
|
34
|
+
frames and nothing else.
|
|
35
|
+
- One process + one Redis is enough for a small team; scale out by adding nodes
|
|
36
|
+
behind a TCP load balancer.
|
|
37
|
+
|
|
38
|
+
Deployment notes (Docker image, nginx front, health checks) live in the repository.
|
|
39
|
+
|
|
40
|
+
Docs: <https://github.com/xusai2014/Nini> · License: MIT
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# nini-relay
|
|
2
|
+
|
|
3
|
+
Stateless public relay for Nini. It exists for one reason: a `nini-service`
|
|
4
|
+
daemon usually sits behind NAT and cannot accept inbound connections, so it dials
|
|
5
|
+
**out** to the relay and lets paired clients reach it through the relay.
|
|
6
|
+
|
|
7
|
+
The relay never parses a payload, never persists anything, never holds business
|
|
8
|
+
state. If it restarts, clients reconnect to the service and replay from their
|
|
9
|
+
last `seq` (contract §6).
|
|
10
|
+
|
|
11
|
+
It also keeps **no record of client addresses**: a client IP is used as an
|
|
12
|
+
in-memory rate-limit key while its window is open, and is never persisted, never
|
|
13
|
+
written to a log (not even masked) and never sent to a machine. See
|
|
14
|
+
[Client addresses](#client-addresses-not-collected).
|
|
15
|
+
|
|
16
|
+
- Contract: `docs/nini/protocol/20261202-nini-contract-v1.md` §6, §16, §19, §20
|
|
17
|
+
- Owner task: N5 (`apps/nini-relay/**`); v1.3 additions by N11
|
|
18
|
+
- Runtime deps: fastapi + uvicorn + websockets (+ `redis` when the Redis backend is on)
|
|
19
|
+
|
|
20
|
+
## Run
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
uv run nini-relay --host 127.0.0.1 --port 4097 # console script
|
|
24
|
+
uv run python -m nini_relay --port 4097 # module form
|
|
25
|
+
uv run nini-relay --text-logs --access-log # human logs + masked access log
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Env overrides: `NINI_RELAY_HOST`, `NINI_RELAY_PORT`, `NINI_RELAY_LOG_LEVEL`,
|
|
29
|
+
`NINI_RELAY_PING_INTERVAL`, `NINI_RELAY_PONG_TIMEOUT`, `NINI_RELAY_PAIRING_TTL`,
|
|
30
|
+
`NINI_RELAY_MAX_PAIRING_TTL`, `NINI_RELAY_CLIENT_TOKEN_TTL`, `NINI_RELAY_BACKEND`,
|
|
31
|
+
`NINI_RELAY_REDIS_URL`, `NINI_RELAY_ENV`, `NINI_RELAY_MAX_PAYLOAD`,
|
|
32
|
+
`NINI_RELAY_MAX_MESSAGE_BYTES`, `NINI_RELAY_RATE_LIMIT_PER_MIN`,
|
|
33
|
+
`NINI_RELAY_RATE_LIMIT_WINDOW`, `NINI_RELAY_TRUST_PROXY`, `NINI_RELAY_ACCESS_LOG`,
|
|
34
|
+
`NINI_RELAY_SHARE_PEER`, `NINI_RELAY_ADMIN_TOKEN`.
|
|
35
|
+
|
|
36
|
+
Registry backend (contract §20):
|
|
37
|
+
|
|
38
|
+
| env | default | meaning |
|
|
39
|
+
| ------------------------ | ---------------------------- | ------------------------------------------------------- |
|
|
40
|
+
| `NINI_RELAY_BACKEND` | `memory` | `memory` (single process) or `redis` (shared state) |
|
|
41
|
+
| `NINI_RELAY_REDIS_URL` | `redis://127.0.0.1:6379/0` | only read when `backend=redis` |
|
|
42
|
+
| `NINI_RELAY_ENV` | `dev` | redis key prefix: `nini:{env}:` |
|
|
43
|
+
| `NINI_RELAY_CLIENT_TOKEN_TTL` | `604800` (7 days) | reconnect-token lifetime |
|
|
44
|
+
|
|
45
|
+
Frame cap knobs:
|
|
46
|
+
|
|
47
|
+
| env | default | meaning |
|
|
48
|
+
| ------------------------------ | -------------------------- | -------------------------------------------------------------------------------------------- |
|
|
49
|
+
| `NINI_RELAY_MAX_PAYLOAD` | `8388608` (8 MiB) | cap on one **decoded** bridged payload — sized so a contract §15 WS RPC `body` of 8 MiB fits |
|
|
50
|
+
| `NINI_RELAY_MAX_MESSAGE_BYTES` | `4/3 * max_payload + 4096` | cap on the raw JSON text frame (base64 + envelope); derived from the payload cap when unset |
|
|
51
|
+
|
|
52
|
+
`NINI_RELAY_MAX_PAYLOAD_BYTES` is still read as a fallback for old deployments.
|
|
53
|
+
|
|
54
|
+
## HTTP
|
|
55
|
+
|
|
56
|
+
| Method | Path | Body / query | Response |
|
|
57
|
+
| ------ | ------------------------ | ------------------------------------------------ | ---------------------------------------------- |
|
|
58
|
+
| GET | `/health` | – | `{ok, name, version, machines, clients, backend, capabilities}` |
|
|
59
|
+
| GET | `/admin` | – | admin HTML shell (404 unless `NINI_RELAY_ADMIN_TOKEN` is set) |
|
|
60
|
+
| GET | `/admin/api/stats` | `Authorization: Bearer <token>` | counters JSON (404 unconfigured, 401 wrong/missing token) |
|
|
61
|
+
| GET | `/metrics` | `Authorization: Bearer <token>` | Prometheus text (same gate as stats) |
|
|
62
|
+
| POST | `/v1/pairing/register` | `{machine, key, code_hash, single_use?, ttl?}` | `{expires_at, single_use}` |
|
|
63
|
+
| POST | `/v1/pairing` | `{machine, key, code?, ttl?}` | `{code, expires_at}` (legacy v1.2, 30 days) |
|
|
64
|
+
| POST | `/v1/machine/rotate` | `{machine, old_key, new_key}` | `{rotated, machine}` |
|
|
65
|
+
|
|
66
|
+
Pairing codes belong to the **service** (contract §16). It generates a 10-char
|
|
67
|
+
Crockford Base32 code (50 bits), shows it as `XXXXX-XXXXX`, and registers
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
code_hash = base64url(sha256(utf8(canonical_code))) // 43 chars, unpadded
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`canonical_code` = uppercase, dashes/whitespace removed, `I`/`L`→`1`, `O`→`0`;
|
|
74
|
+
see `nini_relay/pairing.py` (mirrored in `nini_service/pairing.py`, pinned by a
|
|
75
|
+
test). The relay **stores nothing but the hash** — it cannot leak a usable code.
|
|
76
|
+
`single_use` defaults to `true` (relay default when the field is omitted); the
|
|
77
|
+
service sends `false` for one-code-onboards-three-ends pairing. `ttl` is seconds
|
|
78
|
+
(default 300, max 3600); a new code revokes that machine's previous code.
|
|
79
|
+
|
|
80
|
+
`POST /v1/pairing` remains for v1.2 machines for 30 days: it mints a 6-digit code,
|
|
81
|
+
returns it once and stores only its hash. It is always single-use.
|
|
82
|
+
|
|
83
|
+
`POST /v1/machine/rotate` authenticates with the **old** key, swaps it for the
|
|
84
|
+
new one and immediately closes the old `/v1/agent` connection with `4409`
|
|
85
|
+
(contract §19, no grace period). Every reconnect token of that machine dies with
|
|
86
|
+
it. A `/v1/agent` connection that presents a *different* key for an existing
|
|
87
|
+
`machine_id` is still refused with `401 invalid_key` — claiming a machine id is
|
|
88
|
+
not enough, you need the current key (or the rotate call).
|
|
89
|
+
|
|
90
|
+
## WebSocket: machine side
|
|
91
|
+
|
|
92
|
+
`WS /v1/agent?machine=<id>&key=<key>`
|
|
93
|
+
|
|
94
|
+
```jsonc
|
|
95
|
+
relay -> agent {"t":"ready","machine":"mac-1","v":1} // additive, registration ack
|
|
96
|
+
relay -> agent {"t":"open","ch":"ch_000001"} // a client attached (no address)
|
|
97
|
+
agent -> relay {"t":"frame","ch":"ch_000001","payload":"<b64>"}
|
|
98
|
+
relay -> agent {"t":"frame","ch":"ch_000001","payload":"<b64>"}
|
|
99
|
+
relay -> agent {"t":"close","ch":"ch_000001","reason":"disconnect"} // reason is additive
|
|
100
|
+
relay -> agent {"t":"ping"} agent -> relay {"t":"pong"} // every 25s
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
- A second connection for the same `machine` wins: the old socket is closed with
|
|
104
|
+
`4409` and its channels are closed with `machine_replaced`.
|
|
105
|
+
- Liveness: the relay pings every 25 s and drops the machine after 60 s without
|
|
106
|
+
inbound traffic. **Any** inbound frame counts as liveness, so the service does
|
|
107
|
+
not have to implement `pong` (answering it is still cheap and correct).
|
|
108
|
+
- A machine may `{"t":"close","ch":...}` a channel to hang up on a client.
|
|
109
|
+
|
|
110
|
+
## WebSocket: client side
|
|
111
|
+
|
|
112
|
+
`WS /v1/client?h=<code_hash>` (canonical) or `?ct=<client_token>` (reconnect).
|
|
113
|
+
Legacy `?code=<code>` still works for 30 days: the relay hashes it locally and
|
|
114
|
+
accepts both a 6-digit v1 code and a 10-char Crockford code.
|
|
115
|
+
|
|
116
|
+
```jsonc
|
|
117
|
+
relay -> client {"t":"route","machine":"mac-1","ch":"ch_000001","client_token":"<32B urlsafe>"}
|
|
118
|
+
client -> relay {"t":"frame","ch":"ch_000001","payload":"<b64>"}
|
|
119
|
+
relay -> client {"t":"frame","ch":"ch_000001","payload":"<b64>"}
|
|
120
|
+
relay -> client {"t":"error","code":"machine_offline","ch":"ch_000001"}
|
|
121
|
+
relay -> client {"t":"close","ch":"ch_000001"}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`client_token` is only present on the first route (it is minted then); the relay
|
|
125
|
+
stores `sha256(token)` and never echoes the token again.
|
|
126
|
+
|
|
127
|
+
- Unknown/expired code: `{"t":"error","code":"invalid_code"}` then close `4404`.
|
|
128
|
+
- Known code but machine not connected: `machine_offline` then close `4403`
|
|
129
|
+
(the code is **not** consumed, so retrying works).
|
|
130
|
+
- Single-use: the first successful route atomically consumes the code
|
|
131
|
+
(`consume_code`); later clients get `invalid_code`. With `single_use=false`
|
|
132
|
+
every client inside the TTL gets a channel of its own.
|
|
133
|
+
- Reconnect: send `?ct=<client_token>`. It routes straight to the machine,
|
|
134
|
+
consumes nothing and does not need the pairing code to still exist. Several
|
|
135
|
+
sockets may share one token (browser tabs, phone + web); each gets its own `ch`.
|
|
136
|
+
Expired or revoked token: `{"t":"error","code":"invalid_token"}` then close `4405`.
|
|
137
|
+
Tokens die on rotate-key and when a different key claims the machine.
|
|
138
|
+
- Close codes: `1000` normal, `1009` frame too big, `4403` machine offline,
|
|
139
|
+
`4404` invalid code, `4405` invalid token, `4408` heartbeat timeout,
|
|
140
|
+
`4411` machine gone/replaced, `4429` rate limited.
|
|
141
|
+
|
|
142
|
+
## Frame sequence (happy path + drops)
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
service relay client(web/mobile)
|
|
146
|
+
| WS /v1/agent | |
|
|
147
|
+
|----------------------->| accept, bind machine |
|
|
148
|
+
|<-- {"t":"ready"} ------| |
|
|
149
|
+
| | |
|
|
150
|
+
| generate 10-char code | |
|
|
151
|
+
| POST /v1/pairing/register {code_hash, single_use} |
|
|
152
|
+
|----------------------->| store hash only, TTL 300s |
|
|
153
|
+
|<-- {expires_at} -------| |
|
|
154
|
+
| `nini pair` prints XXXXX-XXXXX |
|
|
155
|
+
| |<--- WS /v1/client?h=<hash> ----|
|
|
156
|
+
| | resolve hash -> machine |
|
|
157
|
+
| | allocate ch_000001, consume |
|
|
158
|
+
| | mint client_token (7d) |
|
|
159
|
+
| |--- {"t":"route","ch","client_token"} -->|
|
|
160
|
+
|<-- {"t":"open","ch"} --| |
|
|
161
|
+
| | |
|
|
162
|
+
|-- {"t":"frame",ch,b64}->|--- {"t":"frame",ch,b64} ---->|
|
|
163
|
+
|<-- {"t":"frame",ch,b64}|--- {"t":"frame",ch,b64} -----|
|
|
164
|
+
| | |
|
|
165
|
+
| (client closes) | |
|
|
166
|
+
|<-- {"t":"close",ch} ---|<- disconnect -----------------|
|
|
167
|
+
| | drop channel, log |
|
|
168
|
+
| | |
|
|
169
|
+
| (machine drops) | |
|
|
170
|
+
|------- close --------->| close all its channels |
|
|
171
|
+
| |--- {"t":"error","code":"machine_offline"} -->|
|
|
172
|
+
| |--- WS close 4411 ------------->|
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Limits and safety rails
|
|
176
|
+
|
|
177
|
+
- **Frame size**: **8 MiB** of _decoded_ payload per frame by default
|
|
178
|
+
(`NINI_RELAY_MAX_PAYLOAD`, contract §15 allows an 8 MiB RPC `body`). Exactly
|
|
179
|
+
8 MiB passes; 8 MiB + 1 is refused. The raw JSON text frame is capped at
|
|
180
|
+
`4/3 * max_payload + 4 KiB` (`NINI_RELAY_MAX_MESSAGE_BYTES`), and uvicorn's
|
|
181
|
+
protocol-level `ws_max_size` is set to the same value so both layers agree.
|
|
182
|
+
Oversize → log `frame.oversize` and close with `1009` (the other side also
|
|
183
|
+
gets `frame_too_big` / `machine_offline`).
|
|
184
|
+
- **Binary frames** are rejected (`1003`): the wire format is JSON with base64.
|
|
185
|
+
- **Bad frames** (`bad_frame`, `bad_payload`, `bad_channel`, `unknown_type`) are
|
|
186
|
+
reported with an `error` frame and do not kill the bridge — except oversize.
|
|
187
|
+
- **Rate limit**: 60 connection attempts per IP per minute (WS upgrade and every
|
|
188
|
+
machine HTTP endpoint); over the limit the WS upgrade is refused with a real
|
|
189
|
+
HTTP `429` + `Retry-After`. The counter lives in process memory only: the
|
|
190
|
+
address is the key, the window is its lifetime, and nothing about it is
|
|
191
|
+
logged or persisted.
|
|
192
|
+
- **Machine authentication**: `machine_key` is a bearer credential. The first key
|
|
193
|
+
seen for a `machine_id` is registered; later mismatches are refused with
|
|
194
|
+
`401 invalid_key`. Rotation goes through `POST /v1/machine/rotate`, which is
|
|
195
|
+
authenticated by the old key (contract §19).
|
|
196
|
+
|
|
197
|
+
## Admin dashboard (opt-in)
|
|
198
|
+
|
|
199
|
+
Set `NINI_RELAY_ADMIN_TOKEN` to mount a read-only dashboard at `/admin`: live
|
|
200
|
+
machines/clients, total frames and bytes, pairing registrations/consumptions,
|
|
201
|
+
error counts and a 60-minute series, plus a per-machine table. It is a single
|
|
202
|
+
self-contained HTML file (no CDN, no external fonts) that polls
|
|
203
|
+
`/admin/api/stats` every 5 seconds.
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
NINI_RELAY_ADMIN_TOKEN=$(openssl rand -hex 32) uv run nini-relay --port 4097
|
|
207
|
+
# open http://127.0.0.1:4097/admin and paste the token
|
|
208
|
+
curl -H "Authorization: Bearer $NINI_RELAY_ADMIN_TOKEN" \
|
|
209
|
+
http://127.0.0.1:4097/admin/api/stats
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Rules that do not bend:
|
|
213
|
+
|
|
214
|
+
- token unset -> `/admin`, `/admin/api/stats` and `/metrics` answer **404**;
|
|
215
|
+
- token set -> the HTML shell is public (it holds no data) and every number
|
|
216
|
+
requires `Authorization: Bearer` (constant-time compare). `?token=` is **not**
|
|
217
|
+
accepted, because query strings end up in access logs and browser history.
|
|
218
|
+
- counters live in process memory only: no payload parsing, no client address,
|
|
219
|
+
no `user_id`, no persistence, and a restart starts from zero. The only
|
|
220
|
+
identifier is `machine_id` (an operator-chosen daemon id). See
|
|
221
|
+
[`docs/RELAY-DASHBOARD.md`](../../docs/RELAY-DASHBOARD.md) for the full metric
|
|
222
|
+
definitions and screenshots.
|
|
223
|
+
|
|
224
|
+
## Logging
|
|
225
|
+
|
|
226
|
+
JSON lines on stderr, one event per line, identifiers only:
|
|
227
|
+
|
|
228
|
+
```json
|
|
229
|
+
{
|
|
230
|
+
"ts": "2026-12-02T05:00:32.123Z",
|
|
231
|
+
"level": "info",
|
|
232
|
+
"event": "channel.opened",
|
|
233
|
+
"machine": "mac-1",
|
|
234
|
+
"ch": "ch_000001"
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Events: `relay.starting`, `relay.public_bind`, `relay.peer_shared`,
|
|
239
|
+
`machine.connected|closed|kicked|replaced|rejected|error`,
|
|
240
|
+
`machine.heartbeat_timeout`, `machine.rotated|rotate_rejected`,
|
|
241
|
+
`pairing.issued|registered|rejected`, `channel.opened|closed|error`,
|
|
242
|
+
`client.rejected`, `conn.rate_limited`, `frame.oversize`, `relay.stopped`.
|
|
243
|
+
`channel.closed` carries `frames_c2m`, `frames_m2c`, `reason`, `lifetime_s`.
|
|
244
|
+
**Payload bytes, pairing codes and client addresses are never logged.**
|
|
245
|
+
`conn.rate_limited` therefore says `kind` + `retry_after` and nothing about who
|
|
246
|
+
was limited.
|
|
247
|
+
|
|
248
|
+
Access log (`--access-log` / `NINI_RELAY_ACCESS_LOG=1`) is **off by default**.
|
|
249
|
+
When it is on, `MaskClientAddressFilter` rewrites the client address to its
|
|
250
|
+
network prefix (`203.0.113.0/24`, `2001:db8:1234::/48`) or `<ip>`, so even this
|
|
251
|
+
line cannot reconstruct a client IP.
|
|
252
|
+
|
|
253
|
+
## Client addresses (not collected)
|
|
254
|
+
|
|
255
|
+
| where | hold time | default |
|
|
256
|
+
| ----- | --------- | ------- |
|
|
257
|
+
| rate-limit key (`ratelimit.py`) | one sliding window, process memory | on |
|
|
258
|
+
| `open` frame `peer` field | sent to the machine | **off** (`NINI_RELAY_SHARE_PEER=0`) |
|
|
259
|
+
| structured logs | never | — |
|
|
260
|
+
| uvicorn access log | never in full (prefix only), log itself is off | off |
|
|
261
|
+
| registry / Redis / disk | never | — |
|
|
262
|
+
| admin dashboard (`metrics.py`) | process memory only; per-machine counters keyed by `machine_id` | on when `NINI_RELAY_ADMIN_TOKEN` is set |
|
|
263
|
+
|
|
264
|
+
`NINI_RELAY_SHARE_PEER=1` restores the legacy `{"t":"open",...,"peer":"<ip>"}`
|
|
265
|
+
field for an old machine build and logs `relay.peer_shared` at startup. The
|
|
266
|
+
official deployment leaves it off: machines have no need for the address, and
|
|
267
|
+
keeping it out is what lets the store/privacy declaration say "no client IP is
|
|
268
|
+
collected".
|
|
269
|
+
|
|
270
|
+
## Tests
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
uv run pytest apps/nini-relay -q # TestClient + real uvicorn/websockets, <20s
|
|
274
|
+
uv run python scripts/e2e_relay.py # subprocess relay: 20 frames, concurrency, 8 MiB boundary
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
`tests/test_privacy.py` pins the address policy: no `peer` on `open` (default),
|
|
278
|
+
opt-in only via `NINI_RELAY_SHARE_PEER=1`, no address in structured logs, no
|
|
279
|
+
address on `AgentConn`/`Channel`, per-address limiting still enforced from
|
|
280
|
+
memory, and access-log masking.
|
|
281
|
+
|
|
282
|
+
`Registry` (`nini_relay/registry.py`) abstracts the store (contract §20):
|
|
283
|
+
`register_machine / drop_machine / register_code / resolve_code / consume_code /
|
|
284
|
+
bind_channel / drop_channel`, plus reconnect tokens. Two backends ship:
|
|
285
|
+
`InMemoryRegistry` and `RedisRegistry` (`NINI_RELAY_BACKEND=redis`). Both run the
|
|
286
|
+
same parameterized test suite (`fakeredis`, no network). Redis keys live under
|
|
287
|
+
`nini:{env}:` and single-use claim is an atomic `GETDEL` — no Lua needed.
|
|
288
|
+
Live websockets/channels stay per-process (see known boundaries).
|
|
289
|
+
|
|
290
|
+
## Known boundaries (V1)
|
|
291
|
+
|
|
292
|
+
- **No end-to-end encryption**: TLS at the edge + short-lived pairing code only
|
|
293
|
+
(contract §6, ADR-0002 known gap). Anyone who sees a code within its TTL can
|
|
294
|
+
attach to that machine — treat codes like a one-time password.
|
|
295
|
+
- **Known horizontal-scaling gap**: machine keys, code hashes and reconnect tokens
|
|
296
|
+
*are* shared through Redis, but live sockets and channels are per process. A
|
|
297
|
+
client routed by instance B for a machine dialled into instance A currently
|
|
298
|
+
fails (`machine_offline`); real multi-instance routing needs a presence/pubsub
|
|
299
|
+
layer. With `backend=memory`, a relay restart forgets every binding and token.
|
|
300
|
+
- **`?ct=` is not in the frozen contract**: v1.3 §16 only defines `?h=`, so
|
|
301
|
+
`invalid_token` / close `4405` are additive (relay-signed reconnect tokens added
|
|
302
|
+
by N11 after N14 found that a consumed single-use code locks out auto-reconnect).
|
|
303
|
+
Clients that only speak v1.3 still work (they can pair again).
|
|
304
|
+
- **No message durability**: frames in flight during a drop are lost. Reconnect +
|
|
305
|
+
`since=seq` replay is the service's job, not the relay's.
|
|
306
|
+
- **Client liveness** relies on TCP/protocol-level pings (uvicorn) — the relay
|
|
307
|
+
only heartbeats the machine side.
|
|
308
|
+
- **Rate limiting is per IP per process**, and the key is in-memory only (see
|
|
309
|
+
[Client addresses](#client-addresses-not-collected)); behind a proxy set
|
|
310
|
+
`NINI_RELAY_TRUST_PROXY=1` so `X-Forwarded-For` is honoured.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# nini-relay
|
|
2
|
+
|
|
3
|
+
A stateless public relay for NAT traversal. Two endpoints that cannot reach each
|
|
4
|
+
other pair up through it, and the relay only ever forwards ciphertext — it holds
|
|
5
|
+
no session state and needs only Redis for connection affinity.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install nini-relay
|
|
9
|
+
nini-relay --port 8787 # see the repo docs for the Redis/URL setup
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- Self-host it if you do not want to use `relay.flygpt.cc`.
|
|
13
|
+
- Payloads are end-to-end encrypted by the daemon and clients; the relay routes
|
|
14
|
+
frames and nothing else.
|
|
15
|
+
- One process + one Redis is enough for a small team; scale out by adding nodes
|
|
16
|
+
behind a TCP load balancer.
|
|
17
|
+
|
|
18
|
+
Deployment notes (Docker image, nginx front, health checks) live in the repository.
|
|
19
|
+
|
|
20
|
+
Docs: <https://github.com/xusai2014/Nini> · License: MIT
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "nini-relay"
|
|
3
|
+
version = "0.4.1"
|
|
4
|
+
description = "Stateless public relay for NAT traversal (skeleton)"
|
|
5
|
+
readme = "README.pypi.md"
|
|
6
|
+
license = { text = "MIT" }
|
|
7
|
+
authors = [{ name = "Jerry xu", email = "jerryxu521@gmail.com" }]
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Programming Language :: Python :: 3.12",
|
|
10
|
+
"License :: OSI Approved :: MIT License",
|
|
11
|
+
"Operating System :: OS Independent",
|
|
12
|
+
"Environment :: Console",
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.12"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"fastapi>=0.115",
|
|
17
|
+
"redis>=5",
|
|
18
|
+
"uvicorn[standard]>=0.32",
|
|
19
|
+
"websockets>=13",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://nini.flygpt.cc"
|
|
24
|
+
Repository = "https://github.com/xusai2014/Nini"
|
|
25
|
+
|
|
26
|
+
[project.scripts]
|
|
27
|
+
nini-relay = "nini_relay.__main__:main"
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["hatchling"]
|
|
31
|
+
build-backend = "hatchling.build"
|
|
32
|
+
|
|
33
|
+
[tool.hatch.build.targets.wheel]
|
|
34
|
+
packages = ["nini_relay"]
|