Files
bypass/.gitea/workflows/convert_surge_to_gogost.py

62 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import re
def convert_surge_to_gogost(input_path, output_path):
with open(input_path, 'r') as infile, open(output_path, 'w') as outfile:
for line in infile:
line = line.strip()
if not line or line.startswith("#"):
# 跳过空行和注释
continue
# DOMAIN 规则 -> 直接匹配域名
if line.startswith("DOMAIN,"):
domain = line.split(",", 1)[1]
outfile.write(f"{domain}\n")
# DOMAIN-SUFFIX 规则 -> 匹配子域名,转为 *.domain.com 格式
elif line.startswith("DOMAIN-SUFFIX,"):
domain = line.split(",", 1)[1]
outfile.write(f"*.{domain}\n")
# DOMAIN-KEYWORD 规则 -> 转为 *keyword* 格式,匹配包含关键字的域名
elif line.startswith("DOMAIN-KEYWORD,"):
keyword = line.split(",", 1)[1]
outfile.write(f"*{keyword}*\n")
# DOMAIN-SET 规则 -> 保持文件路径GoGost 支持 . 开头表示子域名匹配
elif line.startswith("DOMAIN-SET,"):
file_path = line.split(",", 1)[1]
with open(file_path, 'r') as domain_set_file:
for domain in domain_set_file:
domain = domain.strip()
if domain.startswith('.'):
outfile.write(f"*{domain}\n") # 子域名匹配
else:
outfile.write(f"{domain}\n")
# IP-CIDR 规则 -> 直接输出
elif line.startswith("IP-CIDR,") or line.startswith("IP-CIDR6,"):
ip_range = line.split(",", 1)[1]
outfile.write(f"{ip_range}\n")
# GEOIP 规则 -> 直接输出
elif line.startswith("GEOIP,"):
country_code = line.split(",", 1)[1]
outfile.write(f"{country_code}\n")
# 其他规则保持原样输出
else:
outfile.write(f"# 未识别规则: {line}\n")
# 执行转换并指定文件路径
input_dir = "surge" # 替换为你的 Surge 规则文件夹路径
output_dir = "gogost" # 替换为转换后文件的输出路径
os.makedirs(output_dir, exist_ok=True)
# 逐个转换指定目录中的 Surge 规则文件
for filename in os.listdir(input_dir):
if filename.endswith(".list"): # 假设规则文件以 .txt 结尾
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
convert_surge_to_gogost(input_path, output_path)