You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.1 KiB
89 lines
3.1 KiB
import json
|
|
import httpx
|
|
|
|
|
|
def load_config():
|
|
with open("config.json", "r") as f:
|
|
return json.load(f)
|
|
|
|
|
|
config = load_config()
|
|
|
|
START = 1
|
|
END = config["total_container"]
|
|
START_PORT = config["start_port"]
|
|
START_API_PORT = config["start_web_port"]
|
|
|
|
base_url_list = [f'http://192.168.31.201:{START_API_PORT + i}' for i in range(START, END + 1)]
|
|
|
|
|
|
class ProxyManager:
|
|
def __init__(self, base_url):
|
|
self.base_url = base_url
|
|
|
|
def switch_to_global(self):
|
|
print(f"{self.base_url}/api/configs 切换全局代理")
|
|
headers = {
|
|
"accept": "application/json, text/plain, */*",
|
|
"accept-encoding": "gzip, deflate, br, zstd",
|
|
"accept-language": "zh-CN,zh",
|
|
"connection": "keep-alive",
|
|
"content-type": "application/json",
|
|
"host": base_url.replace('http://', ''),
|
|
"origin": base_url,
|
|
"referer": base_url,
|
|
"sec-ch-ua": '"Not A(Brand";v="8", "Chromium";v="132", "Brave";v="132"',
|
|
"sec-ch-ua-mobile": "?0",
|
|
"sec-ch-ua-platform": '"macOS"',
|
|
"sec-fetch-dest": "empty",
|
|
"sec-fetch-mode": "cors",
|
|
"sec-fetch-site": "same-origin",
|
|
"sec-gpc": "1",
|
|
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
|
|
}
|
|
data = {"mode": "Global"}
|
|
try:
|
|
with httpx.Client() as client:
|
|
response = client.patch(self.base_url + "/api/configs", json=data, headers=headers)
|
|
if response.status_code == 204:
|
|
print(f"{self.base_url}/api/configs 切换全局代理成功")
|
|
else:
|
|
print(f"{self.base_url}/api/configs 切换全局代理失败 {response.status_code}")
|
|
exit(1)
|
|
except httpx.RequestError as exc:
|
|
print(f"请求失败: {exc}")
|
|
exit(1)
|
|
|
|
def get_proxy_name(self):
|
|
print(f'{self.base_url}/api/proxies 获取代理名称')
|
|
try:
|
|
response = httpx.get(self.base_url + "/api/proxies")
|
|
response.raise_for_status()
|
|
proxies = response.json()
|
|
return proxies['proxies']['GLOBAL']['all'][2]
|
|
except Exception as e:
|
|
print(e)
|
|
exit(1)
|
|
|
|
def switch_proxy(self, proxy_name):
|
|
print(f'{self.base_url}/api/proxies/GLOBAL 切换代理')
|
|
try:
|
|
target_url = self.base_url + '/api/proxies/GLOBAL'
|
|
response = httpx.put(target_url, json={"name": proxy_name})
|
|
if response.status_code == 204:
|
|
print(f"切换代理成功: {proxy_name}")
|
|
else:
|
|
print(f"切换代理失败: {response.status_code} - {proxy_name}")
|
|
exit(1)
|
|
except Exception as e:
|
|
print(f"切换代理失败: {e}")
|
|
exit(1)
|
|
|
|
|
|
for base_url in base_url_list:
|
|
manager = ProxyManager(base_url)
|
|
manager.switch_to_global()
|
|
proxy_name = manager.get_proxy_name()
|
|
if proxy_name:
|
|
manager.switch_proxy(proxy_name)
|
|
print('\n\n')
|
|
|