diff --git a/go.mod b/go.mod index de7dc8c8..14989419 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,6 @@ require ( github.com/prometheus/client_golang v1.12.1 github.com/raulk/go-watchdog v1.2.0 github.com/stretchr/testify v1.7.0 - github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c diff --git a/go.sum b/go.sum index c398d27b..03b233d4 100644 --- a/go.sum +++ b/go.sum @@ -769,8 +769,6 @@ github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU= github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE= diff --git a/p2p/discovery/mdns_legacy/mdns.go b/p2p/discovery/mdns_legacy/mdns.go deleted file mode 100644 index e5283b52..00000000 --- a/p2p/discovery/mdns_legacy/mdns.go +++ /dev/null @@ -1,221 +0,0 @@ -package mdns_legacy - -import ( - "context" - "errors" - "io" - "net" - "sync" - "time" - - "github.com/libp2p/go-libp2p-core/host" - "github.com/libp2p/go-libp2p-core/peer" - mdnsnew "github.com/libp2p/go-libp2p/p2p/discovery/mdns" - - logging "github.com/ipfs/go-log/v2" - ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr/net" - "github.com/whyrusleeping/mdns" -) - -func init() { - // don't let mdns use logging... - mdns.DisableLogging = true -} - -var log = logging.Logger("mdns_legacy") - -const ServiceTag = "_ipfs-discovery._udp" - -type Service interface { - io.Closer - RegisterNotifee(Notifee) - UnregisterNotifee(Notifee) -} - -type Notifee = mdnsnew.Notifee - -type mdnsService struct { - server *mdns.Server - service *mdns.MDNSService - host host.Host - tag string - - lk sync.Mutex - notifees []Notifee - interval time.Duration -} - -func getDialableListenAddrs(ph host.Host) ([]*net.TCPAddr, error) { - var out []*net.TCPAddr - addrs, err := ph.Network().InterfaceListenAddresses() - if err != nil { - return nil, err - } - for _, addr := range addrs { - na, err := manet.ToNetAddr(addr) - if err != nil { - continue - } - tcp, ok := na.(*net.TCPAddr) - if ok { - out = append(out, tcp) - } - } - if len(out) == 0 { - return nil, errors.New("failed to find good external addr from peerhost") - } - return out, nil -} - -func NewMdnsService(ctx context.Context, peerhost host.Host, interval time.Duration, serviceTag string) (Service, error) { - - var ipaddrs []net.IP - port := 4001 - - addrs, err := getDialableListenAddrs(peerhost) - if err != nil { - log.Warn(err) - } else { - port = addrs[0].Port - for _, a := range addrs { - ipaddrs = append(ipaddrs, a.IP) - } - } - - myid := peerhost.ID().Pretty() - - info := []string{myid} - if serviceTag == "" { - serviceTag = ServiceTag - } - service, err := mdns.NewMDNSService(myid, serviceTag, "", "", port, ipaddrs, info) - if err != nil { - return nil, err - } - - // Create the mDNS server, defer shutdown - server, err := mdns.NewServer(&mdns.Config{Zone: service}) - if err != nil { - return nil, err - } - - s := &mdnsService{ - server: server, - service: service, - host: peerhost, - interval: interval, - tag: serviceTag, - } - - go s.pollForEntries(ctx) - - return s, nil -} - -func (m *mdnsService) Close() error { - return m.server.Shutdown() -} - -func (m *mdnsService) pollForEntries(ctx context.Context) { - ticker := time.NewTicker(m.interval) - defer ticker.Stop() - - for { - //execute mdns query right away at method call and then with every tick - entriesCh := make(chan *mdns.ServiceEntry, 16) - go func() { - for entry := range entriesCh { - m.handleEntry(entry) - } - }() - - log.Debug("starting mdns query") - qp := &mdns.QueryParam{ - Domain: "local", - Entries: entriesCh, - Service: m.tag, - Timeout: time.Second * 5, - } - - err := mdns.Query(qp) - if err != nil { - log.Warnw("mdns lookup error", "error", err) - } - close(entriesCh) - log.Debug("mdns query complete") - - select { - case <-ticker.C: - continue - case <-ctx.Done(): - log.Debug("mdns service halting") - return - } - } -} - -func (m *mdnsService) handleEntry(e *mdns.ServiceEntry) { - log.Debugf("Handling MDNS entry: [IPv4 %s][IPv6 %s]:%d %s", e.AddrV4, e.AddrV6, e.Port, e.Info) - mpeer, err := peer.Decode(e.Info) - if err != nil { - log.Warn("Error parsing peer ID from mdns entry: ", err) - return - } - - if mpeer == m.host.ID() { - log.Debug("got our own mdns entry, skipping") - return - } - - var addr net.IP - if e.AddrV4 != nil { - addr = e.AddrV4 - } else if e.AddrV6 != nil { - addr = e.AddrV6 - } else { - log.Warn("Error parsing multiaddr from mdns entry: no IP address found") - return - } - - maddr, err := manet.FromNetAddr(&net.TCPAddr{ - IP: addr, - Port: e.Port, - }) - if err != nil { - log.Warn("Error parsing multiaddr from mdns entry: ", err) - return - } - - pi := peer.AddrInfo{ - ID: mpeer, - Addrs: []ma.Multiaddr{maddr}, - } - - m.lk.Lock() - for _, n := range m.notifees { - go n.HandlePeerFound(pi) - } - m.lk.Unlock() -} - -func (m *mdnsService) RegisterNotifee(n Notifee) { - m.lk.Lock() - m.notifees = append(m.notifees, n) - m.lk.Unlock() -} - -func (m *mdnsService) UnregisterNotifee(n Notifee) { - m.lk.Lock() - found := -1 - for i, notif := range m.notifees { - if notif == n { - found = i - break - } - } - if found != -1 { - m.notifees = append(m.notifees[:found], m.notifees[found+1:]...) - } - m.lk.Unlock() -} diff --git a/p2p/discovery/mdns_legacy/mdns_test.go b/p2p/discovery/mdns_legacy/mdns_test.go deleted file mode 100644 index 17a5fe77..00000000 --- a/p2p/discovery/mdns_legacy/mdns_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package mdns_legacy - -import ( - "context" - "testing" - "time" - - bhost "github.com/libp2p/go-libp2p/p2p/host/basic" - swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing" - - "github.com/libp2p/go-libp2p-core/host" - "github.com/libp2p/go-libp2p-core/peer" - - "github.com/stretchr/testify/require" -) - -type DiscoveryNotifee struct { - h host.Host -} - -func (n *DiscoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { - n.h.Connect(context.Background(), pi) -} - -func TestMdnsDiscovery(t *testing.T) { - // TODO: re-enable when the new lib will get integrated - t.Skip("TestMdnsDiscovery fails randomly with current lib") - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - a, err := bhost.NewHost(swarmt.GenSwarm(t), nil) - require.NoError(t, err) - b, err := bhost.NewHost(swarmt.GenSwarm(t), nil) - require.NoError(t, err) - - sa, err := NewMdnsService(ctx, a, time.Second, "someTag") - require.NoError(t, err) - - sb, err := NewMdnsService(ctx, b, time.Second, "someTag") - require.NoError(t, err) - _ = sb - - n := &DiscoveryNotifee{a} - - sa.RegisterNotifee(n) - - time.Sleep(time.Second * 2) - - if err := a.Connect(ctx, peer.AddrInfo{ID: b.ID()}); err != nil { - t.Fatal(err) - } -}