lib/connections: Only announce punchable nats (fixes #4519)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4523
This commit is contained in:
Audrius Butkevicius 2017-11-17 14:46:45 +00:00 committed by Jakob Borg
parent 6cf01c1d30
commit 0518a92cdb
1 changed files with 16 additions and 4 deletions

View File

@ -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
}