fix: various fixes
This commit is contained in:
8 files changed
+204
-24
No files matched your search
@@ -5,7 +5,7 @@ import argparse
|
||||
import json
|
||||
import sys
|
||||
|
||||
from proxy_parsing import decode_subscription, fetch_raw, parse_subscription
|
||||
from proxy_parsing import decode_subscription, fetch_raw, parse_hybrid_subscription, parse_subscription
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -18,7 +18,7 @@ def main() -> None:
|
||||
dest="tag_prefix",
|
||||
help="Prefix for outbound tags, e.g. 'my-sub'.",
|
||||
)
|
||||
ap.add_argument("--backend", choices=["sing-box", "xray"], default="sing-box")
|
||||
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()
|
||||
|
||||
@@ -34,9 +34,14 @@ def main() -> None:
|
||||
sys.exit(1)
|
||||
|
||||
lines = decode_subscription(raw)
|
||||
outbounds = parse_subscription(lines, args.tag_prefix, args.routing_mark, args.backend)
|
||||
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 outbounds:
|
||||
if not has_outbounds:
|
||||
print("error: subscription contained no parseable proxy URIs", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
@@ -281,3 +281,48 @@ def parse_subscription(
|
||||
outbounds.append(outbound)
|
||||
|
||||
return outbounds
|
||||
|
||||
|
||||
def parse_hybrid_subscription(
|
||||
lines: list[str], tag_prefix: str, routing_mark: int | None = None
|
||||
) -> dict[str, list[dict]]:
|
||||
outbounds: dict[str, list[dict]] = {"singBox": [], "xray": []}
|
||||
seen_tags: set[str] = set()
|
||||
|
||||
for index, line in enumerate(lines):
|
||||
scheme = detect_scheme(line)
|
||||
if scheme not in PARSERS:
|
||||
continue
|
||||
|
||||
remark = line.split("#", 1)[1] if "#" in line else ""
|
||||
base_tag = make_tag(tag_prefix, remark, index)
|
||||
tag = base_tag
|
||||
|
||||
if tag in seen_tags:
|
||||
suffix = 2
|
||||
while f"{base_tag}-{suffix}" in seen_tags:
|
||||
suffix += 1
|
||||
tag = f"{base_tag}-{suffix}"
|
||||
|
||||
try:
|
||||
outbound = build_outbound(line, tag, routing_mark, "sing-box")
|
||||
outbounds["singBox"].append(outbound)
|
||||
seen_tags.add(tag)
|
||||
continue
|
||||
except Exception as sing_box_exc:
|
||||
sing_box_error = sing_box_exc
|
||||
|
||||
try:
|
||||
outbound = build_outbound(line, tag, routing_mark, "xray")
|
||||
except Exception as xray_exc:
|
||||
print(
|
||||
f"warning: skipping entry {index} ({scheme}): "
|
||||
f"sing-box: {sing_box_error}; xray: {xray_exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
continue
|
||||
|
||||
outbounds["xray"].append(outbound)
|
||||
seen_tags.add(tag)
|
||||
|
||||
return outbounds
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
import base64
|
||||
import unittest
|
||||
from proxy_parsing import decode_subscription, parse_subscription
|
||||
from proxy_parsing import decode_subscription, parse_hybrid_subscription, parse_subscription
|
||||
|
||||
VLESS_URI = "vless://uuid@example.com:443?security=reality&pbk=pubkey&fp=chrome&sni=cdn.example.com&sid=abcd"
|
||||
VLESS_XHTTP_URI = "vless://uuid@example.com:443?type=xhttp&security=tls&sni=cdn.example.com&host=cdn.example.com&path=%2Fx"
|
||||
VLESS_ECH_URI = "vless://uuid@example.com:443?type=tcp&security=tls&sni=cdn.example.com&ech=udp%3A%2F%2F1.1.1.1"
|
||||
SS_URI = "ss://{}@ss.example.com:8388".format(
|
||||
base64.urlsafe_b64encode(b"chacha20-ietf-poly1305:passw0rd").decode().rstrip("=")
|
||||
)
|
||||
@@ -18,10 +20,16 @@ def _make_b64_payload(*uris: str) -> bytes:
|
||||
|
||||
|
||||
def run_fetcher(
|
||||
server_data: bytes, tag_prefix: str = "test", *, routing_mark: int | None = None
|
||||
server_data: bytes,
|
||||
tag_prefix: str = "test",
|
||||
*,
|
||||
routing_mark: int | None = None,
|
||||
backend: str = "sing-box",
|
||||
):
|
||||
lines = decode_subscription(server_data)
|
||||
return parse_subscription(lines, tag_prefix, routing_mark)
|
||||
if backend == "hybrid":
|
||||
return parse_hybrid_subscription(lines, tag_prefix, routing_mark)
|
||||
return parse_subscription(lines, tag_prefix, routing_mark, backend)
|
||||
|
||||
|
||||
class FetchSubscriptionTests(unittest.TestCase):
|
||||
@@ -112,6 +120,23 @@ class FetchSubscriptionTests(unittest.TestCase):
|
||||
obs = run_fetcher(payload)
|
||||
self.assertEqual(len(obs), 2)
|
||||
|
||||
def test_hybrid_subscription_preserves_xray_only_entries(self):
|
||||
payload = _make_b64_payload(VLESS_URI, VLESS_XHTTP_URI, VLESS_ECH_URI)
|
||||
obs = run_fetcher(payload, backend="hybrid")
|
||||
self.assertEqual(len(obs["singBox"]), 1)
|
||||
self.assertEqual(len(obs["xray"]), 2)
|
||||
self.assertEqual(obs["singBox"][0]["type"], "vless")
|
||||
self.assertEqual(obs["xray"][0]["streamSettings"]["network"], "xhttp")
|
||||
self.assertEqual(
|
||||
obs["xray"][1]["streamSettings"]["tlsSettings"]["echConfigList"],
|
||||
"udp://1.1.1.1",
|
||||
)
|
||||
|
||||
def test_hybrid_subscription_all_invalid_returns_empty_backend_lists(self):
|
||||
payload = _make_b64_payload(INVALID_URI, "not-a-uri-at-all")
|
||||
obs = run_fetcher(payload, backend="hybrid")
|
||||
self.assertEqual(obs, {"singBox": [], "xray": []})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user