lilith_spawns.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. # This file is part of DarkFi (https://dark.fi)
  3. #
  4. # Copyright (C) 2020-2026 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 spawns(self):
  59. return await self._make_request("spawns", [])
  60. async def main(argv):
  61. rpc = JsonRpc()
  62. while True:
  63. try:
  64. await rpc.start("localhost", 18927)
  65. break
  66. except OSError:
  67. pass
  68. response = await rpc._make_request("spawns", [])
  69. info = response["result"]
  70. spawns = info["spawns"]
  71. for spawn in spawns:
  72. urls = spawn["urls"]
  73. name = spawn["name"]
  74. whitelist = spawn["whitelist"]
  75. greylist = spawn["greylist"]
  76. anchorlist = spawn["anchorlist"]
  77. print(f"\nname: {name}")
  78. print(f"urls:")
  79. for url in urls:
  80. print(f" {url}")
  81. if whitelist:
  82. print(f"whitelist:")
  83. for host in whitelist:
  84. print(f" {host}")
  85. if greylist:
  86. print(f"greylist:")
  87. for host in greylist:
  88. print(f" {host}")
  89. if anchorlist:
  90. print(f"anchorlist:")
  91. for host in anchorlist:
  92. print(f" {host}")
  93. await rpc.stop()
  94. asyncio.run(main(sys.argv))