lib/stun: Refactor to remove unnecessary logging (fixes #6213) (#6260)

This commit is contained in:
Simon Frei 2020-01-10 10:24:15 +01:00 committed by GitHub
parent 08753ccabe
commit 119d76d035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -115,18 +115,24 @@ func (s *Service) Stop() {
} }
func (s *Service) serve(ctx context.Context) { func (s *Service) serve(ctx context.Context) {
for { defer func() {
disabled:
s.setNATType(NATUnknown) s.setNATType(NATUnknown)
s.setExternalAddress(nil, "") s.setExternalAddress(nil, "")
}()
timer := time.NewTimer(time.Millisecond)
for {
disabled:
select {
case <-ctx.Done():
return
case <-timer.C:
}
if s.cfg.Options().IsStunDisabled() { if s.cfg.Options().IsStunDisabled() {
select { timer.Reset(time.Second)
case <-ctx.Done(): continue
return
case <-time.After(time.Second):
continue
}
} }
l.Debugf("Starting stun for %s", s) l.Debugf("Starting stun for %s", s)
@ -135,44 +141,36 @@ func (s *Service) serve(ctx context.Context) {
// This blocks until we hit an exit condition or there are issues with the STUN server. // This blocks until we hit an exit condition or there are issues with the STUN server.
// This returns a boolean signifying if a different STUN server should be tried (oppose to the whole thing // This returns a boolean signifying if a different STUN server should be tried (oppose to the whole thing
// shutting down and this winding itself down. // shutting down and this winding itself down.
if !s.runStunForServer(ctx, addr) { s.runStunForServer(ctx, addr)
// Check exit conditions.
// Have we been asked to stop? // Have we been asked to stop?
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
default: default:
} }
// Are we disabled? // Are we disabled?
if s.cfg.Options().IsStunDisabled() { if s.cfg.Options().IsStunDisabled() {
l.Infoln("STUN disabled") l.Infoln("STUN disabled")
goto disabled s.setNATType(NATUnknown)
} s.setExternalAddress(nil, "")
goto disabled
}
// Unpunchable NAT? Chillout for some time. // Unpunchable NAT? Chillout for some time.
if !s.isCurrentNATTypePunchable() { if !s.isCurrentNATTypePunchable() {
break break
}
} }
} }
// Failed all servers, sad.
s.setNATType(NATUnknown)
s.setExternalAddress(nil, "")
// We failed to contact all provided stun servers or the nat is not punchable. // We failed to contact all provided stun servers or the nat is not punchable.
// Chillout for a while. // Chillout for a while.
select { timer.Reset(stunRetryInterval)
case <-time.After(stunRetryInterval):
case <-ctx.Done():
return
}
} }
} }
func (s *Service) runStunForServer(ctx context.Context, addr string) (tryNext bool) { func (s *Service) runStunForServer(ctx context.Context, addr string) {
l.Debugf("Running stun for %s via %s", s, addr) l.Debugf("Running stun for %s via %s", s, addr)
// Resolve the address, so that in case the server advertises two // Resolve the address, so that in case the server advertises two
@ -183,20 +181,20 @@ func (s *Service) runStunForServer(ctx context.Context, addr string) (tryNext bo
udpAddr, err := net.ResolveUDPAddr("udp", addr) udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil { if err != nil {
l.Debugf("%s stun addr resolution on %s: %s", s, addr, err) l.Debugf("%s stun addr resolution on %s: %s", s, addr, err)
return true return
} }
s.client.SetServerAddr(udpAddr.String()) s.client.SetServerAddr(udpAddr.String())
natType, extAddr, err := s.client.Discover() natType, extAddr, err := s.client.Discover()
if err != nil || extAddr == nil { if err != nil || extAddr == nil {
l.Debugf("%s stun discovery on %s: %s", s, addr, err) l.Debugf("%s stun discovery on %s: %s", s, addr, err)
return true return
} }
// The stun server is most likely borked, try another one. // The stun server is most likely borked, try another one.
if natType == NATError || natType == NATUnknown || natType == NATBlocked { if natType == NATError || natType == NATUnknown || natType == NATBlocked {
l.Debugf("%s stun discovery on %s resolved to %s", s, addr, natType) l.Debugf("%s stun discovery on %s resolved to %s", s, addr, natType)
return true return
} }
s.setNATType(natType) s.setNATType(natType)
@ -207,13 +205,13 @@ func (s *Service) runStunForServer(ctx context.Context, addr string) (tryNext bo
// and such, just let the caller check the nat type and work it out themselves. // and such, just let the caller check the nat type and work it out themselves.
if !s.isCurrentNATTypePunchable() { if !s.isCurrentNATTypePunchable() {
l.Debugf("%s cannot punch %s, skipping", s, natType) l.Debugf("%s cannot punch %s, skipping", s, natType)
return false return
} }
return s.stunKeepAlive(ctx, addr, extAddr) s.stunKeepAlive(ctx, addr, extAddr)
} }
func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host) (tryNext bool) { func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host) {
var err error var err error
nextSleep := time.Duration(s.cfg.Options().StunKeepaliveStartS) * time.Second nextSleep := time.Duration(s.cfg.Options().StunKeepaliveStartS) * time.Second
@ -234,7 +232,7 @@ func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host)
minSleep := time.Duration(s.cfg.Options().StunKeepaliveMinS) * time.Second minSleep := time.Duration(s.cfg.Options().StunKeepaliveMinS) * time.Second
if nextSleep < minSleep { if nextSleep < minSleep {
l.Debugf("%s keepalive aborting, sleep below min: %s < %s", s, nextSleep, minSleep) l.Debugf("%s keepalive aborting, sleep below min: %s < %s", s, nextSleep, minSleep)
return true return
} }
} }
@ -258,13 +256,13 @@ func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host)
case <-time.After(sleepFor): case <-time.After(sleepFor):
case <-ctx.Done(): case <-ctx.Done():
l.Debugf("%s stopping, aborting stun", s) l.Debugf("%s stopping, aborting stun", s)
return false return
} }
if s.cfg.Options().IsStunDisabled() { if s.cfg.Options().IsStunDisabled() {
// Disabled, give up // Disabled, give up
l.Debugf("%s disabled, aborting stun ", s) l.Debugf("%s disabled, aborting stun ", s)
return false return
} }
// Check if any writes happened while we were sleeping, if they did, sleep again // Check if any writes happened while we were sleeping, if they did, sleep again
@ -279,7 +277,7 @@ func (s *Service) stunKeepAlive(ctx context.Context, addr string, extAddr *Host)
extAddr, err = s.client.Keepalive() extAddr, err = s.client.Keepalive()
if err != nil { if err != nil {
l.Debugf("%s stun keepalive on %s: %s (%v)", s, addr, err, extAddr) l.Debugf("%s stun keepalive on %s: %s (%v)", s, addr, err, extAddr)
return true return
} }
} }
} }