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