扩展下载地址:https://github.com/shadowsocks/shadowsocks-rust/releases
需要下载的版本:64-pc-windows-msvc
import requests
import re
import json
import subprocess
import time
import os
import psutil # 用于查找和终止进程
# 配置
url = "机场地址"
sslocal_path = "D:\\Shadowsocks\\sslocal.exe"
local_ports = list(range(23661, 23668)) # 23661 - 23667 端口范围
# 正则表达式匹配代理信息
pattern = r'- {name: (🇭🇰 香港Y0[1-8][^\n]*), server: ([^,]+), port: (\d+), type: ss, cipher: ([^,]+), password: ([^,]+)'
# 用于存储已启动的 Shadowsocks 进程
sslocal_processes = []
def kill_existing_sslocal():
"""终止之前启动的 Shadowsocks-Rust 进程。"""
global sslocal_processes
for process in sslocal_processes:
try:
process.terminate()
process.wait()
except Exception as e:
print(f"终止 Shadowsocks-Rust 进程时出错:{e}")
sslocal_processes = []
def update_proxies():
try:
# 请求配置文件
response = requests.get(url)
if response.status_code != 200:
print(f"请求失败,状态码:{response.status_code}")
return
content = response.text
matches = re.findall(pattern, content)
# 解析代理信息
proxies = []
for match, port in zip(matches, local_ports):
name, server, server_port, cipher, password = match
proxies.append({
"server": server,
"server_port": int(server_port),
"password": password,
"method": cipher,
"local_address": "127.0.0.1",
"local_port": port
})
# 终止之前启动的 Shadowsocks-Rust 进程
kill_existing_sslocal()
# 启动新的 Shadowsocks-Rust 进程
print("正在启动/重启 Shadowsocks-Rust...")
for proxy in proxies:
config = {
"server": proxy["server"],
"server_port": proxy["server_port"],
"password": proxy["password"],
"method": proxy["method"],
"local_address": proxy["local_address"],
"local_port": proxy["local_port"]
}
# 将每个代理的配置写入文件
config_path = f'ss-rust-config-{proxy["local_port"]}.json'
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
# 启动 Shadowsocks-Rust 进程
process = subprocess.Popen([sslocal_path, "-c", config_path])
sslocal_processes.append(process)
except Exception as e:
print(f"更新代理配置时出错:{e}")
if __name__ == "__main__":
while True:
update_proxies()
# 每隔 10 分钟更新一次
time.sleep(600)