handle secure managers correctly

This commit is contained in:
Dmitriy Ryajov 2019-09-09 17:17:45 -06:00
parent fc36de21c1
commit 525646dcc1
2 changed files with 14 additions and 5 deletions

View File

@ -45,7 +45,7 @@ proc select*(m: MultisteamSelect,
conn: Connection, conn: Connection,
proto: seq[string]): proto: seq[string]):
Future[string] {.async.} = Future[string] {.async.} =
debug "select: initiating handshake" debug "select: initiating handshake", codec = m.codec
## select a remote protocol ## select a remote protocol
await conn.write(m.codec) # write handshake await conn.write(m.codec) # write handshake
if proto.len() > 0: if proto.len() > 0:
@ -55,7 +55,7 @@ proc select*(m: MultisteamSelect,
result = cast[string](await conn.readLp()) # read ms header result = cast[string](await conn.readLp()) # read ms header
result.removeSuffix("\n") result.removeSuffix("\n")
if result != Codec: if result != Codec:
debug "select: handshake failed" debug "select: handshake failed", codec = result
return "" return ""
if proto.len() == 0: # no protocols, must be a handshake call if proto.len() == 0: # no protocols, must be a handshake call

View File

@ -15,7 +15,6 @@ import connection,
multistream, multistream,
protocols/protocol, protocols/protocol,
protocols/secure/secure, # for plain text protocols/secure/secure, # for plain text
protocols/secure/secio,
peerinfo, peerinfo,
multiaddress, multiaddress,
protocols/identify, protocols/identify,
@ -39,9 +38,14 @@ proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
## secure the incoming connection ## secure the incoming connection
# plaintext for now, doesn't do anything # plaintext for now, doesn't do anything
let managers = s.secureManagers.mapIt(it.codec).deduplicate()
if managers.len == 0:
raise newException(CatchableError, "No secure managers registered!")
if (await s.ms.select(conn, s.secureManagers.mapIt(it.codec))).len == 0: if (await s.ms.select(conn, s.secureManagers.mapIt(it.codec))).len == 0:
raise newException(CatchableError, "Unable to negotiate a secure channel!") raise newException(CatchableError, "Unable to negotiate a secure channel!")
var n = await s.secureManagers[0].secure(conn)
result = conn result = conn
proc identify(s: Switch, conn: Connection) {.async, gcsafe.} = proc identify(s: Switch, conn: Connection) {.async, gcsafe.} =
@ -201,9 +205,14 @@ proc newSwitch*(peerInfo: PeerInfo,
result.mount(val) result.mount(val)
for s in secureManagers: for s in secureManagers:
debug "adding secure manager ", codec = s.codec
result.secureManagers.add(s) result.secureManagers.add(s)
result.mount(s) result.mount(s)
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
result.mount(Secure(newPlainText())) let manager = Secure(newPlainText())
result.mount(manager)
result.secureManagers.add(manager)
result.secureManagers = result.secureManagers.deduplicate()