fix: various fixes
This commit is contained in:
17 files changed
+584
-353
No files matched your search
+23
-22
@@ -6,7 +6,7 @@ import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
from proxy_url_parsers import PARSERS
|
||||
from proxy_url_parsers import BACKEND_AWARE_PARSERS, PARSERS
|
||||
|
||||
|
||||
def detect_scheme(url: str) -> str:
|
||||
@@ -19,10 +19,9 @@ def _parse_url(url: str, tag: str, backend: str) -> dict:
|
||||
raise ValueError(f"unsupported scheme '{scheme}'")
|
||||
|
||||
parser = PARSERS[scheme]
|
||||
try:
|
||||
if scheme in BACKEND_AWARE_PARSERS:
|
||||
return parser(url, tag, backend)
|
||||
except TypeError:
|
||||
return parser(url, tag)
|
||||
return parser(url, tag)
|
||||
|
||||
|
||||
def _xray_sockopt(routing_mark: int | None) -> dict:
|
||||
@@ -250,6 +249,24 @@ def make_tag(prefix: str, remark: str, index: int) -> str:
|
||||
return f"{prefix}-{index}"
|
||||
|
||||
|
||||
def _remark_from_line(line: str) -> str:
|
||||
return line.split("#", 1)[1] if "#" in line else ""
|
||||
|
||||
|
||||
def _unique_tag(base_tag: str, seen_tags: set[str]) -> str:
|
||||
if base_tag not in seen_tags:
|
||||
return base_tag
|
||||
|
||||
suffix = 2
|
||||
while f"{base_tag}-{suffix}" in seen_tags:
|
||||
suffix += 1
|
||||
return f"{base_tag}-{suffix}"
|
||||
|
||||
|
||||
def _subscription_tag(line: str, tag_prefix: str, index: int, seen_tags: set[str]) -> str:
|
||||
return _unique_tag(make_tag(tag_prefix, _remark_from_line(line), index), seen_tags)
|
||||
|
||||
|
||||
def parse_subscription(
|
||||
lines: list[str], tag_prefix: str, routing_mark: int | None, backend: str = "sing-box"
|
||||
) -> list[dict]:
|
||||
@@ -261,15 +278,7 @@ def parse_subscription(
|
||||
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}"
|
||||
tag = _subscription_tag(line, tag_prefix, index, seen_tags)
|
||||
|
||||
try:
|
||||
outbound = build_outbound(line, tag, routing_mark, backend)
|
||||
@@ -294,15 +303,7 @@ def parse_hybrid_subscription(
|
||||
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}"
|
||||
tag = _subscription_tag(line, tag_prefix, index, seen_tags)
|
||||
|
||||
try:
|
||||
outbound = build_outbound(line, tag, routing_mark, "sing-box")
|
||||
|
||||
@@ -416,3 +416,7 @@ PARSERS = {
|
||||
"http": parse_http_proxy,
|
||||
"https": parse_http_proxy,
|
||||
}
|
||||
|
||||
BACKEND_AWARE_PARSERS = {
|
||||
"vless",
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import base64
|
||||
import json
|
||||
import unittest
|
||||
|
||||
import proxy_parsing
|
||||
from proxy_parsing import build_outbound
|
||||
|
||||
|
||||
@@ -245,6 +246,19 @@ class BuildOutboundTests(unittest.TestCase):
|
||||
with self.assertRaisesRegex(ValueError, "unsupported scheme"):
|
||||
run_parser("wireguard://example.com")
|
||||
|
||||
def test_backend_aware_parser_type_error_is_not_arity_fallback(self):
|
||||
def broken_parser(url: str, tag: str, backend: str):
|
||||
raise TypeError("real parser bug")
|
||||
|
||||
proxy_parsing.PARSERS["broken"] = broken_parser
|
||||
proxy_parsing.BACKEND_AWARE_PARSERS.add("broken")
|
||||
try:
|
||||
with self.assertRaisesRegex(TypeError, "real parser bug"):
|
||||
build_outbound("broken://example.com:443", "test-outbound")
|
||||
finally:
|
||||
del proxy_parsing.PARSERS["broken"]
|
||||
proxy_parsing.BACKEND_AWARE_PARSERS.remove("broken")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -132,6 +132,16 @@ class FetchSubscriptionTests(unittest.TestCase):
|
||||
"udp://1.1.1.1",
|
||||
)
|
||||
|
||||
def test_hybrid_subscription_deduplicates_tags_across_backend_lists(self):
|
||||
payload = _make_b64_payload(
|
||||
VLESS_URI + "#Same",
|
||||
VLESS_XHTTP_URI + "#Same",
|
||||
VLESS_ECH_URI + "#Same",
|
||||
)
|
||||
obs = run_fetcher(payload, tag_prefix="sub", backend="hybrid")
|
||||
tags = [ob["tag"] for ob in obs["singBox"] + obs["xray"]]
|
||||
self.assertEqual(tags, ["sub-Same", "sub-Same-2", "sub-Same-3"])
|
||||
|
||||
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")
|
||||
|
||||
Reference in new issue
Block a user