zapret-kvn/xray_fluent/application/masking_runtime.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

127 lines
5 KiB
Python

"""Glue between a connect and the pure `xray_fluent.masking` package.
The planners and the stored nodes are never edited: a connect gets masked COPIES of the
selected node, of the pool nodes, or of the Xray pool outbounds (by node id), built from the
current settings. Turning masking off therefore needs no re-import.
"""
from __future__ import annotations
from dataclasses import dataclass, replace
from typing import Any
from ..masking import codec
from ..masking import desync as desync_rules
from ..masking.apply import apply_server, log_line, shape_of
from ..masking.merge import effective
from ..masking.model import NONE, Core, Desync, MaskingOverride, MaskingSettings
from ..profiles.models import Node
@dataclass(frozen=True, slots=True)
class _Trial:
node_id: str
override: MaskingOverride
title: str
# "Подбор" (MaskingTuneRunner): the one candidate the next connect runs with. In memory only, like
# Android `MaskingTrial`: a crash in the middle of a run leaves the stored masking as it was.
_trial: _Trial | None = None
def begin_trial(node_id: str, override: MaskingOverride, title: str) -> None:
global _trial
_trial = _Trial(node_id, override, title)
def end_trial() -> None:
global _trial
_trial = None
def trial_signature() -> dict[str, Any] | None:
"""Part of the transition signature while a trial runs, so each candidate really reconnects."""
if _trial is None:
return None
return codec.encode(MaskingSettings(True, NONE, {_trial.node_id: _trial.override}))["overrides"]
def trial_log_line() -> str | None:
return None if _trial is None else f"маскировка: подбор, вариант «{_trial.title}»"
def stored_settings_of(app_settings: Any) -> MaskingSettings:
document = getattr(app_settings, "masking", None)
return codec.decode(document) if isinstance(document, dict) and document else MaskingSettings()
def settings_of(app_settings: Any) -> MaskingSettings:
"""What a connect runs with: the stored settings, with the trial's server replaced."""
stored = stored_settings_of(app_settings)
trial = _trial
return stored if trial is None else stored.with_trial(trial.node_id, trial.override)
def masked_node(node: Node | None, settings: MaskingSettings, notes: list[str] | None = None) -> Node | None:
if node is None or not settings.enabled or not isinstance(node.outbound, dict) or not node.outbound:
return node
outbound, node_notes = apply_server(node.outbound, settings, node.id)
if notes is not None:
notes.extend(node_notes)
return node if outbound is node.outbound else replace(node, outbound=outbound)
def masked_nodes(nodes: list[Node], settings: MaskingSettings) -> list[Node]:
if not settings.enabled:
return nodes
return [masked_node(node, settings) or node for node in nodes]
def mask_pool_outbounds(
outbounds: list[dict[str, Any]], tags_by_node: dict[str, str], settings: MaskingSettings,
) -> tuple[list[dict[str, Any]], int, list[str]]:
"""Masked copies of Xray pool outbounds; `tags_by_node` is the pool's node id -> tag map."""
if not settings.enabled:
return outbounds, 0, []
node_by_tag = {tag: node_id for node_id, tag in tags_by_node.items()}
result: list[dict[str, Any]] = []
changed = 0
notes: list[str] = []
for outbound in outbounds:
masked, outbound_notes = apply_server(outbound, settings, node_by_tag.get(str(outbound.get("tag") or "")))
changed += masked is not outbound
notes.extend(outbound_notes)
result.append(masked)
return result, changed, notes
#: Note of the connect log when the plan cannot run: a raw Xray config has no sing-box front.
DESYNC_WITHOUT_FRONT = "Пакеты-обманки не применены: свой конфиг Xray запускается без sing-box."
def carries_desync(node: Node | None) -> bool:
outbound = getattr(node, "outbound", None)
return isinstance(outbound, dict) and desync_rules.CARRY_KEY in outbound
def strip_desync(outbounds: list[Any]) -> int:
"""Remove carried packet-trick plans from outbounds that go to Xray as they are."""
return sum(desync_rules.take(outbound) is not None for outbound in outbounds if isinstance(outbound, dict))
def desync_of(node: Node | None, settings: MaskingSettings) -> Desync | None:
"""The packet tricks a connect runs on this node's server socket, or None."""
if node is None or not settings.enabled or not isinstance(node.outbound, dict):
return None
shape = shape_of(node.outbound)
if shape is None or shape.core is not Core.XRAY or not shape.is_tcp_stream:
return None
xray = effective(settings, node.id, shape).masking.xray
if xray is None or not xray.enabled or xray.desync is None or not xray.desync.enabled:
return None
return xray.desync if xray.desync.method in desync_rules.WINDOWS_METHODS else None
def describe(settings: MaskingSettings, changed: int, notes: list[str]) -> str | None:
return log_line(settings.enabled, changed, notes)