2021-11-02 10:29:11 +00:00
|
|
|
|
# Waku Switch utils.
|
2024-06-28 10:34:57 +00:00
|
|
|
|
{.push raises: [].}
|
2022-10-28 09:51:46 +00:00
|
|
|
|
|
2021-11-02 10:29:11 +00:00
|
|
|
|
import
|
2024-05-16 20:29:11 +00:00
|
|
|
|
std/options,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
chronos,
|
|
|
|
|
chronicles,
|
2021-11-02 10:29:11 +00:00
|
|
|
|
eth/keys,
|
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
2023-06-01 19:43:10 +00:00
|
|
|
|
libp2p/protocols/rendezvous,
|
2021-11-02 10:29:11 +00:00
|
|
|
|
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 =
|
2024-03-15 23:08:47 +00:00
|
|
|
|
b.withTransport(
|
|
|
|
|
proc(upgr: Upgrade): Transport =
|
|
|
|
|
WsTransport.new(upgr)
|
|
|
|
|
)
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
proc getSecureKey(path: string): TLSPrivateKey {.raises: [Defect, IOError].} =
|
|
|
|
|
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:
|
2024-08-14 14:40:08 +00:00
|
|
|
|
debug "exception raised from getSecureKey", err = exc.msg
|
2022-10-28 09:51:46 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
proc getSecureCert(path: string): TLSCertificate {.raises: [Defect, IOError].} =
|
|
|
|
|
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:
|
2024-03-15 23:08:47 +00:00
|
|
|
|
let cert = TLSCertificate.init(stringCert)
|
2021-11-10 12:05:36 +00:00
|
|
|
|
return cert
|
2021-11-23 11:40:43 +00:00
|
|
|
|
except TLSStreamProtocolError as exc:
|
2024-08-14 14:40:08 +00:00
|
|
|
|
debug "exception raised from getSecureCert", err = exc.msg
|
2024-03-15 23:08:47 +00:00
|
|
|
|
|
|
|
|
|
proc withWssTransport*(
|
|
|
|
|
b: SwitchBuilder, secureKeyPath: string, secureCertPath: string
|
|
|
|
|
): SwitchBuilder {.raises: [Defect, IOError].} =
|
|
|
|
|
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-10 12:05:36 +00:00
|
|
|
|
|
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),
|
2024-03-15 23:08:47 +00:00
|
|
|
|
secureManagers: openarray[SecureProtocol] = [SecureProtocol.Noise],
|
2021-11-02 10:29:11 +00:00
|
|
|
|
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 = "",
|
2024-03-15 23:08:47 +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] = @[],
|
2023-06-01 19:43:10 +00:00
|
|
|
|
rendezvous: RendezVous = nil,
|
2024-03-15 23:08:47 +00:00
|
|
|
|
): Switch {.raises: [Defect, IOError, LPError].} =
|
|
|
|
|
var b = SwitchBuilder
|
|
|
|
|
.new()
|
|
|
|
|
.withRng(rng)
|
|
|
|
|
.withMaxConnections(maxConnections)
|
|
|
|
|
.withMaxIn(maxIn)
|
|
|
|
|
.withMaxOut(maxOut)
|
|
|
|
|
.withMaxConnsPerPeer(maxConnsPerPeer)
|
|
|
|
|
.withYamux()
|
|
|
|
|
.withMplex(inTimeout, outTimeout)
|
|
|
|
|
.withNoise()
|
|
|
|
|
.withTcpTransport(transportFlags)
|
|
|
|
|
.withNameResolver(nameResolver)
|
|
|
|
|
.withSignedPeerRecord(sendSignedPeerRecord)
|
|
|
|
|
.withCircuitRelay()
|
|
|
|
|
.withAutonat()
|
|
|
|
|
|
|
|
|
|
if peerStoreCapacity.isSome():
|
|
|
|
|
b = b.withPeerStore(peerStoreCapacity.get())
|
|
|
|
|
else:
|
|
|
|
|
let defaultPeerStoreCapacity = int(maxConnections) * 5
|
|
|
|
|
b = b.withPeerStore(defaultPeerStoreCapacity)
|
|
|
|
|
if agentString.isSome():
|
|
|
|
|
b = b.withAgentVersion(agentString.get())
|
|
|
|
|
if privKey.isSome():
|
|
|
|
|
b = b.withPrivateKey(privKey.get())
|
|
|
|
|
if wsAddress.isSome():
|
|
|
|
|
b = b.withAddresses(@[wsAddress.get(), address])
|
|
|
|
|
|
|
|
|
|
if wssEnabled:
|
|
|
|
|
b = b.withWssTransport(secureKeyPath, secureCertPath)
|
2023-01-31 16:26:22 +00:00
|
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
|
b = b.withWsTransport()
|
|
|
|
|
else:
|
|
|
|
|
b = b.withAddress(address)
|
2021-11-02 10:29:11 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
if services.len > 0:
|
|
|
|
|
b = b.withServices(services)
|
2023-01-11 09:57:49 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
if not rendezvous.isNil():
|
|
|
|
|
b = b.withRendezVous(rendezvous)
|
2023-06-01 19:43:10 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
b.build()
|