#!/usr/bin/env python3 import argparse import json import re import sys from pathlib import Path BUILTIN_ACTIVATION_FALLBACKS = { "list-instagram.txt": "general", "list-soundcloud.txt": "general", "list-twitter.txt": "general", } def _hostlist_remove_pattern(stem: str) -> str: escaped = re.escape(stem) return rf'\s+--hostlist=(?:"[^"]*/hostlists/{escaped}\.txt"|[^ ]*/hostlists/{escaped}\.txt)' def _mk_family(match_stems: list[str], remove_stems: list[str]) -> dict: return { "match": [f"list-{s}.txt" for s in match_stems], "remove": [_hostlist_remove_pattern(f"list-{s}") for s in remove_stems], } FAMILY_PATTERNS = { "general": _mk_family(["general", "general-user"], ["general", "general-user"]), "google": _mk_family(["google"], ["google"]), "instagram": _mk_family(["instagram"], ["instagram"]), "soundcloud": _mk_family(["soundcloud"], ["soundcloud"]), "twitter": _mk_family(["twitter"], ["twitter"]), } def locate_nfqws_block(lines: list[str]) -> tuple[int, int]: start = None for index, line in enumerate(lines): if line.startswith('NFQWS_OPT="'): start = index break if start is None: raise ValueError("Could not find NFQWS_OPT block in zapret config") for index in range(start + 1, len(lines)): if lines[index] == '"': return start, index raise ValueError("Could not find the end of the NFQWS_OPT block in zapret config") def strip_trailing_new(line: str) -> str: return re.sub(r"(?:\s+--new)+\s*$", "", line.strip()) def normalize_spaces(line: str) -> str: return re.sub(r"\s+", " ", line).strip() def add_standard_excludes(line: str, standard_excludes: list[str]) -> str: for arg in standard_excludes: if arg not in line: line = f"{line} {arg}" return line def _render_args( line: str, hostlist_path: str, standard_excludes: list[str], *, preset_family: str | None = None, ) -> str: rendered = strip_trailing_new(line) if preset_family is not None: for pattern in FAMILY_PATTERNS[preset_family]["remove"]: rendered = re.sub(pattern, "", rendered) else: rendered = re.sub(r'\s+--hostlist=(?:"[^"]+"|\S+)', "", rendered) rendered = normalize_spaces(rendered) rendered = f'{rendered} --hostlist="{hostlist_path}"' rendered = add_standard_excludes(rendered, standard_excludes) return f"{normalize_spaces(rendered)} --new" def render_preset_clone( line: str, family: str, hostlist_path: str, standard_excludes: list[str], ) -> str: return _render_args(line, hostlist_path, standard_excludes, preset_family=family) def render_custom_args( fragment: str, hostlist_path: str, standard_excludes: list[str], ) -> str: return _render_args(fragment, hostlist_path, standard_excludes) def clone_family_lines( existing_lines: list[str], family: str, hostlist_path: str, standard_excludes: list[str], ) -> list[str]: matches = [ line for line in existing_lines if any(marker in line for marker in FAMILY_PATTERNS[family]["match"]) ] if not matches: raise ValueError( f"Preset family '{family}' is not present in the selected zapret config" ) return [ render_preset_clone(line, family, hostlist_path, standard_excludes) for line in matches ] def activate_builtin_hostlists( existing_lines: list[str], hostlists_dir: Path, standard_excludes: list[str], ) -> list[str]: generated_lines: list[str] = [] full_text = "\n".join(existing_lines) for filename, family in BUILTIN_ACTIVATION_FALLBACKS.items(): marker = str(hostlists_dir / filename) if marker in full_text: continue generated_lines.extend( clone_family_lines(existing_lines, family, marker, standard_excludes) ) return generated_lines def main() -> int: parser = argparse.ArgumentParser( description="Append proxy-suite custom hostlist rules to a zapret config." ) parser.add_argument( "--config", required=True, help="Path to the selected zapret config file" ) parser.add_argument( "--spec", required=True, help="JSON file describing custom hostlist rules" ) args = parser.parse_args() config_path = Path(args.config) spec_path = Path(args.spec) hostlists_dir = config_path.parent / "hostlists" standard_excludes = [ f'--hostlist-exclude="{hostlists_dir / "list-exclude.txt"}"', f'--hostlist-exclude="{hostlists_dir / "list-exclude-user.txt"}"', f'--ipset-exclude="{hostlists_dir / "ipset-exclude.txt"}"', f'--ipset-exclude="{hostlists_dir / "ipset-exclude-user.txt"}"', ] lines = config_path.read_text(encoding="utf-8").splitlines() spec = json.loads(spec_path.read_text(encoding="utf-8")) entries = spec.get("entries", []) include_extra_upstream_lists = spec.get("includeExtraUpstreamLists", False) start, end = locate_nfqws_block(lines) nfqws_lines = lines[start + 1 : end] generated_lines: list[str] = ( activate_builtin_hostlists(nfqws_lines, hostlists_dir, standard_excludes) if include_extra_upstream_lists else [] ) for entry in entries: hostlist_path = str(hostlists_dir / f'list-{entry["name"]}.txt') preset = entry.get("preset") if preset: generated_lines.extend( clone_family_lines( nfqws_lines, preset, hostlist_path, standard_excludes ) ) for fragment in entry.get("nfqwsArgs", []): generated_lines.append( render_custom_args(fragment, hostlist_path, standard_excludes) ) updated_lines = lines[:end] + generated_lines + lines[end:] config_path.write_text("\n".join(updated_lines) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": try: raise SystemExit(main()) except ValueError as exc: print(f"error: {exc}", file=sys.stderr) raise SystemExit(1)