2025-01-09 23:41:22 +05:30
|
|
|
{.push raises: [].}
|
|
|
|
|
|
2026-06-04 14:34:53 +04:00
|
|
|
import std/[net, options], pkg/results, chronos, chronicles
|
2025-01-09 23:41:22 +05:30
|
|
|
|
|
|
|
|
import pkg/libp2p
|
|
|
|
|
|
|
|
|
|
type NatStrategy* = enum
|
2026-05-05 10:55:11 +04:00
|
|
|
NatAuto
|
2025-01-09 23:41:22 +05:30
|
|
|
NatUpnp
|
|
|
|
|
NatPmp
|
|
|
|
|
|
2026-06-04 14:34:53 +04:00
|
|
|
proc getRouteIpv4*(): Result[IpAddress, cstring] =
|
|
|
|
|
let
|
|
|
|
|
publicAddress = TransportAddress(
|
|
|
|
|
family: AddressFamily.IPv4, address_v4: [1'u8, 1, 1, 1], port: Port(0)
|
|
|
|
|
)
|
|
|
|
|
route = getBestRoute(publicAddress)
|
|
|
|
|
|
|
|
|
|
if route.source.isUnspecified():
|
|
|
|
|
err("No best ipv4 route found")
|
2025-01-09 23:41:22 +05:30
|
|
|
else:
|
|
|
|
|
let ip =
|
|
|
|
|
try:
|
|
|
|
|
route.source.address()
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
error "Address conversion error", exception = e.name, msg = e.msg
|
|
|
|
|
return err("Invalid IP address")
|
2025-01-10 15:12:37 +01:00
|
|
|
ok(ip)
|
2026-04-10 18:27:21 +04:00
|
|
|
|
|
|
|
|
proc getRouteIpv6*(): Result[IpAddress, cstring] =
|
|
|
|
|
const googleDnsIpv6 = TransportAddress(
|
|
|
|
|
family: AddressFamily.IPv6,
|
|
|
|
|
address_v6: [32'u8, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136],
|
|
|
|
|
port: Port(0),
|
|
|
|
|
)
|
2026-06-04 14:34:53 +04:00
|
|
|
let route = getBestRoute(googleDnsIpv6)
|
|
|
|
|
if route.source.isUnspecified():
|
|
|
|
|
return err("No best ipv6 route found")
|
|
|
|
|
try:
|
|
|
|
|
ok(route.source.address())
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
error "Address conversion error", exception = e.name, msg = e.msg
|
|
|
|
|
err("Invalid IP address")
|
2026-04-10 18:27:21 +04:00
|
|
|
|
|
|
|
|
proc getBestLocalAddress*(bindIp: IpAddress): Option[IpAddress] =
|
2026-06-04 14:34:53 +04:00
|
|
|
## If bindIp is anyLocal (0.0.0.0 or ::), finds the best local IP via routing table.
|
|
|
|
|
## Otherwise returns bindIp as-is.
|
2026-04-10 18:27:21 +04:00
|
|
|
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)
|