check_all_proxy.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import asyncio
  2. import aiohttp
  3. from urllib.parse import quote
  4. async def check(url, session):
  5. for retry in range(3):
  6. try:
  7. async with session.get(url) as response:
  8. if response.status == 200:
  9. print(f"Success: {url}")
  10. return True
  11. else:
  12. print(f"Failed: {url} (Status: {response.status})")
  13. except Exception as e:
  14. print(f"Error: {url} - {str(e)}")
  15. return False
  16. async def prepare_check(node_url, ports, proxy_name_list):
  17. async with aiohttp.ClientSession() as session:
  18. tasks = []
  19. for port in ports:
  20. proxy_url = f"{node_url}:{port}"
  21. for proxy in proxy_name_list:
  22. url = f"{proxy_url}/api/proxies/{proxy}/delay?timeout=5000&url=http:%2F%2Fwww.gstatic.com%2Fgenerate_204"
  23. tasks.append(check(url, session))
  24. await asyncio.gather(*tasks)
  25. async def load_nodes_details():
  26. tasks = []
  27. async with aiohttp.ClientSession() as session:
  28. for node_url, ports in nodes.items():
  29. clash_tool_url = f"{node_url}:{ports[0]}"
  30. url = f"{clash_tool_url}/api/proxies"
  31. try:
  32. async with session.get(url) as response:
  33. if response.status == 200:
  34. proxies = await response.json()
  35. proxy_name_list = list(proxies['proxies'].keys())
  36. tasks.append(prepare_check(node_url, ports, proxy_name_list))
  37. else:
  38. print(f"Failed to load proxies from {url} (Status: {response.status})")
  39. except Exception as e:
  40. print(f"Error: {url} - {str(e)}")
  41. await asyncio.gather(*tasks)
  42. async def main():
  43. await load_nodes_details()
  44. if __name__ == "__main__":
  45. nodes = {
  46. "http://192.168.31.194": ['58001', '58002', '58003', '58004', '58005', '58006', '58007', '58008', '58009',
  47. '58010'],
  48. "http://192.168.31.201": ['32001', '32002', '32003', '32004', '32005', '32006', '32007', '32008', '32009',
  49. '32010', '32011', '32012']
  50. }
  51. asyncio.run(main())
  52. print("All done!")