zapret-kvn/tests/test_tls_cert_pin_links.py
loop-uh 64f4ee714e Xray owns VMess and Trojan: sidecar path, XHTTP, TLS and masking follow
Owner decision 2026-11-16: VLESS, VMess and Trojan run on the official Xray core;
sing-box stays the gasket (TUN, routing, DNS, protected sockets) and only keeps
Shadowsocks, SOCKS, HTTP and the rest. sing-box had no XHTTP transport for VMess and
Trojan and only approximated the other transports, while Xray runs any of its protocols
over any transport.

- protocol_core: VMess/Trojan in Xray's JSON shape go to Xray; a raw sing-box JSON
  outbound pasted by the user (type key) stays with sing-box.
- link_parser: xray_only for VMess/Trojan (allowInsecure without a pin is dropped like
  VLESS, Xray 26.x has none), TLS validation for all three, VMess alterId > 0 refused,
  vmess:// carries XHTTP mode and extra, WebSocket/HTTPUpgrade drop h2/h3 from alpn.
- masking: VMess/Trojan are Xray servers (shape, presets); sing-box fragment keeps
  AnyTLS, HTTP, ShadowTLS.
- config_builder: dead VMess/Trojan/transport conversion removed.
- tests flipped to Shadowsocks where they need a sing-box server; new
  tests/test_xray_owns_vmess_trojan.py; docs updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 09:44:27 -05:00

130 lines
6 KiB
Python

import base64
import json
import unittest
from xray_fluent.engines.singbox.config_builder import build_singbox_outbound, unenforced_tls_pin_warning
from xray_fluent.importer.link_parser import (
LinkParseError,
parse_single,
validate_node_outbound,
xray_core_tls_problem,
)
from xray_fluent.network.speed_test_worker import should_skip_speed_test
PIN = "ab" * 32
PIN2 = "cd" * 32
PIN_COLONS = ":".join(PIN[i:i + 2].upper() for i in range(0, 64, 2))
UUID = "11111111-1111-1111-1111-111111111111"
def _tls(node):
return node.outbound["streamSettings"]["tlsSettings"]
class XrayShareLinkPinTests(unittest.TestCase):
def test_vless_allow_insecure_with_pin_becomes_pinned_peer_cert(self) -> None:
node = parse_single(
f"vless://{UUID}@a.example:443?security=tls&sni=a.example&allowInsecure=1&pcs={PIN}#V"
)
tls = _tls(node)
self.assertEqual(tls["pinnedPeerCertSha256"], PIN)
self.assertNotIn("allowInsecure", tls)
self.assertIsNone(validate_node_outbound(node))
self.assertIsNone(xray_core_tls_problem(node))
def test_trojan_allow_insecure_with_pin_has_no_allow_insecure(self) -> None:
node = parse_single(f"trojan://pw@a.example:443?security=tls&allow_insecure=1&pcs={PIN}#T")
tls = _tls(node)
self.assertEqual(tls["pinnedPeerCertSha256"], PIN)
self.assertNotIn("allowInsecure", tls)
self.assertNotIn("allowInsecure", json.dumps(node.outbound))
def test_insecure_alias_with_pin(self) -> None:
node = parse_single(f"trojan://pw@a.example:443?insecure=1&pcs={PIN}#T")
self.assertNotIn("allowInsecure", _tls(node))
def test_pin_with_colons_and_list_is_normalized(self) -> None:
node = parse_single(
f"vless://{UUID}@a.example:443?security=tls&pcs={PIN_COLONS},{PIN2.upper()}#V"
)
self.assertEqual(_tls(node)["pinnedPeerCertSha256"], f"{PIN},{PIN2}")
def test_full_name_pin_param_is_accepted(self) -> None:
node = parse_single(f"vless://{UUID}@a.example:443?security=tls&pinnedPeerCertSha256={PIN}#V")
self.assertEqual(_tls(node)["pinnedPeerCertSha256"], PIN)
def test_invalid_pin_rejects_link_instead_of_dropping_it(self) -> None:
for bad in ("abc", "zz" * 32, PIN[:-2]):
with self.assertRaises(LinkParseError):
parse_single(f"vless://{UUID}@a.example:443?security=tls&allowInsecure=1&pcs={bad}#V")
def test_verify_peer_cert_by_name(self) -> None:
node = parse_single(
f"vless://{UUID}@a.example:443?security=tls&pcs={PIN}&vcn=one.example,%20two.example#V"
)
self.assertEqual(_tls(node)["verifyPeerCertByName"], "one.example,two.example")
def test_vless_allow_insecure_without_pin_imports_with_normal_verification(self) -> None:
# Xray нечем заменить allowInsecure без пина: сертификат проверяется
# обычным образом, сервер импортируется и запускается (как на Android).
node = parse_single(f"vless://{UUID}@a.example:443?security=tls&allowInsecure=1#V")
self.assertNotIn("allowInsecure", _tls(node))
self.assertIsNone(validate_node_outbound(node))
self.assertIsNone(xray_core_tls_problem(node))
def test_saved_vless_with_allow_insecure_and_no_link_gets_russian_error(self) -> None:
node = parse_single(f"vless://{UUID}@a.example:443?security=tls#V")
_tls(node)["allowInsecure"] = True
problem = validate_node_outbound(node)
self.assertIsNotNone(problem)
self.assertIn("pcs", problem)
self.assertIn("allowInsecure", problem)
def test_allow_insecure_zero_is_not_emitted(self) -> None:
node = parse_single(f"vless://{UUID}@a.example:443?security=tls&allowInsecure=0#V")
self.assertNotIn("allowInsecure", _tls(node))
self.assertIsNone(validate_node_outbound(node))
def test_trojan_allow_insecure_without_pin_is_verified_normally_like_vless(self) -> None:
# Trojan runs on Xray (2026-11-16): Xray 26.x has no allowInsecure, so the certificate is checked.
node = parse_single("trojan://pw@a.example:443?security=tls&allowInsecure=1#T")
self.assertIsNone(validate_node_outbound(node))
self.assertNotIn("allowInsecure", _tls(node))
self.assertFalse(should_skip_speed_test(node)[0])
def test_vmess_json_fields_are_mapped(self) -> None:
payload = {
"add": "a.example", "port": "443", "id": UUID, "net": "tcp", "tls": "tls",
"allowInsecure": 1, "pcs": PIN, "vcn": "a.example",
}
link = "vmess://" + base64.b64encode(json.dumps(payload).encode()).decode()
tls = _tls(parse_single(link))
self.assertEqual(tls["pinnedPeerCertSha256"], PIN)
self.assertEqual(tls["verifyPeerCertByName"], "a.example")
self.assertNotIn("allowInsecure", tls)
payload.pop("pcs")
node = parse_single("vmess://" + base64.b64encode(json.dumps(payload).encode()).decode())
self.assertNotIn("allowInsecure", _tls(node))
def test_plain_vless_without_pin_is_unchanged(self) -> None:
node = parse_single(f"vless://{UUID}@a.example:443?security=tls&sni=a.example#V")
self.assertEqual(_tls(node), {"serverName": "a.example"})
class XrayPinEnforcementTests(unittest.TestCase):
"""Trojan and VMess run on Xray (2026-11-16), which checks the pin itself; sing-box never saw it."""
def test_trojan_pin_is_enforced_by_xray_so_there_is_no_warning(self) -> None:
node = parse_single(f"trojan://pw@a.example:443?security=tls&sni=a.example&allowInsecure=1&pcs={PIN}#T")
self.assertEqual(_tls(node)["pinnedPeerCertSha256"], PIN)
self.assertNotIn("allowInsecure", _tls(node))
self.assertEqual(unenforced_tls_pin_warning(node), "")
def test_sing_box_nodes_without_tls_have_no_warning(self) -> None:
node = parse_single("ss://YWVzLTI1Ni1nY206cGFzc3dvcmQ@a.example:8388#S")
self.assertEqual(unenforced_tls_pin_warning(node), "")
if __name__ == "__main__":
unittest.main()