2021-02-08 20:33:34 +00:00
|
|
|
import options, tables
|
|
|
|
import chronos
|
|
|
|
import ../libp2p
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2021-02-08 20:33:34 +00:00
|
|
|
export libp2p
|
2020-04-23 01:27:29 +00:00
|
|
|
|
2021-02-08 20:33:34 +00:00
|
|
|
import connmanager
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2020-06-01 06:41:32 +00:00
|
|
|
type
|
|
|
|
SecureProtocol* {.pure.} = enum
|
|
|
|
Noise,
|
2021-02-09 21:41:49 +00:00
|
|
|
Secio {.deprecated.}
|
2020-06-01 06:41:32 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc newStandardSwitch*(privKey = none(PrivateKey),
|
2020-05-31 14:22:49 +00:00
|
|
|
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
|
2020-06-01 06:41:32 +00:00
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
2020-06-28 15:56:38 +00:00
|
|
|
SecureProtocol.Noise,
|
2020-06-01 06:41:32 +00:00
|
|
|
],
|
2020-06-28 15:56:38 +00:00
|
|
|
transportFlags: set[ServerFlags] = {},
|
2020-07-17 18:44:41 +00:00
|
|
|
rng = newRng(),
|
2020-07-27 19:33:51 +00:00
|
|
|
inTimeout: Duration = 5.minutes,
|
2021-01-21 04:00:24 +00:00
|
|
|
outTimeout: Duration = 5.minutes,
|
|
|
|
maxConnections = MaxConnections,
|
|
|
|
maxIn = -1,
|
|
|
|
maxOut = -1,
|
|
|
|
maxConnsPerPeer = MaxConnectionsPerPeer): Switch =
|
2019-12-08 21:06:58 +00:00
|
|
|
proc createMplex(conn: Connection): Muxer =
|
2020-07-17 18:44:41 +00:00
|
|
|
Mplex.init(
|
|
|
|
conn,
|
|
|
|
inTimeout = inTimeout,
|
|
|
|
outTimeout = outTimeout)
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
if rng == nil: # newRng could fail
|
|
|
|
raise (ref CatchableError)(msg: "Cannot initialize RNG")
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
let
|
2020-07-21 20:10:21 +00:00
|
|
|
seckey = privKey.get(otherwise = PrivateKey.random(rng[]).tryGet())
|
2020-02-11 17:53:39 +00:00
|
|
|
peerInfo = PeerInfo.init(seckey, [address])
|
2019-12-08 21:06:58 +00:00
|
|
|
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
|
2020-05-18 19:04:05 +00:00
|
|
|
transports = @[Transport(TcpTransport.init(transportFlags))]
|
2019-12-08 21:06:58 +00:00
|
|
|
muxers = {MplexCodec: mplexProvider}.toTable
|
|
|
|
identify = newIdentify(peerInfo)
|
2020-06-01 06:41:32 +00:00
|
|
|
|
2020-06-06 00:17:05 +00:00
|
|
|
var
|
2020-06-01 06:41:32 +00:00
|
|
|
secureManagerInstances: seq[Secure]
|
|
|
|
for sec in secureManagers:
|
|
|
|
case sec
|
|
|
|
of SecureProtocol.Noise:
|
2020-07-07 11:14:11 +00:00
|
|
|
secureManagerInstances &= newNoise(rng, seckey).Secure
|
2020-06-01 06:41:32 +00:00
|
|
|
of SecureProtocol.Secio:
|
2021-02-08 20:33:34 +00:00
|
|
|
quit("Secio is deprecated!") # use of secio is unsafe
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-08-12 00:05:49 +00:00
|
|
|
let switch = newSwitch(
|
2020-06-01 06:41:32 +00:00
|
|
|
peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
2021-01-21 04:00:24 +00:00
|
|
|
secureManagers = secureManagerInstances,
|
|
|
|
maxConnections = maxConnections,
|
|
|
|
maxIn = maxIn,
|
|
|
|
maxOut = maxOut,
|
|
|
|
maxConnsPerPeer = maxConnsPerPeer)
|
2020-08-12 00:05:49 +00:00
|
|
|
|
|
|
|
return switch
|