From 8f46203c9f910aa23bb1d0e74b79d4ef1e5385e8 Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Wed, 11 Oct 2023 11:50:55 +0300 Subject: [PATCH 1/2] fix: consider WS extMultiAddrs before publishing host address --- tests/test_waku_netconfig.nim | 23 +++++++++++++++++++++++ waku/node/config.nim | 19 +++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 5ecbd258f8..0c02afab13 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -338,3 +338,26 @@ suite "Waku NetConfig": check: netConfig.enrMultiaddrs.contains(dns4TcpEndPoint(dns4DomainName, extPort)) + asyncTest "wsHostAddress is not announced if a WS address is provided in extMultiAddrs": + + let + conf = defaultTestWakuNodeConf() + extAddIp = ValidIpAddress.init("1.2.3.4") + extAddPort = Port(1234) + wssEnabled = false + extMultiAddrs = @[(ip4TcpEndPoint(extAddIp, extAddPort) & wsFlag(wssEnabled))] + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extMultiAddrs = extMultiAddrs + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # Bind address + extAddress + netConfig.announcedAddresses[1] == extMultiAddrs[0] + diff --git a/waku/node/config.nim b/waku/node/config.nim index 71cfaf8bc1..774313d8e6 100644 --- a/waku/node/config.nim +++ b/waku/node/config.nim @@ -7,7 +7,7 @@ import std/[options, sequtils, strutils], stew/results, stew/shims/net, - libp2p/multiaddress + libp2p/[multiaddress, multicodec] import ../../waku/waku_core/peers import @@ -59,6 +59,20 @@ proc formatListenAddress(inputMultiAdd: MultiAddress): MultiAddress = # If MultiAddress contains "0.0.0.0", replace it for "127.0.0.1" return MultiAddress.init(inputStr.replace("0.0.0.0", "127.0.0.1")).get() +proc isWsAddress(ma: MultiAddress): bool = + let + isWs = ma.contains(multiCodec("ws")).get() + isWss = ma.contains(multiCodec("wss")).get() + + return isWs or isWss + +proc findWsAddress(extMultiAddrs: seq[MultiAddress]): Option[MultiAddress] = + for a in extMultiAddrs: + if isWsAddress(a): + return some(a) + + return none(MultiAddress) + proc init*(T: type NetConfig, bindIp: ValidIpAddress, bindPort: Port, @@ -126,7 +140,8 @@ proc init*(T: type NetConfig, if wsExtAddress.isSome(): announcedAddresses.add(wsExtAddress.get()) - elif wsHostAddress.isSome(): + elif wsHostAddress.isSome() and findWsAddress(extMultiAddrs).isNone(): + # Only publish wsHostAddress if a WS address is not set in extMultiAddrs announcedAddresses.add(wsHostAddress.get()) let From ba718eee0eaef21497f1c213db9174f2e7e6e32f Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Mon, 16 Oct 2023 11:08:10 +0300 Subject: [PATCH 2/2] Refactor ws address check --- waku/node/config.nim | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/waku/node/config.nim b/waku/node/config.nim index 774313d8e6..f90aaff1d6 100644 --- a/waku/node/config.nim +++ b/waku/node/config.nim @@ -66,12 +66,8 @@ proc isWsAddress(ma: MultiAddress): bool = return isWs or isWss -proc findWsAddress(extMultiAddrs: seq[MultiAddress]): Option[MultiAddress] = - for a in extMultiAddrs: - if isWsAddress(a): - return some(a) - - return none(MultiAddress) +proc containsWsAddress(extMultiAddrs: seq[MultiAddress]): bool = + return extMultiAddrs.filterIt( it.isWsAddress() ).len > 0 proc init*(T: type NetConfig, bindIp: ValidIpAddress, @@ -140,7 +136,7 @@ proc init*(T: type NetConfig, if wsExtAddress.isSome(): announcedAddresses.add(wsExtAddress.get()) - elif wsHostAddress.isSome() and findWsAddress(extMultiAddrs).isNone(): + elif wsHostAddress.isSome() and not containsWsAddress(extMultiAddrs): # Only publish wsHostAddress if a WS address is not set in extMultiAddrs announcedAddresses.add(wsHostAddress.get())