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
+21
View File
@@ -0,0 +1,21 @@
package main
import (
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
// bootstrapRecords turns a node's addresses into the DNS TXT values that make
// it a bootstrap node, one per publicly routable address. Loopback and private
// addresses are left out: nobody else could use them.
func bootstrapRecords(addrs []string) []string {
var out []string
for _, a := range addrs {
m, err := ma.NewMultiaddr(a)
if err != nil || !manet.IsPublicAddr(m) {
continue
}
out = append(out, "dnsaddr="+a)
}
return out
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"reflect"
"testing"
)
func TestBootstrapRecordsKeepOnlyPublicAddresses(t *testing.T) {
const id = "12D3KooWQYhTNQdmr3ArTeUHRYzFg94BKyTkoWBDWez9kSCVe2Xo"
got := bootstrapRecords([]string{
"/ip4/127.0.0.1/tcp/4001/p2p/" + id, // loopback
"/ip4/192.168.1.20/tcp/4001/p2p/" + id, // private
"/ip4/10.0.0.5/udp/4001/quic-v1/p2p/" + id, // private
"/ip4/93.184.216.34/tcp/4001/p2p/" + id, // public
"/ip4/93.184.216.34/udp/4001/quic-v1/p2p/" + id, // public
"/ip6/::1/tcp/4001/p2p/" + id, // loopback
"/ip6/2606:4700:4700::1111/tcp/4001/p2p/" + id, // public
"/ip4/203.0.113.5/tcp/4001/p2p/" + id, // a documentation range, never routable
"not an address",
})
want := []string{
"dnsaddr=/ip4/93.184.216.34/tcp/4001/p2p/" + id,
"dnsaddr=/ip4/93.184.216.34/udp/4001/quic-v1/p2p/" + id,
"dnsaddr=/ip6/2606:4700:4700::1111/tcp/4001/p2p/" + id,
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v\nwant %v", got, want)
}
}
+1
View File
@@ -21,6 +21,7 @@ const (
dbFile = "allmende.db"
tokenFile = "admin.token"
peersFile = "peers.json" // peers met on earlier runs
)
const usage = `Allmende: a free, distributed marketplace for homes.
+29 -2
View File
@@ -11,6 +11,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
@@ -34,7 +35,12 @@ func cmdRun(args []string) error {
data := dataFlag(fs)
var regions, bootstrap, listen listFlag
fs.Var(&regions, "region", "geohash region (3 characters, e.g. u1x) to replicate; repeatable")
fs.Var(&bootstrap, "bootstrap", "multiaddr of a known peer (…/p2p/<id>); repeatable")
fs.Var(&bootstrap, "bootstrap", "extra bootstrap peer: …/p2p/<id> or /dnsaddr/<name>; repeatable")
noDefaultBootstrap := fs.Bool("no-default-bootstrap", false,
"do not use the built-in bootstrap name "+p2p.DefaultBootstrap)
noDHT := fs.Bool("no-dht", false, "do not look for peers through the DHT")
public := fs.Bool("public", false,
"this node is reachable from the internet: serve the DHT and relay for nodes behind a NAT (for bootstrap nodes)")
fs.Var(&listen, "listen", "libp2p listen multiaddr; repeatable (default: all interfaces, random ports)")
httpAddr := fs.String("http", "127.0.0.1:8080", "address for the HTTP API")
noMDNS := fs.Bool("no-mdns", false, "do not look for peers on the local network")
@@ -46,9 +52,13 @@ func cmdRun(args []string) error {
"how many new conversations each of your listings can start per day")
fs.Parse(args)
if len(regions) == 0 {
// A bootstrap node may follow no region: it then only helps others connect.
if len(regions) == 0 && !*public {
return errors.New("choose at least one region to replicate, e.g. --region u1x (a region is a 3-character geohash, about 150 km wide)")
}
if !*noDefaultBootstrap {
bootstrap = append(bootstrap, p2p.DefaultBootstrap)
}
dev, err := identity.LoadDevice(*data)
if err != nil {
@@ -85,6 +95,9 @@ func cmdRun(args []string) error {
Bootstrap: bootstrap,
Regions: regions,
MDNS: !*noMDNS,
DHT: !*noDHT,
Public: *public,
PeerCache: filepath.Join(*data, peersFile),
Difficulty: *difficulty,
Logger: log,
@@ -139,6 +152,20 @@ func cmdRun(args []string) error {
log.Warn("the HTTP API is reachable from other machines; put it behind TLS if it is public")
}
if *public {
name := strings.TrimPrefix(p2p.DefaultBootstrap, "/dnsaddr/")
if recs := bootstrapRecords(node.Addrs()); len(recs) > 0 {
fmt.Fprintf(os.Stderr, "\nBootstrap node. For fresh nodes to find it, publish these TXT records at _dnsaddr.%s:\n", name)
for _, r := range recs {
fmt.Fprintf(os.Stderr, " %s\n", r)
}
fmt.Fprintln(os.Stderr)
} else {
log.Warn("--public, but this node has no public address; a bootstrap node needs one (a server, or a forwarded port): publish dnsaddr=/ip4/<public ip>/tcp/<port>/p2p/<peer id>",
"peer_id", node.ID().String())
}
}
if token != "" {
// Printed to the terminal, not logged: the token must not end up in a
// log file. It sits in the URL fragment, which a browser never sends to