2021-01-20 17:28:32 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-01-20 17:28:32 +00:00
|
|
|
import std/[options, sequtils]
|
|
|
|
import pkg/[chronos, chronicles, metrics]
|
|
|
|
|
|
|
|
import ../stream/connection,
|
|
|
|
../protocols/secure/secure,
|
|
|
|
../protocols/identify,
|
|
|
|
../multistream,
|
2021-05-21 16:27:01 +00:00
|
|
|
../connmanager,
|
|
|
|
../errors
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
export connmanager, connection, identify, secure, multistream
|
|
|
|
|
|
|
|
declarePublicCounter(libp2p_failed_upgrade, "peers failed upgrade")
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "libp2p upgrade"
|
|
|
|
|
2021-01-20 17:28:32 +00:00
|
|
|
type
|
2021-05-21 16:27:01 +00:00
|
|
|
UpgradeFailedError* = object of LPError
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
Upgrade* = ref object of RootObj
|
|
|
|
ms*: MultistreamSelect
|
|
|
|
identity*: Identify
|
|
|
|
connManager*: ConnManager
|
|
|
|
secureManagers*: seq[Secure]
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method upgradeIncoming*(
|
|
|
|
self: Upgrade,
|
|
|
|
conn: Connection): Future[void] {.base.} =
|
2021-01-20 17:28:32 +00:00
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method upgradeOutgoing*(
|
|
|
|
self: Upgrade,
|
|
|
|
conn: Connection): Future[Connection] {.base.} =
|
2021-01-20 17:28:32 +00:00
|
|
|
doAssert(false, "Not implemented!")
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
proc secure*(
|
|
|
|
self: Upgrade,
|
|
|
|
conn: Connection): Future[Connection] {.async, gcsafe.} =
|
|
|
|
if self.secureManagers.len <= 0:
|
2021-01-20 17:28:32 +00:00
|
|
|
raise newException(UpgradeFailedError, "No secure managers registered!")
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
let codec = await self.ms.select(conn, self.secureManagers.mapIt(it.codec))
|
2021-01-20 17:28:32 +00:00
|
|
|
if codec.len == 0:
|
|
|
|
raise newException(UpgradeFailedError, "Unable to negotiate a secure channel!")
|
|
|
|
|
|
|
|
trace "Securing connection", conn, codec
|
2021-03-18 15:20:36 +00:00
|
|
|
let secureProtocol = self.secureManagers.filterIt(it.codec == codec)
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
# ms.select should deal with the correctness of this
|
|
|
|
# let's avoid duplicating checks but detect if it fails to do it properly
|
|
|
|
doAssert(secureProtocol.len > 0)
|
|
|
|
|
|
|
|
return await secureProtocol[0].secure(conn, true)
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
proc identify*(
|
|
|
|
self: Upgrade,
|
|
|
|
conn: Connection) {.async, gcsafe.} =
|
2021-01-20 17:28:32 +00:00
|
|
|
## identify the connection
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
if (await self.ms.select(conn, self.identity.codec)):
|
|
|
|
let info = await self.identity.identify(conn, conn.peerInfo)
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
if info.pubKey.isNone and isNil(conn):
|
|
|
|
raise newException(UpgradeFailedError,
|
|
|
|
"no public key provided and no existing peer identity found")
|
|
|
|
|
|
|
|
if isNil(conn.peerInfo):
|
|
|
|
conn.peerInfo = PeerInfo.init(info.pubKey.get())
|
|
|
|
|
|
|
|
if info.addrs.len > 0:
|
|
|
|
conn.peerInfo.addrs = info.addrs
|
|
|
|
|
|
|
|
if info.agentVersion.isSome:
|
|
|
|
conn.peerInfo.agentVersion = info.agentVersion.get()
|
|
|
|
|
|
|
|
if info.protoVersion.isSome:
|
|
|
|
conn.peerInfo.protoVersion = info.protoVersion.get()
|
|
|
|
|
|
|
|
if info.protos.len > 0:
|
|
|
|
conn.peerInfo.protocols = info.protos
|
|
|
|
|
|
|
|
trace "identified remote peer", conn, peerInfo = shortLog(conn.peerInfo)
|