diff --git a/lib/nat/service.go b/lib/nat/service.go index 89e6844fe..9d3516a5b 100644 --- a/lib/nat/service.go +++ b/lib/nat/service.go @@ -254,6 +254,7 @@ func (s *Service) acquireNewMappings(mapping *Mapping, nats map[string]Device) ( // the given external port. If external port is 0, picks a pseudo-random port. func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time.Duration) (Address, error) { var err error + var port int // Generate a predictable random which is based on device ID + local port // number so that the ports we'd try to acquire for the mapping would always @@ -263,7 +264,7 @@ func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time if extPort != 0 { // First try renewing our existing mapping, if we have one. name := fmt.Sprintf("syncthing-%d", extPort) - port, err := natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime) + port, err = natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime) if err == nil { extPort = port goto findIP @@ -275,7 +276,7 @@ func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time // Then try up to ten random ports. extPort = 1024 + predictableRand.Intn(65535-1024) name := fmt.Sprintf("syncthing-%d", extPort) - port, err := natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime) + port, err = natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime) if err == nil { extPort = port goto findIP diff --git a/lib/pmp/pmp.go b/lib/pmp/pmp.go index 178dd8db1..9dcabe7d3 100644 --- a/lib/pmp/pmp.go +++ b/lib/pmp/pmp.go @@ -27,6 +27,9 @@ func Discover(renewal, timeout time.Duration) []nat.Device { l.Debugln("Failed to discover gateway", err) return nil } + if ip == nil || ip.IsUnspecified() { + return nil + } l.Debugln("Discovered gateway at", ip)