node_get-info.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/python
  2. # This file is part of DarkFi (https://dark.fi)
  3. #
  4. # Copyright (C) 2020-2025 Dyne.org foundation
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import asyncio, json, random, sys, time
  19. class JsonRpc:
  20. async def start(self, server, port):
  21. reader, writer = await asyncio.open_connection(server, port)
  22. self.reader = reader
  23. self.writer = writer
  24. async def stop(self):
  25. self.writer.close()
  26. await self.writer.wait_closed()
  27. async def _make_request(self, method, params):
  28. ident = random.randint(0, 2**16)
  29. #print(ident)
  30. request = {
  31. "jsonrpc": "2.0",
  32. "method": method,
  33. "params": params,
  34. "id": ident,
  35. }
  36. message = json.dumps(request) + "\n"
  37. self.writer.write(message.encode())
  38. await self.writer.drain()
  39. data = await self.reader.readline()
  40. message = data.decode().strip()
  41. response = json.loads(message)
  42. #print(response)
  43. return response
  44. async def _subscribe(self, method, params):
  45. ident = random.randint(0, 2**16)
  46. request = {
  47. "jsonrpc": "2.0",
  48. "method": method,
  49. "params": params,
  50. "id": ident,
  51. }
  52. message = json.dumps(request) + "\n"
  53. self.writer.write(message.encode())
  54. await self.writer.drain()
  55. #print("Subscribed")
  56. async def ping(self):
  57. return await self._make_request("ping", [])
  58. async def dnet_switch(self, state):
  59. return await self._make_request("dnet.switch", [state])
  60. async def dnet_subscribe_events(self):
  61. return await self._subscribe("dnet.subscribe_events", [])
  62. async def main(argv):
  63. rpc = JsonRpc()
  64. while True:
  65. try:
  66. await rpc.start("localhost", 26660)
  67. break
  68. except OSError:
  69. pass
  70. response = await rpc._make_request("p2p.get_info", [])
  71. info = response["result"]
  72. channels = info["channels"]
  73. channel_lookup = {}
  74. for channel in channels:
  75. id = channel["id"]
  76. channel_lookup[id] = channel
  77. print("inbound:")
  78. for channel in channels:
  79. if channel["session"] != "inbound":
  80. continue
  81. url = channel["url"]
  82. print(f" {url}")
  83. print("outbound:")
  84. for i, id in enumerate(info["outbound_slots"]):
  85. if id == 0:
  86. print(f" {i}: none")
  87. continue
  88. assert id in channel_lookup
  89. url = channel_lookup[id]["url"]
  90. print(f" {i}: {url}")
  91. print("seed:")
  92. for channel in channels:
  93. if channel["session"] != "seed":
  94. continue
  95. url = channel["url"]
  96. print(f" {url}")
  97. print("manual:")
  98. for channel in channels:
  99. if channel["session"] != "manual":
  100. continue
  101. url = channel["url"]
  102. print(f" {url}")
  103. await rpc.stop()
  104. asyncio.run(main(sys.argv))