fix: consider WS extMultiAddrs before publishing host address (#2122)

This commit is contained in:
gabrielmer 2023-10-17 12:53:41 +03:00 committed by GitHub
parent ce5fb3401d
commit a5b1cfd024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -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]

View File

@ -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,16 @@ 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 containsWsAddress(extMultiAddrs: seq[MultiAddress]): bool =
return extMultiAddrs.filterIt( it.isWsAddress() ).len > 0
proc init*(T: type NetConfig,
bindIp: ValidIpAddress,
bindPort: Port,
@ -126,7 +136,8 @@ proc init*(T: type NetConfig,
if wsExtAddress.isSome():
announcedAddresses.add(wsExtAddress.get())
elif wsHostAddress.isSome():
elif wsHostAddress.isSome() and not containsWsAddress(extMultiAddrs):
# Only publish wsHostAddress if a WS address is not set in extMultiAddrs
announcedAddresses.add(wsHostAddress.get())
let