nodetool.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. await rpc.dnet_switch(True)
  71. await rpc.dnet_subscribe_events()
  72. while True:
  73. data = await rpc.reader.readline()
  74. #with open("rpclog", "a") as f:
  75. # f.write(data.decode())
  76. data = json.loads(data)
  77. params = data["params"][0]
  78. ev = params["event"]
  79. if ev in ["send", "recv"]:
  80. continue
  81. info = params["info"]
  82. t = time.localtime()
  83. current_time = time.strftime("%H:%M:%S", t)
  84. match ev:
  85. case "inbound_connected":
  86. addr = info["addr"]
  87. print(f"{current_time} inbound (connect): {addr}")
  88. case "inbound_disconnected":
  89. addr = info["addr"]
  90. print(f"{current_time} inbound (disconnect): {addr}")
  91. case "outbound_slot_sleeping":
  92. slot = info["slot"]
  93. print(f"{current_time} slot {slot}: sleeping")
  94. case "outbound_slot_connecting":
  95. slot = info["slot"]
  96. addr = info["addr"]
  97. print(f"{current_time} slot {slot}: connecting addr={addr}")
  98. case "outbound_slot_connected":
  99. slot = info["slot"]
  100. addr = info["addr"]
  101. channel_id = info["channel_id"]
  102. print(f"{current_time} slot {slot}: connected addr={addr}")
  103. case "outbound_slot_disconnected":
  104. slot = info["slot"]
  105. err = info["err"]
  106. print(f"{current_time} slot {slot}: disconnected err='{err}'")
  107. case "outbound_peer_discovery":
  108. attempt = info["attempt"]
  109. state = info["state"]
  110. print(f"{current_time} peer_discovery: {state} (attempt {attempt})")
  111. #print(data)
  112. await rpc.dnet_switch(False)
  113. await rpc.stop()
  114. asyncio.run(main(sys.argv))