mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-07-05 15:41:36 +00:00
address outstanding discussion
This commit is contained in:
14
dht.go
14
dht.go
@ -205,7 +205,10 @@ func NewDHTClient(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
|
func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
|
||||||
rt, err := makeRoutingTable(h, cfg)
|
dht := new(IpfsDHT)
|
||||||
|
dht.host = h
|
||||||
|
|
||||||
|
rt, err := makeRoutingTable(dht, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to construct routing table,err=%s", err)
|
return nil, fmt.Errorf("failed to construct routing table,err=%s", err)
|
||||||
}
|
}
|
||||||
@ -223,7 +226,7 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dht := &IpfsDHT{
|
*dht = IpfsDHT{
|
||||||
datastore: cfg.datastore,
|
datastore: cfg.datastore,
|
||||||
self: h.ID(),
|
self: h.ID(),
|
||||||
peerstore: h.Peerstore(),
|
peerstore: h.Peerstore(),
|
||||||
@ -256,7 +259,8 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
|
|||||||
return dht, nil
|
return dht, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeRoutingTable(h host.Host, cfg config) (*kb.RoutingTable, error) {
|
func makeRoutingTable(dht *IpfsDHT, cfg config) (*kb.RoutingTable, error) {
|
||||||
|
h := dht.Host()
|
||||||
self := kb.ConvertPeerID(h.ID())
|
self := kb.ConvertPeerID(h.ID())
|
||||||
// construct the routing table with a peer validation function
|
// construct the routing table with a peer validation function
|
||||||
pvF := func(c context.Context, p peer.ID) bool {
|
pvF := func(c context.Context, p peer.ID) bool {
|
||||||
@ -264,7 +268,7 @@ func makeRoutingTable(h host.Host, cfg config) (*kb.RoutingTable, error) {
|
|||||||
rtPvLogger.Errorf("failed to connect to peer %s for validation, err=%s", p, err)
|
rtPvLogger.Errorf("failed to connect to peer %s for validation, err=%s", p, err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !cfg.routingTable.peerFilter(h, h.Network().ConnsToPeer(p)) {
|
if !cfg.routingTable.peerFilter(dht, h.Network().ConnsToPeer(p)) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -407,7 +411,7 @@ func (dht *IpfsDHT) peerFound(ctx context.Context, p peer.ID) {
|
|||||||
logger.Event(ctx, "peerFound", p)
|
logger.Event(ctx, "peerFound", p)
|
||||||
currentConns := dht.host.Network().ConnsToPeer(p)
|
currentConns := dht.host.Network().ConnsToPeer(p)
|
||||||
if len(currentConns) > 0 {
|
if len(currentConns) > 0 {
|
||||||
if dht.routingTablePeerFilter(dht.host, currentConns) {
|
if dht.routingTablePeerFilter(dht, currentConns) {
|
||||||
dht.routingTable.HandlePeerAlive(p)
|
dht.routingTable.HandlePeerAlive(p)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/host"
|
|
||||||
"github.com/libp2p/go-libp2p-core/network"
|
"github.com/libp2p/go-libp2p-core/network"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
netroute "github.com/libp2p/go-netroute"
|
netroute "github.com/libp2p/go-netroute"
|
||||||
@ -14,14 +13,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// QueryFilterFunc is a filter applied when considering peers to dial when querying
|
// QueryFilterFunc is a filter applied when considering peers to dial when querying
|
||||||
type QueryFilterFunc func(h host.Host, ai peer.AddrInfo) bool
|
type QueryFilterFunc func(dht *IpfsDHT, ai peer.AddrInfo) bool
|
||||||
|
|
||||||
// RouteTableFilterFunc is a filter applied when considering connections to keep in
|
// RouteTableFilterFunc is a filter applied when considering connections to keep in
|
||||||
// the local route table.
|
// the local route table.
|
||||||
type RouteTableFilterFunc func(h host.Host, conns []network.Conn) bool
|
type RouteTableFilterFunc func(dht *IpfsDHT, conns []network.Conn) bool
|
||||||
|
|
||||||
// PublicQueryFilter returns true if the peer is suspected of being publicly accessible
|
// PublicQueryFilter returns true if the peer is suspected of being publicly accessible
|
||||||
func PublicQueryFilter(h host.Host, ai peer.AddrInfo) bool {
|
func PublicQueryFilter(_ *IpfsDHT, ai peer.AddrInfo) bool {
|
||||||
if len(ai.Addrs) == 0 {
|
if len(ai.Addrs) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -38,9 +37,11 @@ func PublicQueryFilter(h host.Host, ai peer.AddrInfo) bool {
|
|||||||
return hasPublicAddr
|
return hasPublicAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ QueryFilterFunc = PublicQueryFilter
|
||||||
|
|
||||||
// PublicRoutingTableFilter allows a peer to be added to the routing table if the connections to that peer indicate
|
// 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
|
// that it is on a public network
|
||||||
func PublicRoutingTableFilter(_ host.Host, conns []network.Conn) bool {
|
func PublicRoutingTableFilter(_ *IpfsDHT, conns []network.Conn) bool {
|
||||||
for _, c := range conns {
|
for _, c := range conns {
|
||||||
addr := c.RemoteMultiaddr()
|
addr := c.RemoteMultiaddr()
|
||||||
if !isRelayAddr(addr) && manet.IsPublicAddr(addr) {
|
if !isRelayAddr(addr) && manet.IsPublicAddr(addr) {
|
||||||
@ -50,9 +51,11 @@ func PublicRoutingTableFilter(_ host.Host, conns []network.Conn) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ RouteTableFilterFunc = PublicRoutingTableFilter
|
||||||
|
|
||||||
// PrivateQueryFilter returns true if the peer is suspected of being accessible over a shared private network
|
// 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 {
|
func PrivateQueryFilter(dht *IpfsDHT, ai peer.AddrInfo) bool {
|
||||||
conns := h.Network().ConnsToPeer(ai.ID)
|
conns := dht.Host().Network().ConnsToPeer(ai.ID)
|
||||||
if len(conns) > 0 {
|
if len(conns) > 0 {
|
||||||
for _, c := range conns {
|
for _, c := range conns {
|
||||||
if manet.IsPrivateAddr(c.RemoteMultiaddr()) {
|
if manet.IsPrivateAddr(c.RemoteMultiaddr()) {
|
||||||
@ -80,12 +83,14 @@ func PrivateQueryFilter(h host.Host, ai peer.AddrInfo) bool {
|
|||||||
return hasPrivateAddr
|
return hasPrivateAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ QueryFilterFunc = PrivateQueryFilter
|
||||||
|
|
||||||
// PrivateRoutingTableFilter allows a peer to be added to the routing table if the connections to that peer indicate
|
// 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
|
// that it is on a private network
|
||||||
func PrivateRoutingTableFilter(h host.Host, conns []network.Conn) bool {
|
func PrivateRoutingTableFilter(dht *IpfsDHT, conns []network.Conn) bool {
|
||||||
router, _ := netroute.New()
|
router, _ := netroute.New()
|
||||||
myAdvertisedIPs := make([]net.IP, 0)
|
myAdvertisedIPs := make([]net.IP, 0)
|
||||||
for _, a := range h.Addrs() {
|
for _, a := range dht.Host().Addrs() {
|
||||||
if manet.IsPublicAddr(a) {
|
if manet.IsPublicAddr(a) {
|
||||||
ip, _ := manet.ToIP(a)
|
ip, _ := manet.ToIP(a)
|
||||||
myAdvertisedIPs = append(myAdvertisedIPs, ip)
|
myAdvertisedIPs = append(myAdvertisedIPs, ip)
|
||||||
@ -125,6 +130,8 @@ func PrivateRoutingTableFilter(h host.Host, conns []network.Conn) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ RouteTableFilterFunc = PrivateRoutingTableFilter
|
||||||
|
|
||||||
func isEUI(ip net.IP) bool {
|
func isEUI(ip net.IP) bool {
|
||||||
// per rfc 2373
|
// per rfc 2373
|
||||||
return ip[11] == 0xff && ip[12] == 0xfe
|
return ip[11] == 0xff && ip[12] == 0xfe
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/libp2p/go-libp2p-core/network"
|
"github.com/libp2p/go-libp2p-core/network"
|
||||||
"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"
|
||||||
host "github.com/libp2p/go-libp2p-host"
|
|
||||||
record "github.com/libp2p/go-libp2p-record"
|
record "github.com/libp2p/go-libp2p-record"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -58,8 +57,8 @@ type config struct {
|
|||||||
testProtocols []protocol.ID
|
testProtocols []protocol.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func emptyQueryFilter(h host.Host, ai peer.AddrInfo) bool { return true }
|
func emptyQueryFilter(_ *IpfsDHT, ai peer.AddrInfo) bool { return true }
|
||||||
func emptyRTFilter(h host.Host, conns []network.Conn) bool { return true }
|
func emptyRTFilter(_ *IpfsDHT, conns []network.Conn) bool { return true }
|
||||||
|
|
||||||
// apply applies the given options to this Option
|
// apply applies the given options to this Option
|
||||||
func (c *config) apply(opts ...Option) error {
|
func (c *config) apply(opts ...Option) error {
|
||||||
|
7
query.go
7
query.go
@ -3,6 +3,7 @@ package dht
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/network"
|
"github.com/libp2p/go-libp2p-core/network"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
"github.com/libp2p/go-libp2p-core/routing"
|
"github.com/libp2p/go-libp2p-core/routing"
|
||||||
@ -304,8 +305,12 @@ func (q *query) queryPeer(ctx context.Context, p peer.ID) *queryResult {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add any other know addresses for the candidate peer.
|
||||||
|
curInfo := q.dht.peerstore.PeerInfo(next.ID)
|
||||||
|
next.Addrs = append(next.Addrs, curInfo.Addrs...)
|
||||||
|
|
||||||
// add their addresses to the dialer's peerstore
|
// add their addresses to the dialer's peerstore
|
||||||
if q.dht.queryPeerFilter(q.dht.host, *next) {
|
if q.dht.queryPeerFilter(q.dht, *next) {
|
||||||
q.dht.peerstore.AddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL)
|
q.dht.peerstore.AddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL)
|
||||||
closer := q.localPeers.Add(next.ID)
|
closer := q.localPeers.Add(next.ID)
|
||||||
foundCloserPeer = foundCloserPeer || closer
|
foundCloserPeer = foundCloserPeer || closer
|
||||||
|
Reference in New Issue
Block a user