Oskar Thorén 9518322198
Folder restructure (#274)
* Change folder structure to {v1,v2,common}/...

Addresses https://github.com/status-im/nim-waku/issues/261

* Update waku.nimble paths

* Flatten paths

* Fix import paths

* Pull out utils folder for nat

* Pull out waku_types to top level for v2

* Fix test import paths

* Remove old READMEs and replace with one liner

* Update README and split v1 and v2

* Skeleton READMEs

* Update README.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* Update README.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

Co-authored-by: Kim De Mey <kim.demey@gmail.com>
2020-11-17 17:34:53 +08:00

47 lines
1.4 KiB
Nim

import
std/[strutils, options],
chronicles, stew/shims/net as stewNet,
eth/net/nat
proc setupNat*(natConf, clientId: string, tcpPort, udpPort: Port):
tuple[ip: Option[ValidIpAddress], tcpPort: Option[Port],
udpPort: Option[Port]] {.gcsafe.} =
var nat: NatStrategy
case natConf.toLowerAscii:
of "any":
nat = NatAny
of "none":
nat = NatNone
of "upnp":
nat = NatUpnp
of "pmp":
nat = NatPmp
else:
if natConf.startsWith("extip:"):
try:
# any required port redirection is assumed to be done by hand
result.ip = some(ValidIpAddress.init(natConf[6..^1]))
nat = NatNone
except ValueError:
error "nor a valid IP address", address = natConf[6..^1]
quit QuitFailure
else:
error "not a valid NAT mechanism", value = natConf
quit QuitFailure
if nat != NatNone:
let extIp = getExternalIP(nat)
if extIP.isSome:
result.ip = some(ValidIpAddress.init extIp.get)
# TODO redirectPorts in considered a gcsafety violation
# because it obtains the address of a non-gcsafe proc?
let extPorts = ({.gcsafe.}:
redirectPorts(tcpPort = tcpPort,
udpPort = udpPort,
description = clientId))
if extPorts.isSome:
let (extTcpPort, extUdpPort) = extPorts.get()
result.tcpPort = some(extTcpPort)
result.udpPort = some(extUdpPort)