53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Read a subscription URL from stdin and emit a JSON array of backend outbounds."""
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
|
|
from proxy_parsing import decode_subscription, fetch_raw, parse_hybrid_subscription, parse_subscription
|
|
|
|
|
|
def main() -> None:
|
|
ap = argparse.ArgumentParser(
|
|
description="Fetch a proxy subscription URL from stdin and emit a backend outbound JSON array."
|
|
)
|
|
ap.add_argument(
|
|
"--tag-prefix",
|
|
required=True,
|
|
dest="tag_prefix",
|
|
help="Prefix for outbound tags, e.g. 'my-sub'.",
|
|
)
|
|
ap.add_argument("--backend", choices=["sing-box", "xray", "hybrid"], default="sing-box")
|
|
ap.add_argument("--routing-mark", type=int, default=None, dest="routing_mark")
|
|
args = ap.parse_args()
|
|
|
|
url = sys.stdin.read().strip()
|
|
if not url:
|
|
print("error: no URL provided on stdin", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
try:
|
|
raw = fetch_raw(url)
|
|
except Exception as exc:
|
|
print(f"error: failed to fetch subscription: {exc}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
lines = decode_subscription(raw)
|
|
if args.backend == "hybrid":
|
|
outbounds = parse_hybrid_subscription(lines, args.tag_prefix, args.routing_mark)
|
|
has_outbounds = bool(outbounds["singBox"] or outbounds["xray"])
|
|
else:
|
|
outbounds = parse_subscription(lines, args.tag_prefix, args.routing_mark, args.backend)
|
|
has_outbounds = bool(outbounds)
|
|
|
|
if not has_outbounds:
|
|
print("error: subscription contained no parseable proxy URIs", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
print(json.dumps(outbounds))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|