Commit f6c38c9d authored by vyzo's avatar vyzo
Browse files

filter unspecific relay address

parent c6d1eebf
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
basic "github.com/libp2p/go-libp2p/p2p/host/basic" basic "github.com/libp2p/go-libp2p/p2p/host/basic"
autonat "github.com/libp2p/go-libp2p-autonat" autonat "github.com/libp2p/go-libp2p-autonat"
_ "github.com/libp2p/go-libp2p-circuit"
discovery "github.com/libp2p/go-libp2p-discovery" discovery "github.com/libp2p/go-libp2p-discovery"
host "github.com/libp2p/go-libp2p-host" host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net" inet "github.com/libp2p/go-libp2p-net"
...@@ -23,8 +24,18 @@ var ( ...@@ -23,8 +24,18 @@ var (
DesiredRelays = 3 DesiredRelays = 3
BootDelay = 60 * time.Second BootDelay = 60 * time.Second
unspecificRelay ma.Multiaddr
) )
func init() {
var err error
unspecificRelay, err = ma.NewMultiaddr("/p2p-circuit")
if err != nil {
panic(err)
}
}
// AutoRelayHost is a Host that uses relays for connectivity when a NAT is detected. // AutoRelayHost is a Host that uses relays for connectivity when a NAT is detected.
type AutoRelayHost struct { type AutoRelayHost struct {
*basic.BasicHost *basic.BasicHost
...@@ -61,7 +72,7 @@ func (h *AutoRelayHost) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr { ...@@ -61,7 +72,7 @@ func (h *AutoRelayHost) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
if h.addrs != nil && h.autonat.Status() == autonat.NATStatusPrivate { if h.addrs != nil && h.autonat.Status() == autonat.NATStatusPrivate {
return h.addrs return h.addrs
} else { } else {
return h.addrsF(addrs) return filterUnspecificRelay(h.addrsF(addrs))
} }
} }
...@@ -164,7 +175,7 @@ func (h *AutoRelayHost) doUpdateAddrs() { ...@@ -164,7 +175,7 @@ func (h *AutoRelayHost) doUpdateAddrs() {
h.mx.Lock() h.mx.Lock()
defer h.mx.Unlock() defer h.mx.Unlock()
addrs := h.addrsF(h.AllAddrs()) addrs := filterUnspecificRelay(h.addrsF(h.AllAddrs()))
raddrs := make([]ma.Multiaddr, 0, len(addrs)+len(h.relays)) raddrs := make([]ma.Multiaddr, 0, len(addrs)+len(h.relays))
// remove our public addresses from the list and replace them by just the public IP // remove our public addresses from the list and replace them by just the public IP
...@@ -216,6 +227,17 @@ func (h *AutoRelayHost) doUpdateAddrs() { ...@@ -216,6 +227,17 @@ func (h *AutoRelayHost) doUpdateAddrs() {
h.addrs = raddrs h.addrs = raddrs
} }
func filterUnspecificRelay(addrs []ma.Multiaddr) []ma.Multiaddr {
res := make([]ma.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
if addr.Equal(unspecificRelay) {
continue
}
res = append(res, addr)
}
return res
}
func shuffleRelays(pis []pstore.PeerInfo) { func shuffleRelays(pis []pstore.PeerInfo) {
for i := range pis { for i := range pis {
j := rand.Intn(i + 1) j := rand.Intn(i + 1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment