diff --git a/storage/nat.nim b/storage/nat.nim index 9facf336..0189480f 100644 --- a/storage/nat.nim +++ b/storage/nat.nim @@ -49,6 +49,19 @@ type NatPortMapper* = ref object of RootObj plumInitialized: bool closed: bool +proc resetMappings(m: NatPortMapper) = + if m.tcpMappingId.isSome: + destroyMapping(m.tcpMappingId.get) + m.tcpMappingId = none(cint) + + if m.udpMappingId.isSome: + destroyMapping(m.udpMappingId.get) + m.udpMappingId = none(cint) + + m.activeMappingProtocol = none(MappingProtocol) + m.activeTcpPort = none(Port) + m.activeUdpPort = none(Port) + method mapNatPorts*( m: NatPortMapper ): Future[Option[(Port, Port, MappingProtocol)]] {. @@ -79,17 +92,7 @@ method mapNatPorts*( # If there is only one mapping, something went wrong somewhere # so we delete the mappings to recreate them. - if m.tcpMappingId.isSome: - destroyMapping(m.tcpMappingId.get) - m.tcpMappingId = none(cint) - - if m.udpMappingId.isSome: - destroyMapping(m.udpMappingId.get) - m.udpMappingId = none(cint) - - m.activeMappingProtocol = none(MappingProtocol) - m.activeTcpPort = none(Port) - m.activeUdpPort = none(Port) + m.resetMappings() let tcpRes = await createMapping(TCP, m.tcpPort.uint16, m.tcpPort.uint16) if tcpRes.isErr: @@ -111,17 +114,7 @@ method mapNatPorts*( some((m.activeTcpPort.get, m.activeUdpPort.get, m.activeMappingProtocol.get)) proc close*(m: NatPortMapper) = - if m.tcpMappingId.isSome: - destroyMapping(m.tcpMappingId.get) - m.tcpMappingId = none(cint) - - if m.udpMappingId.isSome: - destroyMapping(m.udpMappingId.get) - m.udpMappingId = none(cint) - - m.activeMappingProtocol = none(MappingProtocol) - m.activeTcpPort = none(Port) - m.activeUdpPort = none(Port) + m.resetMappings() if m.plumInitialized: discard cleanup() diff --git a/storage/storage.nim b/storage/storage.nim index ac32f0f2..20c1e018 100644 --- a/storage/storage.nim +++ b/storage/storage.nim @@ -61,7 +61,6 @@ type autonatService*: Option[AutonatV2Service] autoRelayService*: Option[AutoRelayService] natMapper*: Option[NatPortMapper] - natRouter*: Option[NatRouter] holePunchHandler: Option[connmanager.PeerEventHandler] peerInfoObserver: Option[PeerInfoObserver] bootstrapNodes: seq[SignedPeerRecord] @@ -539,7 +538,6 @@ proc new*( autonatService: autonatService, autoRelayService: autoRelayService, natMapper: natMapper, - natRouter: natRouter, holePunchHandler: holePunchHandler, peerInfoObserver: peerInfoObserver, bootstrapNodes: bootstrapNodes, diff --git a/storage/utils/addrutils.nim b/storage/utils/addrutils.nim index abc3de07..600a38f5 100644 --- a/storage/utils/addrutils.nim +++ b/storage/utils/addrutils.nim @@ -14,6 +14,7 @@ import std/strutils import std/options import pkg/libp2p +import pkg/stew/endians2 func remapAddr*( address: MultiAddress, @@ -47,28 +48,15 @@ func remapAddr*( MultiAddress.init(parts.join("/")).expect("Should construct multiaddress") -proc getMultiAddrWithIPAndUDPPort*(ip: IpAddress, port: Port): MultiAddress = - ## Creates a MultiAddress with the specified IP address and UDP port - ## - ## Parameters: - ## - ip: A valid IP address (IPv4 or IPv6) - ## - port: The UDP port number - ## - ## Returns: - ## A MultiAddress in the format "/ip4/
/udp/