From 8a22c073c772e4735f8eeb2d2d01ce985607bb8d Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni Date: Thu, 23 Apr 2020 10:27:29 +0900 Subject: [PATCH] Fix secure/noise securing explicitly, added noise to pubsub tests --- libp2p.nimble | 11 ++++++----- libp2p/protocols/secure/noise.nim | 1 + libp2p/protocols/secure/secure.nim | 10 +++++----- libp2p/standard_setup.nim | 22 ++++++++++++++++++---- libp2p/switch.nim | 4 ++-- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/libp2p.nimble b/libp2p.nimble index c8c3074de..a43f9f82c 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -16,7 +16,7 @@ requires "nim >= 1.2.0", "secp256k1", "stew" -proc runTest(filename: string) = +proc runTest(filename: string, secure: string = "secio") = exec "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off tests/" & filename rmFile "tests/" & filename.toExe @@ -25,9 +25,10 @@ proc buildSample(filename: string) = rmFile "examples" & filename.toExe task test, "Runs the test suite": - runTest "testnative" - runTest "testdaemon" - runTest "testinterop" + runTest("testnative") + runTest("testnative", "noise") + runTest("testdaemon") + runTest("testinterop") task examples_build, "Build the samples": - buildSample "directchat" + buildSample("directchat") diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index 16ee816da..0516a9c65 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -521,6 +521,7 @@ method init*(p: Noise) {.gcsafe.} = p.codec = NoiseCodec method secure*(p: Noise, conn: Connection): Future[Connection] {.async, gcsafe.} = + trace "Noise.secure called", initiator=p.outgoing try: result = await p.handleConn(conn, p.outgoing) except CatchableError as exc: diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index 6c7859a9f..2b829bf97 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -29,7 +29,7 @@ method writeMessage*(c: SecureConn, data: seq[byte]) {.async, base.} = method handshake(s: Secure, conn: Connection, - initiator: bool = false): Future[SecureConn] {.async, base.} = + initiator: bool): Future[SecureConn] {.async, base.} = doAssert(false, "Not implemented!") proc readLoop(sconn: SecureConn, conn: Connection) {.async.} = @@ -54,7 +54,7 @@ proc readLoop(sconn: SecureConn, conn: Connection) {.async.} = await sconn.close() trace "ending Secure readLoop" -proc handleConn*(s: Secure, conn: Connection, initiator: bool = false): Future[Connection] {.async, gcsafe.} = +proc handleConn*(s: Secure, conn: Connection, initiator: bool): Future[Connection] {.async, gcsafe.} = var sconn = await s.handshake(conn, initiator) proc writeHandler(data: seq[byte]) {.async, gcsafe.} = trace "sending encrypted bytes", bytes = data.shortLog @@ -68,7 +68,7 @@ proc handleConn*(s: Secure, conn: Connection, initiator: bool = false): Future[C method init*(s: Secure) {.gcsafe.} = proc handle(conn: Connection, proto: string) {.async, gcsafe.} = - trace "handling connection" + trace "handling connection upgrade", proto try: # We don't need the result but we definitely need to await the handshake discard await s.handleConn(conn, false) @@ -80,9 +80,9 @@ method init*(s: Secure) {.gcsafe.} = s.handler = handle -method secure*(s: Secure, conn: Connection): Future[Connection] {.async, base, gcsafe.} = +method secure*(s: Secure, conn: Connection, initiator: bool): Future[Connection] {.async, base, gcsafe.} = try: - result = await s.handleConn(conn, true) + result = await s.handleConn(conn, initiator) except CatchableError as exc: warn "securing connection failed", msg = exc.msg if not conn.closed(): diff --git a/libp2p/standard_setup.nim b/libp2p/standard_setup.nim index a8bf722a0..bb96cbee9 100644 --- a/libp2p/standard_setup.nim +++ b/libp2p/standard_setup.nim @@ -1,11 +1,20 @@ +# compile time options here +const + libp2p_secure {.strdefine.} = "" + import options, tables, switch, peer, peerinfo, connection, multiaddress, crypto/crypto, transports/[transport, tcptransport], muxers/[muxer, mplex/mplex, mplex/types], - protocols/[identify, secure/secure, secure/secio], + protocols/[identify, secure/secure], protocols/pubsub/[pubsub, gossipsub, floodsub] +when libp2p_secure == "noise": + import protocols/secure/noise +else: + import protocols/secure/secio + export switch, peer, peerinfo, connection, multiaddress, crypto @@ -23,9 +32,14 @@ proc newStandardSwitch*(privKey = none(PrivateKey), transports = @[Transport(newTransport(TcpTransport))] muxers = {MplexCodec: mplexProvider}.toTable identify = newIdentify(peerInfo) - secureManagers = {SecioCodec: Secure(newSecio seckey)}.toTable - pubSub = if gossip: PubSub newPubSub(GossipSub, peerInfo, triggerSelf) - else: PubSub newPubSub(FloodSub, peerInfo, triggerSelf) + when libp2p_secure == "noise": + let secureManagers = {NoiseCodec: newNoise(seckey).Secure}.toTable + else: + let secureManagers = {SecioCodec: newSecio(seckey).Secure}.toTable + let pubSub = if gossip: + PubSub newPubSub(GossipSub, peerInfo, triggerSelf) + else: + PubSub newPubSub(FloodSub, peerInfo, triggerSelf) result = newSwitch(peerInfo, transports, diff --git a/libp2p/switch.nim b/libp2p/switch.nim index c6387dcad..de1c67c18 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -62,7 +62,7 @@ proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} = if manager.len == 0: raise newException(CatchableError, "Unable to negotiate a secure channel!") - result = await s.secureManagers[manager].secure(conn) + result = await s.secureManagers[manager].secure(conn, true) proc identify(s: Switch, conn: Connection): Future[PeerInfo] {.async, gcsafe.} = ## identify the connection @@ -191,7 +191,7 @@ proc upgradeIncoming(s: Switch, conn: Connection) {.async, gcsafe.} = {.async, gcsafe, closure.} = trace "Securing connection" let secure = s.secureManagers[proto] - let sconn = await secure.secure(conn) + let sconn = await secure.secure(conn, false) if not isNil(sconn): # add the muxer for muxer in s.muxers.values: