Create getTagProxyInfoToOpenwrtFormat.py
parent
ce768a47e6
commit
5f370a4090
|
|
@ -0,0 +1,118 @@
|
|||
import base64
|
||||
import urllib
|
||||
import requests
|
||||
|
||||
|
||||
# Base64 解码函数
|
||||
def base64_decode(encoded_str):
|
||||
padded_str = encoded_str + '=' * (-len(encoded_str) % 4)
|
||||
decoded_bytes = base64.b64decode(padded_str.encode('utf-8'))
|
||||
return decoded_bytes.decode('utf-8')
|
||||
|
||||
|
||||
# 获取订阅内容
|
||||
def get_subscription(url):
|
||||
response = requests.get(url)
|
||||
if response.status_code == 200:
|
||||
return response.text
|
||||
else:
|
||||
raise Exception(f"无法获取订阅数据: HTTP 状态码 {response.status_code}")
|
||||
|
||||
|
||||
# 解析订阅内容
|
||||
def parse_subscription(subscription):
|
||||
decoded_data = base64_decode(subscription)
|
||||
proxy_list = decoded_data.split('\n')
|
||||
return [proxy for proxy in proxy_list if proxy]
|
||||
|
||||
|
||||
# 过滤香港、新加坡和美国节点
|
||||
def filter_proxies_by_region(ss_url_list):
|
||||
filtered_proxies = []
|
||||
for ss_url in ss_url_list:
|
||||
if any(region in ss_url for region in ['香港', '新加坡', '美国']):
|
||||
filtered_proxies.append(ss_url)
|
||||
return filtered_proxies
|
||||
|
||||
|
||||
# 解析 SS 链接,提取信息
|
||||
def parse_ss_url(ss_url):
|
||||
base_str = ss_url[5:] # 移除 'ss://'
|
||||
encryption_and_password = base64_decode(base_str.split("@")[0])
|
||||
encryption_method, password = encryption_and_password.split(":")
|
||||
|
||||
server_info = base_str.split("@")[1].split("/?")[0]
|
||||
server, port = server_info.split(":")
|
||||
|
||||
plugin_str = base_str.split("/?")[1].split("#")[0]
|
||||
plugin_params = {}
|
||||
if "plugin" in plugin_str:
|
||||
plugin_parts = plugin_str.split(";")
|
||||
for part in plugin_parts:
|
||||
if "=" in part:
|
||||
key, value = part.split("=")
|
||||
plugin_params[key] = value
|
||||
|
||||
alias = urllib.parse.unquote(base_str.split("#")[1].rstrip("\r"))
|
||||
|
||||
return {
|
||||
"alias": alias,
|
||||
"server": server,
|
||||
"server_port": port,
|
||||
"password": password,
|
||||
"encryption_method": encryption_method,
|
||||
"plugin": plugin_params.get('plugin', ''),
|
||||
"plugin_opts": f"obfs={plugin_params.get('obfs', '')};obfs-host={plugin_params.get('obfs-host', '')}"
|
||||
}
|
||||
|
||||
|
||||
# 生成配置文件
|
||||
def generate_config(proxy_info, local_port=1234):
|
||||
config = f"""
|
||||
option switch_enable '0'
|
||||
option type 'ss'
|
||||
option alias '{proxy_info['alias']}'
|
||||
option server '{proxy_info['server']}'
|
||||
option server_port '{proxy_info['server_port']}'
|
||||
option password '{proxy_info['password']}'
|
||||
option encrypt_method_ss '{proxy_info['encryption_method']}'
|
||||
option plugin '{proxy_info['plugin']}'
|
||||
option plugin_opts '{proxy_info['plugin_opts']}'
|
||||
option local_port '{local_port}'
|
||||
"""
|
||||
return config.strip()
|
||||
|
||||
|
||||
# 保存配置到文件
|
||||
def save_to_file(configs, file_name):
|
||||
with open(file_name, 'w', encoding='utf-8') as file:
|
||||
for config in configs:
|
||||
file.write(config + '\n\n')
|
||||
print(f"配置已保存到 {file_name}")
|
||||
|
||||
|
||||
# 主函数
|
||||
def convert_subscription_to_config(subscription_url):
|
||||
subscription_data = get_subscription(subscription_url)
|
||||
ss_url_list = parse_subscription(subscription_data)
|
||||
|
||||
# 过滤出符合要求的节点
|
||||
filtered_proxies = filter_proxies_by_region(ss_url_list)
|
||||
|
||||
# 解析并生成配置
|
||||
configs = []
|
||||
for proxy in filtered_proxies:
|
||||
proxy_info = parse_ss_url(proxy)
|
||||
config = generate_config(proxy_info)
|
||||
configs.append(config)
|
||||
|
||||
# 保存配置到文件
|
||||
save_to_file(configs, 'config_servers.conf')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 订阅地址
|
||||
subscription_url = "https://link.nobodys.uk/api/v1/client/subscribe?token=26d553424da6d84dccb20dd05dc844c0"
|
||||
|
||||
# 转换并保存配置
|
||||
convert_subscription_to_config(subscription_url)
|
||||
Loading…
Reference in New Issue