mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-06-27 12:59:30 +00:00
Cleanup
This commit is contained in:
parent
aa28578ca7
commit
37ba19221a
@ -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()
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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/<address>/udp/<port>" or "/ip6/<address>/udp/<port>"
|
||||
|
||||
let ipFamily = if ip.family == IpAddressFamily.IPv4: "/ip4/" else: "/ip6/"
|
||||
return MultiAddress.init(ipFamily & $ip & "/udp/" & $port).expect("valid multiaddr")
|
||||
|
||||
func getTcpPort*(ma: MultiAddress): Option[Port] =
|
||||
let parts = ($ma).split("/")
|
||||
for i, part in parts:
|
||||
if part == "tcp" and i + 1 < parts.len:
|
||||
try:
|
||||
return some(Port(parseInt(parts[i + 1])))
|
||||
except ValueError:
|
||||
return Port.none
|
||||
Port.none
|
||||
## Extracts the TCP port from a multiaddress; none when there is no TCP part.
|
||||
let tcpPart = ma[multiCodec("tcp")]
|
||||
if tcpPart.isErr:
|
||||
return Port.none
|
||||
let portBytes = tcpPart.get().protoArgument()
|
||||
if portBytes.isErr or portBytes.get().len != 2:
|
||||
return Port.none
|
||||
some(Port(fromBytesBE(uint16, portBytes.get())))
|
||||
|
||||
proc getMultiAddrWithIpAndTcpPort*(ip: IpAddress, port: Port): MultiAddress =
|
||||
## Creates a MultiAddress with the specified IP address and TCP port
|
||||
|
||||
@ -1,15 +1,11 @@
|
||||
{.push raises: [].}
|
||||
|
||||
import std/[options, net]
|
||||
import pkg/chronicles
|
||||
import results
|
||||
import libplum/plum
|
||||
import libplum/libplum
|
||||
|
||||
export plum, libplum, results, options, net
|
||||
|
||||
logScope:
|
||||
topics = "nat"
|
||||
|
||||
type NatStrategy* = enum
|
||||
NatAuto
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user