2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2024-03-05 07:06:27 +00:00
|
|
|
# Copyright (c) 2023-2024 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.
|
|
|
|
|
|
|
|
## WebSocket & WebSocket Secure transport implementation
|
2021-08-03 13:48:03 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
import std/[sequtils]
|
2022-09-22 19:55:59 +00:00
|
|
|
import stew/results
|
2021-08-03 13:48:03 +00:00
|
|
|
import chronos, chronicles
|
|
|
|
import transport,
|
|
|
|
../errors,
|
|
|
|
../wire,
|
|
|
|
../multicodec,
|
|
|
|
../multistream,
|
|
|
|
../connmanager,
|
|
|
|
../multiaddress,
|
2022-07-01 18:19:57 +00:00
|
|
|
../utility,
|
2021-08-03 13:48:03 +00:00
|
|
|
../stream/connection,
|
|
|
|
../upgrademngrs/upgrade,
|
|
|
|
websock/websock
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "libp2p wstransport"
|
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
export transport, websock, results
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
const
|
2022-03-17 09:16:48 +00:00
|
|
|
DefaultHeadersTimeout = 3.seconds
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
type
|
|
|
|
WsStream = ref object of Connection
|
|
|
|
session: WSSession
|
|
|
|
|
2022-11-22 09:44:08 +00:00
|
|
|
method initStream*(s: WsStream) =
|
|
|
|
if s.objName.len == 0:
|
|
|
|
s.objName = "WsStream"
|
|
|
|
|
|
|
|
procCall Connection(s).initStream()
|
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc new*(
|
|
|
|
T: type WsStream,
|
|
|
|
session: WSSession,
|
|
|
|
dir: Direction,
|
|
|
|
observedAddr: Opt[MultiAddress],
|
|
|
|
timeout = 10.minutes): T =
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
let stream = T(
|
|
|
|
session: session,
|
|
|
|
timeout: timeout,
|
|
|
|
dir: dir,
|
|
|
|
observedAddr: observedAddr)
|
|
|
|
|
|
|
|
stream.initStream()
|
|
|
|
return stream
|
|
|
|
|
2022-01-28 18:05:07 +00:00
|
|
|
template mapExceptions(body: untyped) =
|
|
|
|
try:
|
|
|
|
body
|
|
|
|
except AsyncStreamIncompleteError:
|
2024-03-03 23:27:35 +00:00
|
|
|
raise newLPStreamIncompleteError()
|
|
|
|
except AsyncStreamLimitError:
|
|
|
|
raise newLPStreamLimitError()
|
2022-01-28 18:05:07 +00:00
|
|
|
except AsyncStreamUseClosedError:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
except WSClosedError:
|
|
|
|
raise newLPStreamEOFError()
|
2024-03-03 23:27:35 +00:00
|
|
|
except WebSocketError:
|
|
|
|
raise newLPStreamEOFError()
|
|
|
|
except CatchableError:
|
|
|
|
raise newLPStreamEOFError()
|
2022-01-28 18:05:07 +00:00
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
method readOnce*(
|
2024-03-05 07:06:27 +00:00
|
|
|
s: WsStream,
|
|
|
|
pbytes: pointer,
|
|
|
|
nbytes: int
|
|
|
|
): Future[int] {.async: (raises: [CancelledError, LPStreamError]).} =
|
2022-01-28 18:05:07 +00:00
|
|
|
let res = mapExceptions(await s.session.recv(pbytes, nbytes))
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
if res == 0 and s.session.readyState == ReadyState.Closed:
|
|
|
|
raise newLPStreamEOFError()
|
2022-05-24 19:41:05 +00:00
|
|
|
s.activity = true # reset activity flag
|
2021-08-03 13:48:03 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
method write*(
|
2024-03-05 07:06:27 +00:00
|
|
|
s: WsStream,
|
|
|
|
msg: seq[byte]
|
|
|
|
): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} =
|
2022-01-28 18:05:07 +00:00
|
|
|
mapExceptions(await s.session.send(msg, Opcode.Binary))
|
2022-05-24 19:41:05 +00:00
|
|
|
s.activity = true # reset activity flag
|
2021-08-03 13:48:03 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
method closeImpl*(s: WsStream): Future[void] {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
await s.session.close()
|
|
|
|
except CatchableError:
|
|
|
|
discard
|
2021-08-03 13:48:03 +00:00
|
|
|
await procCall Connection(s).closeImpl()
|
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
method getWrapped*(s: WsStream): Connection = nil
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
type
|
|
|
|
WsTransport* = ref object of Transport
|
2021-11-24 20:01:12 +00:00
|
|
|
httpservers: seq[HttpServer]
|
2021-08-03 13:48:03 +00:00
|
|
|
wsserver: WSServer
|
|
|
|
connections: array[Direction, seq[WsStream]]
|
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
acceptFuts: seq[Future[HttpRequest]]
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
tlsPrivateKey: TLSPrivateKey
|
|
|
|
tlsCertificate: TLSCertificate
|
|
|
|
tlsFlags: set[TLSFlags]
|
|
|
|
flags: set[ServerFlags]
|
2022-03-17 09:16:48 +00:00
|
|
|
handshakeTimeout: Duration
|
2021-08-03 13:48:03 +00:00
|
|
|
factories: seq[ExtFactory]
|
2023-08-02 15:10:31 +00:00
|
|
|
rng: ref HmacDrbgContext
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
proc secure*(self: WsTransport): bool =
|
|
|
|
not (isNil(self.tlsPrivateKey) or isNil(self.tlsCertificate))
|
|
|
|
|
|
|
|
method start*(
|
|
|
|
self: WsTransport,
|
2021-11-24 20:01:12 +00:00
|
|
|
addrs: seq[MultiAddress]) {.async.} =
|
2021-08-03 13:48:03 +00:00
|
|
|
## listen on the transport
|
|
|
|
##
|
|
|
|
|
|
|
|
if self.running:
|
2021-11-24 20:01:12 +00:00
|
|
|
warn "WS transport already running"
|
2021-08-03 13:48:03 +00:00
|
|
|
return
|
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
await procCall Transport(self).start(addrs)
|
2021-08-03 13:48:03 +00:00
|
|
|
trace "Starting WS transport"
|
|
|
|
|
|
|
|
self.wsserver = WSServer.new(
|
|
|
|
factories = self.factories,
|
|
|
|
rng = self.rng)
|
|
|
|
|
2022-11-22 09:44:08 +00:00
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
for i, ma in addrs:
|
|
|
|
let isWss =
|
|
|
|
if WSS.match(ma):
|
|
|
|
if self.secure: true
|
|
|
|
else:
|
2024-03-05 07:06:27 +00:00
|
|
|
warn "Trying to listen on a WSS address without setting certificate!"
|
2021-11-24 20:01:12 +00:00
|
|
|
false
|
|
|
|
else: false
|
|
|
|
|
|
|
|
let httpserver =
|
|
|
|
if isWss:
|
|
|
|
TlsHttpServer.create(
|
|
|
|
address = ma.initTAddress().tryGet(),
|
|
|
|
tlsPrivateKey = self.tlsPrivateKey,
|
|
|
|
tlsCertificate = self.tlsCertificate,
|
2022-03-17 09:16:48 +00:00
|
|
|
flags = self.flags,
|
|
|
|
handshakeTimeout = self.handshakeTimeout)
|
2021-11-24 20:01:12 +00:00
|
|
|
else:
|
2022-03-17 09:16:48 +00:00
|
|
|
HttpServer.create(
|
|
|
|
ma.initTAddress().tryGet(),
|
|
|
|
handshakeTimeout = self.handshakeTimeout
|
|
|
|
)
|
2021-11-24 20:01:12 +00:00
|
|
|
|
|
|
|
self.httpservers &= httpserver
|
|
|
|
|
2023-06-12 15:45:53 +00:00
|
|
|
let codec =
|
|
|
|
if isWss:
|
|
|
|
if ma.contains(multiCodec("tls")) == MaResult[bool].ok(true):
|
|
|
|
MultiAddress.init("/tls/ws")
|
|
|
|
else:
|
|
|
|
MultiAddress.init("/wss")
|
2021-11-24 20:01:12 +00:00
|
|
|
else:
|
|
|
|
MultiAddress.init("/ws")
|
|
|
|
|
|
|
|
# always get the resolved address in case we're bound to 0.0.0.0:0
|
|
|
|
self.addrs[i] = MultiAddress.init(
|
|
|
|
httpserver.localAddress()).tryGet() & codec.tryGet()
|
|
|
|
|
|
|
|
trace "Listening on", addresses = self.addrs
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
self.running = true
|
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
method stop*(self: WsTransport) {.async.} =
|
2021-08-03 13:48:03 +00:00
|
|
|
## stop the transport
|
|
|
|
##
|
|
|
|
|
|
|
|
self.running = false # mark stopped as soon as possible
|
|
|
|
|
|
|
|
try:
|
|
|
|
trace "Stopping WS transport"
|
|
|
|
await procCall Transport(self).stop() # call base
|
|
|
|
|
|
|
|
checkFutures(
|
|
|
|
await allFinished(
|
|
|
|
self.connections[Direction.In].mapIt(it.close()) &
|
|
|
|
self.connections[Direction.Out].mapIt(it.close())))
|
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
var toWait: seq[Future[void]]
|
|
|
|
for fut in self.acceptFuts:
|
|
|
|
if not fut.finished:
|
|
|
|
toWait.add(fut.cancelAndWait())
|
|
|
|
elif fut.done:
|
|
|
|
toWait.add(fut.read().stream.closeWait())
|
|
|
|
|
|
|
|
for server in self.httpservers:
|
|
|
|
server.stop()
|
|
|
|
toWait.add(server.closeWait())
|
|
|
|
|
|
|
|
await allFutures(toWait)
|
2021-08-03 13:48:03 +00:00
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
self.httpservers = @[]
|
2021-08-03 13:48:03 +00:00
|
|
|
trace "Transport stopped"
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Error shutting down ws transport", exc = exc.msg
|
|
|
|
|
2021-10-14 11:16:34 +00:00
|
|
|
proc connHandler(self: WsTransport,
|
2021-12-16 10:05:20 +00:00
|
|
|
stream: WSSession,
|
2021-11-24 20:01:12 +00:00
|
|
|
secure: bool,
|
2021-10-14 11:16:34 +00:00
|
|
|
dir: Direction): Future[Connection] {.async.} =
|
|
|
|
let observedAddr =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
codec =
|
2021-11-24 20:01:12 +00:00
|
|
|
if secure:
|
2021-10-14 11:16:34 +00:00
|
|
|
MultiAddress.init("/wss")
|
|
|
|
else:
|
|
|
|
MultiAddress.init("/ws")
|
|
|
|
remoteAddr = stream.stream.reader.tsource.remoteAddress
|
|
|
|
|
|
|
|
MultiAddress.init(remoteAddr).tryGet() & codec.tryGet()
|
|
|
|
except CatchableError as exc:
|
|
|
|
trace "Failed to create observedAddr", exc = exc.msg
|
|
|
|
if not(isNil(stream) and stream.stream.reader.closed):
|
|
|
|
await stream.close()
|
|
|
|
raise exc
|
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
let conn = WsStream.new(stream, dir, Opt.some(observedAddr))
|
2021-10-14 11:16:34 +00:00
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
self.connections[dir].add(conn)
|
|
|
|
proc onClose() {.async.} =
|
|
|
|
await conn.session.stream.reader.join()
|
|
|
|
self.connections[dir].keepItIf(it != conn)
|
|
|
|
trace "Cleaned up client"
|
|
|
|
asyncSpawn onClose()
|
2021-10-14 11:16:34 +00:00
|
|
|
return conn
|
2021-08-03 13:48:03 +00:00
|
|
|
|
2023-12-05 07:05:32 +00:00
|
|
|
method accept*(self: WsTransport): Future[Connection] {.async.} =
|
2021-08-03 13:48:03 +00:00
|
|
|
## accept a new WS connection
|
|
|
|
##
|
|
|
|
|
|
|
|
if not self.running:
|
|
|
|
raise newTransportClosedError()
|
|
|
|
|
2022-03-17 09:16:48 +00:00
|
|
|
if self.acceptFuts.len <= 0:
|
|
|
|
self.acceptFuts = self.httpservers.mapIt(it.accept())
|
2021-11-24 20:01:12 +00:00
|
|
|
|
2022-03-17 09:16:48 +00:00
|
|
|
if self.acceptFuts.len <= 0:
|
|
|
|
return
|
2021-11-24 20:01:12 +00:00
|
|
|
|
2022-03-17 09:16:48 +00:00
|
|
|
let
|
|
|
|
finished = await one(self.acceptFuts)
|
|
|
|
index = self.acceptFuts.find(finished)
|
2021-11-24 20:01:12 +00:00
|
|
|
|
2022-03-17 09:16:48 +00:00
|
|
|
self.acceptFuts[index] = self.httpservers[index].accept()
|
2021-11-24 20:01:12 +00:00
|
|
|
|
2022-03-17 09:16:48 +00:00
|
|
|
try:
|
2021-11-24 20:01:12 +00:00
|
|
|
let req = await finished
|
2021-08-03 13:48:03 +00:00
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
try:
|
|
|
|
let
|
2022-11-22 09:44:08 +00:00
|
|
|
wstransp = await self.wsserver.handleRequest(req).wait(self.handshakeTimeout)
|
2021-11-24 20:01:12 +00:00
|
|
|
isSecure = self.httpservers[index].secure
|
|
|
|
|
|
|
|
return await self.connHandler(wstransp, isSecure, Direction.In)
|
|
|
|
except CatchableError as exc:
|
|
|
|
await req.stream.closeWait()
|
|
|
|
raise exc
|
2021-11-26 12:55:35 +00:00
|
|
|
except WebSocketError as exc:
|
|
|
|
debug "Websocket Error", exc = exc.msg
|
2022-03-17 09:16:48 +00:00
|
|
|
except HttpError as exc:
|
|
|
|
debug "Http Error", exc = exc.msg
|
2021-11-26 12:55:35 +00:00
|
|
|
except AsyncStreamError as exc:
|
|
|
|
debug "AsyncStream Error", exc = exc.msg
|
2021-08-03 13:48:03 +00:00
|
|
|
except TransportTooManyError as exc:
|
|
|
|
debug "Too many files opened", exc = exc.msg
|
2023-10-23 15:12:20 +00:00
|
|
|
except TransportAbortedError as exc:
|
|
|
|
debug "Connection aborted", exc = exc.msg
|
2022-11-22 09:44:08 +00:00
|
|
|
except AsyncTimeoutError as exc:
|
|
|
|
debug "Timed out", exc = exc.msg
|
2021-08-03 13:48:03 +00:00
|
|
|
except TransportUseClosedError as exc:
|
|
|
|
debug "Server was closed", exc = exc.msg
|
|
|
|
raise newTransportClosedError(exc)
|
2021-11-24 20:01:12 +00:00
|
|
|
except CancelledError as exc:
|
2023-06-14 15:55:56 +00:00
|
|
|
raise exc
|
|
|
|
except TransportOsError as exc:
|
2023-07-07 09:32:20 +00:00
|
|
|
debug "OS Error", exc = exc.msg
|
2021-08-03 13:48:03 +00:00
|
|
|
except CatchableError as exc:
|
2023-06-14 15:55:56 +00:00
|
|
|
info "Unexpected error accepting connection", exc = exc.msg
|
2021-08-03 13:48:03 +00:00
|
|
|
raise exc
|
|
|
|
|
|
|
|
method dial*(
|
|
|
|
self: WsTransport,
|
2021-11-08 12:02:03 +00:00
|
|
|
hostname: string,
|
2023-01-31 11:46:10 +00:00
|
|
|
address: MultiAddress,
|
2023-12-05 07:05:32 +00:00
|
|
|
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async.} =
|
2021-08-03 13:48:03 +00:00
|
|
|
## dial a peer
|
|
|
|
##
|
|
|
|
|
|
|
|
trace "Dialing remote peer", address = $address
|
|
|
|
|
|
|
|
let
|
|
|
|
secure = WSS.match(address)
|
|
|
|
transp = await WebSocket.connect(
|
|
|
|
address.initTAddress().tryGet(),
|
|
|
|
"",
|
|
|
|
secure = secure,
|
2021-11-08 12:02:03 +00:00
|
|
|
hostName = hostname,
|
2021-08-03 13:48:03 +00:00
|
|
|
flags = self.tlsFlags)
|
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
try:
|
|
|
|
return await self.connHandler(transp, secure, Direction.Out)
|
|
|
|
except CatchableError as exc:
|
|
|
|
await transp.close()
|
|
|
|
raise exc
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
method handles*(t: WsTransport, address: MultiAddress): bool {.gcsafe.} =
|
|
|
|
if procCall Transport(t).handles(address):
|
|
|
|
if address.protocols.isOk:
|
|
|
|
return WebSockets.match(address)
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: typedesc[WsTransport],
|
|
|
|
upgrade: Upgrade,
|
|
|
|
tlsPrivateKey: TLSPrivateKey,
|
|
|
|
tlsCertificate: TLSCertificate,
|
|
|
|
tlsFlags: set[TLSFlags] = {},
|
|
|
|
flags: set[ServerFlags] = {},
|
|
|
|
factories: openArray[ExtFactory] = [],
|
2023-08-02 15:10:31 +00:00
|
|
|
rng: ref HmacDrbgContext = nil,
|
2022-07-01 18:19:57 +00:00
|
|
|
handshakeTimeout = DefaultHeadersTimeout): T {.public.} =
|
|
|
|
## Creates a secure WebSocket transport
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
T(
|
|
|
|
upgrader: upgrade,
|
|
|
|
tlsPrivateKey: tlsPrivateKey,
|
|
|
|
tlsCertificate: tlsCertificate,
|
|
|
|
tlsFlags: tlsFlags,
|
|
|
|
flags: flags,
|
|
|
|
factories: @factories,
|
2022-03-17 09:16:48 +00:00
|
|
|
rng: rng,
|
|
|
|
handshakeTimeout: handshakeTimeout)
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: typedesc[WsTransport],
|
|
|
|
upgrade: Upgrade,
|
|
|
|
flags: set[ServerFlags] = {},
|
|
|
|
factories: openArray[ExtFactory] = [],
|
2023-08-02 15:10:31 +00:00
|
|
|
rng: ref HmacDrbgContext = nil,
|
2022-07-01 18:19:57 +00:00
|
|
|
handshakeTimeout = DefaultHeadersTimeout): T {.public.} =
|
|
|
|
## Creates a clear-text WebSocket transport
|
2021-08-03 13:48:03 +00:00
|
|
|
|
|
|
|
T.new(
|
|
|
|
upgrade = upgrade,
|
|
|
|
tlsPrivateKey = nil,
|
|
|
|
tlsCertificate = nil,
|
|
|
|
flags = flags,
|
|
|
|
factories = @factories,
|
2022-03-17 09:16:48 +00:00
|
|
|
rng = rng,
|
|
|
|
handshakeTimeout = handshakeTimeout)
|