All checks were successful
SurgeToGoGostConverter / convert (push) Successful in 4s
66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
import os
|
||
import re
|
||
|
||
def convert_surge_to_gogost(input_path, output_path):
|
||
print(f"正在转换: {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"):
|
||
input_path = os.path.join(input_dir, filename)
|
||
output_path = os.path.join(output_dir, filename.replace(".list", ".txt"))
|
||
convert_surge_to_gogost(input_path, output_path)
|
||
|
||
# 列出目标目录中的文件,验证输出是否成功
|
||
print("转换完成。目标目录中的文件列表:")
|
||
print(os.listdir(output_dir)) |