go-libp2p-kad-dht/opts/filters.go

94 lines
2.3 KiB
Go
Raw Normal View History

2020-03-03 02:58:39 -05:00
package dhtopts
import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
2020-03-06 15:03:32 -08:00
// QueryFilterFunc is a filter applied when considering peers to dial when querying
type QueryFilterFunc func(h host.Host, ai peer.AddrInfo) bool
// RouteTableFilterFunc is a filter applied when considering connections to keep in
// the local route table.
type RouteTableFilterFunc func(conns []network.Conn) bool
2020-03-03 02:58:39 -05:00
// PublicQueryFilter returns true if the peer is suspected of being publicly accessible
func PublicQueryFilter(h host.Host, ai peer.AddrInfo) bool {
if len(ai.Addrs) == 0 {
return false
}
var hasPublicAddr bool
for _, a := range ai.Addrs {
if isRelayAddr(a) {
return false
}
if manet.IsPublicAddr(a) {
hasPublicAddr = true
}
}
return hasPublicAddr
}
// PublicRoutingTableFilter allows a peer to be added to the routing table if the connections to that peer indicate
// that it is on a public network
func PublicRoutingTableFilter(conns []network.Conn) bool {
for _, c := range conns {
addr := c.RemoteMultiaddr()
if !isRelayAddr(addr) && manet.IsPublicAddr(addr) {
return true
}
}
return false
}
// PrivateQueryFilter returns true if the peer is suspected of being accessible over a shared private network
func PrivateQueryFilter(h host.Host, ai peer.AddrInfo) bool {
conns := h.Network().ConnsToPeer(ai.ID)
if len(conns) > 0 {
for _, c := range conns {
if manet.IsPrivateAddr(c.RemoteMultiaddr()) {
return true
}
}
return false
}
if len(ai.Addrs) == 0 {
return false
}
var hasPrivateAddr bool
for _, a := range ai.Addrs {
if manet.IsPublicAddr(a) {
if !isRelayAddr(a) {
return false
}
} else {
hasPrivateAddr = true
}
}
return hasPrivateAddr
}
// PrivateRoutingTableFilter allows a peer to be added to the routing table if the connections to that peer indicate
// that it is on a private network
func PrivateRoutingTableFilter(conns []network.Conn) bool {
for _, c := range conns {
if manet.IsPrivateAddr(c.RemoteMultiaddr()) {
return true
}
}
return false
}
func isRelayAddr(a ma.Multiaddr) bool {
2020-03-04 17:29:38 -05:00
val, err := a.ValueForProtocol(ma.P_CIRCUIT)
return err != nil && val != ""
2020-03-03 02:58:39 -05:00
}