Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Run Autonat Service while in unknown connectivity mode #75

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should flip back to enabled if we were private. But I guess this is symmetric. Up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the simpler state machine seems nicer. i'll stick with this for now.

}
as.emitStatus()
}
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down