mirror of
https://github.com/octoleo/syncthing.git
synced 2025-02-02 11:58:28 +00:00
lib/{nat,pmp}: Fix shadowing and nil IPs
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/2979
This commit is contained in:
parent
fa1cfd94d0
commit
e61f424ade
@ -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.
|
// 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) {
|
func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time.Duration) (Address, error) {
|
||||||
var err error
|
var err error
|
||||||
|
var port int
|
||||||
|
|
||||||
// Generate a predictable random which is based on device ID + local port
|
// 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
|
// 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 {
|
if extPort != 0 {
|
||||||
// First try renewing our existing mapping, if we have one.
|
// First try renewing our existing mapping, if we have one.
|
||||||
name := fmt.Sprintf("syncthing-%d", extPort)
|
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 {
|
if err == nil {
|
||||||
extPort = port
|
extPort = port
|
||||||
goto findIP
|
goto findIP
|
||||||
@ -275,7 +276,7 @@ func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time
|
|||||||
// Then try up to ten random ports.
|
// Then try up to ten random ports.
|
||||||
extPort = 1024 + predictableRand.Intn(65535-1024)
|
extPort = 1024 + predictableRand.Intn(65535-1024)
|
||||||
name := fmt.Sprintf("syncthing-%d", extPort)
|
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 {
|
if err == nil {
|
||||||
extPort = port
|
extPort = port
|
||||||
goto findIP
|
goto findIP
|
||||||
|
@ -27,6 +27,9 @@ func Discover(renewal, timeout time.Duration) []nat.Device {
|
|||||||
l.Debugln("Failed to discover gateway", err)
|
l.Debugln("Failed to discover gateway", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if ip == nil || ip.IsUnspecified() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
l.Debugln("Discovered gateway at", ip)
|
l.Debugln("Discovered gateway at", ip)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user