websocket config sorted

This commit is contained in:
fryorcraken 2025-04-08 15:10:26 +10:00
parent 298e0a0fe3
commit 1f990e7b0d
8 changed files with 57 additions and 30 deletions

View File

@ -89,7 +89,7 @@ proc withNetworkConfigurationDetails*(
extIp = extIp,
extPort = extPort,
extMultiAddrs = extMultiAddrs,
wsBindPort = wsBindPort,
wsBindPort = some(wsBindPort),
wsEnabled = wsEnabled,
wssEnabled = wssEnabled,
wakuFlags = wakuFlags,

View File

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

View File

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

View File

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

View File

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

View File

@ -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())

View File

@ -46,7 +46,7 @@ import
../waku_enr,
../waku_peer_exchange,
../waku_rln_relay,
./config,
./net_config,
./peer_manager,
../common/rate_limit/setting

View File

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