2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2021-07-16 15:13:36 +00:00
|
|
|
|
2024-05-16 20:29:11 +00:00
|
|
|
import std/[options, strutils, net]
|
|
|
|
import chronicles, eth/net/nat, stew/results, nativesockets
|
2020-09-01 02:09:54 +00:00
|
|
|
|
2021-07-16 15:13:36 +00:00
|
|
|
logScope:
|
|
|
|
topics = "nat"
|
|
|
|
|
2024-06-12 05:49:55 +00:00
|
|
|
## Due to the design of nim-eth/nat module we must ensure it is only initialized once.
|
|
|
|
## see: https://github.com/waku-org/nwaku/issues/2628
|
|
|
|
## Details: nim-eth/nat module starts a meaintenance thread for refreshing the NAT mappings, but everything in the module is global,
|
|
|
|
## there is no room to store multiple configurations.
|
|
|
|
## Exact meaning: redirectPorts cannot be called twice in a program lifetime.
|
|
|
|
## During waku tests we happen to start several node instances in parallel thus resulting in multiple NAT configurations and multiple threads.
|
|
|
|
## Those threads will dead lock each other in tear down.
|
|
|
|
var singletonNat: bool = false
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc setupNat*(
|
|
|
|
natConf, clientId: string, tcpPort, udpPort: Port
|
|
|
|
): Result[
|
|
|
|
tuple[ip: Option[IpAddress], tcpPort: Option[Port], udpPort: Option[Port]], string
|
|
|
|
] {.gcsafe.} =
|
|
|
|
let strategy =
|
|
|
|
case natConf.toLowerAscii()
|
|
|
|
of "any": NatAny
|
|
|
|
of "none": NatNone
|
|
|
|
of "upnp": NatUpnp
|
|
|
|
of "pmp": NatPmp
|
|
|
|
else: NatNone
|
|
|
|
|
|
|
|
var endpoint:
|
|
|
|
tuple[ip: Option[IpAddress], tcpPort: Option[Port], udpPort: Option[Port]]
|
2020-09-01 02:09:54 +00:00
|
|
|
|
2023-05-17 16:32:53 +00:00
|
|
|
if strategy != NatNone:
|
2024-06-12 05:49:55 +00:00
|
|
|
## Only initialize the NAT module once
|
|
|
|
## redirectPorts cannot be called twice in a program lifetime.
|
|
|
|
## We can do it as same happens if getExternalIP fails and returns None
|
|
|
|
if singletonNat:
|
|
|
|
warn "NAT already initialized, skipping as cannot be done multiple times"
|
|
|
|
else:
|
|
|
|
singletonNat = true
|
|
|
|
let extIp = getExternalIP(strategy)
|
|
|
|
if extIP.isSome():
|
|
|
|
endpoint.ip = some(extIp.get())
|
|
|
|
# RedirectPorts in considered a gcsafety violation
|
|
|
|
# because it obtains the address of a non-gcsafe proc?
|
|
|
|
var extPorts: Option[(Port, Port)]
|
|
|
|
try:
|
|
|
|
extPorts = (
|
|
|
|
{.gcsafe.}:
|
|
|
|
redirectPorts(
|
|
|
|
tcpPort = tcpPort, udpPort = udpPort, description = clientId
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except CatchableError:
|
|
|
|
# TODO: nat.nim Error: can raise an unlisted exception: Exception. Isolate here for now.
|
|
|
|
error "unable to determine external ports"
|
|
|
|
extPorts = none((Port, Port))
|
|
|
|
|
|
|
|
if extPorts.isSome():
|
|
|
|
let (extTcpPort, extUdpPort) = extPorts.get()
|
|
|
|
endpoint.tcpPort = some(extTcpPort)
|
|
|
|
endpoint.udpPort = some(extUdpPort)
|
2023-05-17 16:32:53 +00:00
|
|
|
else: # NatNone
|
|
|
|
if not natConf.startsWith("extip:"):
|
|
|
|
return err("not a valid NAT mechanism: " & $natConf)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# any required port redirection is assumed to be done by hand
|
2024-03-15 23:08:47 +00:00
|
|
|
endpoint.ip = some(parseIpAddress(natConf[6 ..^ 1]))
|
2023-05-17 16:32:53 +00:00
|
|
|
except ValueError:
|
2024-03-15 23:08:47 +00:00
|
|
|
return err("not a valid IP address: " & $natConf[6 ..^ 1])
|
2023-05-17 16:32:53 +00:00
|
|
|
|
|
|
|
return ok(endpoint)
|