2021-11-02 10:29:11 +00:00
|
|
|
# Waku Switch utils.
|
2021-11-10 12:05:36 +00:00
|
|
|
{.push raises: [TLSStreamProtocolError, IOError, Defect].}
|
2021-11-02 10:29:11 +00:00
|
|
|
import
|
2021-11-10 12:05:36 +00:00
|
|
|
std/[options, sequtils, strutils],
|
2021-11-02 10:29:11 +00:00
|
|
|
chronos, chronicles,
|
|
|
|
stew/byteutils,
|
|
|
|
eth/keys,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
libp2p/nameresolving/dnsresolver,
|
|
|
|
libp2p/nameresolving/nameresolver,
|
|
|
|
libp2p/builders,
|
|
|
|
libp2p/transports/[transport, tcptransport, wstransport]
|
|
|
|
|
|
|
|
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
|
|
|
|
{.raises: [Defect,TLSStreamProtocolError, IOError].} =
|
|
|
|
trace "Key path is.", path=path
|
|
|
|
var stringkey: string = readFile(path)
|
|
|
|
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
|
|
|
|
{.raises: [Defect,TLSStreamProtocolError, IOError].} =
|
|
|
|
trace "Certificate path is.", path=path
|
|
|
|
var stringCert: string = readFile(path)
|
|
|
|
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,
|
|
|
|
secureCertPath: string): SwitchBuilder =
|
|
|
|
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] = {},
|
|
|
|
rng = crypto.newRng(),
|
|
|
|
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 = "",
|
|
|
|
secureCertPath: string = ""): Switch
|
|
|
|
{.raises: [Defect,TLSStreamProtocolError,IOError, LPError].} =
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
b.build()
|