server: add periodic renewal of port forwarding
The check prevented the creation of port forwardings which were assumed to be present already. After this change the port forwardings which might have been removed from the NAT device can be re-created.
This commit is contained in:
parent
b07499f227
commit
a27ac66eed
@ -65,10 +65,6 @@ func (p *PMP) AddPortMapping(port uint16) error {
|
|||||||
p.forwardedPortsMtx.Lock()
|
p.forwardedPortsMtx.Lock()
|
||||||
defer p.forwardedPortsMtx.Unlock()
|
defer p.forwardedPortsMtx.Unlock()
|
||||||
|
|
||||||
if _, exists := p.forwardedPorts[port]; exists {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := p.client.AddPortMapping("tcp", int(port), int(port), 0)
|
_, err := p.client.AddPortMapping("tcp", int(port), int(port), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -62,10 +62,6 @@ func (u *UPnP) AddPortMapping(port uint16) error {
|
|||||||
u.forwardedPortsMtx.Lock()
|
u.forwardedPortsMtx.Lock()
|
||||||
defer u.forwardedPortsMtx.Unlock()
|
defer u.forwardedPortsMtx.Unlock()
|
||||||
|
|
||||||
if _, exists := u.forwardedPorts[port]; exists {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := u.device.Forward(port, ""); err != nil {
|
if err := u.device.Forward(port, ""); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
15
server.go
15
server.go
@ -1145,6 +1145,21 @@ out:
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Periodically renew the NAT port forwarding.
|
||||||
|
for _, port := range forwardedPorts {
|
||||||
|
err := s.natTraversal.AddPortMapping(port)
|
||||||
|
if err != nil {
|
||||||
|
srvrLog.Warnf("Unable to automatically "+
|
||||||
|
"re-create port forwarding using %s: %v",
|
||||||
|
s.natTraversal.Name(), err)
|
||||||
|
} else {
|
||||||
|
srvrLog.Debugf("Automatically re-created "+
|
||||||
|
"forwarding for port %d using %s to "+
|
||||||
|
"advertise external IP",
|
||||||
|
port, s.natTraversal.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ip.Equal(s.lastDetectedIP) {
|
if ip.Equal(s.lastDetectedIP) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user