Skip to content

M1: the NMDC hub (P1-P5, P7 TLS) on the arisrvx loop - #7

Merged
transfix merged 1 commit into
mainfrom
m1-nmdc-hub
Nov 16, 2026
Merged

transfix merged 1 commit into
mainfrom
m1-nmdc-hub

Conversation

@transfix

Copy link
Copy Markdown
Contributor

M1's NMDC hub, part 2 of 3. It is stacked on #6 (CI on libcvc 4.0.0), so the diff against ci-libcvc-4 is this PR's own. Retarget it to main once #6 merges. Part 3, interop with ncdc and AirDC++ web plus the 6000+ soak, follows as its own PR.

Clean room. This was written only from spec/nmdc/ (main 7b50715), the public documents it cites, this repository, and libcvc's docs and source for the host API. No Direct Connect implementation was opened.

Spec sections implemented

Spec Where What
P1 §1, §6 nmdc/listener.py, hub.py Framing, keepalives, unknown commands ignored, malformed-message counting, every limit in the §6 table
P1 §2 nmdc/codec.py The code page edge: Windows-1252 by default (D8), unassigned bytes kept as their code points, ? for what it can't hold
P1 §3 codec.py The HTML and DCN escapes
P1 §4, §5 codec.py, hub.py $Lock EXTENDEDPROTOCOL_sublevels_<24> Pk=sublevels<ver> and the $Key check. Lenient by default; SUBLEVELS_STRICT_KEY closes on a wrong key
P2 §1-§7 hub.py, nmdc/myinfo.py Covers the following:
- the states and the 30 s login timeout
- $Supports and the hub's own $Supports line
- nick rules, uniqueness, reservation
- $GetPass/$MyPass/$BadPass, password_requires_tls, the 5-per-10-minute failure limit
- $Hello
- $MyINFO kept verbatim, with the 10 s update interval and byte-identical updates skipped
- $GetINFO
- the user list for NoHello, NoGetINFO or neither; $OpList, $BotList, $UserIP
- $Quit, $HubTopic or the topic in $HubName, the message of the day, capacity ($HubIsFull)
P3 §1-§4 hub.py Main chat echoed to everyone, the sender included; $To: sent to the target only; the bot and +help/+motd/+rules (placeholder texts) in chat and by private message; the length and rate limits; the sender checks (3 bad lines close the connection)
P4 §1, §2 hub.py Covers the following:
- the query's shape check
- the active address rewritten to the connection's own
- passive searches only to active and unknown users
- ChatOnly users get no searches, and a $Search from one closes it
- one search per 10 s, a burst of 2, operators exempt, a notice once a minute
- $SR routed with its target stripped; at most 10 per responder per target, within 30 s
- each search encoded once and the same bytes written to every receiver
P5 §1-§3 hub.py $ConnectToMe in both forms, the address rewritten, port and suffix kept; $RevConnectToMe with the both-passive refusal; 20 requests per 10 s, 5 per remote
P7 §1 listener.py NMDCS on its own port (4111): TLS 1.2 and 1.3 only, the certificate reloaded when its file changes; tls_required with $ForceMove
D14 (M1 part) nmdc/counters.py, api.py In-memory counters, listed under "Also" below

Also:

  • D14 counters:
    • totals for connections by protocol, logins, chat and its fan-out, private messages, searches by kind and their fan-out, $SR, CTM/RCTM, and bytes in and out;
    • refusals, drops and closes, each by reason;
    • live connections by protocol, TLS connections, logging-in connections, the client mix, the modes and the total share.
    • GET /admin/stats serves them behind a bearer token from SUBLEVELS_ADMIN_TOKEN_FILE or SUBLEVELS_ADMIN_TOKEN; without a token the route answers 404. GET /stats is the public user count.
  • Published subtree: sublevels.hubs.main holds name, topic, bot, users.count, and users.list keyed u<connection id>, with nick, description, client, mode, share, protocol and op. Addresses are never published. It is written at most once a second.
  • Startup: server.py binds the listeners before host.run(), so an unbindable port stops the program with its traceback. On shutdown the hub closes its connections.
  • Settings: every setting is a HubConfig field, read from SUBLEVELS_* variables (nmdc/config.py). python -m sublevels.server adds --nmdc-listen and --nmdcs-listen.

Where the spec is silent or a "To capture" item blocks: what I chose

  1. $ValidateNick before $Key is ignored, as P2 §1's table says. A client that never sends $Key waits out the login timeout. The captures will show whether any client does this.
  2. $HubName is sent at connect, before $Supports is known, so it carries no topic. For a client without HubTopic, the hub sends $HubName <name> - <topic> again after login, when there is a topic.
  3. Chat's "5 lines in 10 s with a burst of 3" is a sliding window of 5 per any 10 s together with a token bucket of 3, refilled at 0.5 a second. Each dropped line gets a bot line. A dropped search gets a notice at most once a minute.
  4. $SR: a result more than 30 s after the target's last search is dropped. The 10-per-responder count restarts at each new search by the target.
  5. IPv6:
    • A rewritten $Search or $ConnectToMe address for an IPv6 connection is written as [addr]:port.
    • An IPv4-mapped IPv6 peer is treated as IPv4.
    • With two-character modes (IP64), the character for the connection's address family is used.
  6. $ConnectToMe suffixes: any trailing letters after the port are kept (S, NS, …), so NAT-traversal suffixes pass through until the captures say what they are.
  7. $RevConnectToMe between two passive users is never forwarded. P5 §2's NAT-traversal exception waits on a capture.
  8. Passive searches skip SOCKS5 users. P4 says "active or unknown".
  9. Hub commands: ! from a non-operator is ordinary chat. An unknown +command gets "No such command … Type +help."
  10. The message of the day is one bot line per line, then a +rules/+help hint.
  11. $GetNickList while online gets the whole list again.
  12. A $MyINFO of the wrong shape is dropped and counts as malformed. Only one for another nick during login closes the connection.
  13. Identity hash (D9) for NMDC is a salted hash of the address only, since M1 has no bans. The salt is random per run unless SUBLEVELS_IDENTITY_SALT is set.
  14. No registered accounts yet (TX.WTF is M2's). The password path is tested with a fake account store.
  15. Port 411 is the default, as the spec says. It needs the privilege to bind it, so CI and the tests use loopback ports.
  16. The newcomer's user list is written as one buffer rather than in pieces (P2 §6's SHOULD). The soak will show whether that delays other connections.
  17. Writes are gathered per event-loop turn and written as one transport.write. A reader whose unsent output passes 4 MiB is closed.

Not in M1: bans and location policy (D9, D11, D12), country on profiles (D13), $MCTo, ZLIB, TLS detection on the plain port (P7's MAY), operator commands (P6), and native users in the NMDC view (P2 §8).

Tests and checks

  • pytest:

    • test_nmdc_p1.py … test_nmdc_p5.py: the hub per spec item, without sockets, on a fake clock;
    • test_nmdc_listener.py: two clients on real sockets doing chat, PM, search, $SR, RCTM and CTM; an NMDCS login with a self-signed certificate; TLS 1.1 refused; a too-long message; a slow reader; the certificate reload;
    • the harness reading sublevels.hubs.main;
    • the admin API;
    • python -m sublevels.server with an NMDC login.
    • Locally on the 4.0.0 set: 100 passed, with the harness required. On PyPI's stack: 91 passed and the 9 pycvc tests skip.
  • The arisrvx smoke run (server/ci/smoke.sh, arisrvx 4.0.0+cvc.1, locally): tools/nmdc_client.py, run by arisrvx's own python, logs in over NMDC and over NMDCS, chats, and sees the echo. GET /admin/stats counts 2 logins; SIGTERM gives exit 0.

  • In-process timings with null connections at 6000 users. Not a soak; part 3 measures over sockets.

    What Time
    6000 logins 12.6 s in all, about n² since every login is announced to everyone
    memory grew 20 MB
    chat fan-out 1.9 ms
    active search fan-out 2.3 ms
    passive search fan-out (3000 receivers) 2.4 ms
    building the published user list 12 ms
    stats() 4.7 ms

@transfix
transfix changed the base branch from ci-libcvc-4 to main November 16, 2026 04:40
server/src/sublevels/nmdc/ implements spec/nmdc/ P1 (framing, the
Windows-1252 edge, escaping, $Lock/$Key, limits), P2 (the login state
machine, nicks, $MyINFO kept verbatim, the user list), P3 (main chat
echoed, $To, the hub bot with +help/+motd/+rules), P4 (search with the
address rewrite and passive-to-active-only fan-out, $SR routing), P5
($ConnectToMe/$RevConnectToMe with the rewrite and the both-passive
refusal) and P7 §1 (NMDCS on its own port, TLS 1.2+, certificate
reload). D14's counters are kept in memory and served at
/admin/stats behind a bearer token; /stats gives the user count. The
hub publishes sublevels.hubs.<id> (name, topic, user list without
addresses) for native clients.

Tests per spec item run without sockets; test_nmdc_listener.py runs
the hub on real plain and TLS sockets. The arisrvx smoke run now logs
in over NMDC and NMDCS and reads /admin/stats.
@transfix
transfix merged commit 9c32305 into main Nov 16, 2026
3 checks passed
@transfix
transfix deleted the m1-nmdc-hub branch November 16, 2026 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant