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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user