net/nat.nim: "raises" annotations (#487)

This commit is contained in:
Ștefan Talpalaru 2022-03-16 15:21:56 +01:00 committed by GitHub
parent e62fdfe6f1
commit 7b448ed406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 10 deletions

View File

@ -6,12 +6,16 @@
# This file may not be copied, modified, or distributed except according to # This file may not be copied, modified, or distributed except according to
# those terms. # those terms.
{.push raises: [Defect].}
import import
std/[options, os, strutils, times], std/[options, os, strutils, times],
stew/results, nat_traversal/[miniupnpc, natpmp], stew/results, nat_traversal/[miniupnpc, natpmp],
chronicles, json_serialization/std/net, chronos, chronicles, json_serialization/std/net, chronos, confutils,
../common/utils, ./utils as netutils ../common/utils, ./utils as netutils
export ConfigurationError
type type
NatStrategy* = enum NatStrategy* = enum
NatAny NatAny
@ -155,7 +159,7 @@ var
natThread: Thread[PortMappingArgs] natThread: Thread[PortMappingArgs]
natCloseChan: Channel[bool] natCloseChan: Channel[bool]
proc repeatPortMapping(args: PortMappingArgs) {.thread.} = proc repeatPortMapping(args: PortMappingArgs) {.thread, raises: [Defect, ValueError].} =
ignoreSignalsInThread() ignoreSignalsInThread()
let let
(tcpPort, udpPort, description) = args (tcpPort, udpPort, description) = args
@ -173,7 +177,8 @@ proc repeatPortMapping(args: PortMappingArgs) {.thread.} =
while true: while true:
# we're being silly here with this channel polling because we can't # we're being silly here with this channel polling because we can't
# select on Nim channels like on Go ones # select on Nim channels like on Go ones
let (dataAvailable, _) = natCloseChan.tryRecv() let (dataAvailable, _) = try: natCloseChan.tryRecv()
except Exception: (false, false)
if dataAvailable: if dataAvailable:
return return
else: else:
@ -186,9 +191,12 @@ proc repeatPortMapping(args: PortMappingArgs) {.thread.} =
proc stopNatThread() {.noconv.} = proc stopNatThread() {.noconv.} =
# stop the thread # stop the thread
natCloseChan.send(true) try:
natThread.joinThread() natCloseChan.send(true)
natCloseChan.close() natThread.joinThread()
natCloseChan.close()
except Exception as exc:
warn "Failed to stop NAT port mapping renewal thread", exc = exc.msg
# delete our port mappings # delete our port mappings
@ -233,9 +241,12 @@ proc redirectPorts*(tcpPort, udpPort: Port, description: string): Option[(Port,
# NAT-PMP lease expires or the router is rebooted and forgets all about # NAT-PMP lease expires or the router is rebooted and forgets all about
# these mappings. # these mappings.
natCloseChan.open() natCloseChan.open()
natThread.createThread(repeatPortMapping, (externalTcpPort, externalUdpPort, description)) try:
# atexit() in disguise natThread.createThread(repeatPortMapping, (externalTcpPort, externalUdpPort, description))
addQuitProc(stopNatThread) # atexit() in disguise
addQuitProc(stopNatThread)
except Exception as exc:
warn "Failed to create NAT port mapping renewal thread", exc = exc.msg
proc setupNat*(natStrategy: NatStrategy, tcpPort, udpPort: Port, proc setupNat*(natStrategy: NatStrategy, tcpPort, udpPort: Port,
clientId: string): clientId: string):
@ -267,7 +278,7 @@ type
of true: extIp*: ValidIpAddress of true: extIp*: ValidIpAddress
of false: nat*: NatStrategy of false: nat*: NatStrategy
func parseCmdArg*(T: type NatConfig, p: TaintedString): T = func parseCmdArg*(T: type NatConfig, p: TaintedString): T {.raises: [Defect, ConfigurationError].} =
case p.toLowerAscii: case p.toLowerAscii:
of "any": of "any":
NatConfig(hasExtIp: false, nat: NatAny) NatConfig(hasExtIp: false, nat: NatAny)