Files
vpn-rules/scripts/gen_router_paste.py
T

41 lines
1.6 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.
#!/usr/bin/env python3
"""从 direct.yaml(真源)生成路由器「手动粘贴版」规则文件 router/direct-rules.yaml。
适用于 Magic Catling2 等无法联网拉取 rule-provider 的场景:
把生成的规则行粘贴进现有 rules 列表最前面即可,无需 URL 拉取。
"""
from pathlib import Path
from gen_clash_verge_script import load_payload, to_rule
REPO_ROOT = Path(__file__).resolve().parent.parent
OUT = REPO_ROOT / "router" / "direct-rules.yaml"
TEMPLATE = """# vpn-rules 手动粘贴版(无需联网拉取)— 由 direct.yaml 生成,勿手改。
#
# 用法(Magic Catling2 等拉不到 rule-provider 时):
# 1. 打开路由器的规则设置;
# 2. 把下面 `rules:` 里的每一行 `- 规则` 粘贴到【现有规则列表的最前面】;
# 3. 只粘贴规则行,不要用整个文件替换你原来的 rules(否则订阅自带规则会丢)。
#
# 重新生成:python3 scripts/gen_router_paste.py(改真源 direct.yaml 后重跑)。
# 规则与 direct.yaml 逐条对应;对应拉取版 URL:
# https://gitea.puyuanfh.cn/ageorge156/vpn-rules/raw/branch/main/direct.yaml
# 内网源:http://192.168.50.2:3000/ageorge156/vpn-rules/raw/branch/main/direct.yaml
rules:
{rules}
"""
def main() -> None:
payload = load_payload(REPO_ROOT / "direct.yaml")
lines = "\n".join(f" - {to_rule(e)}" for e in payload)
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_text(TEMPLATE.format(rules=lines), encoding="utf-8")
print(f"✅ 已生成 {OUT}({len(payload)} 条规则,可直接粘贴)")
if __name__ == "__main__":
main()