2021-05-21 16:27:01 +00:00
|
|
|
## Nim-Libp2p
|
|
|
|
## Copyright (c) 2020 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-04-02 01:20:51 +00:00
|
|
|
import
|
2021-05-25 17:00:48 +00:00
|
|
|
options, tables, chronos, chronicles, bearssl,
|
2021-04-02 01:20:51 +00:00
|
|
|
switch, peerid, peerinfo, stream/connection, multiaddress,
|
|
|
|
crypto/crypto, transports/[transport, tcptransport],
|
|
|
|
muxers/[muxer, mplex/mplex],
|
2021-04-05 19:37:14 +00:00
|
|
|
protocols/[identify, secure/secure, secure/noise],
|
2021-05-24 17:55:33 +00:00
|
|
|
connmanager, upgrademngrs/muxedupgrade,
|
|
|
|
errors
|
2021-04-02 01:20:51 +00:00
|
|
|
|
|
|
|
export
|
2021-05-24 17:55:33 +00:00
|
|
|
switch, peerid, peerinfo, connection, multiaddress, crypto, errors
|
2021-04-02 01:20:51 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SecureProtocol* {.pure.} = enum
|
|
|
|
Noise,
|
|
|
|
Secio {.deprecated.}
|
|
|
|
|
2021-04-05 22:06:45 +00:00
|
|
|
TcpTransportOpts = object
|
|
|
|
enable: bool
|
|
|
|
flags: set[ServerFlags]
|
|
|
|
|
|
|
|
MplexOpts = object
|
|
|
|
enable: bool
|
|
|
|
newMuxer: MuxerConstructor
|
|
|
|
|
2021-04-02 01:20:51 +00:00
|
|
|
SwitchBuilder* = ref object
|
|
|
|
privKey: Option[PrivateKey]
|
|
|
|
address: MultiAddress
|
|
|
|
secureManagers: seq[SecureProtocol]
|
2021-04-05 22:06:45 +00:00
|
|
|
mplexOpts: MplexOpts
|
|
|
|
tcpTransportOpts: TcpTransportOpts
|
2021-04-02 01:20:51 +00:00
|
|
|
rng: ref BrHmacDrbgContext
|
|
|
|
maxConnections: int
|
|
|
|
maxIn: int
|
|
|
|
maxOut: int
|
|
|
|
maxConnsPerPeer: int
|
2021-04-05 22:06:45 +00:00
|
|
|
protoVersion: string
|
|
|
|
agentVersion: string
|
2021-04-02 01:20:51 +00:00
|
|
|
|
2021-04-06 20:16:23 +00:00
|
|
|
proc new*(T: type[SwitchBuilder]): T =
|
2021-05-21 16:27:01 +00:00
|
|
|
|
|
|
|
let address = MultiAddress
|
|
|
|
.init("/ip4/127.0.0.1/tcp/0")
|
2021-05-22 18:27:30 +00:00
|
|
|
.expect("Should initialize to default")
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-04-02 01:20:51 +00:00
|
|
|
SwitchBuilder(
|
|
|
|
privKey: none(PrivateKey),
|
2021-05-21 16:27:01 +00:00
|
|
|
address: address,
|
2021-04-02 01:20:51 +00:00
|
|
|
secureManagers: @[],
|
2021-04-05 22:06:45 +00:00
|
|
|
tcpTransportOpts: TcpTransportOpts(),
|
2021-04-02 01:20:51 +00:00
|
|
|
maxConnections: MaxConnections,
|
|
|
|
maxIn: -1,
|
|
|
|
maxOut: -1,
|
|
|
|
maxConnsPerPeer: MaxConnectionsPerPeer,
|
2021-04-05 22:06:45 +00:00
|
|
|
protoVersion: ProtoVersion,
|
|
|
|
agentVersion: AgentVersion)
|
2021-04-02 01:20:51 +00:00
|
|
|
|
|
|
|
proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder =
|
|
|
|
b.privKey = some(privateKey)
|
|
|
|
b
|
|
|
|
|
|
|
|
proc withAddress*(b: SwitchBuilder, address: MultiAddress): SwitchBuilder =
|
|
|
|
b.address = address
|
|
|
|
b
|
|
|
|
|
2021-04-05 22:06:45 +00:00
|
|
|
proc withMplex*(b: SwitchBuilder, inTimeout = 5.minutes, outTimeout = 5.minutes): SwitchBuilder =
|
|
|
|
proc newMuxer(conn: Connection): Muxer =
|
|
|
|
Mplex.init(
|
|
|
|
conn,
|
|
|
|
inTimeout = inTimeout,
|
|
|
|
outTimeout = outTimeout)
|
|
|
|
|
|
|
|
b.mplexOpts = MplexOpts(
|
|
|
|
enable: true,
|
|
|
|
newMuxer: newMuxer,
|
|
|
|
)
|
2021-04-02 01:20:51 +00:00
|
|
|
|
|
|
|
b
|
|
|
|
|
2021-04-05 22:06:45 +00:00
|
|
|
proc withNoise*(b: SwitchBuilder): SwitchBuilder =
|
|
|
|
b.secureManagers.add(SecureProtocol.Noise)
|
2021-04-02 01:20:51 +00:00
|
|
|
b
|
|
|
|
|
2021-04-05 22:06:45 +00:00
|
|
|
proc withTcpTransport*(b: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder =
|
|
|
|
b.tcpTransportOpts.enable = true
|
|
|
|
b.tcpTransportOpts.flags = flags
|
2021-04-02 01:20:51 +00:00
|
|
|
b
|
|
|
|
|
2021-04-05 22:06:45 +00:00
|
|
|
proc withRng*(b: SwitchBuilder, rng: ref BrHmacDrbgContext): SwitchBuilder =
|
|
|
|
b.rng = rng
|
2021-04-02 01:20:51 +00:00
|
|
|
b
|
|
|
|
|
|
|
|
proc withMaxConnections*(b: SwitchBuilder, maxConnections: int): SwitchBuilder =
|
|
|
|
b.maxConnections = maxConnections
|
|
|
|
b
|
|
|
|
|
|
|
|
proc withMaxIn*(b: SwitchBuilder, maxIn: int): SwitchBuilder =
|
|
|
|
b.maxIn = maxIn
|
|
|
|
b
|
|
|
|
|
|
|
|
proc withMaxOut*(b: SwitchBuilder, maxOut: int): SwitchBuilder =
|
|
|
|
b.maxOut = maxOut
|
|
|
|
b
|
|
|
|
|
|
|
|
proc withMaxConnsPerPeer*(b: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder =
|
|
|
|
b.maxConnsPerPeer = maxConnsPerPeer
|
|
|
|
b
|
|
|
|
|
|
|
|
proc withProtoVersion*(b: SwitchBuilder, protoVersion: string): SwitchBuilder =
|
2021-04-05 22:06:45 +00:00
|
|
|
b.protoVersion = protoVersion
|
2021-04-02 01:20:51 +00:00
|
|
|
b
|
|
|
|
|
|
|
|
proc withAgentVersion*(b: SwitchBuilder, agentVersion: string): SwitchBuilder =
|
2021-04-05 22:06:45 +00:00
|
|
|
b.agentVersion = agentVersion
|
2021-04-02 01:20:51 +00:00
|
|
|
b
|
|
|
|
|
2021-05-22 18:27:30 +00:00
|
|
|
proc build*(b: SwitchBuilder): Switch
|
|
|
|
{.raises: [Defect, LPError].} =
|
|
|
|
|
2021-04-02 01:20:51 +00:00
|
|
|
if b.rng == nil: # newRng could fail
|
2021-05-21 16:27:01 +00:00
|
|
|
raise newException(Defect, "Cannot initialize RNG")
|
2021-04-02 01:20:51 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
let pkRes = PrivateKey.random(b.rng[])
|
2021-04-02 01:20:51 +00:00
|
|
|
let
|
2021-05-22 18:27:30 +00:00
|
|
|
seckey = b.privKey.get(otherwise = pkRes.expect("Expected default Private Key"))
|
2021-04-05 22:06:45 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
secureManagerInstances: seq[Secure]
|
|
|
|
if SecureProtocol.Noise in b.secureManagers:
|
|
|
|
secureManagerInstances.add(newNoise(b.rng, seckey).Secure)
|
|
|
|
|
|
|
|
let
|
2021-05-21 16:27:01 +00:00
|
|
|
peerInfo = PeerInfo.init(
|
|
|
|
seckey,
|
|
|
|
[b.address],
|
|
|
|
protoVersion = b.protoVersion,
|
|
|
|
agentVersion = b.agentVersion)
|
2021-04-05 22:06:45 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
muxers = block:
|
|
|
|
var muxers: Table[string, MuxerProvider]
|
|
|
|
if b.mplexOpts.enable:
|
2021-05-25 17:00:48 +00:00
|
|
|
muxers[MplexCodec] = newMuxerProvider(b.mplexOpts.newMuxer, MplexCodec)
|
2021-04-05 22:06:45 +00:00
|
|
|
muxers
|
|
|
|
|
|
|
|
let
|
|
|
|
identify = newIdentify(peerInfo)
|
2021-04-05 19:37:14 +00:00
|
|
|
connManager = ConnManager.init(b.maxConnsPerPeer, b.maxConnections, b.maxIn, b.maxOut)
|
|
|
|
ms = newMultistream()
|
|
|
|
muxedUpgrade = MuxedUpgrade.init(identify, muxers, secureManagerInstances, connManager, ms)
|
2021-04-05 22:06:45 +00:00
|
|
|
|
|
|
|
let
|
2021-04-02 01:20:51 +00:00
|
|
|
transports = block:
|
|
|
|
var transports: seq[Transport]
|
2021-04-05 22:06:45 +00:00
|
|
|
if b.tcpTransportOpts.enable:
|
2021-04-05 19:37:14 +00:00
|
|
|
transports.add(Transport(TcpTransport.init(b.tcpTransportOpts.flags, muxedUpgrade)))
|
2021-04-02 01:20:51 +00:00
|
|
|
transports
|
|
|
|
|
|
|
|
if b.secureManagers.len == 0:
|
|
|
|
b.secureManagers &= SecureProtocol.Noise
|
|
|
|
|
2021-04-06 20:16:23 +00:00
|
|
|
if isNil(b.rng):
|
|
|
|
b.rng = newRng()
|
|
|
|
|
2021-06-02 13:39:10 +00:00
|
|
|
let switch = newSwitch(
|
|
|
|
peerInfo = peerInfo,
|
|
|
|
transports = transports,
|
|
|
|
identity = identify,
|
|
|
|
muxers = muxers,
|
|
|
|
secureManagers = secureManagerInstances,
|
2021-04-05 19:37:14 +00:00
|
|
|
connManager = connManager,
|
|
|
|
ms = ms)
|
2021-04-02 01:20:51 +00:00
|
|
|
|
|
|
|
return switch
|
|
|
|
|
2021-05-24 22:48:18 +00:00
|
|
|
proc newStandardSwitch*(
|
|
|
|
privKey = none(PrivateKey),
|
|
|
|
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
|
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
|
|
|
SecureProtocol.Noise,
|
|
|
|
],
|
|
|
|
transportFlags: set[ServerFlags] = {},
|
|
|
|
rng = newRng(),
|
|
|
|
inTimeout: Duration = 5.minutes,
|
|
|
|
outTimeout: Duration = 5.minutes,
|
|
|
|
maxConnections = MaxConnections,
|
|
|
|
maxIn = -1,
|
|
|
|
maxOut = -1,
|
|
|
|
maxConnsPerPeer = MaxConnectionsPerPeer): Switch
|
|
|
|
{.raises: [Defect, LPError].} =
|
2021-04-05 22:06:45 +00:00
|
|
|
if SecureProtocol.Secio in secureManagers:
|
|
|
|
quit("Secio is deprecated!") # use of secio is unsafe
|
|
|
|
|
2021-04-02 01:20:51 +00:00
|
|
|
var b = SwitchBuilder
|
2021-04-06 20:16:23 +00:00
|
|
|
.new()
|
2021-05-24 22:48:18 +00:00
|
|
|
.withAddress(address)
|
2021-04-02 01:20:51 +00:00
|
|
|
.withRng(rng)
|
|
|
|
.withMaxConnections(maxConnections)
|
|
|
|
.withMaxIn(maxIn)
|
|
|
|
.withMaxOut(maxOut)
|
|
|
|
.withMaxConnsPerPeer(maxConnsPerPeer)
|
2021-04-05 22:06:45 +00:00
|
|
|
.withMplex(inTimeout, outTimeout)
|
2021-04-02 01:20:51 +00:00
|
|
|
.withTcpTransport(transportFlags)
|
2021-04-05 22:06:45 +00:00
|
|
|
.withNoise()
|
2021-04-02 01:20:51 +00:00
|
|
|
|
|
|
|
if privKey.isSome():
|
|
|
|
b = b.withPrivateKey(privKey.get())
|
|
|
|
|
|
|
|
b.build()
|