Secure managers are now sorted, giving priority to noise (#191)

* Secure managers are now sorted, giving priority to noise

* fix nimble test command

* Fix native tests

* fix directchat sample

* Could not write to connection - reduce verbosity

* fix interop testing

* Remove more tables

* test interop fixes

* directchat fix

* fix interop/remove some deprecation
This commit is contained in:
Giovanni Petrantoni 2020-06-01 15:41:32 +09:00 committed by GitHub
parent 6affcda937
commit 37b98ad45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 43 deletions

View File

@ -178,7 +178,7 @@ proc processInput(rfd: AsyncFD) {.async.} =
let transports = @[Transport(TcpTransport.init())] let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable() let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo) let identify = newIdentify(peerInfo)
let secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable() let secureManagers = [Secure(newSecio(seckey))]
let switch = newSwitch(peerInfo, let switch = newSwitch(peerInfo,
transports, transports,
identify, identify,

View File

@ -16,11 +16,9 @@ requires "nim >= 1.2.0",
"secp256k1", "secp256k1",
"stew" "stew"
proc runTest(filename: string, secure: string = "secio", verify: bool = true, sign: bool = true) = proc runTest(filename: string, verify: bool = true, sign: bool = true) =
var excstr: string = "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off" var excstr: string = "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off"
excstr.add(" ") excstr.add(" ")
excstr.add("-d:libp2p_secure=" & $secure)
excstr.add(" ")
excstr.add("-d:libp2p_pubsub_sign=" & $sign) excstr.add("-d:libp2p_pubsub_sign=" & $sign)
excstr.add(" ") excstr.add(" ")
excstr.add("-d:libp2p_pubsub_verify=" & $verify) excstr.add("-d:libp2p_pubsub_verify=" & $verify)
@ -45,11 +43,9 @@ task testinterop, "Runs interop tests":
task testpubsub, "Runs pubsub tests": task testpubsub, "Runs pubsub tests":
runTest("pubsub/testpubsub") runTest("pubsub/testpubsub")
runTest("pubsub/testpubsub", sign = false, verify = false) runTest("pubsub/testpubsub", sign = false, verify = false)
# runTest("pubsub/testpubsub", "noise")
task test, "Runs the test suite": task test, "Runs the test suite":
exec "nimble testnative" exec "nimble testnative"
# runTest("testnative", "noise")
exec "nimble testpubsub" exec "nimble testpubsub"
exec "nimble testdaemon" exec "nimble testdaemon"
exec "nimble testinterop" exec "nimble testinterop"

View File

@ -1,6 +1,5 @@
# compile time options here # compile time options here
const const
libp2p_secure {.strdefine.} = ""
libp2p_pubsub_sign {.booldefine.} = true libp2p_pubsub_sign {.booldefine.} = true
libp2p_pubsub_verify {.booldefine.} = true libp2p_pubsub_verify {.booldefine.} = true
@ -12,23 +11,31 @@ import
protocols/[identify, secure/secure], protocols/[identify, secure/secure],
protocols/pubsub/[pubsub, gossipsub, floodsub] protocols/pubsub/[pubsub, gossipsub, floodsub]
when libp2p_secure == "noise": import
import protocols/secure/noise protocols/secure/noise,
else: protocols/secure/secio
import protocols/secure/secio
export export
switch, peer, peerinfo, connection, multiaddress, crypto switch, peer, peerinfo, connection, multiaddress, crypto
type
SecureProtocol* {.pure.} = enum
Noise,
Secio
proc newStandardSwitch*(privKey = none(PrivateKey), proc newStandardSwitch*(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(),
triggerSelf = false, triggerSelf = false,
gossip = false, gossip = false,
secureManagers: openarray[SecureProtocol] = [
SecureProtocol.Noise, # array cos order matters
SecureProtocol.Secio
],
verifySignature = libp2p_pubsub_verify, verifySignature = libp2p_pubsub_verify,
sign = libp2p_pubsub_sign, sign = libp2p_pubsub_sign,
transportFlags: set[ServerFlags] = {}): Switch = transportFlags: set[ServerFlags] = {}): Switch =
proc createMplex(conn: Connection): Muxer = proc createMplex(conn: Connection): Muxer =
result = newMplex(conn) newMplex(conn)
let let
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA).tryGet()) seckey = privKey.get(otherwise = PrivateKey.random(ECDSA).tryGet())
@ -37,27 +44,33 @@ proc newStandardSwitch*(privKey = none(PrivateKey),
transports = @[Transport(TcpTransport.init(transportFlags))] transports = @[Transport(TcpTransport.init(transportFlags))]
muxers = {MplexCodec: mplexProvider}.toTable muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo) identify = newIdentify(peerInfo)
when libp2p_secure == "noise":
let secureManagers = {NoiseCodec: newNoise(seckey).Secure}.toTable var
else: secureManagerInstances: seq[Secure]
let secureManagers = {SecioCodec: newSecio(seckey).Secure}.toTable for sec in secureManagers:
case sec
of SecureProtocol.Noise:
secureManagerInstances &= newNoise(seckey).Secure
of SecureProtocol.Secio:
secureManagerInstances &= newSecio(seckey).Secure
let pubSub = if gossip: let pubSub = if gossip:
PubSub newPubSub(GossipSub, newPubSub(GossipSub,
peerInfo = peerInfo, peerInfo = peerInfo,
triggerSelf = triggerSelf, triggerSelf = triggerSelf,
verifySignature = verifySignature, verifySignature = verifySignature,
sign = sign) sign = sign).PubSub
else: else:
PubSub newPubSub(FloodSub, newPubSub(FloodSub,
peerInfo = peerInfo, peerInfo = peerInfo,
triggerSelf = triggerSelf, triggerSelf = triggerSelf,
verifySignature = verifySignature, verifySignature = verifySignature,
sign = sign) sign = sign).PubSub
result = newSwitch(peerInfo, newSwitch(
transports, peerInfo,
identify, transports,
muxers, identify,
secureManagers = secureManagers, muxers,
pubSub = some(pubSub)) secureManagers = secureManagerInstances,
pubSub = some(pubSub))

View File

@ -44,7 +44,7 @@ type
ms*: MultistreamSelect ms*: MultistreamSelect
identity*: Identify identity*: Identify
streamHandler*: StreamHandler streamHandler*: StreamHandler
secureManagers*: Table[string, Secure] secureManagers*: OrderedTable[string, Secure]
pubSub*: Option[PubSub] pubSub*: Option[PubSub]
dialedPubSubPeers: HashSet[string] dialedPubSubPeers: HashSet[string]
@ -412,7 +412,7 @@ proc newSwitch*(peerInfo: PeerInfo,
transports: seq[Transport], transports: seq[Transport],
identity: Identify, identity: Identify,
muxers: Table[string, MuxerProvider], muxers: Table[string, MuxerProvider],
secureManagers: Table[string, Secure] = initTable[string, Secure](), secureManagers: openarray[Secure] = [],
pubSub: Option[PubSub] = none(PubSub)): Switch = pubSub: Option[PubSub] = none(PubSub)): Switch =
new result new result
result.peerInfo = peerInfo result.peerInfo = peerInfo
@ -422,7 +422,7 @@ proc newSwitch*(peerInfo: PeerInfo,
result.muxed = initTable[string, Muxer]() result.muxed = initTable[string, Muxer]()
result.identity = identity result.identity = identity
result.muxers = muxers result.muxers = muxers
result.secureManagers = initTable[string, Secure]() result.secureManagers = initOrderedTable[string, Secure]()
result.dialedPubSubPeers = initHashSet[string]() result.dialedPubSubPeers = initHashSet[string]()
let s = result # can't capture result let s = result # can't capture result
@ -448,9 +448,9 @@ proc newSwitch*(peerInfo: PeerInfo,
# try establishing a pubsub connection # try establishing a pubsub connection
await s.subscribeToPeer(muxer.connection.peerInfo) await s.subscribeToPeer(muxer.connection.peerInfo)
for k in secureManagers.keys: for proto in secureManagers:
trace "adding secure manager ", codec = secureManagers[k].codec trace "adding secure manager ", codec = proto.codec
result.secureManagers[k] = secureManagers[k] result.secureManagers[proto.codec] = proto
if result.secureManagers.len == 0: if result.secureManagers.len == 0:
# use plain text if no secure managers are provided # use plain text if no secure managers are provided

View File

@ -73,7 +73,7 @@ proc createNode*(privKey: Option[PrivateKey] = none(PrivateKey),
let transports = @[Transport(TcpTransport.init())] let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable() let muxers = [(MplexCodec, mplexProvider)].toTable()
let identify = newIdentify(peerInfo) let identify = newIdentify(peerInfo)
let secureManagers = [(SecioCodec, Secure(newSecio(seckey.get())))].toTable() let secureManagers = [Secure(newSecio(seckey.get()))]
var pubSub: Option[PubSub] var pubSub: Option[PubSub]
if gossip: if gossip:

View File

@ -60,7 +60,7 @@ proc createSwitch(ma: MultiAddress; outgoing: bool): (Switch, PeerInfo) =
let mplexProvider = newMuxerProvider(createMplex, MplexCodec) let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(TcpTransport.init())] let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable() let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(NoiseCodec, Secure(newNoise(peerInfo.privateKey, outgoing = outgoing)))].toTable() let secureManagers = [Secure(newNoise(peerInfo.privateKey, outgoing = outgoing))]
let switch = newSwitch(peerInfo, let switch = newSwitch(peerInfo,
transports, transports,
identify, identify,
@ -86,7 +86,7 @@ suite "Noise":
defer: defer:
await sconn.close() await sconn.close()
await conn.close() await conn.close()
await sconn.write(cstring("Hello!"), 6) await sconn.write("Hello!")
let let
transport1: TcpTransport = TcpTransport.init() transport1: TcpTransport = TcpTransport.init()
@ -141,7 +141,7 @@ suite "Noise":
conn = await transport2.dial(transport1.ma) conn = await transport2.dial(transport1.ma)
sconn = await clientNoise.secure(conn, true) sconn = await clientNoise.secure(conn, true)
await sconn.write("Hello!".cstring, 6) await sconn.write("Hello!")
await readTask await readTask
await sconn.close() await sconn.close()
await conn.close() await conn.close()

View File

@ -41,7 +41,7 @@ proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) =
let mplexProvider = newMuxerProvider(createMplex, MplexCodec) let mplexProvider = newMuxerProvider(createMplex, MplexCodec)
let transports = @[Transport(TcpTransport.init())] let transports = @[Transport(TcpTransport.init())]
let muxers = [(MplexCodec, mplexProvider)].toTable() let muxers = [(MplexCodec, mplexProvider)].toTable()
let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable() let secureManagers = [Secure(newSecio(peerInfo.privateKey))]
let switch = newSwitch(peerInfo, let switch = newSwitch(peerInfo,
transports, transports,
identify, identify,