From 2fd5b6c4eaed4b3a1e4a23e794956381d8ad2f3f Mon Sep 17 00:00:00 2001 From: Hanno Cornelius <68783915+jm-clius@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:10:38 +0100 Subject: [PATCH] Domain name config for `nwaku` node (#852) --- tests/v2/test_wakunode.nim | 19 +++++++++++++++ waku/v2/node/config.nim | 5 ++++ waku/v2/node/wakunode2.nim | 48 +++++++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/tests/v2/test_wakunode.nim b/tests/v2/test_wakunode.nim index 2eef23576..1e4cb66c5 100644 --- a/tests/v2/test_wakunode.nim +++ b/tests/v2/test_wakunode.nim @@ -1482,3 +1482,22 @@ procSuite "WakuNode": node.switch.peerInfo.addrs.contains(announcedEndpoint) await node.stop() + + asyncTest "Node can use dns4 in announced addresses": + let + nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[] + bindIp = ValidIpAddress.init("0.0.0.0") + bindPort = Port(60000) + extIp = some(ValidIpAddress.init("127.0.0.1")) + extPort = some(Port(60002)) + domainName = "example.com" + expectedDns4Addr = MultiAddress.init("/dns4/" & domainName & "/tcp/" & $(extPort.get())).get() + node = WakuNode.new( + nodeKey, + bindIp, bindPort, + extIp, extPort, + dns4DomainName = some(domainName)) + + check: + node.announcedAddresses.len == 1 + node.announcedAddresses.contains(expectedDns4Addr) diff --git a/waku/v2/node/config.nim b/waku/v2/node/config.nim index c5a3d6acb..b38449a53 100644 --- a/waku/v2/node/config.nim +++ b/waku/v2/node/config.nim @@ -76,6 +76,11 @@ type desc: "DNS name server IPs to query for DNS multiaddrs resolution. Argument may be repeated." defaultValue: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")] name: "dns-addrs-name-server" }: seq[ValidIpAddress] + + dns4DomainName* {. + desc: "The domain name resolving to the node's public IPv4 address", + defaultValue: "" + name: "dns4-domain-name" }: string ## Relay config diff --git a/waku/v2/node/wakunode2.nim b/waku/v2/node/wakunode2.nim index 5e0519042..36468ccbe 100644 --- a/waku/v2/node/wakunode2.nim +++ b/waku/v2/node/wakunode2.nim @@ -142,10 +142,19 @@ proc updateSwitchPeerInfo(node: WakuNode) = if node.announcedAddresses.len > 0: node.switch.peerInfo.addrs = node.announcedAddresses -template tcpEndPoint(address, port): auto = +template ip4TcpEndPoint(address, port): MultiAddress = MultiAddress.init(address, tcpProtocol, port) -func wsFlag(wssEnabled: bool): MultiAddress {.raises: [Defect, LPError]} = +template dns4Ma(dns4DomainName: string): MultiAddress = + MultiAddress.init("/dns4/" & dns4DomainName).tryGet() + +template tcpPortMa(port: Port): MultiAddress = + MultiAddress.init("/tcp/" & $port).tryGet() + +template dns4TcpEndPoint(dns4DomainName: string, port: Port): MultiAddress = + dns4Ma(dns4DomainName) & tcpPortMa(port) + +template wsFlag(wssEnabled: bool): MultiAddress = if wssEnabled: MultiAddress.init("/wss").tryGet() else: MultiAddress.init("/ws").tryGet() @@ -161,6 +170,7 @@ proc new*(T: type WakuNode, nodeKey: crypto.PrivateKey, secureCert: string = "", wakuFlags = none(WakuEnrBitfield), nameResolver: NameResolver = nil, + dns4DomainName = none(string) ): T {.raises: [Defect, LPError, IOError, TLSStreamProtocolError].} = ## Creates a Waku Node. @@ -171,16 +181,28 @@ proc new*(T: type WakuNode, nodeKey: crypto.PrivateKey, ## Initialize addresses let # Bind addresses - hostAddress = tcpEndPoint(bindIp, bindPort) - wsHostAddress = if wsEnabled or wssEnabled: some(tcpEndPoint(bindIp, wsbindPort) & wsFlag(wssEnabled)) + hostAddress = ip4TcpEndPoint(bindIp, bindPort) + wsHostAddress = if wsEnabled or wssEnabled: some(ip4TcpEndPoint(bindIp, wsbindPort) & wsFlag(wssEnabled)) else: none(MultiAddress) - # External addresses - hostExtAddress = if extIp.isNone() or extPort.isNone(): none(MultiAddress) - else: some(tcpEndPoint(extIp.get(), extPort.get())) - wsExtAddress = if wsHostAddress.isNone(): none(MultiAddress) - elif hostExtAddress.isNone(): none(MultiAddress) - else: some(tcpEndPoint(extIp.get(), wsBindPort) & wsFlag(wssEnabled)) + # Setup external addresses, if available + var + hostExtAddress, wsExtAddress = none(MultiAddress) + + if (dns4DomainName.isSome()): + # Use dns4 for externally announced addresses + + hostExtAddress = some(dns4TcpEndPoint(dns4DomainName.get(), extPort.get())) + + if (wsHostAddress.isSome()): + wsExtAddress = some(dns4TcpEndPoint(dns4DomainName.get(), wsBindPort) & wsFlag(wssEnabled)) + else: + # No public domain name, use ext IP if available + if extIp.isSome() and extPort.isSome(): + hostExtAddress = some(ip4TcpEndPoint(extIp.get(), extPort.get())) + + if (wsHostAddress.isSome()): + wsExtAddress = some(ip4TcpEndPoint(extIp.get(), wsBindPort) & wsFlag(wssEnabled)) var announcedAddresses: seq[MultiAddress] if hostExtAddress.isSome: @@ -1030,6 +1052,9 @@ when isMainModule: else: extTcpPort + dns4DomainName = if conf.dns4DomainName != "": some(conf.dns4DomainName) + else: none(string) + wakuFlags = initWakuFlags(conf.lightpush, conf.filter, conf.store, @@ -1046,7 +1071,8 @@ when isMainModule: conf.websocketSecureKeyPath, conf.websocketSecureCertPath, some(wakuFlags), - dnsResolver + dnsResolver, + dns4DomainName ) if conf.discv5Discovery: