switch_random_proxy_web3.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # -*- coding: utf-8 -*-
  2. '''
  3. 切换到随机代理
  4. '''
  5. import random
  6. from urllib.parse import quote
  7. import httpx
  8. import time
  9. import subprocess
  10. from typing import List
  11. BASE_URL = "http://192.168.31.194"
  12. PORT_LIST = ['58001', '58002', '58003', '58004', '58005', '58006', '58007', '58008', '58009', '58010']
  13. PROXY_GROUP = ['香港', 'HK', '新加坡', 'SG', '台湾', 'TW', '日本', 'JP', '韩国', '澳门']
  14. GROUP = 1
  15. class ClashProxyManager:
  16. def __init__(self, base_url, base_port):
  17. self.key_group = 0
  18. self.base_url = base_url
  19. self.base_port = base_port
  20. self.all_proxies = []
  21. self.used_proxy = []
  22. def get_all_proxies(self, clash_tool_url: str) -> List[str]:
  23. # 连接其中一个代理服务器, 获取所有代理节点, 因为每个代理服务器的所有节点都是一样的, 所以获取一个就行了
  24. url = f"{clash_tool_url}/api/proxies"
  25. try:
  26. response = httpx.get(url)
  27. response.raise_for_status()
  28. proxies = response.json()
  29. # 输出读取的所有代理信息
  30. # for proxy_name, proxy_info in proxies['proxies'].items():
  31. # logging.info(f"Name: {proxy_name}, Type: {proxy_info.get('type', 'Unknown')}")
  32. proxy_list = list(proxies['proxies'].keys())
  33. result_data = []
  34. if GROUP == 1:
  35. for filtered in proxy_list:
  36. for group in PROXY_GROUP:
  37. if group.lower() in filtered.lower() and filtered not in {'REJECT', 'GLOBAL', 'DIRECT'}:
  38. result_data.append(filtered)
  39. elif GROUP == 2:
  40. for filtered in proxy_list:
  41. if all(group.lower() not in filtered.lower() for group in PROXY_GROUP) and filtered not in {
  42. 'REJECT', 'GLOBAL', 'DIRECT'}:
  43. result_data.append(filtered)
  44. else:
  45. print(f'选择代理组为 {GROUP}')
  46. exit(1)
  47. print(f'读取所有代理节点, 一共 {len(result_data)} 个节点')
  48. return result_data
  49. except Exception as e:
  50. print(f"Failed to get proxies: {e}")
  51. return []
  52. def switch_proxy(self, proxy_name: str, url_and_port: str) -> None:
  53. # 根据节点名称和代理服务器url, 切换到指定的代理节点
  54. url = f"{url_and_port}/api/proxies/GLOBAL"
  55. data = {"name": proxy_name}
  56. try:
  57. response = httpx.put(url, json=data)
  58. if response.status_code == 204:
  59. print(f"Switched to proxy: {proxy_name}")
  60. else:
  61. print(f"Failed to switch proxy: {response.status_code} - {proxy_name}")
  62. except Exception as e:
  63. print(f"Failed to switch proxy: {e}")
  64. def update_configs(self):
  65. for base_port in self.base_port:
  66. url_and_port = self.base_url + ":" + base_port
  67. key = "/api/configs"
  68. url = url_and_port + key
  69. # 构造 curl 命令
  70. curl_cmd = [
  71. "curl",
  72. "-X", "PATCH",
  73. url,
  74. "-H", "Content-Type: application/json",
  75. "-d", '{"mode": "Global"}'
  76. ]
  77. try:
  78. # 使用 subprocess 运行 curl 命令
  79. result = subprocess.run(curl_cmd, capture_output=True, text=True, check=True)
  80. # 检查 curl 命令的返回状态
  81. if result.returncode != 0:
  82. print(f"请求失败,状态码: {result.returncode}")
  83. print("错误信息:", result.stderr.strip())
  84. except subprocess.CalledProcessError as exc:
  85. print(f"请求失败: {exc}")
  86. print("错误信息:", exc.stderr.strip())
  87. def check_proxy(self, proxy_url, choose_proxy):
  88. encode_proxy_name = quote(choose_proxy, safe="")
  89. command = [
  90. "curl",
  91. "-X", "GET",
  92. f"{proxy_url}/api/proxies/{encode_proxy_name}/delay?timeout=5000&url=http:%2F%2Fwww.gstatic.com%2Fgenerate_204"
  93. ]
  94. try:
  95. result = subprocess.run(command, capture_output=True, text=True, check=True)
  96. print("Output:", result.stdout)
  97. if 'Timeout' in result.stdout:
  98. return False
  99. return True
  100. except subprocess.CalledProcessError as e:
  101. print("Error:", e.stderr)
  102. return False
  103. def main(self) -> None:
  104. self.update_configs()
  105. if not self.all_proxies:
  106. for port_list in self.base_port:
  107. base_url = self.base_url + ":" + port_list
  108. clash_tool_url = f"{base_url}"
  109. self.all_proxies = self.get_all_proxies(clash_tool_url)
  110. if self.all_proxies:
  111. break
  112. if not self.all_proxies:
  113. return
  114. else:
  115. print(f'待切换节点一共 {len(self.all_proxies)} 个')
  116. for base_port in self.base_port:
  117. url_and_port = self.base_url + ":" + base_port
  118. while True:
  119. choose_proxy = random.choice(self.all_proxies)
  120. if choose_proxy in self.used_proxy:
  121. if len(self.used_proxy) >= len(self.all_proxies):
  122. print('=' * 88)
  123. print(f'所有节点已尝试过, 退出')
  124. print('=' * 88)
  125. return
  126. continue
  127. self.switch_proxy(choose_proxy, url_and_port)
  128. self.used_proxy.append(choose_proxy)
  129. if self.check_proxy(url_and_port, choose_proxy):
  130. print(f"代理 {choose_proxy} 切换成功,检测通过!")
  131. print('=' * 88)
  132. break
  133. else:
  134. print(f"{url_and_port} 切换 xxxxxxxx {choose_proxy} xxxxxxxx 检测失败")
  135. time.sleep(1)
  136. if __name__ == "__main__":
  137. manager = ClashProxyManager(BASE_URL, PORT_LIST)
  138. manager.main()