浏览代码

lib/connections: Stun resolves server adress beforehand (fixes #4453)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4454
Audrius Butkevicius 8 年之前
父节点
当前提交
a9c221189b
共有 1 个文件被更改,包括 12 次插入1 次删除
  1. 12 1
      lib/connections/kcp_listen.go

+ 12 - 1
lib/connections/kcp_listen.go

@@ -200,6 +200,7 @@ func (t *kcpListener) stunRenewal(listener net.PacketConn) {
 
 	var natType stun.NATType
 	var extAddr *stun.Host
+	var udpAddr *net.UDPAddr
 	var err error
 
 	oldType := stun.NATUnknown
@@ -218,7 +219,17 @@ func (t *kcpListener) stunRenewal(listener net.PacketConn) {
 		}
 
 		for _, addr := range t.cfg.StunServers() {
-			client.SetServerAddr(addr)
+			// Resolve the address, so that in case the server advertises two
+			// IPs, we always hit the same one, as otherwise, the mapping might
+			// expire as we hit the other address, and cause us to flip flop
+			// between servers/external addresses, as a result flooding discovery
+			// servers.
+			udpAddr, err = net.ResolveUDPAddr("udp", addr)
+			if err != nil {
+				l.Debugf("%s stun addr resolution on %s: %s", t.uri, addr, err)
+				continue
+			}
+			client.SetServerAddr(udpAddr.String())
 
 			natType, extAddr, err = client.Discover()
 			if err != nil || extAddr == nil {