Fix secure/noise securing explicitly, added noise to pubsub tests

This commit is contained in:
Giovanni Petrantoni 2020-04-23 10:27:29 +09:00 committed by Dmitriy Ryajov
parent 917b5f5c84
commit 8a22c073c7
5 changed files with 32 additions and 16 deletions

View File

@ -16,7 +16,7 @@ requires "nim >= 1.2.0",
"secp256k1", "secp256k1",
"stew" "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 exec "nim c -r --opt:speed -d:debug --verbosity:0 --hints:off tests/" & filename
rmFile "tests/" & filename.toExe rmFile "tests/" & filename.toExe
@ -25,9 +25,10 @@ proc buildSample(filename: string) =
rmFile "examples" & filename.toExe rmFile "examples" & filename.toExe
task test, "Runs the test suite": task test, "Runs the test suite":
runTest "testnative" runTest("testnative")
runTest "testdaemon" runTest("testnative", "noise")
runTest "testinterop" runTest("testdaemon")
runTest("testinterop")
task examples_build, "Build the samples": task examples_build, "Build the samples":
buildSample "directchat" buildSample("directchat")

View File

@ -521,6 +521,7 @@ method init*(p: Noise) {.gcsafe.} =
p.codec = NoiseCodec p.codec = NoiseCodec
method secure*(p: Noise, conn: Connection): Future[Connection] {.async, gcsafe.} = method secure*(p: Noise, conn: Connection): Future[Connection] {.async, gcsafe.} =
trace "Noise.secure called", initiator=p.outgoing
try: try:
result = await p.handleConn(conn, p.outgoing) result = await p.handleConn(conn, p.outgoing)
except CatchableError as exc: except CatchableError as exc:

View File

@ -29,7 +29,7 @@ method writeMessage*(c: SecureConn, data: seq[byte]) {.async, base.} =
method handshake(s: Secure, method handshake(s: Secure,
conn: Connection, conn: Connection,
initiator: bool = false): Future[SecureConn] {.async, base.} = initiator: bool): Future[SecureConn] {.async, base.} =
doAssert(false, "Not implemented!") doAssert(false, "Not implemented!")
proc readLoop(sconn: SecureConn, conn: Connection) {.async.} = proc readLoop(sconn: SecureConn, conn: Connection) {.async.} =
@ -54,7 +54,7 @@ proc readLoop(sconn: SecureConn, conn: Connection) {.async.} =
await sconn.close() await sconn.close()
trace "ending Secure readLoop" 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) var sconn = await s.handshake(conn, initiator)
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
trace "sending encrypted bytes", bytes = data.shortLog 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.} = method init*(s: Secure) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} = proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
trace "handling connection" trace "handling connection upgrade", proto
try: try:
# We don't need the result but we definitely need to await the handshake # We don't need the result but we definitely need to await the handshake
discard await s.handleConn(conn, false) discard await s.handleConn(conn, false)
@ -80,9 +80,9 @@ method init*(s: Secure) {.gcsafe.} =
s.handler = handle 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: try:
result = await s.handleConn(conn, true) result = await s.handleConn(conn, initiator)
except CatchableError as exc: except CatchableError as exc:
warn "securing connection failed", msg = exc.msg warn "securing connection failed", msg = exc.msg
if not conn.closed(): if not conn.closed():

View File

@ -1,11 +1,20 @@
# compile time options here
const
libp2p_secure {.strdefine.} = ""
import import
options, tables, options, tables,
switch, peer, peerinfo, connection, multiaddress, switch, peer, peerinfo, connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport], crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex, mplex/types], muxers/[muxer, mplex/mplex, mplex/types],
protocols/[identify, secure/secure, secure/secio], protocols/[identify, secure/secure],
protocols/pubsub/[pubsub, gossipsub, floodsub] protocols/pubsub/[pubsub, gossipsub, floodsub]
when libp2p_secure == "noise":
import protocols/secure/noise
else:
import protocols/secure/secio
export export
switch, peer, peerinfo, connection, multiaddress, crypto switch, peer, peerinfo, connection, multiaddress, crypto
@ -23,9 +32,14 @@ proc newStandardSwitch*(privKey = none(PrivateKey),
transports = @[Transport(newTransport(TcpTransport))] transports = @[Transport(newTransport(TcpTransport))]
muxers = {MplexCodec: mplexProvider}.toTable muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo) identify = newIdentify(peerInfo)
secureManagers = {SecioCodec: Secure(newSecio seckey)}.toTable when libp2p_secure == "noise":
pubSub = if gossip: PubSub newPubSub(GossipSub, peerInfo, triggerSelf) let secureManagers = {NoiseCodec: newNoise(seckey).Secure}.toTable
else: PubSub newPubSub(FloodSub, peerInfo, triggerSelf) 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, result = newSwitch(peerInfo,
transports, transports,

View File

@ -62,7 +62,7 @@ proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
if manager.len == 0: if manager.len == 0:
raise newException(CatchableError, "Unable to negotiate a secure channel!") 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.} = proc identify(s: Switch, conn: Connection): Future[PeerInfo] {.async, gcsafe.} =
## identify the connection ## identify the connection
@ -191,7 +191,7 @@ proc upgradeIncoming(s: Switch, conn: Connection) {.async, gcsafe.} =
{.async, gcsafe, closure.} = {.async, gcsafe, closure.} =
trace "Securing connection" trace "Securing connection"
let secure = s.secureManagers[proto] let secure = s.secureManagers[proto]
let sconn = await secure.secure(conn) let sconn = await secure.secure(conn, false)
if not isNil(sconn): if not isNil(sconn):
# add the muxer # add the muxer
for muxer in s.muxers.values: for muxer in s.muxers.values: