mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-08-25 08:11:15 +00:00
Update imports
This commit is contained in:
@@ -74,38 +74,3 @@ proc getMultiAddrWithIpAndTcpPort*(ip: IpAddress, port: Port): MultiAddress =
|
||||
return MultiAddress.init(ipFamily & $ip & "/tcp/" & $port).expect(
|
||||
"Failed to construct multiaddress with IP and TCP port"
|
||||
)
|
||||
|
||||
proc getAddressAndPort*(
|
||||
ma: MultiAddress
|
||||
): tuple[ip: Option[IpAddress], port: Option[Port]] =
|
||||
try:
|
||||
# Try IPv4 first
|
||||
let ipv4Result = ma[multiCodec("ip4")]
|
||||
let ip =
|
||||
if ipv4Result.isOk:
|
||||
let ipBytes = ipv4Result.get().protoArgument().expect("Invalid IPv4 format")
|
||||
let ipArray = [ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3]]
|
||||
some(IpAddress(family: IPv4, address_v4: ipArray))
|
||||
else:
|
||||
# Try IPv6 if IPv4 not found
|
||||
let ipv6Result = ma[multiCodec("ip6")]
|
||||
if ipv6Result.isOk:
|
||||
let ipBytes = ipv6Result.get().protoArgument().expect("Invalid IPv6 format")
|
||||
var ipArray: array[16, byte]
|
||||
for i in 0 .. 15:
|
||||
ipArray[i] = ipBytes[i]
|
||||
some(IpAddress(family: IPv6, address_v6: ipArray))
|
||||
else:
|
||||
none(IpAddress)
|
||||
|
||||
# Get TCP Port
|
||||
let portResult = ma[multiCodec("tcp")]
|
||||
let port =
|
||||
if portResult.isOk:
|
||||
let portBytes = portResult.get().protoArgument().expect("Invalid port format")
|
||||
some(Port(fromBytesBE(uint16, portBytes)))
|
||||
else:
|
||||
none(Port)
|
||||
(ip: ip, port: port)
|
||||
except Exception:
|
||||
(ip: none(IpAddress), port: none(Port))
|
||||
|
||||
+19
-31
@@ -1,6 +1,6 @@
|
||||
{.push raises: [].}
|
||||
|
||||
import std/[net, tables, hashes, options], pkg/results, chronos, chronicles
|
||||
import std/[net, options], pkg/results, chronos, chronicles
|
||||
|
||||
import pkg/libp2p
|
||||
|
||||
@@ -9,54 +9,42 @@ type NatStrategy* = enum
|
||||
NatUpnp
|
||||
NatPmp
|
||||
|
||||
func isGlobalUnicast*(address: TransportAddress): bool =
|
||||
if address.isGlobal() and address.isUnicast(): true else: false
|
||||
proc getRouteIpv4*(): Result[IpAddress, cstring] =
|
||||
let
|
||||
publicAddress = TransportAddress(
|
||||
family: AddressFamily.IPv4, address_v4: [1'u8, 1, 1, 1], port: Port(0)
|
||||
)
|
||||
route = getBestRoute(publicAddress)
|
||||
|
||||
func isGlobalUnicast*(address: IpAddress): bool =
|
||||
let a = initTAddress(address, Port(0))
|
||||
a.isGlobalUnicast()
|
||||
|
||||
proc getRoute(publicAddress: TransportAddress): Result[IpAddress, cstring] =
|
||||
let route = getBestRoute(publicAddress)
|
||||
|
||||
if route.source.family == AddressFamily.None or route.source.isUnspecified():
|
||||
err("No best route found")
|
||||
if route.source.isUnspecified():
|
||||
err("No best ipv4 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")
|
||||
ok(ip)
|
||||
|
||||
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),
|
||||
)
|
||||
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")
|
||||
|
||||
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] =
|
||||
## If bindIp is anyLocal (0.0.0.0 or ::), finds the best local IP via routing table.
|
||||
## Otherwise returns bindIp as-is.
|
||||
let bindAddress = initTAddress(bindIp, Port(0))
|
||||
if bindAddress.isAnyLocal():
|
||||
let ip =
|
||||
|
||||
Reference in New Issue
Block a user