2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# 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-01-20 17:28:32 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-01-20 17:28:32 +00:00
|
|
|
import std/[tables, sequtils]
|
|
|
|
import pkg/[chronos, chronicles, metrics]
|
|
|
|
|
|
|
|
import ../upgrademngrs/upgrade,
|
|
|
|
../muxers/muxer
|
|
|
|
|
|
|
|
export Upgrade
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
logScope:
|
|
|
|
topics = "libp2p muxedupgrade"
|
|
|
|
|
2021-01-20 17:28:32 +00:00
|
|
|
type
|
|
|
|
MuxedUpgrade* = ref object of Upgrade
|
2022-07-22 10:54:09 +00:00
|
|
|
muxers*: seq[MuxerProvider]
|
2021-01-20 17:28:32 +00:00
|
|
|
streamHandler*: StreamHandler
|
|
|
|
|
2022-07-22 10:54:09 +00:00
|
|
|
proc getMuxerByCodec(self: MuxedUpgrade, muxerName: string): MuxerProvider =
|
|
|
|
for m in self.muxers:
|
|
|
|
if muxerName in m.codecs:
|
|
|
|
return m
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
proc identify*(
|
|
|
|
self: MuxedUpgrade,
|
|
|
|
muxer: Muxer) {.async, gcsafe.} =
|
2021-01-20 17:28:32 +00:00
|
|
|
# new stream for identify
|
|
|
|
var stream = await muxer.newStream()
|
|
|
|
if stream == nil:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2021-03-18 15:20:36 +00:00
|
|
|
await self.identify(stream)
|
2021-09-08 09:07:46 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
muxer.connection.shortAgent = stream.shortAgent
|
2021-01-20 17:28:32 +00:00
|
|
|
finally:
|
|
|
|
await stream.closeWithEOF()
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
proc mux*(
|
|
|
|
self: MuxedUpgrade,
|
|
|
|
conn: Connection): Future[Muxer] {.async, gcsafe.} =
|
2021-09-08 09:07:46 +00:00
|
|
|
## mux outgoing connection
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
trace "Muxing connection", conn
|
2021-03-18 15:20:36 +00:00
|
|
|
if self.muxers.len == 0:
|
2021-01-20 17:28:32 +00:00
|
|
|
warn "no muxers registered, skipping upgrade flow", conn
|
|
|
|
return
|
|
|
|
|
2022-07-22 10:54:09 +00:00
|
|
|
let muxerName = await self.ms.select(conn, self.muxers.mapIt(it.codec))
|
2021-01-20 17:28:32 +00:00
|
|
|
if muxerName.len == 0 or muxerName == "na":
|
|
|
|
debug "no muxer available, early exit", conn
|
|
|
|
return
|
|
|
|
|
|
|
|
trace "Found a muxer", conn, muxerName
|
|
|
|
|
|
|
|
# create new muxer for connection
|
2022-07-22 10:54:09 +00:00
|
|
|
let muxer = self.getMuxerByCodec(muxerName).newMuxer(conn)
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
# install stream handler
|
2021-03-18 15:20:36 +00:00
|
|
|
muxer.streamHandler = self.streamHandler
|
2021-01-20 17:28:32 +00:00
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
self.connManager.storeConn(conn)
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
# store it in muxed connections if we have a peer for it
|
2021-03-18 15:20:36 +00:00
|
|
|
self.connManager.storeMuxer(muxer, muxer.handle()) # store muxer and start read loop
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
try:
|
2021-03-18 15:20:36 +00:00
|
|
|
await self.identify(muxer)
|
2021-01-20 17:28:32 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
# Identify is non-essential, though if it fails, it might indicate that
|
|
|
|
# the connection was closed already - this will be picked up by the read
|
|
|
|
# loop
|
|
|
|
debug "Could not identify connection", conn, msg = exc.msg
|
|
|
|
|
|
|
|
return muxer
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method upgradeOutgoing*(
|
|
|
|
self: MuxedUpgrade,
|
2022-09-05 12:31:14 +00:00
|
|
|
conn: Connection,
|
|
|
|
peerId: Opt[PeerId]): Future[Connection] {.async, gcsafe.} =
|
2021-01-20 17:28:32 +00:00
|
|
|
trace "Upgrading outgoing connection", conn
|
|
|
|
|
2022-09-05 12:31:14 +00:00
|
|
|
let sconn = await self.secure(conn, peerId) # secure the connection
|
2021-01-20 17:28:32 +00:00
|
|
|
if isNil(sconn):
|
|
|
|
raise newException(UpgradeFailedError,
|
|
|
|
"unable to secure connection, stopping upgrade")
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
let muxer = await self.mux(sconn) # mux it if possible
|
2021-01-20 17:28:32 +00:00
|
|
|
if muxer == nil:
|
|
|
|
# TODO this might be relaxed in the future
|
|
|
|
raise newException(UpgradeFailedError,
|
|
|
|
"a muxer is required for outgoing connections")
|
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
conn.shortAgent = muxer.connection.shortAgent
|
|
|
|
|
|
|
|
if sconn.closed():
|
2021-01-20 17:28:32 +00:00
|
|
|
await sconn.close()
|
|
|
|
raise newException(UpgradeFailedError,
|
|
|
|
"Connection closed or missing peer info, stopping upgrade")
|
|
|
|
|
|
|
|
trace "Upgraded outgoing connection", conn, sconn
|
|
|
|
|
|
|
|
return sconn
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
method upgradeIncoming*(
|
|
|
|
self: MuxedUpgrade,
|
2021-05-25 17:05:50 +00:00
|
|
|
incomingConn: Connection) {.async, gcsafe.} = # noraises
|
2021-01-20 17:28:32 +00:00
|
|
|
trace "Upgrading incoming connection", incomingConn
|
2021-06-07 07:32:08 +00:00
|
|
|
let ms = MultistreamSelect.new()
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
# secure incoming connections
|
|
|
|
proc securedHandler(conn: Connection,
|
|
|
|
proto: string)
|
|
|
|
{.async, gcsafe, closure.} =
|
|
|
|
trace "Starting secure handler", conn
|
2021-03-18 15:20:36 +00:00
|
|
|
let secure = self.secureManagers.filterIt(it.codec == proto)[0]
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
var cconn = conn
|
|
|
|
try:
|
2022-09-05 12:31:14 +00:00
|
|
|
var sconn = await secure.secure(cconn, false, Opt.none(PeerId))
|
2021-01-20 17:28:32 +00:00
|
|
|
if isNil(sconn):
|
|
|
|
return
|
|
|
|
|
|
|
|
cconn = sconn
|
|
|
|
# add the muxer
|
2022-07-22 10:54:09 +00:00
|
|
|
for muxer in self.muxers:
|
2021-01-20 17:28:32 +00:00
|
|
|
ms.addHandler(muxer.codecs, muxer)
|
|
|
|
|
|
|
|
# handle subsequent secure requests
|
|
|
|
await ms.handle(cconn)
|
|
|
|
except CatchableError as exc:
|
|
|
|
debug "Exception in secure handler during incoming upgrade", msg = exc.msg, conn
|
|
|
|
if not cconn.isUpgraded:
|
|
|
|
cconn.upgrade(exc)
|
|
|
|
finally:
|
|
|
|
if not isNil(cconn):
|
|
|
|
await cconn.close()
|
|
|
|
|
|
|
|
trace "Stopped secure handler", conn
|
|
|
|
|
|
|
|
try:
|
|
|
|
if (await ms.select(incomingConn)): # just handshake
|
|
|
|
# add the secure handlers
|
2021-03-18 15:20:36 +00:00
|
|
|
for k in self.secureManagers:
|
2021-01-20 17:28:32 +00:00
|
|
|
ms.addHandler(k.codec, securedHandler)
|
|
|
|
|
|
|
|
# handle un-secured connections
|
|
|
|
# we handshaked above, set this ms handler as active
|
|
|
|
await ms.handle(incomingConn, active = true)
|
|
|
|
except CatchableError as exc:
|
|
|
|
debug "Exception upgrading incoming", exc = exc.msg
|
|
|
|
if not incomingConn.isUpgraded:
|
|
|
|
incomingConn.upgrade(exc)
|
|
|
|
finally:
|
|
|
|
if not isNil(incomingConn):
|
|
|
|
await incomingConn.close()
|
|
|
|
|
2021-03-18 15:20:36 +00:00
|
|
|
proc muxerHandler(
|
|
|
|
self: MuxedUpgrade,
|
|
|
|
muxer: Muxer) {.async, gcsafe.} =
|
2021-01-20 17:28:32 +00:00
|
|
|
let
|
|
|
|
conn = muxer.connection
|
|
|
|
|
|
|
|
# store incoming connection
|
2021-03-18 15:20:36 +00:00
|
|
|
self.connManager.storeConn(conn)
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
# store muxer and muxed connection
|
2021-03-18 15:20:36 +00:00
|
|
|
self.connManager.storeMuxer(muxer)
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
try:
|
2021-03-18 15:20:36 +00:00
|
|
|
await self.identify(muxer)
|
2021-11-08 16:42:56 +00:00
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
#TODO Passing data between layers is a pain
|
|
|
|
if muxer.connection of SecureConn:
|
|
|
|
let secureConn = (SecureConn)muxer.connection
|
|
|
|
secureConn.stream.shortAgent = muxer.connection.shortAgent
|
2021-01-20 17:28:32 +00:00
|
|
|
except IdentifyError as exc:
|
|
|
|
# Identify is non-essential, though if it fails, it might indicate that
|
|
|
|
# the connection was closed already - this will be picked up by the read
|
|
|
|
# loop
|
|
|
|
debug "Could not identify connection", conn, msg = exc.msg
|
|
|
|
except LPStreamClosedError as exc:
|
|
|
|
debug "Identify stream closed", conn, msg = exc.msg
|
|
|
|
except LPStreamEOFError as exc:
|
|
|
|
debug "Identify stream EOF", conn, msg = exc.msg
|
|
|
|
except CancelledError as exc:
|
|
|
|
await muxer.close()
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
await muxer.close()
|
|
|
|
trace "Exception in muxer handler", conn, msg = exc.msg
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc new*(
|
2021-01-20 17:28:32 +00:00
|
|
|
T: type MuxedUpgrade,
|
|
|
|
identity: Identify,
|
2022-07-22 10:54:09 +00:00
|
|
|
muxers: seq[MuxerProvider],
|
2021-12-16 10:05:20 +00:00
|
|
|
secureManagers: openArray[Secure] = [],
|
2021-01-20 17:28:32 +00:00
|
|
|
connManager: ConnManager,
|
|
|
|
ms: MultistreamSelect): T =
|
|
|
|
|
|
|
|
let upgrader = T(
|
|
|
|
identity: identity,
|
|
|
|
muxers: muxers,
|
|
|
|
secureManagers: @secureManagers,
|
|
|
|
connManager: connManager,
|
|
|
|
ms: ms)
|
|
|
|
|
2021-05-25 17:05:50 +00:00
|
|
|
upgrader.streamHandler = proc(conn: Connection)
|
|
|
|
{.async, gcsafe, raises: [Defect].} =
|
|
|
|
trace "Starting stream handler", conn
|
|
|
|
try:
|
|
|
|
await upgrader.ms.handle(conn) # handle incoming connection
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception in stream handler", conn, msg = exc.msg
|
|
|
|
finally:
|
|
|
|
await conn.closeWithEOF()
|
|
|
|
trace "Stream handler done", conn
|
2021-01-20 17:28:32 +00:00
|
|
|
|
|
|
|
for _, val in muxers:
|
|
|
|
val.streamHandler = upgrader.streamHandler
|
2021-05-25 17:05:50 +00:00
|
|
|
val.muxerHandler = proc(muxer: Muxer): Future[void]
|
|
|
|
{.raises: [Defect].} =
|
2021-01-20 17:28:32 +00:00
|
|
|
upgrader.muxerHandler(muxer)
|
|
|
|
|
|
|
|
return upgrader
|