Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.2 KiB
Nim
Raw Normal View History

{.push raises: [].}
2026-04-10 18:27:21 +04:00
import std/[net, tables, hashes, options], pkg/results, chronos, chronicles
import pkg/libp2p
type NatStrategy* = enum
NatAny
NatUpnp
NatPmp
NatNone
func isGlobalUnicast*(address: TransportAddress): bool =
if address.isGlobal() and address.isUnicast(): true else: false
func isGlobalUnicast*(address: IpAddress): bool =
let a = initTAddress(address, Port(0))
a.isGlobalUnicast()
2026-04-10 18:27:21 +04:00
proc getRoute(publicAddress: TransportAddress): Result[IpAddress, cstring] =
let route = getBestRoute(publicAddress)
2026-04-13 11:38:23 +04:00
if route.source.family == AddressFamily.None or route.source.isUnspecified():
2026-04-10 18:27:21 +04:00
err("No best route found")
else:
let ip =
try:
route.source.address()
except ValueError as e:
# This should not occur really.
error "Address conversion error", exception = e.name, msg = e.msg
return err("Invalid IP address")
Chore/update nim version (#1052) * Move to version 2.0.6 * Update nim-confutils submodule to latest version * Update dependencies * Update Nim version to 2.0.12 * Add gcsafe pragma * Add missing import * Update specific conf for Nim 2.x * Fix method signatures * Revert erasure coding attempt to fix bug * More gcsafe pragma * Duplicate code from libp2p because it is not exported anymore * Fix camelcase function names * Use alreadySeen because need is not a bool anymore * newLPStreamReadError does not exist anymore so use another error * Replace ValidIpAddress by IpAddress * Add gcsafe pragma * Restore maintenance parameter deleted by mistake when removing esasure coding fix attempt code * Update method signatures * Copy LPStreamReadError code from libp2p which was removed * Fix camel case * Fix enums in tests * Fix camel case * Extract node components to a variable to make Nim 2 happy * Update the tests using ValidIpAddress to IpAddress * Fix cast for value which is already an option * Set nim version to 2.0.x for CI * Set nim version to 2.0.x for CI * Move to miniupnp version 2.2.4 to avoid symlink error * Set core.symlinks to false for Windows for miniupnp >= 2.2.5 support * Update to Nim 2.0.14 * Update CI nim versions to 2.0.14 * Try with GCC 14 * Replace apt-fast by apt-get * Update ubuntu runner to latest * Use Ubuntu 20.04 for coverage * Disable CI cache for coverage * Add coverage property description * Remove commented test * Check the node value of seen instead of using alreadySeen * Fix the merge. The taskpool work was reverted. * Update nim-ethers submodule * Remove deprecated ValidIpAddress. Fix missing case and imports. * Fix a weird issue where nim-confutils cannot find NatAny * Fix tests and remove useless static keyword
2025-01-10 15:12:37 +01:00
ok(ip)
2026-04-10 18:27:21 +04:00
proc getRouteIpv4*(): Result[IpAddress, cstring] =
# Avoiding Exception with initTAddress and can't make it work with static.
# Note: `publicAddress` is only used an "example" IP to find the best route,
# no data is send over the network to this IP!
let publicAddress = TransportAddress(
family: AddressFamily.IPv4, address_v4: [1'u8, 1, 1, 1], port: Port(0)
)
return getRoute(publicAddress)
proc getRouteIpv6*(): Result[IpAddress, cstring] =
# Note: `googleDnsIpv6` is only used as an "example" IP to find the best route,
# no data is sent over the network to this IP!
const googleDnsIpv6 = TransportAddress(
family: AddressFamily.IPv6,
# 2001:4860:4860::8888
address_v6: [32'u8, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136],
port: Port(0),
)
return getRoute(googleDnsIpv6)
# If bindIp is a anyLocal address (0.0.0.0 or ::),
# the function will find the best ip address.
# Otherwise, it will just return the ip as it is.
proc getBestLocalAddress*(bindIp: IpAddress): Option[IpAddress] =
let bindAddress = initTAddress(bindIp, Port(0))
if bindAddress.isAnyLocal():
let ip =
if bindIp.family == IpAddressFamily.IPv6:
getRouteIpv6()
else:
getRouteIpv4()
if ip.isOk():
return some(ip.get())
return none(IpAddress)
else:
return some(bindIp)