This commit is contained in:
Dmitriy Ryajov 2021-04-05 16:06:45 -06:00 committed by GitHub
parent 795a651839
commit 290866dd62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 71 deletions

View File

@ -14,43 +14,41 @@ type
Noise, Noise,
Secio {.deprecated.} Secio {.deprecated.}
TcpTransportOpts = object
enable: bool
flags: set[ServerFlags]
MplexOpts = object
enable: bool
newMuxer: MuxerConstructor
SwitchBuilder* = ref object SwitchBuilder* = ref object
privKey: Option[PrivateKey] privKey: Option[PrivateKey]
address: MultiAddress address: MultiAddress
secureManagers: seq[SecureProtocol] secureManagers: seq[SecureProtocol]
tcpTransportFlags: Option[set[ServerFlags]] mplexOpts: MplexOpts
tcpTransportOpts: TcpTransportOpts
rng: ref BrHmacDrbgContext rng: ref BrHmacDrbgContext
inTimeout: Duration
outTimeout: Duration
maxConnections: int maxConnections: int
maxIn: int maxIn: int
maxOut: int maxOut: int
maxConnsPerPeer: int maxConnsPerPeer: int
protoVersion: Option[string] protoVersion: string
agentVersion: Option[string] agentVersion: string
proc init*(_: type[SwitchBuilder]): SwitchBuilder = proc init*(T: type[SwitchBuilder]): T =
SwitchBuilder( SwitchBuilder(
privKey: none(PrivateKey), privKey: none(PrivateKey),
address: MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(), address: MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
secureManagers: @[], secureManagers: @[],
tcpTransportFlags: block: tcpTransportOpts: TcpTransportOpts(),
let flags: set[ServerFlags] = {}
some(flags),
rng: newRng(), rng: newRng(),
inTimeout: 5.minutes,
outTimeout: 5.minutes,
maxConnections: MaxConnections, maxConnections: MaxConnections,
maxIn: -1, maxIn: -1,
maxOut: -1, maxOut: -1,
maxConnsPerPeer: MaxConnectionsPerPeer, maxConnsPerPeer: MaxConnectionsPerPeer,
protoVersion: none(string), protoVersion: ProtoVersion,
agentVersion: none(string)) agentVersion: AgentVersion)
# so I tried using var inpit and return but did not work...
# as in proc privateKey*(builder: var SwitchBuilder, privateKey: PrivateKey): var SwitchBuilder =
# so in nim we are stuck with this hardly efficient way and hopey compiler figures it out.. heh
# maybe {.byref.} works.... I would not bet on it but let's use it.
proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder = proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder =
b.privKey = some(privateKey) b.privKey = some(privateKey)
@ -60,26 +58,33 @@ proc withAddress*(b: SwitchBuilder, address: MultiAddress): SwitchBuilder =
b.address = address b.address = address
b b
proc withSecureManager*(b: SwitchBuilder, secureManager: SecureProtocol): SwitchBuilder = proc withMplex*(b: SwitchBuilder, inTimeout = 5.minutes, outTimeout = 5.minutes): SwitchBuilder =
b.secureManagers &= secureManager proc newMuxer(conn: Connection): Muxer =
Mplex.init(
conn,
inTimeout = inTimeout,
outTimeout = outTimeout)
b.mplexOpts = MplexOpts(
enable: true,
newMuxer: newMuxer,
)
b
proc withNoise*(b: SwitchBuilder): SwitchBuilder =
b.secureManagers.add(SecureProtocol.Noise)
b b
proc withTcpTransport*(b: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder = proc withTcpTransport*(b: SwitchBuilder, flags: set[ServerFlags] = {}): SwitchBuilder =
b.tcpTransportFlags = some(flags) b.tcpTransportOpts.enable = true
b.tcpTransportOpts.flags = flags
b b
proc withRng*(b: SwitchBuilder, rng: ref BrHmacDrbgContext): SwitchBuilder = proc withRng*(b: SwitchBuilder, rng: ref BrHmacDrbgContext): SwitchBuilder =
b.rng = rng b.rng = rng
b b
proc withInTimeout*(b: SwitchBuilder, inTimeout: Duration): SwitchBuilder =
b.inTimeout = inTimeout
b
proc withOutTimeout*(b: SwitchBuilder, outTimeout: Duration): SwitchBuilder =
b.outTimeout = outTimeout
b
proc withMaxConnections*(b: SwitchBuilder, maxConnections: int): SwitchBuilder = proc withMaxConnections*(b: SwitchBuilder, maxConnections: int): SwitchBuilder =
b.maxConnections = maxConnections b.maxConnections = maxConnections
b b
@ -97,62 +102,58 @@ proc withMaxConnsPerPeer*(b: SwitchBuilder, maxConnsPerPeer: int): SwitchBuilder
b b
proc withProtoVersion*(b: SwitchBuilder, protoVersion: string): SwitchBuilder = proc withProtoVersion*(b: SwitchBuilder, protoVersion: string): SwitchBuilder =
b.protoVersion = some(protoVersion) b.protoVersion = protoVersion
b b
proc withAgentVersion*(b: SwitchBuilder, agentVersion: string): SwitchBuilder = proc withAgentVersion*(b: SwitchBuilder, agentVersion: string): SwitchBuilder =
b.agentVersion = some(agentVersion) b.agentVersion = agentVersion
b b
proc build*(b: SwitchBuilder): Switch = proc build*(b: SwitchBuilder): Switch =
let
inTimeout = b.inTimeout
outTimeout = b.outTimeout
proc createMplex(conn: Connection): Muxer =
Mplex.init(
conn,
inTimeout = inTimeout,
outTimeout = outTimeout)
if b.rng == nil: # newRng could fail if b.rng == nil: # newRng could fail
raise (ref CatchableError)(msg: "Cannot initialize RNG") raise (ref CatchableError)(msg: "Cannot initialize RNG")
let let
seckey = b.privKey.get(otherwise = PrivateKey.random(b.rng[]).tryGet()) seckey = b.privKey.get(otherwise = PrivateKey.random(b.rng[]).tryGet())
var
secureManagerInstances: seq[Secure]
if SecureProtocol.Noise in b.secureManagers:
secureManagerInstances.add(newNoise(b.rng, seckey).Secure)
let
peerInfo = block: peerInfo = block:
let info = PeerInfo.init(seckey, [b.address]) var info = PeerInfo.init(seckey, [b.address])
if b.protoVersion.isSome(): info.protoVersion = b.protoVersion
info.protoVersion = b.protoVersion.get() info.agentVersion = b.agentVersion
if b.agentVersion.isSome():
info.agentVersion = b.agentVersion.get()
info info
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let
muxers = block:
var muxers: Table[string, MuxerProvider]
if b.mplexOpts.enable:
muxers.add(MplexCodec, newMuxerProvider(b.mplexOpts.newMuxer, MplexCodec))
muxers
let
identify = newIdentify(peerInfo)
connManager = ConnManager.init(b.maxConnsPerPeer, b.maxConnections, b.maxIn, b.maxOut)
let
transports = block: transports = block:
var transports: seq[Transport] var transports: seq[Transport]
if b.tcpTransportFlags.isSome(): if b.tcpTransportOpts.enable:
transports &= Transport(TcpTransport.init(b.tcpTransportFlags.get())) transports.add(Transport(TcpTransport.init(b.tcpTransportOpts.flags)))
transports transports
muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo)
if b.secureManagers.len == 0: if b.secureManagers.len == 0:
b.secureManagers &= SecureProtocol.Noise b.secureManagers &= SecureProtocol.Noise
var
secureManagerInstances: seq[Secure]
for sec in b.secureManagers:
case sec
of SecureProtocol.Noise:
secureManagerInstances &= newNoise(b.rng, seckey).Secure
of SecureProtocol.Secio:
quit("Secio is deprecated!") # use of secio is unsafe
let switch = newSwitch( let switch = newSwitch(
peerInfo, peerInfo = peerInfo,
transports, transports = transports,
identify, identity = identify,
muxers, muxers = muxers,
secureManagers = secureManagerInstances, secureManagers = secureManagerInstances,
maxConnections = b.maxConnections, maxConnections = b.maxConnections,
maxIn = b.maxIn, maxIn = b.maxIn,
@ -174,22 +175,22 @@ proc newStandardSwitch*(privKey = none(PrivateKey),
maxIn = -1, maxIn = -1,
maxOut = -1, maxOut = -1,
maxConnsPerPeer = MaxConnectionsPerPeer): Switch = maxConnsPerPeer = MaxConnectionsPerPeer): Switch =
if SecureProtocol.Secio in secureManagers:
quit("Secio is deprecated!") # use of secio is unsafe
var b = SwitchBuilder var b = SwitchBuilder
.init() .init()
.withAddress(address) .withAddress(address)
.withRng(rng) .withRng(rng)
.withInTimeout(inTimeout)
.withOutTimeout(outTimeout)
.withMaxConnections(maxConnections) .withMaxConnections(maxConnections)
.withMaxIn(maxIn) .withMaxIn(maxIn)
.withMaxOut(maxOut) .withMaxOut(maxOut)
.withMaxConnsPerPeer(maxConnsPerPeer) .withMaxConnsPerPeer(maxConnsPerPeer)
.withMplex(inTimeout, outTimeout)
.withTcpTransport(transportFlags) .withTcpTransport(transportFlags)
.withNoise()
if privKey.isSome(): if privKey.isSome():
b = b.withPrivateKey(privKey.get()) b = b.withPrivateKey(privKey.get())
for sm in secureManagers:
b = b.withSecureManager(sm)
b.build() b.build()

View File

@ -577,8 +577,10 @@ method init*(p: Noise) {.gcsafe.} =
p.codec = NoiseCodec p.codec = NoiseCodec
proc newNoise*( proc newNoise*(
rng: ref BrHmacDrbgContext, privateKey: PrivateKey; rng: ref BrHmacDrbgContext,
outgoing: bool = true; commonPrologue: seq[byte] = @[]): Noise = privateKey: PrivateKey;
outgoing: bool = true;
commonPrologue: seq[byte] = @[]): Noise =
result = Noise( result = Noise(
rng: rng, rng: rng,
outgoing: outgoing, outgoing: outgoing,