various fixes
This commit is contained in:
11 files changed
+413
-181
No files matched your search
@@ -10,6 +10,15 @@ def _qs(query: str) -> dict:
|
||||
return dict(urllib.parse.parse_qsl(query))
|
||||
|
||||
|
||||
def _split_host_port(hostpart: str) -> tuple[str, str]:
|
||||
host, separator, port = hostpart.rpartition(":")
|
||||
if not separator or not host or not port:
|
||||
raise ValueError("URL must include host and port")
|
||||
if host.startswith("[") and host.endswith("]"):
|
||||
host = host[1:-1]
|
||||
return host, port
|
||||
|
||||
|
||||
def _parse_url_parts(url: str, scheme: str) -> tuple[str, str, str, dict]:
|
||||
rest = url[len(f"{scheme}://") :]
|
||||
rest, _, _ = rest.partition("#")
|
||||
@@ -18,9 +27,7 @@ def _parse_url_parts(url: str, scheme: str) -> tuple[str, str, str, dict]:
|
||||
else:
|
||||
userinfo = ""
|
||||
hostpart, _, query = rest.partition("?")
|
||||
host, _, port = hostpart.rpartition(":")
|
||||
if host.startswith("[") and host.endswith("]"):
|
||||
host = host[1:-1]
|
||||
host, port = _split_host_port(hostpart)
|
||||
return userinfo, host, port, _qs(query)
|
||||
|
||||
|
||||
@@ -183,18 +190,44 @@ def parse_trojan(url: str, tag: str) -> dict:
|
||||
return ob
|
||||
|
||||
|
||||
def parse_shadowsocks(url: str, tag: str) -> dict:
|
||||
userinfo, host, port, _ = _parse_url_parts(url, "ss")
|
||||
def _decode_ss_userinfo(userinfo: str) -> tuple[str, str]:
|
||||
plain = urllib.parse.unquote(userinfo)
|
||||
if ":" in plain:
|
||||
return tuple(plain.split(":", 1))
|
||||
|
||||
pad = "=" * (-len(userinfo) % 4)
|
||||
try:
|
||||
decoded = base64.urlsafe_b64decode(userinfo + pad).decode()
|
||||
if ":" not in decoded:
|
||||
raise ValueError
|
||||
method, password = decoded.split(":", 1)
|
||||
except (binascii.Error, UnicodeDecodeError, ValueError):
|
||||
plain = urllib.parse.unquote(userinfo)
|
||||
method, password = plain.split(":", 1)
|
||||
return tuple(decoded.split(":", 1))
|
||||
except (binascii.Error, UnicodeDecodeError, ValueError) as exc:
|
||||
raise ValueError("invalid shadowsocks userinfo") from exc
|
||||
|
||||
|
||||
def parse_shadowsocks(url: str, tag: str) -> dict:
|
||||
# Support both SIP002 form:
|
||||
# ss://base64(method:password)@host:port#name
|
||||
# and legacy subscriptions that base64-encode the whole endpoint:
|
||||
# ss://base64(method:password@host:port)#name
|
||||
rest = url[len("ss://") :]
|
||||
rest, _, _ = rest.partition("#")
|
||||
endpoint, _, _ = rest.partition("?")
|
||||
|
||||
if "@" in endpoint:
|
||||
userinfo, host, port, _ = _parse_url_parts(url, "ss")
|
||||
else:
|
||||
pad = "=" * (-len(endpoint) % 4)
|
||||
try:
|
||||
decoded = base64.urlsafe_b64decode(endpoint + pad).decode()
|
||||
except (binascii.Error, UnicodeDecodeError) as exc:
|
||||
raise ValueError("invalid shadowsocks URL") from exc
|
||||
userinfo, separator, hostpart = decoded.rpartition("@")
|
||||
if not separator:
|
||||
raise ValueError("invalid legacy shadowsocks URL")
|
||||
host, port = _split_host_port(hostpart)
|
||||
|
||||
method, password = _decode_ss_userinfo(userinfo)
|
||||
|
||||
return {
|
||||
"type": "shadowsocks",
|
||||
|
||||
@@ -55,6 +55,39 @@ class BuildOutboundTests(unittest.TestCase):
|
||||
self.assertEqual(ob["type"], "shadowsocks")
|
||||
self.assertEqual(ob["method"], "chacha20-ietf-poly1305")
|
||||
|
||||
|
||||
def test_shadowsocks_legacy_full_base64(self):
|
||||
legacy = (
|
||||
base64.urlsafe_b64encode(
|
||||
b"chacha20-ietf-poly1305:passw0rd@legacy.example.com:8388"
|
||||
)
|
||||
.decode()
|
||||
.rstrip("=")
|
||||
)
|
||||
ob = run_parser(f"ss://{legacy}#Legacy Server")
|
||||
self.assertEqual(ob["type"], "shadowsocks")
|
||||
self.assertEqual(ob["server"], "legacy.example.com")
|
||||
self.assertEqual(ob["server_port"], 8388)
|
||||
self.assertEqual(ob["method"], "chacha20-ietf-poly1305")
|
||||
self.assertEqual(ob["password"], "passw0rd")
|
||||
|
||||
|
||||
def test_shadowsocks_plain_userinfo(self):
|
||||
ob = run_parser("ss://none:plain%20pass@plain.example.com:8388")
|
||||
self.assertEqual(ob["type"], "shadowsocks")
|
||||
self.assertEqual(ob["method"], "none")
|
||||
self.assertEqual(ob["password"], "plain pass")
|
||||
|
||||
def test_shadowsocks_legacy_ipv6_strips_brackets(self):
|
||||
legacy = (
|
||||
base64.urlsafe_b64encode(b"none:pass@[2001:db8::1]:8388")
|
||||
.decode()
|
||||
.rstrip("=")
|
||||
)
|
||||
ob = run_parser(f"ss://{legacy}#Legacy IPv6")
|
||||
self.assertEqual(ob["server"], "2001:db8::1")
|
||||
self.assertEqual(ob["server_port"], 8388)
|
||||
|
||||
def test_hysteria2(self):
|
||||
ob = run_parser(
|
||||
"hy2://secret@example.com:443?sni=hy.example.com&insecure=1&obfs=salamander&obfs-password=mask"
|
||||
|
||||
Reference in new issue
Block a user