Files
bypass/.gitea/workflows/convert_surge_to_gogost.py
yuanzhen869 65953f7b84
Some checks failed
SurgeToGoGostConverter / convert (push) Failing after 4s
更新 .gitea/workflows/convert_surge_to_gogost.py
2024-11-09 07:40:18 +00:00

72 lines
2.9 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
# 输出 surge 目录中的文件列表
print("转换前文件列表:")
for filename in os.listdir(input_dir):
file_path = os.path.join(input_dir, filename)
print(f"文件: {filename}")
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))