2019-08-27 21:46:12 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-27 21:46:12 +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.
|
|
|
|
|
2019-09-04 22:00:39 +00:00
|
|
|
import tables, sequtils, options, strformat
|
2019-09-09 17:33:32 +00:00
|
|
|
import chronos, chronicles
|
2019-09-11 19:03:30 +00:00
|
|
|
import connection,
|
|
|
|
transports/transport,
|
|
|
|
multistream,
|
2019-09-06 07:13:47 +00:00
|
|
|
protocols/protocol,
|
2019-09-14 13:55:52 +00:00
|
|
|
protocols/secure/secure,
|
|
|
|
protocols/secure/plaintext, # for plain text
|
2019-09-11 19:03:30 +00:00
|
|
|
peerinfo,
|
|
|
|
protocols/identify,
|
|
|
|
protocols/pubsub/pubsub,
|
2019-09-06 07:13:47 +00:00
|
|
|
muxers/muxer,
|
2019-09-09 17:33:32 +00:00
|
|
|
peer
|
2019-08-28 19:12:15 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
logScope:
|
|
|
|
topic = "Switch"
|
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
#TODO: General note - use a finite state machine to manage the different
|
2019-12-08 21:06:58 +00:00
|
|
|
# steps of connections establishing and upgrading. This makes everything
|
|
|
|
# more robust and less prone to ordering attacks - i.e. muxing can come if
|
|
|
|
# and only if the channel has been secured (i.e. if a secure manager has been
|
2019-12-04 04:44:54 +00:00
|
|
|
# previously provided)
|
|
|
|
|
2019-08-28 19:12:15 +00:00
|
|
|
type
|
2019-09-11 19:03:30 +00:00
|
|
|
NoPubSubException = object of CatchableError
|
|
|
|
|
2019-08-28 19:12:15 +00:00
|
|
|
Switch* = ref object of RootObj
|
2019-08-30 05:17:07 +00:00
|
|
|
peerInfo*: PeerInfo
|
2019-09-13 18:41:48 +00:00
|
|
|
connections*: Table[string, Connection]
|
|
|
|
muxed*: Table[string, Muxer]
|
2019-08-30 05:17:07 +00:00
|
|
|
transports*: seq[Transport]
|
|
|
|
protocols*: seq[LPProtocol]
|
2019-09-04 22:00:39 +00:00
|
|
|
muxers*: Table[string, MuxerProvider]
|
2019-08-30 05:17:07 +00:00
|
|
|
ms*: MultisteamSelect
|
2019-09-04 01:50:17 +00:00
|
|
|
identity*: Identify
|
2019-09-06 07:13:47 +00:00
|
|
|
streamHandler*: StreamHandler
|
2019-09-13 18:41:48 +00:00
|
|
|
secureManagers*: Table[string, Secure]
|
2019-09-11 19:03:30 +00:00
|
|
|
pubSub*: Option[PubSub]
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc newNoPubSubException(): ref Exception {.inline.} =
|
2019-09-11 19:03:30 +00:00
|
|
|
result = newException(NoPubSubException, "no pubsub provided!")
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
|
2019-09-06 07:13:47 +00:00
|
|
|
## secure the incoming connection
|
2019-09-04 22:00:39 +00:00
|
|
|
|
2019-09-13 18:41:48 +00:00
|
|
|
let managers = toSeq(s.secureManagers.keys)
|
2019-09-09 23:17:45 +00:00
|
|
|
if managers.len == 0:
|
|
|
|
raise newException(CatchableError, "No secure managers registered!")
|
|
|
|
|
2019-09-13 18:41:48 +00:00
|
|
|
let manager = await s.ms.select(conn, toSeq(s.secureManagers.values).mapIt(it.codec))
|
|
|
|
if manager.len == 0:
|
2019-09-06 07:13:47 +00:00
|
|
|
raise newException(CatchableError, "Unable to negotiate a secure channel!")
|
2019-09-09 23:17:45 +00:00
|
|
|
|
2019-09-13 18:41:48 +00:00
|
|
|
result = await s.secureManagers[manager].secure(conn)
|
2019-08-31 17:58:49 +00:00
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
proc identify(s: Switch, conn: Connection): Future[PeerInfo] {.async, gcsafe.} =
|
2019-09-04 01:50:17 +00:00
|
|
|
## identify the connection
|
2019-09-28 19:54:32 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if not isNil(conn.peerInfo):
|
|
|
|
result = conn.peerInfo
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
try:
|
|
|
|
if (await s.ms.select(conn, s.identity.codec)):
|
|
|
|
let info = await s.identity.identify(conn, conn.peerInfo)
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if info.pubKey.isNone and isNil(result):
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"no public key provided and no existing peer identity found")
|
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
if info.pubKey.isSome:
|
2019-12-07 16:36:39 +00:00
|
|
|
result = PeerInfo.init(info.pubKey.get())
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "identify: identified remote peer", peer = result.id
|
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
if info.addrs.len > 0:
|
|
|
|
result.addrs = info.addrs
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
if info.protos.len > 0:
|
|
|
|
result.protocols = info.protos
|
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
except IdentityInvalidMsgError as exc:
|
2019-09-23 21:51:28 +00:00
|
|
|
error "identify: invalid message", msg = exc.msg
|
2019-09-06 07:13:47 +00:00
|
|
|
except IdentityNoMatchError as exc:
|
2019-09-23 21:51:28 +00:00
|
|
|
error "identify: peer's public keys don't match ", msg = exc.msg
|
2019-09-06 07:13:47 +00:00
|
|
|
|
2019-09-08 07:43:33 +00:00
|
|
|
proc mux(s: Switch, conn: Connection): Future[void] {.async, gcsafe.} =
|
2019-09-04 01:50:17 +00:00
|
|
|
## mux incoming connection
|
2019-09-28 19:54:32 +00:00
|
|
|
|
|
|
|
trace "muxing connection"
|
2019-09-04 22:00:39 +00:00
|
|
|
let muxers = toSeq(s.muxers.keys)
|
2019-09-13 20:04:46 +00:00
|
|
|
if muxers.len == 0:
|
2019-09-23 21:51:28 +00:00
|
|
|
warn "no muxers registered, skipping upgrade flow"
|
2019-09-13 20:04:46 +00:00
|
|
|
return
|
|
|
|
|
2019-09-04 22:00:39 +00:00
|
|
|
let muxerName = await s.ms.select(conn, muxers)
|
2019-09-06 07:13:47 +00:00
|
|
|
if muxerName.len == 0 or muxerName == "na":
|
2019-09-04 22:00:39 +00:00
|
|
|
return
|
|
|
|
|
2019-09-14 15:55:58 +00:00
|
|
|
# create new muxer for connection
|
2019-09-04 22:00:39 +00:00
|
|
|
let muxer = s.muxers[muxerName].newMuxer(conn)
|
|
|
|
# install stream handler
|
2019-09-06 07:13:47 +00:00
|
|
|
muxer.streamHandler = s.streamHandler
|
2019-09-04 22:00:39 +00:00
|
|
|
|
2019-09-14 15:55:58 +00:00
|
|
|
# new stream for identify
|
2019-12-04 04:44:54 +00:00
|
|
|
var stream = await muxer.newStream()
|
2019-09-08 07:43:33 +00:00
|
|
|
let handlerFut = muxer.handle()
|
|
|
|
|
|
|
|
# add muxer handler cleanup proc
|
2019-12-08 21:06:58 +00:00
|
|
|
handlerFut.addCallback do (udata: pointer = nil):
|
|
|
|
trace "muxer handler completed for peer",
|
2019-12-10 20:50:35 +00:00
|
|
|
peer = conn.peerInfo.id
|
2019-09-14 15:55:58 +00:00
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
# do identify first, so that we have a
|
2019-09-14 15:55:58 +00:00
|
|
|
# PeerInfo in case we didn't before
|
2019-12-10 20:50:35 +00:00
|
|
|
conn.peerInfo = await s.identify(stream)
|
2019-12-04 04:44:54 +00:00
|
|
|
|
|
|
|
await stream.close() # close identify stream
|
|
|
|
|
2020-02-04 14:17:24 +00:00
|
|
|
trace "connection's peerInfo", peerInfo = $conn.peerInfo
|
2019-09-14 15:55:58 +00:00
|
|
|
|
2019-09-05 15:19:39 +00:00
|
|
|
# store it in muxed connections if we have a peer for it
|
2019-12-10 20:50:35 +00:00
|
|
|
if not isNil(conn.peerInfo):
|
|
|
|
trace "adding muxer for peer", peer = conn.peerInfo.id
|
|
|
|
s.muxed[conn.peerInfo.id] = muxer
|
2019-09-04 22:00:39 +00:00
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
proc cleanupConn(s: Switch, conn: Connection) {.async, gcsafe.} =
|
2019-12-10 20:50:35 +00:00
|
|
|
if not isNil(conn.peerInfo):
|
|
|
|
let id = conn.peerInfo.id
|
|
|
|
trace "cleaning up connection for peer", peerId = id
|
|
|
|
if id in s.muxed:
|
|
|
|
await s.muxed[id].close()
|
|
|
|
s.muxed.del(id)
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if id in s.connections:
|
2019-12-13 15:25:07 +00:00
|
|
|
if not s.connections[id].closed:
|
|
|
|
await s.connections[id].close()
|
2019-12-10 20:50:35 +00:00
|
|
|
s.connections.del(id)
|
2019-09-28 19:54:32 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc disconnect*(s: Switch, peer: PeerInfo) {.async, gcsafe.} =
|
|
|
|
let conn = s.connections.getOrDefault(peer.id)
|
2019-12-10 20:50:35 +00:00
|
|
|
if not isNil(conn):
|
2019-12-08 21:06:58 +00:00
|
|
|
await s.cleanupConn(conn)
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc getMuxedStream(s: Switch, peerInfo: PeerInfo): Future[Connection] {.async, gcsafe.} =
|
2019-09-28 19:54:32 +00:00
|
|
|
# if there is a muxer for the connection
|
|
|
|
# use it instead to create a muxed stream
|
2019-12-04 04:44:54 +00:00
|
|
|
if peerInfo.id in s.muxed:
|
|
|
|
trace "connection is muxed, setting up a stream"
|
|
|
|
let muxer = s.muxed[peerInfo.id]
|
2019-09-28 19:54:32 +00:00
|
|
|
let conn = await muxer.newStream()
|
2019-12-10 20:50:35 +00:00
|
|
|
result = conn
|
2019-09-28 19:54:32 +00:00
|
|
|
|
2019-09-12 12:02:25 +00:00
|
|
|
proc upgradeOutgoing(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
|
2020-02-04 14:17:24 +00:00
|
|
|
trace "handling connection", conn = $conn
|
2019-09-06 07:13:47 +00:00
|
|
|
result = conn
|
2019-12-04 04:44:54 +00:00
|
|
|
|
|
|
|
# don't mux/secure twise
|
2019-12-10 20:50:35 +00:00
|
|
|
if conn.peerInfo.id in s.muxed:
|
2019-12-04 04:44:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
result = await s.secure(result) # secure the connection
|
2019-12-13 15:25:07 +00:00
|
|
|
if isNil(result):
|
|
|
|
return
|
|
|
|
|
2019-09-08 07:43:33 +00:00
|
|
|
await s.mux(result) # mux it if possible
|
2019-12-10 20:50:35 +00:00
|
|
|
s.connections[conn.peerInfo.id] = result
|
2019-09-04 22:00:39 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc upgradeIncoming(s: Switch, conn: Connection) {.async, gcsafe.} =
|
2019-09-28 19:54:32 +00:00
|
|
|
trace "upgrading incoming connection"
|
|
|
|
let ms = newMultistream()
|
2019-09-04 01:50:17 +00:00
|
|
|
|
2019-09-28 19:54:32 +00:00
|
|
|
# secure incoming connections
|
|
|
|
proc securedHandler (conn: Connection,
|
|
|
|
proto: string)
|
|
|
|
{.async, gcsafe, closure.} =
|
|
|
|
trace "Securing connection"
|
|
|
|
let secure = s.secureManagers[proto]
|
|
|
|
let sconn = await secure.secure(conn)
|
|
|
|
if not isNil(sconn):
|
|
|
|
# add the muxer
|
|
|
|
for muxer in s.muxers.values:
|
|
|
|
ms.addHandler(muxer.codec, muxer)
|
|
|
|
|
|
|
|
# handle subsequent requests
|
|
|
|
await ms.handle(sconn)
|
|
|
|
|
|
|
|
if (await ms.select(conn)): # just handshake
|
|
|
|
# add the secure handlers
|
|
|
|
for k in s.secureManagers.keys:
|
|
|
|
ms.addHandler(k, securedHandler)
|
|
|
|
|
|
|
|
# handle secured connections
|
|
|
|
await ms.handle(conn)
|
2019-09-11 19:03:30 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc dial*(s: Switch,
|
|
|
|
peer: PeerInfo,
|
|
|
|
proto: string = ""):
|
|
|
|
Future[Connection] {.async.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
let id = peer.id
|
2019-12-08 21:06:58 +00:00
|
|
|
trace "Dialing peer", peer = id
|
2019-12-23 18:44:51 +00:00
|
|
|
var conn = s.connections.getOrDefault(id)
|
|
|
|
if conn.isNil or conn.closed:
|
2019-12-08 21:06:58 +00:00
|
|
|
for t in s.transports: # for each transport
|
|
|
|
for a in peer.addrs: # for each address
|
2019-12-23 18:44:51 +00:00
|
|
|
if t.handles(a): # check if it can dial it
|
2019-12-08 21:06:58 +00:00
|
|
|
trace "Dialing address", address = $a
|
2019-12-23 18:44:51 +00:00
|
|
|
conn = await t.dial(a)
|
2019-12-04 04:44:54 +00:00
|
|
|
# make sure to assign the peer to the connection
|
2019-12-23 18:44:51 +00:00
|
|
|
conn.peerInfo = peer
|
|
|
|
conn = await s.upgradeOutgoing(conn)
|
|
|
|
if isNil(conn):
|
2019-12-13 15:25:07 +00:00
|
|
|
continue
|
|
|
|
|
2019-12-23 18:44:51 +00:00
|
|
|
conn.closeEvent.wait()
|
2019-12-13 15:25:07 +00:00
|
|
|
.addCallback do (udata: pointer):
|
2019-12-23 18:44:51 +00:00
|
|
|
asyncCheck s.cleanupConn(conn)
|
2019-12-09 17:17:29 +00:00
|
|
|
break
|
2019-12-08 21:06:58 +00:00
|
|
|
else:
|
|
|
|
trace "Reusing existing connection"
|
|
|
|
|
2019-12-23 18:44:51 +00:00
|
|
|
if isNil(conn):
|
|
|
|
raise newException(CatchableError, "Unable to establish outgoing link")
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-12-23 18:44:51 +00:00
|
|
|
if proto.len > 0 and not conn.closed:
|
2019-12-08 21:06:58 +00:00
|
|
|
let stream = await s.getMuxedStream(peer)
|
2019-12-10 20:50:35 +00:00
|
|
|
if not isNil(stream):
|
2019-12-08 21:06:58 +00:00
|
|
|
trace "Connection is muxed, return muxed stream"
|
2019-12-10 20:50:35 +00:00
|
|
|
result = stream
|
2019-12-08 21:06:58 +00:00
|
|
|
trace "Attempting to select remote", proto = proto
|
|
|
|
|
|
|
|
if not await s.ms.select(result, proto):
|
|
|
|
error "Unable to select sub-protocol", proto = proto
|
|
|
|
raise newException(CatchableError, &"unable to select protocol: {proto}")
|
|
|
|
|
|
|
|
proc mount*[T: LPProtocol](s: Switch, proto: T) {.gcsafe.} =
|
2019-08-31 17:58:49 +00:00
|
|
|
if isNil(proto.handler):
|
2019-12-04 04:44:54 +00:00
|
|
|
raise newException(CatchableError,
|
2019-08-31 17:58:49 +00:00
|
|
|
"Protocol has to define a handle method or proc")
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
if proto.codec.len == 0:
|
2019-12-04 04:44:54 +00:00
|
|
|
raise newException(CatchableError,
|
2019-08-31 18:52:56 +00:00
|
|
|
"Protocol has to define a codec string")
|
|
|
|
|
2019-08-31 17:58:49 +00:00
|
|
|
s.ms.addHandler(proto.codec, proto)
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc start*(s: Switch): Future[seq[Future[void]]] {.async, gcsafe.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "starting switch"
|
|
|
|
|
2019-08-31 18:52:56 +00:00
|
|
|
proc handle(conn: Connection): Future[void] {.async, closure, gcsafe.} =
|
2019-09-04 22:00:39 +00:00
|
|
|
try:
|
2019-09-12 12:02:25 +00:00
|
|
|
await s.upgradeIncoming(conn) # perform upgrade on incoming connection
|
2019-12-04 04:44:54 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
trace "exception occured", exc = exc.msg
|
2019-09-27 13:24:59 +00:00
|
|
|
finally:
|
2019-12-13 15:25:07 +00:00
|
|
|
if not isNil(conn) and not conn.closed:
|
|
|
|
await conn.close()
|
|
|
|
|
2019-09-04 22:00:39 +00:00
|
|
|
await s.cleanupConn(conn)
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-09-12 00:15:04 +00:00
|
|
|
var startFuts: seq[Future[void]]
|
2019-08-28 02:30:53 +00:00
|
|
|
for t in s.transports: # for each transport
|
2019-09-28 19:54:32 +00:00
|
|
|
for i, a in s.peerInfo.addrs:
|
2019-08-28 02:30:53 +00:00
|
|
|
if t.handles(a): # check if it handles the multiaddr
|
2019-09-12 00:15:04 +00:00
|
|
|
var server = await t.listen(a, handle)
|
2019-09-28 19:54:32 +00:00
|
|
|
s.peerInfo.addrs[i] = t.ma # update peer's address
|
2019-09-12 00:15:04 +00:00
|
|
|
startFuts.add(server)
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if s.pubSub.isSome:
|
|
|
|
await s.pubSub.get().start()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-09-12 00:15:04 +00:00
|
|
|
result = startFuts # listen for incoming connections
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc stop*(s: Switch) {.async.} =
|
2019-12-04 04:44:54 +00:00
|
|
|
trace "stopping switch"
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
if s.pubSub.isSome:
|
|
|
|
await s.pubSub.get().stop()
|
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
await allFutures(toSeq(s.connections.values).mapIt(s.cleanupConn(it)))
|
2019-08-31 17:58:49 +00:00
|
|
|
await allFutures(s.transports.mapIt(it.close()))
|
2019-09-06 07:13:47 +00:00
|
|
|
|
2019-09-11 19:03:30 +00:00
|
|
|
proc subscribeToPeer*(s: Switch, peerInfo: PeerInfo) {.async, gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
## Subscribe to pub sub peer
|
2019-09-11 19:03:30 +00:00
|
|
|
if s.pubSub.isSome:
|
|
|
|
let conn = await s.dial(peerInfo, s.pubSub.get().codec)
|
|
|
|
await s.pubSub.get().subscribeToPeer(conn)
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc subscribe*(s: Switch, topic: string, handler: TopicHandler): Future[void] {.gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
## subscribe to a pubsub topic
|
2019-09-11 19:03:30 +00:00
|
|
|
if s.pubSub.isNone:
|
|
|
|
raise newNoPubSubException()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-09-11 19:03:30 +00:00
|
|
|
result = s.pubSub.get().subscribe(topic, handler)
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc unsubscribe*(s: Switch, topics: seq[TopicPair]): Future[void] {.gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
## unsubscribe from topics
|
2019-09-11 19:03:30 +00:00
|
|
|
if s.pubSub.isNone:
|
|
|
|
raise newNoPubSubException()
|
2019-12-04 04:44:54 +00:00
|
|
|
|
2019-09-11 19:03:30 +00:00
|
|
|
result = s.pubSub.get().unsubscribe(topics)
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc publish*(s: Switch, topic: string, data: seq[byte]): Future[void] {.gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
# pubslish to pubsub topic
|
2019-09-11 19:03:30 +00:00
|
|
|
if s.pubSub.isNone:
|
|
|
|
raise newNoPubSubException()
|
|
|
|
|
|
|
|
result = s.pubSub.get().publish(topic, data)
|
|
|
|
|
2019-12-17 05:24:03 +00:00
|
|
|
proc addValidator*(s: Switch,
|
|
|
|
topics: varargs[string],
|
|
|
|
hook: ValidatorHandler) =
|
|
|
|
# add validator
|
|
|
|
if s.pubSub.isNone:
|
|
|
|
raise newNoPubSubException()
|
|
|
|
|
|
|
|
s.pubSub.get().addValidator(topics, hook)
|
|
|
|
|
|
|
|
proc removeValidator*(s: Switch,
|
|
|
|
topics: varargs[string],
|
|
|
|
hook: ValidatorHandler) =
|
|
|
|
# pubslish to pubsub topic
|
|
|
|
if s.pubSub.isNone:
|
|
|
|
raise newNoPubSubException()
|
|
|
|
|
|
|
|
s.pubSub.get().removeValidator(topics, hook)
|
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
proc newSwitch*(peerInfo: PeerInfo,
|
|
|
|
transports: seq[Transport],
|
|
|
|
identity: Identify,
|
2019-09-09 17:33:32 +00:00
|
|
|
muxers: Table[string, MuxerProvider],
|
2019-09-13 20:04:46 +00:00
|
|
|
secureManagers: Table[string, Secure] = initTable[string, Secure](),
|
2019-09-11 19:03:30 +00:00
|
|
|
pubSub: Option[PubSub] = none(PubSub)): Switch =
|
2019-09-06 07:13:47 +00:00
|
|
|
new result
|
|
|
|
result.peerInfo = peerInfo
|
|
|
|
result.ms = newMultistream()
|
|
|
|
result.transports = transports
|
2019-09-13 18:41:48 +00:00
|
|
|
result.connections = initTable[string, Connection]()
|
|
|
|
result.muxed = initTable[string, Muxer]()
|
2019-09-06 07:13:47 +00:00
|
|
|
result.identity = identity
|
|
|
|
result.muxers = muxers
|
2019-09-13 18:41:48 +00:00
|
|
|
result.secureManagers = initTable[string, Secure]()
|
2019-09-06 07:13:47 +00:00
|
|
|
|
|
|
|
let s = result # can't capture result
|
2019-12-08 21:06:58 +00:00
|
|
|
result.streamHandler = proc(stream: Connection) {.async, gcsafe.} =
|
2020-02-04 14:17:24 +00:00
|
|
|
trace "handling connection for", peerInfo = $stream.peerInfo
|
2019-09-06 07:13:47 +00:00
|
|
|
await s.ms.handle(stream) # handle incoming connection
|
|
|
|
|
|
|
|
result.mount(identity)
|
|
|
|
for key, val in muxers:
|
|
|
|
val.streamHandler = result.streamHandler
|
2019-09-12 02:10:38 +00:00
|
|
|
val.muxerHandler = proc(muxer: Muxer) {.async, gcsafe.} =
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "got new muxer"
|
2019-09-12 02:10:38 +00:00
|
|
|
let stream = await muxer.newStream()
|
2019-12-10 20:50:35 +00:00
|
|
|
muxer.connection.peerInfo = await s.identify(stream)
|
2019-09-28 19:54:32 +00:00
|
|
|
await stream.close()
|
2019-09-12 02:10:38 +00:00
|
|
|
|
2019-09-13 18:41:48 +00:00
|
|
|
for k in secureManagers.keys:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "adding secure manager ", codec = secureManagers[k].codec
|
2019-09-13 18:41:48 +00:00
|
|
|
result.secureManagers[k] = secureManagers[k]
|
2019-09-09 17:33:32 +00:00
|
|
|
|
|
|
|
if result.secureManagers.len == 0:
|
|
|
|
# use plain text if no secure managers are provided
|
2019-09-23 21:51:28 +00:00
|
|
|
warn "no secure managers, falling back to palin text", codec = PlainTextCodec
|
2019-09-13 18:41:48 +00:00
|
|
|
result.secureManagers[PlainTextCodec] = Secure(newPlainText())
|
2019-09-11 19:03:30 +00:00
|
|
|
|
|
|
|
if pubSub.isSome:
|
|
|
|
result.pubSub = pubSub
|
|
|
|
result.mount(pubSub.get())
|