2018-07-17 18:39:04 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2018-11-15 02:25:06 +00:00
|
|
|
iaddr "github.com/ipfs/go-ipfs-addr"
|
2019-02-06 16:20:38 +00:00
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
2018-07-17 18:39:04 +00:00
|
|
|
ps "github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
mdns "github.com/libp2p/go-libp2p/p2p/discovery"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-11-26 16:18:43 +00:00
|
|
|
"go.opencensus.io/trace"
|
2018-07-17 18:39:04 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "p2p")
|
|
|
|
|
2018-07-17 18:39:04 +00:00
|
|
|
// Discovery interval for multicast DNS querying.
|
|
|
|
var discoveryInterval = 1 * time.Minute
|
|
|
|
|
|
|
|
// mDNSTag is the name of the mDNS service.
|
|
|
|
var mDNSTag = mdns.ServiceTag
|
|
|
|
|
2018-09-20 04:14:31 +00:00
|
|
|
// TODO(287): add other discovery protocols such as DHT, etc.
|
2018-11-15 02:25:06 +00:00
|
|
|
// startmDNSDiscovery supports discovery via multicast DNS peer discovery.
|
|
|
|
func startmDNSDiscovery(ctx context.Context, host host.Host) error {
|
2018-07-17 18:39:04 +00:00
|
|
|
mdnsService, err := mdns.NewMdnsService(ctx, host, discoveryInterval, mDNSTag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-29 16:32:54 +00:00
|
|
|
mdnsService.RegisterNotifee(&discovery{ctx, host})
|
2018-07-17 18:39:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-15 02:25:06 +00:00
|
|
|
// startDHTDiscovery supports discovery via DHT.
|
|
|
|
func startDHTDiscovery(ctx context.Context, host host.Host, bootstrapAddr string) error {
|
2018-11-26 16:18:43 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "p2p_startDHTDiscovery")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-04-27 19:08:27 +00:00
|
|
|
peerinfo, err := peerInfoFromAddr(bootstrapAddr)
|
2018-11-15 02:25:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = host.Connect(ctx, *peerinfo)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-27 19:08:27 +00:00
|
|
|
func peerInfoFromAddr(address string) (*ps.PeerInfo, error) {
|
|
|
|
addr, err := iaddr.ParseString(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ps.InfoFromP2pAddr(addr.Multiaddr())
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:39:04 +00:00
|
|
|
// Discovery implements mDNS notifee interface.
|
|
|
|
type discovery struct {
|
|
|
|
ctx context.Context
|
|
|
|
host host.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandlePeerFound registers the peer with the host.
|
|
|
|
func (d *discovery) HandlePeerFound(pi ps.PeerInfo) {
|
2018-08-14 16:16:21 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"peer addrs": pi.Addrs,
|
|
|
|
"peer id": pi.ID,
|
|
|
|
}).Debug("Attempting to connect to a peer")
|
|
|
|
|
2018-07-17 18:39:04 +00:00
|
|
|
d.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, ps.PermanentAddrTTL)
|
|
|
|
if err := d.host.Connect(d.ctx, pi); err != nil {
|
|
|
|
log.Warnf("Failed to connect to peer: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2019-04-27 20:05:19 +00:00
|
|
|
"peers": d.host.Network().Peers(),
|
2018-07-21 17:51:18 +00:00
|
|
|
}).Debug("Peers are now")
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|