2022-03-23 13:20:07 +00:00
|
|
|
{.push raises: [Defect].}
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
|
|
import
|
2021-02-05 10:49:11 +00:00
|
|
|
std/[options, sequtils, sets],
|
2021-06-09 14:37:08 +00:00
|
|
|
chronicles,
|
2020-12-15 15:48:00 +00:00
|
|
|
json_rpc/rpcserver,
|
2022-08-05 15:05:17 +00:00
|
|
|
libp2p/[peerinfo, switch]
|
|
|
|
import
|
|
|
|
../../protocol/waku_store,
|
2022-08-05 14:38:00 +00:00
|
|
|
../../protocol/waku_filter,
|
2022-09-06 19:07:37 +00:00
|
|
|
../../protocol/waku_relay,
|
2022-09-06 18:36:53 +00:00
|
|
|
../../protocol/waku_swap/waku_swap,
|
2021-03-26 08:49:51 +00:00
|
|
|
../peer_manager/peer_manager,
|
2022-10-18 14:05:53 +00:00
|
|
|
../waku_node,
|
2020-12-21 09:14:51 +00:00
|
|
|
./jsonrpc_types
|
2020-12-15 15:48:00 +00:00
|
|
|
|
2020-12-23 09:33:28 +00:00
|
|
|
export jsonrpc_types
|
|
|
|
|
2021-03-23 08:04:51 +00:00
|
|
|
logScope:
|
|
|
|
topics = "admin api"
|
|
|
|
|
2021-03-11 07:05:39 +00:00
|
|
|
const futTimeout* = 30.seconds # Max time to wait for futures
|
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
proc constructMultiaddrStr*(wireaddr: MultiAddress, peerId: PeerId): string =
|
|
|
|
# Constructs a multiaddress with both wire address and p2p identity
|
|
|
|
$wireaddr & "/p2p/" & $peerId
|
|
|
|
|
2020-12-15 15:48:00 +00:00
|
|
|
proc constructMultiaddrStr*(peerInfo: PeerInfo): string =
|
2021-02-05 10:49:11 +00:00
|
|
|
# Constructs a multiaddress with both location (wire) address and p2p identity
|
|
|
|
constructMultiaddrStr(peerInfo.addrs[0], peerInfo.peerId)
|
2020-12-15 15:48:00 +00:00
|
|
|
|
2021-10-06 12:29:08 +00:00
|
|
|
proc constructMultiaddrStr*(remotePeerInfo: RemotePeerInfo): string =
|
|
|
|
# Constructs a multiaddress with both location (wire) address and p2p identity
|
|
|
|
constructMultiaddrStr(remotePeerInfo.addrs[0], remotePeerInfo.peerId)
|
|
|
|
|
2020-12-15 15:48:00 +00:00
|
|
|
proc installAdminApiHandlers*(node: WakuNode, rpcsrv: RpcServer) =
|
|
|
|
|
|
|
|
## Admin API version 1 definitions
|
2021-03-11 07:05:39 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("post_waku_v2_admin_v1_peers") do(peers: seq[string]) -> bool:
|
|
|
|
## Connect to a list of peers
|
|
|
|
debug "post_waku_v2_admin_v1_peers"
|
|
|
|
|
|
|
|
if (await node.connectToNodes(peers).withTimeout(futTimeout)):
|
|
|
|
# Successfully connected to peers
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
# Failed to connect to peers
|
|
|
|
raise newException(ValueError, "Failed to connect to peers: " & $peers)
|
2020-12-15 15:48:00 +00:00
|
|
|
|
|
|
|
rpcsrv.rpc("get_waku_v2_admin_v1_peers") do() -> seq[WakuPeer]:
|
2021-04-08 09:55:19 +00:00
|
|
|
## Returns a list of peers registered for this node
|
2020-12-15 15:48:00 +00:00
|
|
|
debug "get_waku_v2_admin_v1_peers"
|
|
|
|
|
|
|
|
# Create a single list of peers from mounted protocols.
|
|
|
|
# @TODO since the switch does not expose its connections, retrieving the connected peers requires a peer store/peer management
|
|
|
|
|
|
|
|
var wPeers: seq[WakuPeer] = @[]
|
|
|
|
|
2021-02-05 10:49:11 +00:00
|
|
|
## Managed peers
|
|
|
|
|
|
|
|
if not node.wakuRelay.isNil:
|
2021-02-11 08:58:25 +00:00
|
|
|
# Map managed peers to WakuPeers and add to return list
|
|
|
|
wPeers.insert(node.peerManager.peers(WakuRelayCodec)
|
|
|
|
.mapIt(WakuPeer(multiaddr: constructMultiaddrStr(toSeq(it.addrs.items)[0], it.peerId),
|
|
|
|
protocol: WakuRelayCodec,
|
2021-02-12 08:53:52 +00:00
|
|
|
connected: node.peerManager.connectedness(it.peerId) == Connectedness.Connected)),
|
2021-02-11 08:58:25 +00:00
|
|
|
wPeers.len) # Append to the end of the sequence
|
|
|
|
|
|
|
|
if not node.wakuFilter.isNil:
|
|
|
|
# Map WakuFilter peers to WakuPeers and add to return list
|
|
|
|
wPeers.insert(node.peerManager.peers(WakuFilterCodec)
|
|
|
|
.mapIt(WakuPeer(multiaddr: constructMultiaddrStr(toSeq(it.addrs.items)[0], it.peerId),
|
|
|
|
protocol: WakuFilterCodec,
|
2021-02-12 08:53:52 +00:00
|
|
|
connected: node.peerManager.connectedness(it.peerId) == Connectedness.Connected)),
|
2021-02-05 10:49:11 +00:00
|
|
|
wPeers.len) # Append to the end of the sequence
|
|
|
|
|
2020-12-15 15:48:00 +00:00
|
|
|
if not node.wakuSwap.isNil:
|
|
|
|
# Map WakuSwap peers to WakuPeers and add to return list
|
2021-02-11 08:58:25 +00:00
|
|
|
wPeers.insert(node.peerManager.peers(WakuSwapCodec)
|
|
|
|
.mapIt(WakuPeer(multiaddr: constructMultiaddrStr(toSeq(it.addrs.items)[0], it.peerId),
|
|
|
|
protocol: WakuSwapCodec,
|
2021-02-12 08:53:52 +00:00
|
|
|
connected: node.peerManager.connectedness(it.peerId) == Connectedness.Connected)),
|
2020-12-15 15:48:00 +00:00
|
|
|
wPeers.len) # Append to the end of the sequence
|
|
|
|
|
|
|
|
if not node.wakuStore.isNil:
|
|
|
|
# Map WakuStore peers to WakuPeers and add to return list
|
2021-02-11 08:58:25 +00:00
|
|
|
wPeers.insert(node.peerManager.peers(WakuStoreCodec)
|
|
|
|
.mapIt(WakuPeer(multiaddr: constructMultiaddrStr(toSeq(it.addrs.items)[0], it.peerId),
|
|
|
|
protocol: WakuStoreCodec,
|
2021-02-12 08:53:52 +00:00
|
|
|
connected: node.peerManager.connectedness(it.peerId) == Connectedness.Connected)),
|
2020-12-15 15:48:00 +00:00
|
|
|
wPeers.len) # Append to the end of the sequence
|
|
|
|
|
|
|
|
# @TODO filter output on protocol/connected-status
|
|
|
|
return wPeers
|