From 9142ab5e6dbf865c55c691a6ed6d5434bc602088 Mon Sep 17 00:00:00 2001 From: yuanzhen869 Date: Sat, 9 Nov 2024 07:12:06 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20.gitea/workflows/convert?= =?UTF-8?q?=5Fsurge=5Fto=5Fgogost.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/convert_surge_to_gogost.py | 62 +++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 .gitea/workflows/convert_surge_to_gogost.py diff --git a/.gitea/workflows/convert_surge_to_gogost.py b/.gitea/workflows/convert_surge_to_gogost.py new file mode 100644 index 0000000..8801a18 --- /dev/null +++ b/.gitea/workflows/convert_surge_to_gogost.py @@ -0,0 +1,62 @@ +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(".txt"): # 假设规则文件以 .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) \ No newline at end of file