From 7b72774efa5b2d6aecc68124ba5c944895e517a9 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Tue, 7 Apr 2020 10:55:15 -0700 Subject: [PATCH] Run Autonat Service while in unknown connectivity mode This supports local LAN connectivity / discovery --- autonat.go | 10 +++++----- svc_test.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/autonat.go b/autonat.go index 04ec7a1..aa355d3 100644 --- a/autonat.go +++ b/autonat.go @@ -46,6 +46,7 @@ type AmbientAutoNAT struct { emitReachabilityChanged event.Emitter } +// StaticAutoNAT is a simple AutoNAT implementation when a single NAT status is desired. type StaticAutoNAT struct { ctx context.Context host host.Host @@ -85,15 +86,12 @@ func New(ctx context.Context, h host.Host, options ...Option) (AutoNAT, error) { if err != nil { return nil, err } + service.Enable() } if conf.forceReachability { emitReachabilityChanged.Emit(event.EvtLocalReachabilityChanged{Reachability: conf.reachability}) - // The serice will only exist when reachability is public. - if service != nil { - service.Enable() - } return &StaticAutoNAT{ ctx: ctx, host: h, @@ -297,7 +295,7 @@ func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) { as.status.Store(autoNATResult{network.ReachabilityUnknown, nil}) if currentStatus.Reachability != network.ReachabilityUnknown { if as.service != nil { - as.service.Disable() + as.service.Enable() } as.emitStatus() } @@ -362,10 +360,12 @@ func shufflePeers(peers []peer.AddrInfo) { } } +// Status returns the AutoNAT observed reachability status. func (s *StaticAutoNAT) Status() network.Reachability { return s.reachability } +// PublicAddr returns the publicly connectable Multiaddr of this node if one is known. func (s *StaticAutoNAT) PublicAddr() (ma.Multiaddr, error) { if s.reachability != network.ReachabilityPublic { return nil, errors.New("NAT status is not public") diff --git a/svc_test.go b/svc_test.go index 195a523..e1df6c8 100644 --- a/svc_test.go +++ b/svc_test.go @@ -188,8 +188,8 @@ func TestAutoNATServiceStartup(t *testing.T) { connect(t, h, hc) _, err = ac.DialBack(ctx, h.ID()) - if err == nil { - t.Fatal("autonat should not be started / advertising.") + if err != nil { + t.Fatal("autonat service be active in unknown mode.") } sub, _ := h.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))