check_all_proxy.py 2.1 KB

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