allmende address: how others can join through a running node (status field 'share')

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-19 10:31:19 +01:00
co-authored by Claude Sonnet 5.5
parent d94c7b5b86
commit f83a305ee5
9 changed files with 246 additions and 3 deletions
+4 -2
View File
@@ -198,8 +198,10 @@ func (s *server) status(w http.ResponseWriter, r *http.Request) {
return
}
writeJSON(w, http.StatusOK, map[string]any{
"peer_id": s.Node.ID().String(),
"addrs": s.Node.Addrs(),
"peer_id": s.Node.ID().String(),
"addrs": s.Node.Addrs(),
// What someone else can pass to --bootstrap to join through this node.
"share": s.Node.ShareAddrs(),
"author": s.Device.Cert.Author,
"device": s.Device.Cert.Device,
"regions": s.regions(),
+47
View File
@@ -0,0 +1,47 @@
package p2p
import (
"slices"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
// ShareAddrs returns the addresses another person could use to join the
// network through this node (`allmende run --bootstrap <address>`): the ones
// reachable from the internet, with a direct address before one through a
// relay. Local and private addresses are left out, as nobody else can dial
// them. A node behind a router has none until it knows its public address or
// has taken a relay reservation, which takes a few seconds after the start.
func (n *Node) ShareAddrs() []string { return shareable(n.Addrs()) }
func shareable(addrs []string) []string {
var direct, relayed []string
for _, a := range addrs {
m, err := ma.NewMultiaddr(a)
if err != nil || !manet.IsPublicAddr(m) {
continue
}
if isCircuit(m) {
relayed = append(relayed, a)
} else {
direct = append(direct, a)
}
}
slices.Sort(direct)
slices.Sort(relayed)
return append(direct, relayed...)
}
// isCircuit reports whether m goes through a relay (contains /p2p-circuit).
func isCircuit(m ma.Multiaddr) bool {
found := false
ma.ForEach(m, func(c ma.Component) bool {
if c.Protocol().Code == ma.P_CIRCUIT {
found = true
return false
}
return true
})
return found
}
+39
View File
@@ -0,0 +1,39 @@
package p2p
import (
"slices"
"testing"
)
const (
idA = "12D3KooWQsiJgoN2o9KzvSFdvB5RZPursnQUqSD3v5i9sXrZZciw"
idB = "12D3KooWPA8Gigzxf2cBVkBKytSh8HUPpcufsFdVmiin8RQ8tFnx"
)
func TestOnlyAddressesFromTheInternetAreShared(t *testing.T) {
got := shareable([]string{
"/ip4/127.0.0.1/tcp/4001/p2p/" + idB,
"/ip4/192.168.1.20/tcp/4001/p2p/" + idB,
"/ip6/::1/tcp/4001/p2p/" + idB,
"/ip4/10.1.2.3/udp/4001/quic-v1/p2p/" + idB,
"/ip4/1.1.1.1/tcp/4001/p2p/" + idA + "/p2p-circuit/p2p/" + idB, // through a relay
"/ip4/8.8.8.8/tcp/4001/p2p/" + idB, // direct
"/dns4/node.example.org/tcp/4001/p2p/" + idB,
"not an address",
})
want := []string{
"/dns4/node.example.org/tcp/4001/p2p/" + idB,
"/ip4/8.8.8.8/tcp/4001/p2p/" + idB,
"/ip4/1.1.1.1/tcp/4001/p2p/" + idA + "/p2p-circuit/p2p/" + idB, // relayed last
}
if !slices.Equal(got, want) {
t.Fatalf("got\n %v\nwant\n %v", got, want)
}
}
func TestANodeWithOnlyLocalAddressesSharesNothing(t *testing.T) {
a := startNode(t, nil, "u1x") // listens on 127.0.0.1 only
if got := a.ShareAddrs(); len(got) != 0 {
t.Fatalf("shared %v", got)
}
}