From 164f2dac3abdc1f4882cdca1084045141d4e9a04 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 17 Jun 2026 09:26:17 +0400 Subject: [PATCH] Fallback on relay when port mapping is not reachable --- storage/nat.nim | 14 +++---------- tests/storage/testnatreaction.nim | 34 +++---------------------------- 2 files changed, 6 insertions(+), 42 deletions(-) diff --git a/storage/nat.nim b/storage/nat.nim index 63ffff9a..02b54a34 100644 --- a/storage/nat.nim +++ b/storage/nat.nim @@ -217,17 +217,9 @@ method handleNatStatus*( # If the relay is running, the addresses will be updated on reservation. discovery.announceDirectAddrs(@[], udpPort = discoveryPort) - if dialBackAddr.isNone: - warn "Got empty dialback address in AutoNat when node is NotReachable" - - if m.hasMappingIds(): - m.close() - elif m.hasMappingIds(): - warn "Not Reachable with active port mapping. The port mapping will be deleted and relay will start." - - # The mapping was created the the node is still not reachable. - # In that case, we delete the mapping and relay will start. - m.close() + if m.hasMappingIds(): + # The mapping was created but the node is still not reachable. + debug "Not Reachable with active port mapping, keeping it and starting relay if not started" else: debug "Node is not reachable trying port mapping now" diff --git a/tests/storage/testnatreaction.nim b/tests/storage/testnatreaction.nim index 0a0781c1..4e229f6e 100644 --- a/tests/storage/testnatreaction.nim +++ b/tests/storage/testnatreaction.nim @@ -104,7 +104,7 @@ asyncchecksuite "NAT reaction - port mapping": check not autoRelay.isRunning check disc.protocol.clientMode - test "handleNatStatus starts autoRelay when NotReachable and no dialBackAddr but no mapped ports": + test "handleNatStatus starts autoRelay when NotReachable with no mapped ports": let mapper = MockNatPortMapper(mappedPorts: none((Port, Port, MappingProtocol))) autorelayservice.setup(autoRelay, sw) @@ -112,23 +112,11 @@ asyncchecksuite "NAT reaction - port mapping": NotReachable, Opt.none(MultiAddress), discoveryPort, disc, sw, autoRelay ) - check autoRelay.isRunning - check disc.protocol.clientMode - - test "handleNatStatus starts autoRelay when NotReachable and dialBackAddr but no mapped ports": - let dialBack = MultiAddress.init("/ip4/1.2.3.4/tcp/8080").expect("valid") - let mapper = MockNatPortMapper(mappedPorts: none((Port, Port, MappingProtocol))) - - autorelayservice.setup(autoRelay, sw) - await mapper.handleNatStatus( - NotReachable, Opt.some(dialBack), discoveryPort, disc, sw, autoRelay - ) - check autoRelay.isRunning check disc.announceAddrs == newSeq[MultiAddress]() check disc.protocol.clientMode - test "handleNatStatus tears down an active mapping and starts relay when NotReachable with dialBackAddr": + test "handleNatStatus starts relay when NotReachable with an active mapping": privateAccess(NatPortMapper) let dialBack = MultiAddress.init("/ip4/1.2.3.4/tcp/8080").expect("valid") let mapper = MockNatPortMapper() @@ -143,23 +131,7 @@ asyncchecksuite "NAT reaction - port mapping": check autoRelay.isRunning check disc.announceAddrs == newSeq[MultiAddress]() check disc.protocol.clientMode - check not mapper.hasMappingIds() # the active mapping was torn down - - test "handleNatStatus tears down an active mapping and starts relay when NotReachable without dialBackAddr": - privateAccess(NatPortMapper) - let mapper = MockNatPortMapper() - mapper.tcpMappingId = some(cint(1)) - mapper.udpMappingId = some(cint(2)) - - autorelayservice.setup(autoRelay, sw) - await mapper.handleNatStatus( - NotReachable, Opt.none(MultiAddress), discoveryPort, disc, sw, autoRelay - ) - - check autoRelay.isRunning - check disc.announceAddrs == newSeq[MultiAddress]() - check disc.protocol.clientMode - check not mapper.hasMappingIds() # the active mapping was torn down + check mapper.hasMappingIds() # the active mapping is kept test "handleNatStatus stops relay and exits client mode when mapping is created and node is Reachable": let dialBack = MultiAddress.init("/ip4/1.2.3.4/tcp/8080").expect("valid")