2019-08-27 21:46:12 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2018 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.
|
|
|
|
|
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,
|
|
|
|
stream/lpstream,
|
|
|
|
multistream,
|
2019-09-06 07:13:47 +00:00
|
|
|
protocols/protocol,
|
2019-09-09 17:33:32 +00:00
|
|
|
protocols/secure/secure, # for plain text
|
2019-09-11 19:03:30 +00:00
|
|
|
peerinfo,
|
2019-09-06 07:13:47 +00:00
|
|
|
multiaddress,
|
2019-09-11 19:03:30 +00:00
|
|
|
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-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
|
|
|
|
connections*: TableRef[string, Connection]
|
2019-09-04 22:00:39 +00:00
|
|
|
muxed*: TableRef[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-09 17:33:32 +00:00
|
|
|
secureManagers*: seq[Secure]
|
2019-09-11 19:03:30 +00:00
|
|
|
pubSub*: Option[PubSub]
|
|
|
|
|
|
|
|
proc newNoPubSubException(): ref Exception {.inline.} =
|
|
|
|
result = newException(NoPubSubException, "no pubsub provided!")
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
proc secure(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
|
|
|
|
## secure the incoming connection
|
2019-09-04 22:00:39 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
# plaintext for now, doesn't do anything
|
2019-09-09 23:17:45 +00:00
|
|
|
let managers = s.secureManagers.mapIt(it.codec).deduplicate()
|
|
|
|
if managers.len == 0:
|
|
|
|
raise newException(CatchableError, "No secure managers registered!")
|
|
|
|
|
2019-09-09 17:33:32 +00:00
|
|
|
if (await s.ms.select(conn, s.secureManagers.mapIt(it.codec))).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
|
|
|
|
|
|
|
var n = await s.secureManagers[0].secure(conn)
|
2019-09-06 07:13:47 +00:00
|
|
|
result = conn
|
2019-08-31 17:58:49 +00:00
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
proc identify*(s: Switch, conn: Connection) {.async, gcsafe.} =
|
2019-09-04 01:50:17 +00:00
|
|
|
## identify the connection
|
2019-09-06 07:13:47 +00:00
|
|
|
# s.peerInfo.protocols = await s.ms.list(conn) # update protos before engagin in identify
|
|
|
|
try:
|
|
|
|
if (await s.ms.select(conn, s.identity.codec)):
|
|
|
|
let info = await s.identity.identify(conn, conn.peerInfo)
|
|
|
|
|
2019-09-11 19:03:30 +00:00
|
|
|
let id = if conn.peerInfo.peerId.isSome:
|
|
|
|
conn.peerInfo.peerId.get().pretty
|
|
|
|
else:
|
2019-09-06 07:13:47 +00:00
|
|
|
""
|
2019-09-11 19:03:30 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
if id.len > 0 and s.connections.contains(id):
|
|
|
|
let connection = s.connections[id]
|
2019-09-11 19:03:30 +00:00
|
|
|
var peerInfo = conn.peerInfo
|
|
|
|
|
|
|
|
if info.pubKey.isSome:
|
|
|
|
peerInfo.peerId = some(PeerID.init(info.pubKey.get())) # we might not have a peerId at all
|
|
|
|
|
|
|
|
if info.addrs.len > 0:
|
|
|
|
peerInfo.addrs = info.addrs
|
|
|
|
|
|
|
|
if info.protos.len > 0:
|
|
|
|
peerInfo.protocols = info.protos
|
|
|
|
|
|
|
|
debug "identify: identified remote peer ", peer = peerInfo.peerId.get().pretty
|
2019-09-06 07:13:47 +00:00
|
|
|
except IdentityInvalidMsgError as exc:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "identify: invalid message", msg = exc.msg
|
2019-09-06 07:13:47 +00:00
|
|
|
except IdentityNoMatchError as exc:
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "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-04 22:00:39 +00:00
|
|
|
let muxers = toSeq(s.muxers.keys)
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# do identify first, so that we have a
|
|
|
|
# PeerInfo in case we didn't before
|
2019-09-08 07:43:33 +00:00
|
|
|
let stream = await muxer.newStream()
|
|
|
|
let handlerFut = muxer.handle()
|
|
|
|
|
|
|
|
# add muxer handler cleanup proc
|
|
|
|
handlerFut.addCallback(
|
|
|
|
proc(udata: pointer = nil) {.gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
debug "mux: Muxer handler completed for peer ",
|
|
|
|
peer = conn.peerInfo.peerId.get().pretty
|
2019-09-08 07:43:33 +00:00
|
|
|
)
|
|
|
|
await s.identify(stream)
|
|
|
|
await stream.close() # close idenity stream
|
2019-09-08 06:34:08 +00:00
|
|
|
|
2019-09-05 15:19:39 +00:00
|
|
|
# store it in muxed connections if we have a peer for it
|
|
|
|
# TODO: We should make sure that this are cleaned up properly
|
|
|
|
# on exit even if there is no peer for it. This shouldn't
|
|
|
|
# happen once secio is in place, but still something to keep
|
|
|
|
# in mind
|
2019-09-11 19:03:30 +00:00
|
|
|
if conn.peerInfo.peerId.isSome:
|
|
|
|
s.muxed[conn.peerInfo.peerId.get().pretty] = muxer
|
2019-09-04 22:00:39 +00:00
|
|
|
|
|
|
|
proc handleConn(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
|
2019-09-06 07:13:47 +00:00
|
|
|
result = conn
|
2019-09-04 01:50:17 +00:00
|
|
|
## perform upgrade flow
|
2019-09-11 19:03:30 +00:00
|
|
|
if result.peerInfo.peerId.isSome:
|
|
|
|
let id = result.peerInfo.peerId.get().pretty
|
2019-09-04 22:00:39 +00:00
|
|
|
if s.connections.contains(id):
|
|
|
|
# if we already have a connection for this peer,
|
|
|
|
# close the incoming connection and return the
|
|
|
|
# existing one
|
2019-09-06 07:13:47 +00:00
|
|
|
await result.close()
|
2019-09-04 22:00:39 +00:00
|
|
|
return s.connections[id]
|
2019-09-06 07:13:47 +00:00
|
|
|
s.connections[id] = result
|
2019-09-04 22:00:39 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
result = await s.secure(conn) # secure the connection
|
2019-09-08 07:43:33 +00:00
|
|
|
await s.mux(result) # mux it if possible
|
2019-09-04 22:00:39 +00:00
|
|
|
|
|
|
|
proc cleanupConn(s: Switch, conn: Connection) {.async, gcsafe.} =
|
2019-09-11 19:03:30 +00:00
|
|
|
if conn.peerInfo.peerId.isSome:
|
|
|
|
let id = conn.peerInfo.peerId.get().pretty
|
2019-09-04 22:00:39 +00:00
|
|
|
if s.muxed.contains(id):
|
|
|
|
await s.muxed[id].close
|
|
|
|
|
|
|
|
if s.connections.contains(id):
|
|
|
|
await s.connections[id].close()
|
2019-09-04 01:50:17 +00:00
|
|
|
|
2019-09-11 19:03:30 +00:00
|
|
|
proc getMuxedStream(s: Switch, peerInfo: PeerInfo): Future[Option[Connection]] {.async, gcsafe.} =
|
|
|
|
# if there is a muxer for the connection
|
|
|
|
# use it instead to create a muxed stream
|
|
|
|
if s.muxed.contains(peerInfo.peerId.get().pretty):
|
|
|
|
let muxer = s.muxed[peerInfo.peerId.get().pretty]
|
|
|
|
let conn = await muxer.newStream()
|
|
|
|
result = some(conn)
|
|
|
|
|
2019-09-05 15:19:39 +00:00
|
|
|
proc dial*(s: Switch,
|
|
|
|
peer: PeerInfo,
|
|
|
|
proto: string = ""):
|
|
|
|
Future[Connection] {.async.} =
|
2019-08-31 17:58:49 +00:00
|
|
|
for t in s.transports: # for each transport
|
|
|
|
for a in peer.addrs: # for each address
|
|
|
|
if t.handles(a): # check if it can dial it
|
2019-09-06 07:13:47 +00:00
|
|
|
result = await t.dial(a)
|
2019-09-11 19:03:30 +00:00
|
|
|
# make sure to assign the peer to the connection
|
|
|
|
result.peerInfo = peer
|
2019-09-06 07:13:47 +00:00
|
|
|
result = await s.handleConn(result)
|
2019-09-08 07:43:33 +00:00
|
|
|
|
2019-09-11 19:03:30 +00:00
|
|
|
let stream = await s.getMuxedStream(peer)
|
|
|
|
if stream.isSome:
|
|
|
|
result = stream.get()
|
2019-09-08 07:43:33 +00:00
|
|
|
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "dial: attempting to select remote ", proto = proto
|
2019-09-08 06:34:08 +00:00
|
|
|
if not (await s.ms.select(result, proto)):
|
2019-09-09 17:33:32 +00:00
|
|
|
debug "dial: Unable to select protocol: ", proto = proto
|
2019-08-31 18:52:56 +00:00
|
|
|
raise newException(CatchableError,
|
2019-09-04 22:00:39 +00:00
|
|
|
&"Unable to select protocol: {proto}")
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
proc mount*[T: LPProtocol](s: Switch, proto: T) {.gcsafe.} =
|
2019-08-31 17:58:49 +00:00
|
|
|
if isNil(proto.handler):
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"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-08-31 18:52:56 +00:00
|
|
|
raise newException(CatchableError,
|
|
|
|
"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-09-12 00:15:04 +00:00
|
|
|
proc start*(s: Switch): Future[seq[Future[void]]] {.async.} =
|
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 00:15:04 +00:00
|
|
|
if (await s.ms.select(conn)): # just handshake
|
2019-09-06 07:13:47 +00:00
|
|
|
await s.ms.handle(conn) # handle incoming connection
|
2019-09-04 22:00:39 +00:00
|
|
|
except:
|
|
|
|
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
|
|
|
|
for a in s.peerInfo.addrs:
|
|
|
|
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)
|
|
|
|
startFuts.add(server)
|
|
|
|
result = startFuts # listen for incoming connections
|
2019-08-27 21:46:12 +00:00
|
|
|
|
2019-08-28 02:30:53 +00:00
|
|
|
proc stop*(s: Switch) {.async.} =
|
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)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
result = s.pubSub.get().subscribe(topic, handler)
|
|
|
|
|
|
|
|
proc unsubscribe*(s: Switch, topics: seq[string]): 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()
|
|
|
|
|
|
|
|
result = s.pubSub.get().unsubscribe(topics)
|
|
|
|
|
|
|
|
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-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-11 19:03:30 +00:00
|
|
|
secureManagers: seq[Secure] = @[],
|
|
|
|
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
|
|
|
|
result.connections = newTable[string, Connection]()
|
|
|
|
result.muxed = newTable[string, Muxer]()
|
|
|
|
result.identity = identity
|
|
|
|
result.muxers = muxers
|
|
|
|
|
|
|
|
let s = result # can't capture result
|
|
|
|
result.streamHandler = proc(stream: Connection) {.async, gcsafe.} =
|
2019-09-12 02:10:38 +00:00
|
|
|
debug "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.} =
|
|
|
|
debug "got new muxer"
|
|
|
|
let stream = await muxer.newStream()
|
|
|
|
await s.identify(stream)
|
|
|
|
|
2019-09-06 07:13:47 +00:00
|
|
|
result.mount(val)
|
|
|
|
|
2019-09-12 02:10:38 +00:00
|
|
|
for s in secureManagers.deduplicate():
|
2019-09-09 23:17:45 +00:00
|
|
|
debug "adding secure manager ", codec = s.codec
|
2019-09-09 17:33:32 +00:00
|
|
|
result.secureManagers.add(s)
|
|
|
|
result.mount(s)
|
|
|
|
|
|
|
|
if result.secureManagers.len == 0:
|
|
|
|
# use plain text if no secure managers are provided
|
2019-09-09 23:17:45 +00:00
|
|
|
let manager = Secure(newPlainText())
|
|
|
|
result.mount(manager)
|
|
|
|
result.secureManagers.add(manager)
|
|
|
|
|
|
|
|
result.secureManagers = result.secureManagers.deduplicate()
|
2019-09-11 19:03:30 +00:00
|
|
|
|
|
|
|
if pubSub.isSome:
|
|
|
|
result.pubSub = pubSub
|
|
|
|
result.mount(pubSub.get())
|