diff --git a/examples/chat-with-mdns/README.md b/examples/chat-with-mdns/README.md index 05833f32e7..5b09f604b8 100644 --- a/examples/chat-with-mdns/README.md +++ b/examples/chat-with-mdns/README.md @@ -57,12 +57,12 @@ func handleStream(stream net.Stream) { 3. **Find peers nearby using mdns** -Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery_legacy#NewMdnsService) service in host. +Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#NewMdnsService) service in host. ```go -ser, err := discovery.NewMdnsService(ctx, peerhost, time.Hour, rendezvous) +ser, err := discovery.NewMdnsService(peerhost, rendezvous) ``` -register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery_legacy#Notifee) with service so that we get notified about peer discovery +register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#Notifee) with service so that we get notified about peer discovery ```go n := &discoveryNotifee{} diff --git a/examples/go.sum b/examples/go.sum index 3eea8d2567..7c2de5c88d 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -94,7 +94,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -558,6 +561,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= diff --git a/examples/ipfs-camp-2019/06-Pubsub/main.go b/examples/ipfs-camp-2019/06-Pubsub/main.go index f92c255ce2..b5296e0cf8 100644 --- a/examples/ipfs-camp-2019/06-Pubsub/main.go +++ b/examples/ipfs-camp-2019/06-Pubsub/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" @@ -18,8 +17,8 @@ import ( mplex "github.com/libp2p/go-libp2p-mplex" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" - tcp "github.com/libp2p/go-tcp-transport" + "github.com/libp2p/go-libp2p/p2p/discovery" + "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" ) @@ -102,10 +101,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) } - mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "") - if err != nil { - panic(err) - } + mdns := discovery.NewMdnsService(host, "") notifee := &discoveryNotifee{h: host, ctx: ctx} mdns.RegisterNotifee(notifee) diff --git a/examples/ipfs-camp-2019/07-Messaging/main.go b/examples/ipfs-camp-2019/07-Messaging/main.go index dba70a1618..3a720373a5 100644 --- a/examples/ipfs-camp-2019/07-Messaging/main.go +++ b/examples/ipfs-camp-2019/07-Messaging/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" @@ -17,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + "github.com/libp2p/go-libp2p/p2p/discovery" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -110,10 +109,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) - mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "") - if err != nil { - panic(err) - } + mdns := discovery.NewMdnsService(host, "") mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx}) err = dht.Bootstrap(ctx) diff --git a/examples/ipfs-camp-2019/08-End/main.go b/examples/ipfs-camp-2019/08-End/main.go index b08760d71f..dc7bbbebaa 100644 --- a/examples/ipfs-camp-2019/08-End/main.go +++ b/examples/ipfs-camp-2019/08-End/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" @@ -17,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + "github.com/libp2p/go-libp2p/p2p/discovery" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -109,10 +108,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) - mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "") - if err != nil { - panic(err) - } + mdns := discovery.NewMdnsService(host, "") mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx}) err = dht.Bootstrap(ctx) diff --git a/examples/ipfs-camp-2019/go.sum b/examples/ipfs-camp-2019/go.sum index 6772a4273d..10542b29c6 100644 --- a/examples/ipfs-camp-2019/go.sum +++ b/examples/ipfs-camp-2019/go.sum @@ -95,7 +95,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -561,6 +564,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= @@ -862,7 +867,6 @@ github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a/go.mod h1:x6AKhvS github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -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= diff --git a/examples/pubsub/chat/go.sum b/examples/pubsub/chat/go.sum index f06373c0e9..5a4f7ed164 100644 --- a/examples/pubsub/chat/go.sum +++ b/examples/pubsub/chat/go.sum @@ -94,7 +94,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -523,6 +526,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= @@ -821,7 +826,6 @@ github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49u github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -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= diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go index 3ca19be345..43c60b49a5 100644 --- a/examples/pubsub/chat/main.go +++ b/examples/pubsub/chat/main.go @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/peer" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + "github.com/libp2p/go-libp2p/p2p/discovery" "github.com/libp2p/go-libp2p-core/host" @@ -107,10 +107,7 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { // This lets us automatically discover peers on the same LAN and connect to them. func setupDiscovery(ctx context.Context, h host.Host) error { // setup mDNS discovery to find local peers - disc, err := discovery.NewMdnsService(ctx, h, DiscoveryInterval, DiscoveryServiceTag) - if err != nil { - return err - } + disc := discovery.NewMdnsService(h, DiscoveryServiceTag) n := discoveryNotifee{h: h} disc.RegisterNotifee(&n) diff --git a/go.mod b/go.mod index f1b6563a9b..8a6b8f7fa7 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/libp2p/go-stream-muxer-multistream v0.3.0 github.com/libp2p/go-tcp-transport v0.2.7 github.com/libp2p/go-ws-transport v0.5.0 + github.com/libp2p/zeroconf/v2 v2.0.0 github.com/miekg/dns v1.1.43 // indirect github.com/multiformats/go-multiaddr v0.4.0 github.com/multiformats/go-multiaddr-dns v0.3.1 diff --git a/go.sum b/go.sum index 0b4da93fdf..5f5cec0c9b 100644 --- a/go.sum +++ b/go.sum @@ -95,7 +95,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -598,6 +601,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= diff --git a/p2p/discovery/mdns.go b/p2p/discovery/mdns.go new file mode 100644 index 0000000000..bfd0ec72c9 --- /dev/null +++ b/p2p/discovery/mdns.go @@ -0,0 +1,218 @@ +package discovery + +import ( + "context" + "errors" + "io" + "net" + "strings" + "sync" + + logging "github.com/ipfs/go-log/v2" + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/zeroconf/v2" + + ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr/net" +) + +const ( + ServiceName = "_p2p._udp" + mdnsDomain = "local" + dnsaddrPrefix = "dnsaddr=" +) + +var log = logging.Logger("mdns") + +type Service interface { + io.Closer + RegisterNotifee(Notifee) + UnregisterNotifee(Notifee) +} + +type Notifee interface { + HandlePeerFound(peer.AddrInfo) +} + +type mdnsService struct { + host host.Host + serviceName string + + // This ctx is passed to the resolver. + // It is closed when Close() is called. + ctx context.Context + ctxCancel context.CancelFunc + + resolverRunning chan struct{} + server *zeroconf.Server + + mutex sync.Mutex + notifees []Notifee +} + +func NewMdnsService(host host.Host, serviceName string) *mdnsService { + ctx, cancel := context.WithCancel(context.Background()) + if serviceName == "" { + serviceName = ServiceName + } + s := &mdnsService{ + ctx: ctx, + ctxCancel: cancel, + resolverRunning: make(chan struct{}), + host: host, + serviceName: serviceName, + } + s.startServer() + s.startResolver() + return s +} + +func (s *mdnsService) Close() error { + s.ctxCancel() + if s.server != nil { + s.server.Shutdown() + } + <-s.resolverRunning + return nil +} + +// We don't really care about the IP addresses, but the spec (and various routers / firewalls) require us +// to send A and AAAA records. +func (s *mdnsService) getIPs(addrs []ma.Multiaddr) ([]string, error) { + var ip4, ip6 string + for _, addr := range addrs { + network, hostport, err := manet.DialArgs(addr) + if err != nil { + continue + } + host, _, err := net.SplitHostPort(hostport) + if err != nil { + continue + } + if ip4 == "" && (network == "udp4" || network == "tcp4") { + ip4 = host + } else if ip6 == "" && (network == "udp6" || network == "tcp6") { + ip6 = host + } + } + ips := make([]string, 0, 2) + if ip4 != "" { + ips = append(ips, ip4) + } + if ip6 != "" { + ips = append(ips, ip6) + } + if len(ips) == 0 { + return nil, errors.New("didn't find any IP addresses") + } + return ips, nil +} + +func (s *mdnsService) mdnsInstance() string { + return string(s.host.ID()) +} + +func (s *mdnsService) startServer() error { + interfaceAddrs, err := s.host.Network().InterfaceListenAddresses() + if err != nil { + return err + } + addrs, err := peer.AddrInfoToP2pAddrs(&peer.AddrInfo{ + ID: s.host.ID(), + Addrs: interfaceAddrs, + }) + if err != nil { + return err + } + var txts []string + for _, addr := range addrs { + if manet.IsThinWaist(addr) { // don't announce circuit addresses + txts = append(txts, dnsaddrPrefix+addr.String()) + } + } + + ips, err := s.getIPs(addrs) + if err != nil { + return err + } + + server, err := zeroconf.RegisterProxy( + s.mdnsInstance(), + s.serviceName, + mdnsDomain, + 4001, + s.host.ID().Pretty(), // TODO: deals with peer IDs longer than 63 characters + ips, + txts, + nil, + ) + if err != nil { + return err + } + s.server = server + return nil +} + +func (s *mdnsService) startResolver() { + entryChan := make(chan *zeroconf.ServiceEntry, 1000) + go func() { + for entry := range entryChan { + // We only care about the TXT records. + // Ignore A, AAAA and PTR. + addrs := make([]ma.Multiaddr, 0, len(entry.Text)) // assume that all TXT records are dnsaddrs + for _, s := range entry.Text { + if !strings.HasPrefix(s, dnsaddrPrefix) { + log.Debug("missing dnsaddr prefix") + continue + } + addr, err := ma.NewMultiaddr(s[len(dnsaddrPrefix):]) + if err != nil { + log.Debugf("failed to parse multiaddr: %s", err) + continue + } + addrs = append(addrs, addr) + } + infos, err := peer.AddrInfosFromP2pAddrs(addrs...) + if err != nil { + log.Debugf("failed to get peer info: %s", err) + continue + } + s.mutex.Lock() + for _, info := range infos { + for _, notif := range s.notifees { + go notif.HandlePeerFound(info) + } + } + s.mutex.Unlock() + } + }() + go func() { + defer close(s.resolverRunning) + if err := zeroconf.Browse(s.ctx, s.serviceName, mdnsDomain, entryChan); err != nil { + log.Debugf("zeroconf browsing failed: %s", err) + } + }() +} + +func (s *mdnsService) RegisterNotifee(n Notifee) { + s.mutex.Lock() + s.notifees = append(s.notifees, n) + s.mutex.Unlock() +} + +func (s *mdnsService) UnregisterNotifee(n Notifee) { + s.mutex.Lock() + defer s.mutex.Unlock() + + found := -1 + for i, notif := range s.notifees { + if notif == n { + found = i + break + } + } + if found != -1 { + s.notifees = append(s.notifees[:found], s.notifees[found+1:]...) + } +} diff --git a/p2p/discovery/mdns_test.go b/p2p/discovery/mdns_test.go new file mode 100644 index 0000000000..7f7b2a547c --- /dev/null +++ b/p2p/discovery/mdns_test.go @@ -0,0 +1,118 @@ +package discovery + +import ( + "context" + "sync" + "testing" + "time" + + "github.com/libp2p/go-libp2p" + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/peer" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func setupMDNS(t *testing.T, notifee Notifee) (host.Host, *mdnsService) { + t.Helper() + host, err := libp2p.New(context.Background(), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) + require.NoError(t, err) + s := NewMdnsService(host, "") + s.RegisterNotifee(notifee) + return host, s +} + +type notif struct { + mutex sync.Mutex + infos []peer.AddrInfo +} + +var _ Notifee = ¬if{} + +func (n *notif) HandlePeerFound(info peer.AddrInfo) { + n.mutex.Lock() + n.infos = append(n.infos, info) + n.mutex.Unlock() +} + +func (n *notif) GetPeers() []peer.AddrInfo { + n.mutex.Lock() + defer n.mutex.Unlock() + infos := make([]peer.AddrInfo, 0, len(n.infos)) + infos = append(infos, n.infos...) + return infos +} + +func TestSelfDiscovery(t *testing.T) { + notif := ¬if{} + host, s := setupMDNS(t, notif) + defer s.Close() + assert.Eventuallyf( + t, + func() bool { + var found bool + for _, info := range notif.GetPeers() { + if info.ID == host.ID() { + found = true + break + } + } + return found + }, + 5*time.Second, + 5*time.Millisecond, + "expected peer to find itself", + ) +} + +func TestOtherDiscovery(t *testing.T) { + const n = 4 + + notifs := make([]*notif, n) + hostIDs := make([]peer.ID, n) + for i := 0; i < n; i++ { + notif := ¬if{} + notifs[i] = notif + var s *mdnsService + host, s := setupMDNS(t, notif) + hostIDs[i] = host.ID() + defer s.Close() + } + + containsAllHostIDs := func(ids []peer.ID) bool { + for _, id := range hostIDs { + var found bool + for _, i := range ids { + if id == i { + found = true + break + } + } + if !found { + return false + } + } + return true + } + + assert.Eventuallyf( + t, + func() bool { + for _, notif := range notifs { + infos := notif.GetPeers() + ids := make([]peer.ID, 0, len(infos)) + for _, info := range infos { + ids = append(ids, info.ID) + } + if !containsAllHostIDs(ids) { + return false + } + } + return true + }, + 25*time.Second, + 5*time.Millisecond, + "expected peers to find each other", + ) +} diff --git a/p2p/discovery_legacy/mdns.go b/p2p/discovery_legacy/mdns.go index d78ace24bf..b1d0c025d8 100644 --- a/p2p/discovery_legacy/mdns.go +++ b/p2p/discovery_legacy/mdns.go @@ -1,15 +1,15 @@ -package discovery +package discovery_legacy import ( "context" "errors" - "io" "net" "sync" "time" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p/p2p/discovery" logging "github.com/ipfs/go-log/v2" ma "github.com/multiformats/go-multiaddr" @@ -22,19 +22,12 @@ func init() { mdns.DisableLogging = true } -var log = logging.Logger("mdns") +var log = logging.Logger("mdns_legacy") const ServiceTag = "_ipfs-discovery._udp" -type Service interface { - io.Closer - RegisterNotifee(Notifee) - UnregisterNotifee(Notifee) -} - -type Notifee interface { - HandlePeerFound(peer.AddrInfo) -} +type Service = discovery.Service +type Notifee = discovery.Notifee type mdnsService struct { server *mdns.Server diff --git a/p2p/discovery_legacy/mdns_test.go b/p2p/discovery_legacy/mdns_test.go index 8307eb0db4..cfedfb6068 100644 --- a/p2p/discovery_legacy/mdns_test.go +++ b/p2p/discovery_legacy/mdns_test.go @@ -1,4 +1,4 @@ -package discovery +package discovery_legacy import ( "context"