p2p: delete port mappings abandoned by geth

This commit is contained in:
qqqeck 2024-08-09 17:33:29 +09:00
parent 59a2943dee
commit 8bc9d82942
3 changed files with 11 additions and 4 deletions

View File

@ -49,6 +49,9 @@ func (n *pmp) AddMapping(protocol string, extport, intport int, name string, lif
if lifetime <= 0 {
return 0, errors.New("lifetime must not be <= 0")
}
if extport == 0 {
extport = intport
}
// Note order of port arguments is switched between our
// AddMapping and the client's AddPortMapping.
res, err := n.c.AddPortMapping(strings.ToLower(protocol), intport, extport, int(lifetime/time.Second))

View File

@ -87,6 +87,13 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li
protocol = strings.ToUpper(protocol)
lifetimeS := uint32(lifetime / time.Second)
if extport == 0 {
extport = intport
} else {
// Only delete port mapping if the external port was already used by geth.
n.DeleteMapping(protocol, extport, intport)
}
err = n.withRateLimit(func() error {
return n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS)
})

View File

@ -150,10 +150,7 @@ func (srv *Server) portMappingLoop() {
continue
}
external := m.port
if m.extPort != 0 {
external = m.extPort
}
external := m.extPort
log := newLogger(m.protocol, external, m.port)
log.Trace("Attempting port mapping")