From 2c67a96430bb0e459014ec79765d709367fa9643 Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 16 Oct 2018 14:39:19 +0300 Subject: [PATCH 1/5] extract service from package --- p2p/host/autonat/addr.go | 12 +- p2p/host/autonat/autonat_test.go | 85 +++++++++--- p2p/host/autonat/proto.go | 15 --- p2p/host/autonat/svc.go | 214 ------------------------------- p2p/host/autonat/svc_test.go | 131 ------------------- 5 files changed, 75 insertions(+), 382 deletions(-) delete mode 100644 p2p/host/autonat/svc.go delete mode 100644 p2p/host/autonat/svc_test.go diff --git a/p2p/host/autonat/addr.go b/p2p/host/autonat/addr.go index 4e078e3765..eee254f8a0 100644 --- a/p2p/host/autonat/addr.go +++ b/p2p/host/autonat/addr.go @@ -6,7 +6,7 @@ import ( ma "github.com/multiformats/go-multiaddr" ) -var private4, private6 []*net.IPNet +var Private4, Private6 []*net.IPNet var privateCIDR4 = []string{ // localhost "127.0.0.0/8", @@ -28,8 +28,8 @@ var privateCIDR6 = []string{ } func init() { - private4 = parsePrivateCIDR(privateCIDR4) - private6 = parsePrivateCIDR(privateCIDR6) + Private4 = parsePrivateCIDR(privateCIDR4) + Private6 = parsePrivateCIDR(privateCIDR6) } func parsePrivateCIDR(cidrs []string) []*net.IPNet { @@ -44,15 +44,15 @@ func parsePrivateCIDR(cidrs []string) []*net.IPNet { return ipnets } -func isPublicAddr(a ma.Multiaddr) bool { +func IsPublicAddr(a ma.Multiaddr) bool { ip, err := a.ValueForProtocol(ma.P_IP4) if err == nil { - return !inAddrRange(ip, private4) + return !inAddrRange(ip, Private4) } ip, err = a.ValueForProtocol(ma.P_IP6) if err == nil { - return !inAddrRange(ip, private6) + return !inAddrRange(ip, Private6) } return false diff --git a/p2p/host/autonat/autonat_test.go b/p2p/host/autonat/autonat_test.go index 6d31d46a87..6170d01d19 100644 --- a/p2p/host/autonat/autonat_test.go +++ b/p2p/host/autonat/autonat_test.go @@ -2,12 +2,18 @@ package autonat import ( "context" - "net" "testing" "time" - libp2p "github.com/libp2p/go-libp2p" + pb "github.com/libp2p/go-libp2p-autonat/pb" + + ggio "github.com/gogo/protobuf/io" + bhost "github.com/libp2p/go-libp2p-blankhost" host "github.com/libp2p/go-libp2p-host" + inet "github.com/libp2p/go-libp2p-net" + pstore "github.com/libp2p/go-libp2p-peerstore" + swarmt "github.com/libp2p/go-libp2p-swarm/testing" + ma "github.com/multiformats/go-multiaddr" ) func init() { @@ -17,24 +23,76 @@ func init() { AutoNATIdentifyDelay = 100 * time.Millisecond } -func makeAutoNAT(ctx context.Context, t *testing.T) (host.Host, AutoNAT) { - h, err := libp2p.New(ctx, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) - if err != nil { - t.Fatal(err) +// these are mock service implementations for testing +func makeAutoNATServicePrivate(ctx context.Context, t *testing.T) host.Host { + h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h.SetStreamHandler(AutoNATProto, sayAutoNATPrivate) + return h +} + +func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host { + h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) + h.SetStreamHandler(AutoNATProto, sayAutoNATPublic) + return h +} + +func sayAutoNATPrivate(s inet.Stream) { + defer s.Close() + w := ggio.NewDelimitedWriter(s) + res := pb.Message{ + Type: pb.Message_DIAL_RESPONSE.Enum(), + DialResponse: newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses"), } + w.WriteMsg(&res) +} + +func sayAutoNATPublic(s inet.Stream) { + defer s.Close() + w := ggio.NewDelimitedWriter(s) + res := pb.Message{ + Type: pb.Message_DIAL_RESPONSE.Enum(), + DialResponse: newDialResponseOK(s.Conn().RemoteMultiaddr()), + } + w.WriteMsg(&res) +} +func newDialResponseOK(addr ma.Multiaddr) *pb.Message_DialResponse { + dr := new(pb.Message_DialResponse) + dr.Status = pb.Message_OK.Enum() + dr.Addr = addr.Bytes() + return dr +} + +func newDialResponseError(status pb.Message_ResponseStatus, text string) *pb.Message_DialResponse { + dr := new(pb.Message_DialResponse) + dr.Status = status.Enum() + dr.StatusText = &text + return dr +} + +func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) { + h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) a := NewAutoNAT(ctx, h) + a.(*AmbientAutoNAT).peers[ash.ID()] = struct{}{} return h, a } -// Note: these tests assume the host has only private inet addresses! +func connect(t *testing.T, a, b host.Host) { + pinfo := pstore.PeerInfo{ID: a.ID(), Addrs: a.Addrs()} + err := b.Connect(context.Background(), pinfo) + if err != nil { + t.Fatal(err) + } +} + +// tests func TestAutoNATPrivate(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - hs, _ := makeAutoNATService(ctx, t) - hc, an := makeAutoNAT(ctx, t) + hs := makeAutoNATServicePrivate(ctx, t) + hc, an := makeAutoNAT(ctx, t, hs) status := an.Status() if status != NATStatusUnknown { @@ -54,11 +112,8 @@ func TestAutoNATPublic(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - save := private4 - private4 = []*net.IPNet{} - - hs, _ := makeAutoNATService(ctx, t) - hc, an := makeAutoNAT(ctx, t) + hs := makeAutoNATServicePublic(ctx, t) + hc, an := makeAutoNAT(ctx, t, hs) status := an.Status() if status != NATStatusUnknown { @@ -72,6 +127,4 @@ func TestAutoNATPublic(t *testing.T) { if status != NATStatusPublic { t.Fatalf("unexpected NAT status: %d", status) } - - private4 = save } diff --git a/p2p/host/autonat/proto.go b/p2p/host/autonat/proto.go index c9768cbdbd..2dbb8ebaa7 100644 --- a/p2p/host/autonat/proto.go +++ b/p2p/host/autonat/proto.go @@ -5,7 +5,6 @@ import ( logging "github.com/ipfs/go-log" pstore "github.com/libp2p/go-libp2p-peerstore" - ma "github.com/multiformats/go-multiaddr" ) const AutoNATProto = "/libp2p/autonat/1.0.0" @@ -25,17 +24,3 @@ func newDialMessage(pi pstore.PeerInfo) *pb.Message { return msg } - -func newDialResponseOK(addr ma.Multiaddr) *pb.Message_DialResponse { - dr := new(pb.Message_DialResponse) - dr.Status = pb.Message_OK.Enum() - dr.Addr = addr.Bytes() - return dr -} - -func newDialResponseError(status pb.Message_ResponseStatus, text string) *pb.Message_DialResponse { - dr := new(pb.Message_DialResponse) - dr.Status = status.Enum() - dr.StatusText = &text - return dr -} diff --git a/p2p/host/autonat/svc.go b/p2p/host/autonat/svc.go deleted file mode 100644 index 4d395d22ec..0000000000 --- a/p2p/host/autonat/svc.go +++ /dev/null @@ -1,214 +0,0 @@ -package autonat - -import ( - "context" - "sync" - "time" - - pb "github.com/libp2p/go-libp2p-autonat/pb" - - ggio "github.com/gogo/protobuf/io" - libp2p "github.com/libp2p/go-libp2p" - host "github.com/libp2p/go-libp2p-host" - inet "github.com/libp2p/go-libp2p-net" - peer "github.com/libp2p/go-libp2p-peer" - pstore "github.com/libp2p/go-libp2p-peerstore" - ma "github.com/multiformats/go-multiaddr" -) - -const P_CIRCUIT = 290 - -var ( - AutoNATServiceDialTimeout = 42 * time.Second - AutoNATServiceResetInterval = 1 * time.Minute - - AutoNATServiceThrottle = 3 -) - -// AutoNATService provides NAT autodetection services to other peers -type AutoNATService struct { - ctx context.Context - dialer host.Host - - // rate limiter - mx sync.Mutex - reqs map[peer.ID]int -} - -// NewAutoNATService creates a new AutoNATService instance attached to a host -func NewAutoNATService(ctx context.Context, h host.Host, opts ...libp2p.Option) (*AutoNATService, error) { - opts = append(opts, libp2p.NoListenAddrs) - dialer, err := libp2p.New(ctx, opts...) - if err != nil { - return nil, err - } - - as := &AutoNATService{ - ctx: ctx, - dialer: dialer, - reqs: make(map[peer.ID]int), - } - h.SetStreamHandler(AutoNATProto, as.handleStream) - - go as.resetRateLimiter() - - return as, nil -} - -func (as *AutoNATService) handleStream(s inet.Stream) { - defer s.Close() - - pid := s.Conn().RemotePeer() - log.Debugf("New stream from %s", pid.Pretty()) - - r := ggio.NewDelimitedReader(s, inet.MessageSizeMax) - w := ggio.NewDelimitedWriter(s) - - var req pb.Message - var res pb.Message - - err := r.ReadMsg(&req) - if err != nil { - log.Debugf("Error reading message from %s: %s", pid.Pretty(), err.Error()) - s.Reset() - return - } - - t := req.GetType() - if t != pb.Message_DIAL { - log.Debugf("Unexpected message from %s: %s (%d)", pid.Pretty(), t.String(), t) - s.Reset() - return - } - - dr := as.handleDial(pid, s.Conn().RemoteMultiaddr(), req.GetDial().GetPeer()) - res.Type = pb.Message_DIAL_RESPONSE.Enum() - res.DialResponse = dr - - err = w.WriteMsg(&res) - if err != nil { - log.Debugf("Error writing response to %s: %s", pid.Pretty(), err.Error()) - s.Reset() - return - } -} - -func (as *AutoNATService) handleDial(p peer.ID, obsaddr ma.Multiaddr, mpi *pb.Message_PeerInfo) *pb.Message_DialResponse { - if mpi == nil { - return newDialResponseError(pb.Message_E_BAD_REQUEST, "missing peer info") - } - - mpid := mpi.GetId() - if mpid != nil { - mp, err := peer.IDFromBytes(mpid) - if err != nil { - return newDialResponseError(pb.Message_E_BAD_REQUEST, "bad peer id") - } - - if mp != p { - return newDialResponseError(pb.Message_E_BAD_REQUEST, "peer id mismatch") - } - } - - addrs := make([]ma.Multiaddr, 0) - seen := make(map[string]struct{}) - - // add observed addr to the list of addresses to dial - if !as.skipDial(obsaddr) { - addrs = append(addrs, obsaddr) - seen[obsaddr.String()] = struct{}{} - } - - for _, maddr := range mpi.GetAddrs() { - addr, err := ma.NewMultiaddrBytes(maddr) - if err != nil { - log.Debugf("Error parsing multiaddr: %s", err.Error()) - continue - } - - if as.skipDial(addr) { - continue - } - - str := addr.String() - _, ok := seen[str] - if ok { - continue - } - - addrs = append(addrs, addr) - seen[str] = struct{}{} - } - - if len(addrs) == 0 { - return newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses") - } - - return as.doDial(pstore.PeerInfo{ID: p, Addrs: addrs}) -} - -func (as *AutoNATService) skipDial(addr ma.Multiaddr) bool { - // skip relay addresses - _, err := addr.ValueForProtocol(P_CIRCUIT) - if err == nil { - return true - } - - // skip private network (unroutable) addresses - if !isPublicAddr(addr) { - return true - } - - return false -} - -func (as *AutoNATService) doDial(pi pstore.PeerInfo) *pb.Message_DialResponse { - // rate limit check - as.mx.Lock() - count := as.reqs[pi.ID] - if count >= AutoNATServiceThrottle { - as.mx.Unlock() - return newDialResponseError(pb.Message_E_DIAL_REFUSED, "too many dials") - } - as.reqs[pi.ID] = count + 1 - as.mx.Unlock() - - ctx, cancel := context.WithTimeout(as.ctx, AutoNATServiceDialTimeout) - defer cancel() - - err := as.dialer.Connect(ctx, pi) - if err != nil { - log.Debugf("error dialing %s: %s", pi.ID.Pretty(), err.Error()) - // wait for the context to timeout to avoid leaking timing information - // this renders the service ineffective as a port scanner - <-ctx.Done() - return newDialResponseError(pb.Message_E_DIAL_ERROR, "dial failed") - } - - conns := as.dialer.Network().ConnsToPeer(pi.ID) - if len(conns) == 0 { - log.Errorf("supposedly connected to %s, but no connection to peer", pi.ID.Pretty()) - return newDialResponseError(pb.Message_E_INTERNAL_ERROR, "internal service error") - } - - ra := conns[0].RemoteMultiaddr() - as.dialer.Network().ClosePeer(pi.ID) - return newDialResponseOK(ra) -} - -func (as *AutoNATService) resetRateLimiter() { - ticker := time.NewTicker(AutoNATServiceResetInterval) - defer ticker.Stop() - - for { - select { - case <-ticker.C: - as.mx.Lock() - as.reqs = make(map[peer.ID]int) - as.mx.Unlock() - - case <-as.ctx.Done(): - return - } - } -} diff --git a/p2p/host/autonat/svc_test.go b/p2p/host/autonat/svc_test.go deleted file mode 100644 index cfa4068bfa..0000000000 --- a/p2p/host/autonat/svc_test.go +++ /dev/null @@ -1,131 +0,0 @@ -package autonat - -import ( - "context" - "net" - "testing" - "time" - - libp2p "github.com/libp2p/go-libp2p" - host "github.com/libp2p/go-libp2p-host" - pstore "github.com/libp2p/go-libp2p-peerstore" -) - -func makeAutoNATService(ctx context.Context, t *testing.T) (host.Host, *AutoNATService) { - h, err := libp2p.New(ctx, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) - if err != nil { - t.Fatal(err) - } - - as, err := NewAutoNATService(ctx, h) - if err != nil { - t.Fatal(err) - } - - return h, as -} - -func makeAutoNATClient(ctx context.Context, t *testing.T) (host.Host, AutoNATClient) { - h, err := libp2p.New(ctx, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) - if err != nil { - t.Fatal(err) - } - - cli := NewAutoNATClient(h) - return h, cli -} - -func connect(t *testing.T, a, b host.Host) { - pinfo := pstore.PeerInfo{ID: a.ID(), Addrs: a.Addrs()} - err := b.Connect(context.Background(), pinfo) - if err != nil { - t.Fatal(err) - } -} - -// Note: these tests assume that the host has only private inet addresses! -func TestAutoNATServiceDialError(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - save := AutoNATServiceDialTimeout - AutoNATServiceDialTimeout = 1 * time.Second - - hs, _ := makeAutoNATService(ctx, t) - hc, ac := makeAutoNATClient(ctx, t) - connect(t, hs, hc) - - _, err := ac.DialBack(ctx, hs.ID()) - if err == nil { - t.Fatal("Dial back succeeded unexpectedly!") - } - - if !IsDialError(err) { - t.Fatal(err) - } - - AutoNATServiceDialTimeout = save -} - -func TestAutoNATServiceDialSuccess(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - save := private4 - private4 = []*net.IPNet{} - - hs, _ := makeAutoNATService(ctx, t) - hc, ac := makeAutoNATClient(ctx, t) - connect(t, hs, hc) - - _, err := ac.DialBack(ctx, hs.ID()) - if err != nil { - t.Fatalf("Dial back failed: %s", err.Error()) - } - - private4 = save -} - -func TestAutoNATServiceDialRateLimiter(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - save1 := AutoNATServiceDialTimeout - AutoNATServiceDialTimeout = 1 * time.Second - save2 := AutoNATServiceResetInterval - AutoNATServiceResetInterval = 1 * time.Second - save3 := AutoNATServiceThrottle - AutoNATServiceThrottle = 1 - save4 := private4 - private4 = []*net.IPNet{} - - hs, _ := makeAutoNATService(ctx, t) - hc, ac := makeAutoNATClient(ctx, t) - connect(t, hs, hc) - - _, err := ac.DialBack(ctx, hs.ID()) - if err != nil { - t.Fatal(err) - } - - _, err = ac.DialBack(ctx, hs.ID()) - if err == nil { - t.Fatal("Dial back succeeded unexpectedly!") - } - - if !IsDialRefused(err) { - t.Fatal(err) - } - - time.Sleep(2 * time.Second) - - _, err = ac.DialBack(ctx, hs.ID()) - if err != nil { - t.Fatal(err) - } - - AutoNATServiceDialTimeout = save1 - AutoNATServiceResetInterval = save2 - AutoNATServiceThrottle = save3 - private4 = save4 -} From 0bc8c8010da218451a14e6ce72f4a572a5b1806b Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 16 Oct 2018 14:53:35 +0300 Subject: [PATCH 2/5] add docstring for IsPublicAddr --- p2p/host/autonat/addr.go | 1 + 1 file changed, 1 insertion(+) diff --git a/p2p/host/autonat/addr.go b/p2p/host/autonat/addr.go index eee254f8a0..d653914150 100644 --- a/p2p/host/autonat/addr.go +++ b/p2p/host/autonat/addr.go @@ -44,6 +44,7 @@ func parsePrivateCIDR(cidrs []string) []*net.IPNet { return ipnets } +// IsPublicAddr retruns true if the IP part of the multiaddr is not in a private network func IsPublicAddr(a ma.Multiaddr) bool { ip, err := a.ValueForProtocol(ma.P_IP4) if err == nil { From b11fa1c0604446c90f32cdf5429b095d776b9f50 Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 16 Oct 2018 16:33:22 +0300 Subject: [PATCH 3/5] move addr.go to go-multiaddr-net --- p2p/host/autonat/addr.go | 71 ---------------------------------------- 1 file changed, 71 deletions(-) delete mode 100644 p2p/host/autonat/addr.go diff --git a/p2p/host/autonat/addr.go b/p2p/host/autonat/addr.go deleted file mode 100644 index d653914150..0000000000 --- a/p2p/host/autonat/addr.go +++ /dev/null @@ -1,71 +0,0 @@ -package autonat - -import ( - "net" - - ma "github.com/multiformats/go-multiaddr" -) - -var Private4, Private6 []*net.IPNet -var privateCIDR4 = []string{ - // localhost - "127.0.0.0/8", - // private networks - "10.0.0.0/8", - "100.64.0.0/10", - "172.16.0.0/12", - "192.168.0.0/16", - // link local - "169.254.0.0/16", -} -var privateCIDR6 = []string{ - // localhost - "::1/128", - // ULA reserved - "fc00::/7", - // link local - "fe80::/10", -} - -func init() { - Private4 = parsePrivateCIDR(privateCIDR4) - Private6 = parsePrivateCIDR(privateCIDR6) -} - -func parsePrivateCIDR(cidrs []string) []*net.IPNet { - ipnets := make([]*net.IPNet, len(cidrs)) - for i, cidr := range cidrs { - _, ipnet, err := net.ParseCIDR(cidr) - if err != nil { - panic(err) - } - ipnets[i] = ipnet - } - return ipnets -} - -// IsPublicAddr retruns true if the IP part of the multiaddr is not in a private network -func IsPublicAddr(a ma.Multiaddr) bool { - ip, err := a.ValueForProtocol(ma.P_IP4) - if err == nil { - return !inAddrRange(ip, Private4) - } - - ip, err = a.ValueForProtocol(ma.P_IP6) - if err == nil { - return !inAddrRange(ip, Private6) - } - - return false -} - -func inAddrRange(s string, ipnets []*net.IPNet) bool { - ip := net.ParseIP(s) - for _, ipnet := range ipnets { - if ipnet.Contains(ip) { - return true - } - } - - return false -} From 9a4502abb523346d1c005142ecb654def0f66024 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 17 Oct 2018 23:51:32 +0300 Subject: [PATCH 4/5] add address factory --- p2p/host/autonat/autonat.go | 20 ++++++++++++++------ p2p/host/autonat/client.go | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/p2p/host/autonat/autonat.go b/p2p/host/autonat/autonat.go index 1eace01770..176c77a2eb 100644 --- a/p2p/host/autonat/autonat.go +++ b/p2p/host/autonat/autonat.go @@ -48,6 +48,8 @@ type AmbientAutoNAT struct { ctx context.Context host host.Host + getAddrs GetAddrs + mx sync.Mutex peers map[peer.ID]struct{} status NATStatus @@ -61,12 +63,18 @@ type AmbientAutoNAT struct { } // NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host -func NewAutoNAT(ctx context.Context, h host.Host) AutoNAT { +func NewAutoNAT(ctx context.Context, h host.Host, ga ...GetAddrs) AutoNAT { + getAddrs := h.Addrs + if len(ga) > 0 { + getAddrs = ga[0] + } + as := &AmbientAutoNAT{ - ctx: ctx, - host: h, - peers: make(map[peer.ID]struct{}), - status: NATStatusUnknown, + ctx: ctx, + host: h, + getAddrs: getAddrs, + peers: make(map[peer.ID]struct{}), + status: NATStatusUnknown, } h.Network().Notify(as) @@ -123,7 +131,7 @@ func (as *AmbientAutoNAT) autodetect() { return } - cli := NewAutoNATClient(as.host) + cli := NewAutoNATClient(as.host, as.getAddrs) failures := 0 for _, p := range peers { diff --git a/p2p/host/autonat/client.go b/p2p/host/autonat/client.go index 468bc68106..96c5255faa 100644 --- a/p2p/host/autonat/client.go +++ b/p2p/host/autonat/client.go @@ -27,13 +27,21 @@ type AutoNATError struct { Text string } +// GetAddrs is a function that returns the addresses to dial back +type GetAddrs func() []ma.Multiaddr + // NewAutoNATClient creates a fresh instance of an AutoNATClient -func NewAutoNATClient(h host.Host) AutoNATClient { - return &client{h: h} +func NewAutoNATClient(h host.Host, ga ...GetAddrs) AutoNATClient { + getAddrs := h.Addrs + if len(ga) > 0 { + getAddrs = ga[0] + } + return &client{h: h, getAddrs: getAddrs} } type client struct { - h host.Host + h host.Host + getAddrs GetAddrs } func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error) { @@ -46,7 +54,7 @@ func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error) r := ggio.NewDelimitedReader(s, inet.MessageSizeMax) w := ggio.NewDelimitedWriter(s) - req := newDialMessage(pstore.PeerInfo{ID: c.h.ID(), Addrs: c.h.Addrs()}) + req := newDialMessage(pstore.PeerInfo{ID: c.h.ID(), Addrs: c.getAddrs()}) err = w.WriteMsg(req) if err != nil { return nil, err From 00f3153d29959965f0564042ebdbf08cb5a5c3e3 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 18 Oct 2018 18:05:26 +0300 Subject: [PATCH 5/5] make getAddrs a static argument (not variadic) --- p2p/host/autonat/autonat.go | 8 ++++---- p2p/host/autonat/autonat_test.go | 2 +- p2p/host/autonat/client.go | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/p2p/host/autonat/autonat.go b/p2p/host/autonat/autonat.go index 176c77a2eb..0aa987ca63 100644 --- a/p2p/host/autonat/autonat.go +++ b/p2p/host/autonat/autonat.go @@ -63,10 +63,10 @@ type AmbientAutoNAT struct { } // NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host -func NewAutoNAT(ctx context.Context, h host.Host, ga ...GetAddrs) AutoNAT { - getAddrs := h.Addrs - if len(ga) > 0 { - getAddrs = ga[0] +// If getAddrs is nil, h.Addrs will be used +func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT { + if getAddrs == nil { + getAddrs = h.Addrs } as := &AmbientAutoNAT{ diff --git a/p2p/host/autonat/autonat_test.go b/p2p/host/autonat/autonat_test.go index 6170d01d19..aa8d879135 100644 --- a/p2p/host/autonat/autonat_test.go +++ b/p2p/host/autonat/autonat_test.go @@ -72,7 +72,7 @@ func newDialResponseError(status pb.Message_ResponseStatus, text string) *pb.Mes func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) { h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx)) - a := NewAutoNAT(ctx, h) + a := NewAutoNAT(ctx, h, nil) a.(*AmbientAutoNAT).peers[ash.ID()] = struct{}{} return h, a diff --git a/p2p/host/autonat/client.go b/p2p/host/autonat/client.go index 96c5255faa..0fd665d3df 100644 --- a/p2p/host/autonat/client.go +++ b/p2p/host/autonat/client.go @@ -31,10 +31,10 @@ type AutoNATError struct { type GetAddrs func() []ma.Multiaddr // NewAutoNATClient creates a fresh instance of an AutoNATClient -func NewAutoNATClient(h host.Host, ga ...GetAddrs) AutoNATClient { - getAddrs := h.Addrs - if len(ga) > 0 { - getAddrs = ga[0] +// If getAddrs is nil, h.Addrs will be used +func NewAutoNATClient(h host.Host, getAddrs GetAddrs) AutoNATClient { + if getAddrs == nil { + getAddrs = h.Addrs } return &client{h: h, getAddrs: getAddrs} }