Merge branch 'dev/etan/ex-secure' into dev/etan/ex-x

X
This commit is contained in:
Etan Kissling 2024-03-06 20:58:35 +01:00
commit 96de774957
No known key found for this signature in database
GPG Key ID: B21DA824C5A3D03D
7 changed files with 51 additions and 42 deletions

View File

@ -292,7 +292,9 @@ proc new*(T: typedesc[RelayClient], canHop: bool = false,
maxCircuitPerPeer: maxCircuitPerPeer,
msgSize: msgSize,
isCircuitRelayV1: circuitRelayV1)
proc handleStream(conn: Connection, proto: string) {.async.} =
proc handleStream(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
try:
case proto:
of RelayV1Codec: await cl.handleStreamV1(conn)

View File

@ -336,7 +336,9 @@ proc new*(T: typedesc[Relay],
msgSize: msgSize,
isCircuitRelayV1: circuitRelayV1)
proc handleStream(conn: Connection, proto: string) {.async.} =
proc handleStream(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
try:
case proto:
of RelayV2HopCodec: await r.handleHopStreamV2(conn)

View File

@ -1,5 +1,5 @@
# Nim-LibP2P
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
@ -151,14 +151,16 @@ proc new*(
identify
method init*(p: Identify) =
proc handle(conn: Connection, proto: string) {.async.} =
proc handle(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
try:
trace "handling identify request", conn
var pb = encodeMsg(p.peerInfo, conn.observedAddr, p.sendSignedPeerRecord)
await conn.writeLp(pb.buffer)
except CancelledError as exc:
raise exc
except CatchableError as exc:
except LPStreamError as exc:
trace "exception in identify handler", exc = exc.msg, conn
finally:
trace "exiting identify handler", conn
@ -207,7 +209,9 @@ proc new*(T: typedesc[IdentifyPush], handler: IdentifyPushHandler = nil): T {.pu
identifypush
proc init*(p: IdentifyPush) =
proc handle(conn: Connection, proto: string) {.async.} =
proc handle(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
trace "handling identify push", conn
try:
var message = await conn.readLp(64*1024)

View File

@ -36,10 +36,8 @@ type
PingError* = object of LPError
WrongPingAckError* = object of PingError
PingHandler* {.public.} = proc (
peer: PeerId):
Future[void]
{.gcsafe, raises: [].}
PingHandler* {.public.} =
proc (peer: PeerId): Future[void] {.async: (raises: []).}
Ping* = ref object of LPProtocol
pingHandler*: PingHandler
@ -51,7 +49,9 @@ proc new*(T: typedesc[Ping], handler: PingHandler = nil, rng: ref HmacDrbgContex
ping
method init*(p: Ping) =
proc handle(conn: Connection, proto: string) {.async.} =
proc handle(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
try:
trace "handling ping", conn
var buf: array[PingSize, byte]
@ -62,7 +62,7 @@ method init*(p: Ping) =
await p.pingHandler(conn.peerId)
except CancelledError as exc:
raise exc
except CatchableError as exc:
except LPStreamError as exc:
trace "exception in ping handler", exc = exc.msg, conn
p.handler = handle

View File

@ -1,5 +1,5 @@
# Nim-LibP2P
# Copyright (c) 2023 Status Research & Development GmbH
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
@ -19,14 +19,13 @@ const
type
LPProtoHandler* = proc (
conn: Connection,
proto: string):
Future[void]
{.gcsafe, raises: [].}
conn: Connection,
proto: string
): Future[void] {.async: (raises: [CancelledError]).}
LPProtocol* = ref object of RootObj
codecs*: seq[string]
handler*: LPProtoHandler ## this handler gets invoked by the protocol negotiator
handler*: LPProtoHandler ## gets invoked by the protocol negotiator
started*: bool
maxIncomingStreams: Opt[int]
@ -50,10 +49,10 @@ func `codec=`*(p: LPProtocol, codec: string) =
p.codecs.insert(codec, 0)
proc new*(
T: type LPProtocol,
codecs: seq[string],
handler: LPProtoHandler,
maxIncomingStreams: Opt[int] | int = Opt.none(int)): T =
T: type LPProtocol,
codecs: seq[string],
handler: LPProtoHandler,
maxIncomingStreams: Opt[int] | int = Opt.none(int)): T =
T(
codecs: codecs,
handler: handler,

View File

@ -641,7 +641,9 @@ proc new*(
sema: newAsyncSemaphore(SemaphoreDefaultSize)
)
logScope: topics = "libp2p discovery rendezvous"
proc handleStream(conn: Connection, proto: string) {.async.} =
proc handleStream(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
try:
let
buf = await conn.readLp(4096)

View File

@ -88,10 +88,12 @@ method handshake*(
CancelledError, LPStreamError], raw: true), base.} =
raiseAssert("Not implemented!")
proc handleConn(s: Secure,
conn: Connection,
initiator: bool,
peerId: Opt[PeerId]): Future[Connection] {.async.} =
proc handleConn(
s: Secure,
conn: Connection,
initiator: bool,
peerId: Opt[PeerId]
): Future[Connection] {.async: (raises: [CancelledError, LPStreamError]).} =
var sconn = await s.handshake(conn, initiator, peerId)
# mark connection bottom level transport direction
# this is the safest place to do this
@ -115,14 +117,8 @@ proc handleConn(s: Secure,
fut1 = sconn.close()
fut2 = conn.close()
await allFutures(fut1, fut2)
if fut1.failed:
let err = fut1.error()
if not (err of CancelledError):
debug "error cleaning up secure connection", err = err.msg, sconn
if fut2.failed:
let err = fut2.error()
if not (err of CancelledError):
debug "error cleaning up secure connection", err = err.msg, sconn
static: doAssert typeof(fut1).E is void # Cannot fail
static: doAssert typeof(fut2).E is void # Cannot fail
except CancelledError:
# This is top-level procedure which will work as separate task, so it
@ -133,12 +129,14 @@ proc handleConn(s: Secure,
# All the errors are handled inside `cleanup()` procedure.
asyncSpawn cleanup()
return sconn
sconn
method init*(s: Secure) =
procCall LPProtocol(s).init()
proc handle(conn: Connection, proto: string) {.async.} =
proc handle(
conn: Connection,
proto: string) {.async: (raises: [CancelledError]).} =
trace "handling connection upgrade", proto, conn
try:
# We don't need the result but we
@ -149,16 +147,18 @@ method init*(s: Secure) =
warn "securing connection canceled", conn
await conn.close()
raise exc
except CatchableError as exc:
except LPStreamError as exc:
warn "securing connection failed", err = exc.msg, conn
await conn.close()
s.handler = handle
method secure*(s: Secure,
conn: Connection,
peerId: Opt[PeerId]):
Future[Connection] {.base.} =
method secure*(
s: Secure,
conn: Connection,
peerId: Opt[PeerId]
): Future[Connection] {.async: (raises: [
CancelledError, LPStreamError], raw: true), base.} =
s.handleConn(conn, conn.dir == Direction.Out, peerId)
method readOnce*(