2021-11-02 10:29:11 +00:00
|
|
|
|
# Waku Switch utils.
|
2022-10-28 09:51:46 +00:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
|
import
|
2023-01-31 16:26:22 +00:00
|
|
|
|
std/[options, math],
|
2021-11-02 10:29:11 +00:00
|
|
|
|
chronos, chronicles,
|
|
|
|
|
eth/keys,
|
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
|
libp2p/nameresolving/nameresolver,
|
|
|
|
|
libp2p/builders,
|
2023-01-11 09:57:49 +00:00
|
|
|
|
libp2p/switch,
|
2021-11-02 10:29:11 +00:00
|
|
|
|
libp2p/transports/[transport, tcptransport, wstransport]
|
|
|
|
|
|
2022-11-29 16:35:25 +00:00
|
|
|
|
# override nim-libp2p default value (which is also 1)
|
|
|
|
|
const MaxConnectionsPerPeer* = 1
|
|
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
|
proc withWsTransport*(b: SwitchBuilder): SwitchBuilder =
|
|
|
|
|
b.withTransport(proc(upgr: Upgrade): Transport = WsTransport.new(upgr))
|
|
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
|
proc getSecureKey(path : string): TLSPrivateKey
|
2022-10-28 09:51:46 +00:00
|
|
|
|
{.raises: [Defect, IOError].} =
|
|
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
|
trace "Key path is.", path=path
|
2022-10-28 09:51:46 +00:00
|
|
|
|
let stringkey: string = readFile(path)
|
2021-11-10 12:05:36 +00:00
|
|
|
|
try:
|
|
|
|
|
let key = TLSPrivateKey.init(stringkey)
|
|
|
|
|
return key
|
2021-11-23 11:40:43 +00:00
|
|
|
|
except TLSStreamProtocolError as exc:
|
|
|
|
|
debug "exception raised from getSecureKey", msg=exc.msg
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
|
|
|
|
proc getSecureCert(path : string): TLSCertificate
|
2022-10-28 09:51:46 +00:00
|
|
|
|
{.raises: [Defect, IOError].} =
|
|
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
|
trace "Certificate path is.", path=path
|
2022-10-28 09:51:46 +00:00
|
|
|
|
let stringCert: string = readFile(path)
|
2021-11-10 12:05:36 +00:00
|
|
|
|
try:
|
|
|
|
|
let cert = TLSCertificate.init(stringCert)
|
|
|
|
|
return cert
|
2021-11-23 11:40:43 +00:00
|
|
|
|
except TLSStreamProtocolError as exc:
|
|
|
|
|
debug "exception raised from getSecureCert", msg=exc.msg
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
|
|
|
|
proc withWssTransport*(b: SwitchBuilder,
|
|
|
|
|
secureKeyPath: string,
|
2022-10-28 09:51:46 +00:00
|
|
|
|
secureCertPath: string): SwitchBuilder
|
|
|
|
|
{.raises: [Defect, IOError].} =
|
2022-11-24 13:11:23 +00:00
|
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
|
let key : TLSPrivateKey = getSecureKey(secureKeyPath)
|
|
|
|
|
let cert : TLSCertificate = getSecureCert(secureCertPath)
|
|
|
|
|
b.withTransport(proc(upgr: Upgrade): Transport = WsTransport.new(upgr,
|
|
|
|
|
tlsPrivateKey = key,
|
|
|
|
|
tlsCertificate = cert,
|
|
|
|
|
{TLSFlags.NoVerifyHost, TLSFlags.NoVerifyServerName}))
|
|
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
|
proc newWakuSwitch*(
|
|
|
|
|
privKey = none(crypto.PrivateKey),
|
|
|
|
|
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
|
2021-12-06 19:51:37 +00:00
|
|
|
|
wsAddress = none(MultiAddress),
|
2021-11-02 10:29:11 +00:00
|
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
|
|
|
|
SecureProtocol.Noise,
|
|
|
|
|
],
|
|
|
|
|
transportFlags: set[ServerFlags] = {},
|
2023-02-06 11:53:05 +00:00
|
|
|
|
rng: ref HmacDrbgContext,
|
2021-11-02 10:29:11 +00:00
|
|
|
|
inTimeout: Duration = 5.minutes,
|
|
|
|
|
outTimeout: Duration = 5.minutes,
|
|
|
|
|
maxConnections = MaxConnections,
|
|
|
|
|
maxIn = -1,
|
|
|
|
|
maxOut = -1,
|
|
|
|
|
maxConnsPerPeer = MaxConnectionsPerPeer,
|
|
|
|
|
nameResolver: NameResolver = nil,
|
2022-03-29 08:09:48 +00:00
|
|
|
|
sendSignedPeerRecord = false,
|
2021-11-10 12:05:36 +00:00
|
|
|
|
wssEnabled: bool = false,
|
|
|
|
|
secureKeyPath: string = "",
|
2022-10-28 13:12:06 +00:00
|
|
|
|
secureCertPath: string = "",
|
2023-01-11 09:57:49 +00:00
|
|
|
|
agentString = none(string), # defaults to nim-libp2p version
|
2023-01-31 16:26:22 +00:00
|
|
|
|
peerStoreCapacity = none(int), # defaults to 1.25 maxConnections
|
2023-01-11 09:57:49 +00:00
|
|
|
|
services: seq[switch.Service] = @[],
|
2022-10-28 13:12:06 +00:00
|
|
|
|
): Switch
|
2022-10-28 09:51:46 +00:00
|
|
|
|
{.raises: [Defect, IOError, LPError].} =
|
2021-11-10 12:05:36 +00:00
|
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
|
var b = SwitchBuilder
|
|
|
|
|
.new()
|
|
|
|
|
.withRng(rng)
|
|
|
|
|
.withMaxConnections(maxConnections)
|
|
|
|
|
.withMaxIn(maxIn)
|
|
|
|
|
.withMaxOut(maxOut)
|
|
|
|
|
.withMaxConnsPerPeer(maxConnsPerPeer)
|
|
|
|
|
.withMplex(inTimeout, outTimeout)
|
|
|
|
|
.withNoise()
|
|
|
|
|
.withTcpTransport(transportFlags)
|
|
|
|
|
.withNameResolver(nameResolver)
|
2022-03-29 08:09:48 +00:00
|
|
|
|
.withSignedPeerRecord(sendSignedPeerRecord)
|
2022-11-28 14:56:01 +00:00
|
|
|
|
.withCircuitRelay()
|
|
|
|
|
.withAutonat()
|
2022-03-29 08:09:48 +00:00
|
|
|
|
|
2022-11-24 13:11:23 +00:00
|
|
|
|
if peerStoreCapacity.isSome():
|
|
|
|
|
b = b.withPeerStore(peerStoreCapacity.get())
|
2023-01-31 16:26:22 +00:00
|
|
|
|
else:
|
|
|
|
|
let defaultPeerStoreCapacity = int(round(float64(maxConnections)*1.25))
|
|
|
|
|
b = b.withPeerStore(defaultPeerStoreCapacity)
|
2022-10-28 13:12:06 +00:00
|
|
|
|
if agentString.isSome():
|
|
|
|
|
b = b.withAgentVersion(agentString.get())
|
2021-11-02 10:29:11 +00:00
|
|
|
|
if privKey.isSome():
|
|
|
|
|
b = b.withPrivateKey(privKey.get())
|
2021-12-06 19:51:37 +00:00
|
|
|
|
if wsAddress.isSome():
|
|
|
|
|
b = b.withAddresses(@[wsAddress.get(), address])
|
|
|
|
|
|
|
|
|
|
if wssEnabled:
|
|
|
|
|
b = b.withWssTransport(secureKeyPath, secureCertPath)
|
|
|
|
|
else:
|
|
|
|
|
b = b.withWsTransport()
|
|
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
|
else :
|
|
|
|
|
b = b.withAddress(address)
|
|
|
|
|
|
2023-01-11 09:57:49 +00:00
|
|
|
|
if services.len > 0:
|
|
|
|
|
b = b.withServices(services)
|
|
|
|
|
|
2022-11-24 13:11:23 +00:00
|
|
|
|
b.build()
|