From 62ba9150c83e40b6bd50760bc2d2d39d444c2871 Mon Sep 17 00:00:00 2001 From: itdoginfo Date: Mon, 27 Jan 2025 01:19:22 +0300 Subject: [PATCH] Added script for srs files --- Dockerfile | 15 ++++++++++++++ compile-srs.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Dockerfile create mode 100755 compile-srs.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..252ba41 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM ghcr.io/sagernet/sing-box:v1.10.7 AS sing-box + +FROM python:3.10.16-alpine3.21 + +COPY --from=sing-box /usr/local/bin/sing-box /bin/sing-box + +WORKDIR /app + +COPY compile-srs.py /app/compile-srs.py + +VOLUME ["/app/Categories", "/app/Services"] + +CMD ["python3", "compile-srs.py"] + +# docker run --rm -v ./Categories:/app/Categories -v ./Services:/app/Services -v ./json:/app/json py-sg:1 \ No newline at end of file diff --git a/compile-srs.py b/compile-srs.py new file mode 100755 index 0000000..b6227c1 --- /dev/null +++ b/compile-srs.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3.10 + +import json +import os +import subprocess + +directories = ['Categories', 'Services'] + +output_directory = 'JSON' +os.makedirs(output_directory, exist_ok=True) +compiled_output_directory = 'SRS' +os.makedirs(compiled_output_directory, exist_ok=True) + +for directory in directories: + for filename in os.listdir(directory): + file_path = os.path.join(directory, filename) + + if os.path.isfile(file_path): + domains = [] + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + domain = line.strip() + if domain: + domains.append(domain) + + data = { + "version": 2, + "rules": [ + { + "domain_suffix": domains + } + ] + } + + output_file_path = os.path.join(output_directory, f"{os.path.splitext(filename)[0]}.json") + + with open(output_file_path, 'w', encoding='utf-8') as output_file: + json.dump(data, output_file, indent=4) + + print(f"JSON file generated: {output_file_path}") + +print("\nCompile JSON files to .srs files...") +for filename in os.listdir(output_directory): + if filename.endswith('.json'): + json_file_path = os.path.join(output_directory, filename) + srs_file_path = os.path.join(compiled_output_directory, f"{os.path.splitext(filename)[0]}.srs") + try: + subprocess.run( + ["sing-box", "rule-set", "compile", json_file_path, "-o", srs_file_path], check=True + ) + print(f"Compiled .srs file: {srs_file_path}") + except subprocess.CalledProcessError as e: + print(f"Compile error {json_file_path}: {e}") \ No newline at end of file