|
@@ -0,0 +1,83 @@
|
|
|
|
|
+import httpx
|
|
|
|
|
+import time
|
|
|
|
|
+import json
|
|
|
|
|
+import uuid
|
|
|
|
|
+import logging
|
|
|
|
|
+import websockets
|
|
|
|
|
+from typing import Optional
|
|
|
|
|
+from websockets import WebSocketCommonProtocol
|
|
|
|
|
+
|
|
|
|
|
+# 配置日志
|
|
|
|
|
+logging.basicConfig(level=logging.INFO)
|
|
|
|
|
+
|
|
|
|
|
+# Clash API 的基础 URL
|
|
|
|
|
+CLASH_API_BASE_URL = "http://localhost:17888/api"
|
|
|
|
|
+
|
|
|
|
|
+# 服务的基础 URL 和端口
|
|
|
|
|
+SERVER_HOSTNAME = "proxy.wynd.network"
|
|
|
|
|
+SERVER_PORT = 4444
|
|
|
|
|
+SERVER_URL = f"wss://{SERVER_HOSTNAME}:{SERVER_PORT}/"
|
|
|
|
|
+TEST_USER_ID = '2pcRp86m1r76nSzdtGy1DNpXjrF'
|
|
|
|
|
+PROXY_URL = "http://127.0.0.1:17890" # 代理地址
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def get_all_proxies():
|
|
|
|
|
+ """获取Clash中所有可用的代理"""
|
|
|
|
|
+ url = f"{CLASH_API_BASE_URL}/proxies"
|
|
|
|
|
+ try:
|
|
|
|
|
+ response = httpx.get(url)
|
|
|
|
|
+ response.raise_for_status() # 检查请求是否成功
|
|
|
|
|
+ proxies = response.json()
|
|
|
|
|
+ logging.info("Available proxies:")
|
|
|
|
|
+ for proxy_name, proxy_info in proxies['proxies'].items():
|
|
|
|
|
+ logging.info(f"Name: {proxy_name}, Type: {proxy_info.get('type', 'Unknown')}")
|
|
|
|
|
+ return list(proxies['proxies'].keys())
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logging.error(f"Failed to get proxies: {e}")
|
|
|
|
|
+ return []
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def switch_proxy(proxy_name):
|
|
|
|
|
+ """切换到指定的代理"""
|
|
|
|
|
+ url = f"{CLASH_API_BASE_URL}/proxies/GLOBAL"
|
|
|
|
|
+ data = {"name": proxy_name}
|
|
|
|
|
+ response = httpx.put(url, json=data)
|
|
|
|
|
+ if response.status_code == 204:
|
|
|
|
|
+ logging.info(f"Switched to proxy: {proxy_name}")
|
|
|
|
|
+ else:
|
|
|
|
|
+ logging.error(f"Failed to switch proxy: {response.status_code} - {proxy_name}")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def test_grassio(proxy_name):
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def main():
|
|
|
|
|
+ # 获取所有代理
|
|
|
|
|
+ proxies = get_all_proxies()
|
|
|
|
|
+ if proxies:
|
|
|
|
|
+ logging.info(f"Found {len(proxies)} proxies.")
|
|
|
|
|
+
|
|
|
|
|
+ ok_proxy_list = []
|
|
|
|
|
+
|
|
|
|
|
+ for proxy_name in proxies:
|
|
|
|
|
+ if proxy_name in ["DIRECT", "REJECT", "GLOBAL"]:
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ # 切换到指定的代理
|
|
|
|
|
+ switch_proxy(proxy_name)
|
|
|
|
|
+ # 暂停一段时间,避免频繁切换
|
|
|
|
|
+ time.sleep(0.5)
|
|
|
|
|
+ # 测试通过代理连接 grassio 服务
|
|
|
|
|
+ result = test_grassio(proxy_name)
|
|
|
|
|
+ if result:
|
|
|
|
|
+ ok_proxy_list.append(proxy_name)
|
|
|
|
|
+
|
|
|
|
|
+ for proxy_name in ok_proxy_list:
|
|
|
|
|
+ logging.info(f"Proxy {proxy_name} is OK.")
|
|
|
|
|
+ else:
|
|
|
|
|
+ logging.info("No proxies found. Exiting.")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ main()
|