2023-06-22 20:58:14 +00:00
|
|
|
import
|
2023-09-27 13:02:24 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/multiaddress,
|
|
|
|
libp2p/nameresolving/dnsresolver,
|
2023-06-29 19:59:53 +00:00
|
|
|
std/options,
|
2023-06-22 20:58:14 +00:00
|
|
|
stew/results,
|
2023-09-27 13:02:24 +00:00
|
|
|
stew/shims/net
|
2023-06-22 20:58:14 +00:00
|
|
|
import
|
|
|
|
../../waku/common/utils/nat,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/node/config,
|
|
|
|
../../waku/waku_enr/capabilities,
|
2023-06-22 20:58:14 +00:00
|
|
|
./external_config
|
|
|
|
|
2023-06-29 19:59:53 +00:00
|
|
|
proc validateExtMultiAddrs*(vals: seq[string]):
|
|
|
|
Result[seq[MultiAddress], string] =
|
|
|
|
var multiaddrs: seq[MultiAddress]
|
|
|
|
for val in vals:
|
|
|
|
let multiaddr = ? MultiAddress.init(val)
|
|
|
|
multiaddrs.add(multiaddr)
|
|
|
|
return ok(multiaddrs)
|
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
proc dnsResolve*(domain: string, conf: WakuNodeConf): Future[Result[string, string]] {.async} =
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
# Use conf's DNS servers
|
|
|
|
var nameServers: seq[TransportAddress]
|
|
|
|
for ip in conf.dnsAddrsNameServers:
|
|
|
|
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
let dnsResolver = DnsResolver.new(nameServers)
|
|
|
|
|
|
|
|
# Resolve domain IP
|
|
|
|
let resolved = await dnsResolver.resolveIp(domain, 0.Port, Domain.AF_UNSPEC)
|
|
|
|
|
|
|
|
if resolved.len > 0:
|
|
|
|
return ok(resolved[0].host) # Use only first answer
|
|
|
|
else:
|
|
|
|
return err("Could not resolve IP from DNS: empty response")
|
|
|
|
|
2023-06-29 19:59:53 +00:00
|
|
|
proc networkConfiguration*(conf: WakuNodeConf,
|
|
|
|
clientId: string,
|
|
|
|
): NetConfigResult =
|
2023-06-22 20:58:14 +00:00
|
|
|
|
|
|
|
## `udpPort` is only supplied to satisfy underlying APIs but is not
|
|
|
|
## actually a supported transport for libp2p traffic.
|
|
|
|
let natRes = setupNat(conf.nat, clientId,
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift),
|
2023-06-29 19:59:53 +00:00
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift))
|
2023-06-22 20:58:14 +00:00
|
|
|
if natRes.isErr():
|
|
|
|
return err("failed to setup NAT: " & $natRes.error)
|
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
var (extIp, extTcpPort, _) = natRes.get()
|
2023-06-22 20:58:14 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
dns4DomainName = if conf.dns4DomainName != "": some(conf.dns4DomainName)
|
2023-06-29 19:59:53 +00:00
|
|
|
else: none(string)
|
2023-06-22 20:58:14 +00:00
|
|
|
|
2023-06-29 19:59:53 +00:00
|
|
|
discv5UdpPort = if conf.discv5Discovery:
|
|
|
|
some(Port(uint16(conf.discv5UdpPort) + conf.portsShift))
|
2023-06-22 20:58:14 +00:00
|
|
|
else: none(Port)
|
|
|
|
|
2023-06-29 19:59:53 +00:00
|
|
|
## TODO: the NAT setup assumes a manual port mapping configuration if extIp
|
|
|
|
## config is set. This probably implies adding manual config item for
|
|
|
|
## extPort as well. The following heuristic assumes that, in absence of
|
|
|
|
## manual config, the external port is the same as the bind port.
|
|
|
|
|
|
|
|
extPort = if (extIp.isSome() or dns4DomainName.isSome()) and
|
|
|
|
extTcpPort.isNone():
|
2023-06-22 20:58:14 +00:00
|
|
|
some(Port(uint16(conf.tcpPort) + conf.portsShift))
|
|
|
|
else:
|
|
|
|
extTcpPort
|
2023-06-29 19:59:53 +00:00
|
|
|
|
2023-06-22 20:58:14 +00:00
|
|
|
extMultiAddrs = if (conf.extMultiAddrs.len > 0):
|
2023-06-29 19:59:53 +00:00
|
|
|
let extMultiAddrsValidationRes =
|
|
|
|
validateExtMultiAddrs(conf.extMultiAddrs)
|
2023-06-22 20:58:14 +00:00
|
|
|
if extMultiAddrsValidationRes.isErr():
|
2023-06-29 19:59:53 +00:00
|
|
|
return err("invalid external multiaddress: " &
|
|
|
|
$extMultiAddrsValidationRes.error)
|
2023-06-22 20:58:14 +00:00
|
|
|
else:
|
|
|
|
extMultiAddrsValidationRes.get()
|
|
|
|
else:
|
|
|
|
@[]
|
|
|
|
|
|
|
|
wakuFlags = CapabilitiesBitfield.init(
|
|
|
|
lightpush = conf.lightpush,
|
|
|
|
filter = conf.filter,
|
|
|
|
store = conf.store,
|
|
|
|
relay = conf.relay
|
|
|
|
)
|
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
# Resolve and use DNS domain IP
|
|
|
|
if dns4DomainName.isSome() and extIp.isNone():
|
|
|
|
try:
|
|
|
|
let dnsRes = waitFor dnsResolve(conf.dns4DomainName, conf)
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
if dnsRes.isErr():
|
|
|
|
return err($dnsRes.error) # Pass error down the stack
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-09-27 13:02:24 +00:00
|
|
|
extIp = some(ValidIpAddress.init(dnsRes.get()))
|
|
|
|
except CatchableError:
|
|
|
|
return err("Could not update extIp to resolved DNS IP: " & getCurrentExceptionMsg())
|
2023-10-11 06:58:45 +00:00
|
|
|
|
2023-06-22 20:58:14 +00:00
|
|
|
# Wrap in none because NetConfig does not have a default constructor
|
2023-06-29 19:59:53 +00:00
|
|
|
# TODO: We could change bindIp in NetConfig to be something less restrictive
|
|
|
|
# than ValidIpAddress, which doesn't allow default construction
|
2023-06-22 20:58:14 +00:00
|
|
|
let netConfigRes = NetConfig.init(
|
2023-10-11 06:58:45 +00:00
|
|
|
clusterId = conf.clusterId,
|
2023-06-22 20:58:14 +00:00
|
|
|
bindIp = conf.listenAddress,
|
|
|
|
bindPort = Port(uint16(conf.tcpPort) + conf.portsShift),
|
|
|
|
extIp = extIp,
|
|
|
|
extPort = extPort,
|
|
|
|
extMultiAddrs = extMultiAddrs,
|
|
|
|
wsBindPort = Port(uint16(conf.websocketPort) + conf.portsShift),
|
|
|
|
wsEnabled = conf.websocketSupport,
|
|
|
|
wssEnabled = conf.websocketSecureSupport,
|
|
|
|
dns4DomainName = dns4DomainName,
|
|
|
|
discv5UdpPort = discv5UdpPort,
|
|
|
|
wakuFlags = some(wakuFlags),
|
|
|
|
)
|
|
|
|
|
2023-06-29 19:59:53 +00:00
|
|
|
return netConfigRes
|