-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: proper use of setupNat #1740
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule nim-eth
updated
67 files
Submodule nim-secp256k1
updated
20 files
+0 −41 | .appveyor.yml | |
+1 −1 | .github/workflows/ci.yml | |
+2 −0 | .gitignore | |
+2 −2 | .gitmodules | |
+0 −27 | .travis.yml | |
+0 −2 | README.md | |
+14 −18 | ci/Jenkinsfile | |
+0 −25 | nimble.lock | |
+171 −28 | secp256k1.nim | |
+4 −3 | secp256k1.nimble | |
+231 −24 | secp256k1/abi.nim | |
+0 −347 | secp256k1_wrapper/README.md | |
+0 −1,163 | secp256k1_wrapper/ecmult_static_context.h | |
+0 −13 | secp256k1_wrapper/gen.sh | |
+0 −118 | secp256k1_wrapper/libsecp256k1-config.h | |
+0 −1 | secp256k1_wrapper/secp256k1 | |
+50 −1 | tests/test_secp256k1.nim | |
+14 −0 | tests/test_secp256k1_abi.nim | |
+3 −0 | vendor/README.md | |
+1 −0 | vendor/secp256k1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,67 @@ | ||
|
||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
std/[strutils, options], | ||
chronicles, stew/shims/net as stewNet, | ||
eth/net/nat | ||
std/[options, strutils] | ||
import | ||
chronicles, | ||
eth/net/nat, | ||
stew/results, | ||
stew/shims/net, | ||
nativesockets | ||
|
||
logScope: | ||
topics = "nat" | ||
|
||
proc setupNat*(natConf, clientId: string, tcpPort, udpPort: Port): | ||
tuple[ip: Option[ValidIpAddress], | ||
tcpPort: Option[Port], | ||
udpPort: Option[Port]] {.gcsafe, deprecated: | ||
"Unsafe: this proc quits the app if something is not ok".} = | ||
proc setupNat*(natConf, clientId: string, | ||
tcpPort, udpPort: Port): | ||
Result[tuple[ip: Option[ValidIpAddress], | ||
tcpPort: Option[Port], | ||
udpPort: Option[Port]], string] | ||
{.gcsafe.} = | ||
|
||
var | ||
endpoint: tuple[ip: Option[ValidIpAddress], | ||
tcpPort: Option[Port], | ||
udpPort: Option[Port]] | ||
nat: NatStrategy | ||
let strategy = case natConf.toLowerAscii(): | ||
of "any": NatAny | ||
of "none": NatNone | ||
of "upnp": NatUpnp | ||
of "pmp": NatPmp | ||
else: NatNone | ||
|
||
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 | ||
endpoint.ip = some(ValidIpAddress.init(natConf[6..^1])) | ||
nat = NatNone | ||
except ValueError: | ||
error "not a valid IP address", address = natConf[6..^1] | ||
quit QuitFailure | ||
else: | ||
error "not a valid NAT mechanism", value = natConf | ||
quit QuitFailure | ||
var endpoint: tuple[ip: Option[ValidIpAddress], tcpPort: Option[Port], udpPort: Option[Port]] | ||
|
||
if nat != NatNone: | ||
let extIp = getExternalIP(nat) | ||
if extIP.isSome: | ||
endpoint.ip = some(ValidIpAddress.init extIp.get) | ||
# TODO redirectPorts in considered a gcsafety violation | ||
if strategy != NatNone: | ||
let extIp = getExternalIP(strategy) | ||
if extIP.isSome(): | ||
endpoint.ip = some(ValidIpAddress.init(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 Exception: | ||
# @TODO: nat.nim Error: can raise an unlisted exception: Exception. Isolate here for now. | ||
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: | ||
if extPorts.isSome(): | ||
let (extTcpPort, extUdpPort) = extPorts.get() | ||
endpoint.tcpPort = some(extTcpPort) | ||
endpoint.udpPort = some(extUdpPort) | ||
|
||
return endpoint | ||
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 | ||
endpoint.ip = some(ValidIpAddress.init(natConf[6..^1])) | ||
except ValueError: | ||
return err("not a valid IP address: " & $natConf[6..^1]) | ||
|
||
return ok(endpoint) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are calling
quit()
in thewakubridge
, but not here - should the reaction be consistent accross apps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree indeed. However, this is consistent within the chat2bridge.nim itself. I think it is better if we raise a separate PR for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vpavlin - Next week I will create a separate PR for adding consistency in the parameters checks for all the apps.