From 8bc9d82942da5f0878f5afa6dfe361e92cb3f4c4 Mon Sep 17 00:00:00 2001 From: qqqeck Date: Fri, 9 Aug 2024 17:33:29 +0900 Subject: [PATCH] p2p: delete port mappings abandoned by geth --- p2p/nat/natpmp.go | 3 +++ p2p/nat/natupnp.go | 7 +++++++ p2p/server_nat.go | 5 +---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/p2p/nat/natpmp.go b/p2p/nat/natpmp.go index 94ef1f4b68..5598b6da73 100644 --- a/p2p/nat/natpmp.go +++ b/p2p/nat/natpmp.go @@ -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)) diff --git a/p2p/nat/natupnp.go b/p2p/nat/natupnp.go index 673a0b36d6..b07f67f0b9 100644 --- a/p2p/nat/natupnp.go +++ b/p2p/nat/natupnp.go @@ -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) }) diff --git a/p2p/server_nat.go b/p2p/server_nat.go index 933993bc1f..599fda8fa1 100644 --- a/p2p/server_nat.go +++ b/p2p/server_nat.go @@ -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")