From 0518a92cdb025e952a1a405e19306b51e1736595 Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Fri, 17 Nov 2017 14:46:45 +0000 Subject: [PATCH] lib/connections: Only announce punchable nats (fixes #4519) GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4523 --- lib/connections/kcp_listen.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/connections/kcp_listen.go b/lib/connections/kcp_listen.go index 389f26fec..c756c6a1f 100644 --- a/lib/connections/kcp_listen.go +++ b/lib/connections/kcp_listen.go @@ -24,6 +24,8 @@ import ( "github.com/syncthing/syncthing/lib/nat" ) +const stunRetryInterval = 5 * time.Minute + func init() { factory := &kcpListenerFactory{} for _, scheme := range []string{"kcp", "kcp4", "kcp6"} { @@ -246,6 +248,13 @@ func (t *kcpListener) stunRenewal(listener net.PacketConn) { if oldType != natType { l.Infof("%s detected NAT type: %s", t.uri, natType) t.nat.Store(natType) + oldType = natType + } + + // We can't punch through this one, so no point doing keepalives + // and such, just try again in a minute and hope that the NAT type changes. + if !isPunchable(natType) { + break } for { @@ -285,12 +294,11 @@ func (t *kcpListener) stunRenewal(listener net.PacketConn) { break } } - - oldType = natType } - // We failed to contact all provided stun servers, chillout for a while. - time.Sleep(time.Minute) + // We failed to contact all provided stun servers or the nat is not punchable. + // Chillout for a while. + time.Sleep(stunRetryInterval) } } @@ -312,3 +320,7 @@ func (f *kcpListenerFactory) New(uri *url.URL, cfg *config.Wrapper, tlsCfg *tls. func (kcpListenerFactory) Enabled(cfg config.Configuration) bool { return true } + +func isPunchable(natType stun.NATType) bool { + return natType == stun.NATNone || natType == stun.NATPortRestricted || natType == stun.NATRestricted || natType == stun.NATFull +}