Browse Source

filter relay addrs in address set cleanup

pull/598/head
vyzo 6 years ago
parent
commit
528c473840
  1. 21
      p2p/host/relay/addrsplosion.go
  2. 4
      p2p/host/relay/relay.go

21
p2p/host/relay/addrsplosion.go

@ -3,6 +3,7 @@ package relay
import (
"encoding/binary"
circuit "github.com/libp2p/go-libp2p-circuit"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
@ -14,6 +15,10 @@ func cleanupAddressSet(pi pstore.PeerInfo) pstore.PeerInfo {
var public, private []ma.Multiaddr
for _, a := range pi.Addrs {
if isRelayAddr(a) {
continue
}
if manet.IsPublicAddr(a) || isDNSAddr(a) {
public = append(public, a)
continue
@ -33,6 +38,22 @@ func cleanupAddressSet(pi pstore.PeerInfo) pstore.PeerInfo {
return pstore.PeerInfo{ID: pi.ID, Addrs: addrs}
}
func isRelayAddr(a ma.Multiaddr) bool {
isRelay := false
ma.ForEach(a, func(c ma.Component) bool {
switch c.Protocol().Code {
case circuit.P_CIRCUIT:
isRelay = true
return false
default:
return true
}
})
return isRelay
}
func isDNSAddr(a ma.Multiaddr) bool {
if first, _ := ma.SplitFirst(a); first != nil {
switch first.Protocol().Code {

4
p2p/host/relay/relay.go

@ -4,7 +4,6 @@ import (
"context"
"time"
circuit "github.com/libp2p/go-libp2p-circuit"
discovery "github.com/libp2p/go-libp2p-discovery"
ma "github.com/multiformats/go-multiaddr"
)
@ -29,8 +28,7 @@ func Advertise(ctx context.Context, advertise discovery.Advertiser) {
func Filter(addrs []ma.Multiaddr) []ma.Multiaddr {
raddrs := make([]ma.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
_, err := addr.ValueForProtocol(circuit.P_CIRCUIT)
if err == nil {
if isRelayAddr(addr) {
continue
}
raddrs = append(raddrs, addr)

Loading…
Cancel
Save