node_get_info.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env 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 get_info(self):
  63. return await self._make_request("p2p.get_info", [])
  64. async def main(argv):
  65. rpc = JsonRpc()
  66. while True:
  67. try:
  68. await rpc.start("localhost", 26660)
  69. break
  70. except OSError:
  71. pass
  72. response = await rpc._make_request("p2p.get_info", [])
  73. if "error" in response:
  74. print("Error: ", response["error"])
  75. await rpc.stop()
  76. return
  77. info = response["result"]
  78. channels = info["channels"]
  79. channel_lookup = {}
  80. for channel in channels:
  81. id = channel["id"]
  82. channel_lookup[id] = channel
  83. print("inbound:")
  84. for channel in channels:
  85. if channel["session"] != "inbound":
  86. continue
  87. url = channel["url"]
  88. print(f" {url}")
  89. print("outbound:")
  90. for i, id in enumerate(info["outbound_slots"]):
  91. if id == 0:
  92. print(f" {i}: none")
  93. continue
  94. assert id in channel_lookup
  95. url = channel_lookup[id]["url"]
  96. print(f" {i}: {url}")
  97. print("seed:")
  98. for channel in channels:
  99. if channel["session"] != "seed":
  100. continue
  101. url = channel["url"]
  102. print(f" {url}")
  103. print("manual:")
  104. for channel in channels:
  105. if channel["session"] != "manual":
  106. continue
  107. url = channel["url"]
  108. print(f" {url}")
  109. await rpc.stop()
  110. if __name__ == "__main__":
  111. asyncio.run(main(sys.argv))