Add bootstrap nodes, DNS-based discovery, a DHT per region and NAT traversal

- Bootstrap: built-in /dnsaddr/allmende.stinnesbeck.com (TXT records at
  _dnsaddr.<name>), resolved at every redial with the last good answer kept;
  --bootstrap takes /p2p/ or /dnsaddr/ entries, --no-default-bootstrap drops
  the built-in one. Stopping never waits for a stuck resolver.
- Remembered peers in peers.json (saved every 5 min and on shutdown, redialled
  at the next start)
- Private Kademlia DHT (/allmende prefix), advertised and looked up per region
  with quick first attempts and a backoff; --no-dht turns it off
- NAT traversal: port mapping, hole punching, AutoRelay through bootstrap nodes
- --public: DHT server and relay for a bootstrap node; prints the TXT records
- Docker, NixOS module (public, defaultBootstrap, QUIC/UDP) and docs updated
- e2e: bootstrap node scenario, seed cache restart, and polling that really polls

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 22:14:34 +01:00
co-authored by Claude Sonnet 5.5
parent a81595e9ee
commit e7bec4fb63
18 changed files with 1281 additions and 69 deletions
+29 -6
View File
@@ -23,7 +23,11 @@ let
cfg.httpAddress
"--listen"
"/ip4/0.0.0.0/tcp/${toString cfg.p2pPort}"
"--listen"
"/ip4/0.0.0.0/udp/${toString cfg.p2pPort}/quic-v1"
]
++ lib.optional cfg.public "--public"
++ lib.optional (!cfg.defaultBootstrap) "--no-default-bootstrap"
++ lib.concatMap (r: [
"--region"
r
@@ -47,10 +51,12 @@ in
regions = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "u1j" ];
description = ''
Regions to replicate: 3-character geohashes, each about 150 km wide.
`u1j` is Bonn, `u1h` Cologne, `u1x` Hamburg.
`u1j` is Bonn, `u1h` Cologne, `u1x` Hamburg. May be empty only for a
bootstrap node ({option}`public`).
'';
};
@@ -58,7 +64,23 @@ in
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "/ip4/203.0.113.5/tcp/4001/p2p/12D3KooW..." ];
description = "Known peers to connect to, as multiaddrs ending in /p2p/<peer id>.";
description = "Extra peers to connect to: /p2p/<peer id> multiaddrs or /dnsaddr/<name>.";
};
defaultBootstrap = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Also use the built-in bootstrap name (allmende.stinnesbeck.com).";
};
public = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
This machine is reachable from the internet: serve the DHT and relay for
nodes behind a NAT. Turn it on for a bootstrap node, and publish the TXT
records the node prints (see `journalctl -u allmende`) in DNS.
'';
};
httpAddress = lib.mkOption {
@@ -74,13 +96,13 @@ in
p2pPort = lib.mkOption {
type = lib.types.port;
default = 4001;
description = "TCP port other nodes connect to.";
description = "Port other nodes connect to, over TCP and over UDP (QUIC).";
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Open the peer-to-peer port ({option}`p2pPort`) in the firewall.";
description = "Open the peer-to-peer port ({option}`p2pPort`, TCP and UDP) in the firewall.";
};
dataDir = lib.mkOption {
@@ -100,8 +122,8 @@ in
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.regions != [ ];
message = "services.allmende.regions needs at least one region, for example [ \"u1j\" ].";
assertion = cfg.regions != [ ] || cfg.public;
message = "services.allmende.regions needs at least one region, for example [ \"u1j\" ] (only a public bootstrap node may have none).";
}
];
@@ -154,6 +176,7 @@ in
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.p2pPort ];
networking.firewall.allowedUDPPorts = lib.mkIf cfg.openFirewall [ cfg.p2pPort ]; # QUIC
environment.systemPackages = [ admin ];
};