mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-01-09 09:03:10 +00:00
* remove redundant gcsafe/raises * rework async raises to chronos 4.0 where this was not yet done * streamline logging between http/socket/ws * don't log error when raising exceptions (whoever handles should log) * debug-log requests in all variants of server and client * unify ipv4/ipv6 address resolution, with preference for ipv6 * fix server start so that it consistently raises only when no addresses could be bound
63 lines
1.8 KiB
Nim
63 lines
1.8 KiB
Nim
import chronos, ../errors
|
|
|
|
from std/net import IPv6_any, IPv4_any
|
|
|
|
template processResolvedAddresses(what: string) =
|
|
if ips.len == 0:
|
|
# Addresses could not be resolved, critical error.
|
|
raise newException(RpcAddressUnresolvableError, "Unable to resolve " & what)
|
|
|
|
var dualStack = Opt.none(Port)
|
|
for ip in ips:
|
|
# Only yield the "any" address once because we try to use dual stack where
|
|
# available
|
|
if ip.toIpAddress() == IPv6_any():
|
|
dualStack = Opt.some(ip.port)
|
|
elif ip.toIpAddress() == IPv4_any() and dualStack == Opt.some(ip.port):
|
|
continue
|
|
yield ip
|
|
|
|
iterator resolveIP*(
|
|
addresses: openArray[string]
|
|
): TransportAddress {.raises: [JsonRpcError].} =
|
|
var ips: seq[TransportAddress]
|
|
# Resolve IPv6 first so that dual stack detection works as expected
|
|
for address in addresses:
|
|
try:
|
|
for resolved in resolveTAddress(address, AddressFamily.IPv6):
|
|
if resolved notin ips:
|
|
ips.add resolved
|
|
except TransportAddressError:
|
|
discard
|
|
|
|
for address in addresses:
|
|
try:
|
|
for resolved in resolveTAddress(address, AddressFamily.IPv4):
|
|
if resolved notin ips:
|
|
ips.add resolved
|
|
except TransportAddressError:
|
|
discard
|
|
|
|
processResolvedAddresses($addresses)
|
|
|
|
iterator resolveIP*(
|
|
address: string, port: Port
|
|
): TransportAddress {.raises: [JsonRpcError].} =
|
|
var ips: seq[TransportAddress]
|
|
# Resolve IPv6 first so that dual stack detection works as expected
|
|
try:
|
|
for resolved in resolveTAddress(address, port, AddressFamily.IPv6):
|
|
if resolved notin ips:
|
|
ips.add resolved
|
|
except TransportAddressError:
|
|
discard
|
|
|
|
try:
|
|
for resolved in resolveTAddress(address, port, AddressFamily.IPv4):
|
|
if resolved notin ips:
|
|
ips.add resolved
|
|
except TransportAddressError:
|
|
discard
|
|
|
|
processResolvedAddresses($address & ":" & $port)
|