use manet.ToIP instead of hand-rolling multiaddr to IP conversion

This commit is contained in:
vyzo 2020-03-27 20:18:38 +02:00
parent 95094393a8
commit 68b86a4b66

View File

@ -3,6 +3,7 @@ package pubsub
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"sync" "sync"
"time" "time"
@ -10,7 +11,7 @@ import (
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-libp2p-core/protocol"
ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net"
) )
type peerStats struct { type peerStats struct {
@ -724,16 +725,25 @@ func (ps *peerScore) getIPs(p peer.ID) []string {
res := make([]string, 0, len(conns)) res := make([]string, 0, len(conns))
for _, c := range conns { for _, c := range conns {
remote := c.RemoteMultiaddr() remote := c.RemoteMultiaddr()
ip, err := manet.ToIP(remote)
if err != nil {
continue
}
ip4, err := remote.ValueForProtocol(ma.P_IP4) if len(ip) == 4 {
if err == nil { // IPv4 address
ip4 := ip.String()
res = append(res, ip4) res = append(res, ip4)
continue continue
} }
ip6, err := remote.ValueForProtocol(ma.P_IP6) if len(ip) == 16 {
if err == nil { // IPv6 address -- we add both the actual address and the /64 subnet
ip6 := ip.String()
res = append(res, ip6) res = append(res, ip6)
ip6mask := ip.Mask(net.CIDRMask(64, 128)).String()
res = append(res, ip6mask)
} }
} }