From 1f990e7b0dfa9d1da392f969a0c3f563e4090ef6 Mon Sep 17 00:00:00 2001 From: fryorcraken Date: Tue, 8 Apr 2025 15:10:26 +1000 Subject: [PATCH] websocket config sorted --- waku/factory/builder.nim | 2 +- waku/factory/internal_config.nim | 20 +++++++++++++++----- waku/factory/node_factory.nim | 11 +++++++++-- waku/factory/waku_conf.nim | 10 ++++++---- waku/factory/waku_conf_builder.nim | 18 +++++++++--------- waku/node/{config.nim => net_config.nim} | 20 ++++++++++++++------ waku/node/waku_node.nim | 2 +- waku/waku_node.nim | 4 ++-- 8 files changed, 57 insertions(+), 30 deletions(-) rename waku/node/{config.nim => net_config.nim} (90%) diff --git a/waku/factory/builder.nim b/waku/factory/builder.nim index caa84db63..c7ab043ff 100644 --- a/waku/factory/builder.nim +++ b/waku/factory/builder.nim @@ -89,7 +89,7 @@ proc withNetworkConfigurationDetails*( extIp = extIp, extPort = extPort, extMultiAddrs = extMultiAddrs, - wsBindPort = wsBindPort, + wsBindPort = some(wsBindPort), wsEnabled = wsEnabled, wssEnabled = wssEnabled, wakuFlags = wakuFlags, diff --git a/waku/factory/internal_config.nim b/waku/factory/internal_config.nim index 4372ab2b2..d12930d10 100644 --- a/waku/factory/internal_config.nim +++ b/waku/factory/internal_config.nim @@ -9,11 +9,10 @@ import import ./external_config, ../common/utils/nat, - ../node/config, + ../node/net_config, ../waku_enr/capabilities, ../waku_enr, ../waku_core, - ./networks_config, ./waku_conf proc enrConfiguration*( @@ -115,6 +114,17 @@ proc networkConfiguration*(conf: WakuConf, clientId: string): NetConfigResult = return err("Could not update extIp to resolved DNS IP: " & getCurrentExceptionMsg()) + let (wsEnabled, wsBindPort, wssEnabled) = + if conf.webSocketConf.isSome: + let webSocketConf = conf.webSocketConf.get() + ( + true, + some(Port(webSocketConf.port.uint16 + conf.portsShift)), + webSocketConf.secureConf.isSome, + ) + else: + (false, none(Port), false) + # Wrap in none because NetConfig does not have a default constructor # TODO: We could change bindIp in NetConfig to be something less restrictive # than IpAddress, which doesn't allow default construction @@ -126,9 +136,9 @@ proc networkConfiguration*(conf: WakuConf, clientId: string): NetConfigResult = extPort = extPort, extMultiAddrs = conf.extMultiAddrs, extMultiAddrsOnly = conf.extMultiAddrsOnly, - wsBindPort = Port(uint16(conf.webSocketConf.webSocketPort) + conf.portsShift), - wsEnabled = conf.webSocketConf.webSocketSupport, - wssEnabled = conf.webSocketConf.webSocketSecureSupport, + wsBindPort = wsBindPort, + wsEnabled = wsEnabled, + wssEnabled = wssEnabled, dns4DomainName = conf.dns4DomainName.map( proc(dn: DomainName): string = dn.string diff --git a/waku/factory/node_factory.nim b/waku/factory/node_factory.nim index ca9dba74d..efa166481 100644 --- a/waku/factory/node_factory.nim +++ b/waku/factory/node_factory.nim @@ -87,6 +87,13 @@ proc initNode( else: peerStore.get() + let (secureKey, secureCert) = + if conf.webSocketConf.isSome and conf.webSocketConf.get().secureConf.isSome: + let wssConf = conf.webSocketConf.get().secureConf.get() + (some(wssConf.keyPath), some(wssConf.certPath)) + else: + (none(string), none(string)) + # Build waku node instance var builder = WakuNodeBuilder.init() builder.withRng(rng) @@ -96,8 +103,8 @@ proc initNode( builder.withPeerStorage(pStorage, capacity = conf.peerStoreCapacity) builder.withSwitchConfiguration( maxConnections = some(conf.maxConnections.int), - secureKey = some(conf.webSocketSecureKeyPath), - secureCert = some(conf.webSocketSecureCertPath), + secureKey = secureKey, + secureCert = secureCert, nameResolver = dnsResolver, sendSignedPeerRecord = conf.relayPeerExchange, # We send our own signed peer record when peer exchange enabled diff --git a/waku/factory/waku_conf.nim b/waku/factory/waku_conf.nim index 4528ea55a..da3fa7594 100644 --- a/waku/factory/waku_conf.nim +++ b/waku/factory/waku_conf.nim @@ -33,12 +33,12 @@ type RlnRelayConf* = ref object ethClientAddress*: EthRpcUrl type WebSocketSecureConf* = ref object - webSocketSecureKeyPath*: string - webSocketSecureCertPath*: string + keyPath*: string + certPath*: string type WebSocketConf* = ref object - webSocketPort*: Port - webSocketSecureConf*: Option[WebSocketSecureConf] + port*: Port + secureConf*: Option[WebSocketSecureConf] ## `WakuConf` is a valid configuration for a Waku node ## All information needed by a waku node should be contained @@ -57,6 +57,8 @@ type WakuConf* = ref object lightPush*: bool peerExchange*: bool storeSync*: bool + # TODO: remove relay peer exchange + relayPeerExchange*: bool discv5Conf*: Option[Discv5Conf] diff --git a/waku/factory/waku_conf_builder.nim b/waku/factory/waku_conf_builder.nim index 0c8168f10..a2fde11e9 100644 --- a/waku/factory/waku_conf_builder.nim +++ b/waku/factory/waku_conf_builder.nim @@ -197,11 +197,7 @@ proc build(builder: WebSocketConfBuilder): Result[Option[WebSocketConf], string] if not builder.webSocketSecureSupport.get(false): return ok( - some( - WebSocketConf( - webSocketPort: websocketPort, webSocketSecureConf: none(WebSocketSecureConf) - ) - ) + some(WebSocketConf(port: websocketPort, secureConf: none(WebSocketSecureConf))) ) let webSocketSecureKeyPath = builder.webSocketSecureKeyPath.get("") @@ -215,11 +211,10 @@ proc build(builder: WebSocketConfBuilder): Result[Option[WebSocketConf], string] return ok( some( WebSocketConf( - webSocketPort: webSocketPort, - webSocketSecureConf: some( + port: webSocketPort, + secureConf: some( WebSocketSecureConf( - webSocketSecureKeyPath: webSocketSecureKeyPath, - webSocketSecureCertPath: webSocketSecureCertPath, + keyPath: webSocketSecureKeyPath, certPath: webSocketSecureCertPath ) ), ) @@ -243,6 +238,7 @@ type WakuConfBuilder* = ref object lightPush: Option[bool] peerExchange: Option[bool] storeSync: Option[bool] + relayPeerExchange: Option[bool] clusterConf: Option[ClusterConf] @@ -285,6 +281,7 @@ with(WakuConfBuilder, clusterId, uint16) with(WakuConfBuilder, relay, bool) with(WakuConfBuilder, filter, bool) with(WakuConfBuilder, storeSync, bool) +with(WakuConfBuilder, relayPeerExchange, bool) with(WakuConfBuilder, maxMessageSizeBytes, int) with(WakuConfBuilder, dnsAddrs, bool) with(WakuConfbuilder, peerPersistence, bool) @@ -359,6 +356,8 @@ proc build*( warn "whether to mount storeSync is not specified, defaulting to not mounting" false + let relayPeerExchange = builder.relayPeerExchange.get(false) + # Apply cluster conf - values passed manually override cluster conf # Should be applied **first**, before individual values are pulled if builder.clusterConf.isSome: @@ -584,6 +583,7 @@ proc build*( filter: filter, lightPush: lightPush, peerExchange: peerExchange, + relayPeerExchange: relayPeerExchange, discv5Conf: discv5Conf, rlnRelayConf: rlnRelayConf, maxMessageSizeBytes: maxMessageSizeBytes, diff --git a/waku/node/config.nim b/waku/node/net_config.nim similarity index 90% rename from waku/node/config.nim rename to waku/node/net_config.nim index 311e26771..6722998d8 100644 --- a/waku/node/config.nim +++ b/waku/node/net_config.nim @@ -60,6 +60,8 @@ proc isWsAddress*(ma: MultiAddress): bool = proc containsWsAddress(extMultiAddrs: seq[MultiAddress]): bool = return extMultiAddrs.filterIt(it.isWsAddress()).len > 0 +const DefaultWsBindPort = static(Port(8000)) +# TODO: migrate to builder pattern proc init*( T: type NetConfig, bindIp: IpAddress, @@ -68,7 +70,7 @@ proc init*( extPort = none(Port), extMultiAddrs = newSeq[MultiAddress](), extMultiAddrsOnly: bool = false, - wsBindPort: Port = Port(8000), + wsBindPort: Option[Port] = some(DefaultWsBindPort), wsEnabled: bool = false, wssEnabled: bool = false, dns4DomainName = none(string), @@ -84,7 +86,9 @@ proc init*( var wsHostAddress = none(MultiAddress) if wsEnabled or wssEnabled: try: - wsHostAddress = some(ip4TcpEndPoint(bindIp, wsbindPort) & wsFlag(wssEnabled)) + wsHostAddress = some( + ip4TcpEndPoint(bindIp, wsbindPort.get(DefaultWsBindPort)) & wsFlag(wssEnabled) + ) except CatchableError: return err(getCurrentExceptionMsg()) @@ -111,8 +115,10 @@ proc init*( if wsHostAddress.isSome(): try: - wsExtAddress = - some(dns4TcpEndPoint(dns4DomainName.get(), wsBindPort) & wsFlag(wssEnabled)) + wsExtAddress = some( + dns4TcpEndPoint(dns4DomainName.get(), wsBindPort.get(DefaultWsBindPort)) & + wsFlag(wssEnabled) + ) except CatchableError: return err(getCurrentExceptionMsg()) else: @@ -122,8 +128,10 @@ proc init*( if wsHostAddress.isSome(): try: - wsExtAddress = - some(ip4TcpEndPoint(extIp.get(), wsBindPort) & wsFlag(wssEnabled)) + wsExtAddress = some( + ip4TcpEndPoint(extIp.get(), wsBindPort.get(DefaultWsBindPort)) & + wsFlag(wssEnabled) + ) except CatchableError: return err(getCurrentExceptionMsg()) diff --git a/waku/node/waku_node.nim b/waku/node/waku_node.nim index 18986d5c0..c392cdcf9 100644 --- a/waku/node/waku_node.nim +++ b/waku/node/waku_node.nim @@ -46,7 +46,7 @@ import ../waku_enr, ../waku_peer_exchange, ../waku_rln_relay, - ./config, + ./net_config, ./peer_manager, ../common/rate_limit/setting diff --git a/waku/waku_node.nim b/waku/waku_node.nim index f1c647111..74415e9de 100644 --- a/waku/waku_node.nim +++ b/waku/waku_node.nim @@ -1,7 +1,7 @@ import - ./node/config, + ./node/net_config, ./node/waku_switch as switch, ./node/waku_node as node, ./node/health_monitor as health_monitor -export config, switch, node, health_monitor +export net_config, switch, node, health_monitor