zapret-kvn/tests/test_windivert_release.py
loop-uh 06ba09a2a1
Some checks failed
Windows project source guards / test (push) Failing after 2m19s
Masking: packet tricks on Windows (sing-box front, WinDivert spoof)
«Пакеты-обманки» for VLESS over TCP now run on Windows, in the sing-box
front that owns the socket to the server (the Xray sidecar dials through
the protect inbound):

- «Обманка» = sing-box tls_spoof over WinDivert (owner 2026-11-16): a
  decoy ClientHello with a wrong TCP sequence number, so no TTL needed.
- «Беспорядок» / «Разрез» = core patch kvn-desync.patch: the Android
  0017 engine (common/kvndesync) with a Windows socket layer (IP_TTL,
  SIO_TCP_INFO BytesOut) and a `kvn_desync` route option.
- The plan rides on the masked Xray outbound copy and the planner takes it
  off into one route-options rule per server; Xray never sees it.

Owner 2026-11-16 rules:
- One desync per flow: a server with its own tricks gets the winws2 rule
  `pass` and needs no bypass.
- WinDivert unloaded and available: after a sing-box that opened
  WinDivert exits, the WinDivert service is stopped unless winws/winws2/
  GoodbyeDPI still run.

Also: the settings group kept dropping a global packet-trick set on any
other change; it now has its own «Пакеты-обманки» choice.

win10 lab: disorder handshake ~310 ms (300 ms RTO: TTL 1 piece died at the
router and was resent), split 5 ms; pktmon shows the vk.com decoy before
the real hello; spoof loads WinDivert, winws2 starts on it, release
refused while winws2 runs, service gone after; winws2 then loads Monkey
and spoof works on it. All 186 test files pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:10:14 -05:00

87 lines
3.8 KiB
Python

"""WinDivert after «Пакеты-обманки»: unloaded once the spoofing core exited, never under winws2.
Owner 2026-11-16: when spoofing is turned off, the session stops or the app exits, WinDivert
is unloaded and available again.
"""
from __future__ import annotations
import unittest
from unittest.mock import Mock, patch
from PyQt6.QtCore import QCoreApplication, QProcess
from xray_fluent.engines import windivert_release
from xray_fluent.engines.singbox.manager import SingBoxManager
_APP = QCoreApplication.instance() or QCoreApplication([])
_SPOOF_CONFIG = {"route": {"rules": [
{"inbound": ["__app_hybrid_protect_in"], "domain": ["srv.example"], "action": "route-options",
"tls_spoof": "vk.com", "tls_spoof_method": "wrong-sequence"},
]}}
class DecisionTests(unittest.TestCase):
def test_running_driver_without_other_users_is_released(self) -> None:
self.assertTrue(windivert_release.decide(state="running", users=[]).release)
def test_winws2_keeps_the_shared_driver(self) -> None:
decision = windivert_release.decide(state="running", users=["winws2.exe"])
self.assertFalse(decision.release)
self.assertIn("winws2.exe", decision.message)
def test_nothing_to_do_when_not_loaded(self) -> None:
for state in (None, "stopped"):
self.assertFalse(windivert_release.decide(state=state, users=[]).release)
self.assertFalse(windivert_release.decide(state="stop_pending", users=[]).release)
def test_release_stops_and_waits_until_the_service_is_gone(self) -> None:
states = iter(["running", "stop_pending", None])
stop = Mock(return_value=True)
outcome = windivert_release.release_if_unused(query=lambda: next(states), stop=stop, users=list)
stop.assert_called_once_with()
self.assertEqual("WinDivert выгружен, драйвер свободен", outcome)
def test_release_never_stops_under_another_user(self) -> None:
stop = Mock(return_value=True)
outcome = windivert_release.release_if_unused(
query=lambda: "running", stop=stop, users=lambda: ["winws2.exe"])
stop.assert_not_called()
self.assertIn("оставлен", outcome)
class ManagerTests(unittest.TestCase):
def _manager(self, *, pending: bool) -> tuple[SingBoxManager, list[str]]:
manager = SingBoxManager()
fake = Mock()
fake.state.return_value = QProcess.ProcessState.NotRunning
manager._process = fake
manager._windivert_pending = pending
notices: list[str] = []
manager.notice.connect(notices.append)
return manager, notices
def test_stop_after_a_spoofing_core_releases_the_driver_once(self) -> None:
manager, notices = self._manager(pending=True)
with patch.object(windivert_release, "release_if_unused", return_value="WinDivert выгружен, драйвер свободен") as release:
self.assertTrue(manager.stop())
self.assertTrue(manager.stop())
release.assert_called_once_with()
self.assertEqual(["WinDivert выгружен, драйвер свободен"], notices)
def test_core_without_spoof_never_touches_the_driver(self) -> None:
manager, notices = self._manager(pending=False)
with patch.object(windivert_release, "release_if_unused") as release:
self.assertTrue(manager.stop())
release.assert_not_called()
self.assertEqual([], notices)
def test_config_with_tls_spoof_marks_the_driver_for_release(self) -> None:
from xray_fluent.masking import desync
self.assertTrue(desync.uses_windivert(_SPOOF_CONFIG))
self.assertFalse(desync.uses_windivert({"route": {"rules": [{"action": "route-options", "kvn_desync": "disorder:sni"}]}}))
if __name__ == "__main__":
unittest.main()