zapret-kvn/xray_fluent/client_reports/reporter.py
loop-uh 79c3024464
Some checks failed
Windows project source guards / test (push) Failing after 2m53s
client reports: only the shipped (frozen) app sends
Field check 2026-11-21: test processes had twice sent app errors of the dev
tree to production (0.8.101 and 0.8.102 rows, empty core versions).
app_errors._early hands errors noted before any sink to the first real
controller, so a single source-run process without the off switch was
enough. reports_enabled() now needs sys.frozen: a run from source never
sends, whatever the environment says.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 20:04:37 -05:00

954 lines
44 KiB
Python

"""Network side of the connection reports: queue on disk, send directly first, then via the tunnel.
The decisions (contract, classification, queue, token) live in ``contract``. Here:
the install id, ``GET /v1/network`` (only directly — through the tunnel the ingest
would see the VPN server's network), ``POST /v1/reports`` and the one functional HTTPS
probe through the Xray relay after a VLESS connect (owner decision 19.11.2026: like the
Hysteria/AmneziaWG engines already do at start). No timers: work runs when a report
is queued and at start-up, on one background thread.
Schema 3 (vpnbot «schema 3 live» / «completion live» 2026-11-19): the app sends facts, vpnbot judges and answers
with a verdict per report, whose text goes onto the failure notice (``on_failure_changed``). A failure goes at once;
when the path is checked (``probe.BlockProbe``, rarely: ``PATH_CHECK_PER_SERVER_MS``, ``PATH_CHECK_PER_INSTALL_MS``)
the same report follows whole. The «Отправить отчёт» button is ``force_report``. After a success the flow is
measured through the session, once per server per ``FLOW_CHECK_PER_SERVER_MS``.
"""
from __future__ import annotations
import json
import os
import random
import re
import secrets
import sys
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
from collections.abc import Callable, Mapping
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Any
from ..engines.socks_probe import HTTPS_ENDPOINTS
from .contract import (
INGEST_FEEDS,
AttemptDetails,
ForeignServer,
short_version,
new_report_id,
FAILED_FOR_MS,
MAX_BATCH,
MAX_BODY_BYTES,
MAX_FEED_BYTES,
TRIES_PER_PATH,
MAX_ELAPSED_MS,
SENT_DIRECT,
SENT_TUNNEL,
AppIdentity,
AttemptFacts,
AttemptRecord,
IngestHostCache,
NetworkToken,
NetworkTokenCache,
Outcome,
ReportClock,
ReportQueue,
SendAction,
ServerRef,
after_response,
ServerVerdict,
attempt_core_code,
batch_json,
date_header_ms,
deliver,
domain_of,
ingest_order,
is_install_id,
is_network_token,
outcome_of,
parse_ingest_feed,
parse_reply,
products_for_scheme,
take_batch,
)
from . import checks as k
from .checks import Check, carrier_of
from .app_errors import AppError, AppErrorRecord, AppErrorStore, record_json
from .probe import (
BlockProbe, NoDataProbe, PathFacts, ProbeTarget, Relay, literal_address, measure_flow, needs_path_check, relay_health,
)
APP_NAME = "zapret-kvn"
PLATFORM = "windows"
#: App errors per batch: 16 frames each keep a batch far under the ingest's body ceiling.
APP_ERRORS_PER_BATCH = 20
_TIMEOUT = 15.0
_PROBE_TIMEOUT = 10.0
_MAX_ANSWER_BYTES = 4096
#: 50 verdicts with their texts.
_MAX_REPLY_BYTES = 64 * 1024
UDP_SCHEMES = frozenset({"hysteria2", "hy2", "wireguard", "awg", "amneziawg", "wg", "tuic"})
#: Path checks are rare (owner 2026-11-19: «check as rarely as possible and only if it's really necessary»): one per
#: server (host and IP) a day, one per install in ten minutes, so clicking through servers probes at most one. vpnbot
#: sums the reports of every install, so a rare check loses little.
PATH_CHECK_PER_SERVER_MS = 24 * 3600 * 1000
PATH_CHECK_PER_INSTALL_MS = 10 * 60 * 1000
#: The flow probe runs inside the tunnel (nothing new on the wire), still not on every connect.
FLOW_CHECK_PER_SERVER_MS = 6 * 3600 * 1000
#: The notice while vpnbot's answer is not here (no rule and no verdict text in the app: owner 2026-11-19).
WAITING_TEXT = "Разбор сбоя появится, когда отчёт дойдёт до сервиса."
def server_ref_for(node: Any, subscriptions: list[Any]) -> ServerRef | None:
"""The catalogue server a node of the service's own subscription names; ``None`` otherwise."""
subscription_id = getattr(node, "subscription_id", None)
if not subscription_id:
return None
subscription = next((item for item in subscriptions if getattr(item, "id", None) == subscription_id), None)
if subscription is None or not getattr(subscription, "service", False):
return None
entry = (getattr(subscription, "server_ids", None) or {}).get(getattr(node, "provider_name", "") or "")
if not isinstance(entry, dict):
return None
products = products_for_scheme(getattr(node, "scheme", ""))
server_id, product = entry.get("server_id"), entry.get("product")
if not isinstance(server_id, str) or product not in products:
return None
via = entry.get("via", "")
return ServerRef(server_id, str(product), via if isinstance(via, str) else "")
def probe_target_for(node: Any) -> ProbeTarget | None:
"""The server of a node as a path-check target: address, port, TLS name, whether TLS, whether UDP."""
host = str(getattr(node, "server", "") or "").strip()
try:
port = int(getattr(node, "port", 0) or 0)
except (TypeError, ValueError):
return None
if not host or not 0 < port < 65536:
return None
scheme = str(getattr(node, "scheme", "") or "").lower()
udp = scheme in UDP_SCHEMES
outbound = getattr(node, "outbound", None) or {}
stream = outbound.get("streamSettings") if isinstance(outbound, dict) else None
stream = stream if isinstance(stream, dict) else {}
security = str(stream.get("security") or "").lower()
name = None
for key in ("realitySettings", "tlsSettings"):
settings = stream.get(key)
if isinstance(settings, dict) and str(settings.get("serverName") or "").strip():
name = str(settings["serverName"]).strip()
break
if name is None and isinstance(outbound, dict) and isinstance(outbound.get("tls"), dict):
name = str(outbound["tls"].get("server_name") or "").strip() or None
quic = scheme in {"hysteria2", "hy2", "hysteria", "tuic"}
network = str(stream.get("network") or "").lower()
tls_settings = stream.get("tlsSettings") if isinstance(stream.get("tlsSettings"), dict) else {}
alpn = tls_settings.get("alpn") if isinstance(tls_settings.get("alpn"), list) else []
transport = "quic" if quic else "udp" if udp else (network or "tcp")
report_security = "tls" if quic else (security or "none")
# Hysteria/TUIC carry TLS over QUIC: there is no TCP TLS step for them.
return ProbeTarget(
host=host, port=port, server_name=name, uses_tls=not udp and security in {"tls", "reality"}, udp=udp,
protocol="hysteria2" if scheme == "hy2" else scheme, transport=transport, security=report_security,
carrier=carrier_of(scheme, network, alpn),
)
_VERSION_NUMBER = re.compile(r"v?(\d+\.\d+\.\d+[0-9A-Za-z.+_-]*)")
_versions_cache: tuple[tuple[str, str], ...] | None = None
def core_versions() -> tuple[tuple[str, str], ...]:
"""OS and core versions for the report, asked once per app run (each core answers ``version``)."""
global _versions_cache
if _versions_cache is not None:
return _versions_cache
found: list[tuple[str, str]] = []
try:
import platform
os_version = short_version(platform.version())
if os_version:
found.append(("os", os_version))
from ..constants import HYSTERIA_PATH_DEFAULT, SINGBOX_PATH_DEFAULT
from ..engines.singbox.core_updater import installed_version
from ..engines.xray import get_xray_version
for key, text in (
("xray", get_xray_version("") or ""),
("singbox", installed_version(SINGBOX_PATH_DEFAULT) if SINGBOX_PATH_DEFAULT.exists() else ""),
("hysteria", _hysteria_version(HYSTERIA_PATH_DEFAULT)),
):
match = _VERSION_NUMBER.search(text or "")
value = short_version(match.group(1)) if match else None
if value:
found.append((key, value))
except Exception:
pass
_versions_cache = tuple(found)
return _versions_cache
def _hysteria_version(exe: Path) -> str:
if not exe.exists():
return ""
try:
from ..platform.windows.subprocess_utils import result_output_text, run_text
text = result_output_text(run_text([str(exe), "version"], timeout=5.0))
except Exception:
return ""
for line in str(text).splitlines():
if "version" in line.lower():
return line
return ""
def probe_relay(port: int, username: str, password: str, *, timeout: float = _PROBE_TIMEOUT) -> str:
"""One HTTPS request through the Xray relay SOCKS inbound; the first answering endpoint wins. The result is the
``health`` probe's ``r``: ``answered`` or why no endpoint answered."""
return relay_health(Relay(port, username, password), HTTPS_ENDPOINTS, timeout)
class SendStatus(str, Enum):
AVAILABLE = "available"
SENDING = "sending"
SENT = "sent"
QUEUED = "queued"
#: Not a server of the service's subscription: the diagnosis is shown, the button waits for schema 2.
NOT_OURS = "not_ours"
@dataclass(frozen=True, slots=True)
class FailureReport:
"""The last failure for the notice: what the path check found and what happened to the report."""
attempt: int
checking: bool
#: vpnbot's verdict text for this report (Russian, shown as is), or WAITING_TEXT until it arrives; None — nothing
#: to show yet (the check is running).
diagnosis_text: str | None
#: vpnbot's detail code of that verdict.
detail: str | None
status: SendStatus
class _PendingFailure:
def __init__(self, attempt: int, ref: ServerRef | None, scheme: str, facts: AttemptFacts, elapsed_ms: int,
attempted_at_ms: int, target: ProbeTarget | None, name: str = "") -> None:
self.attempt = attempt
#: The node's name: the name of a foreign server in its report.
self.name = name
self.ref = ref
self.scheme = scheme
self.facts = facts
self.elapsed_ms = elapsed_ms
self.attempted_at_ms = attempted_at_ms
self.target = target
self.checking = False
self.checked = False
#: The Xray relay of a VLESS start whose check found no data (the tunnel was up).
self.relay: Relay | None = None
self.checks: dict[str, Check] = {}
self.probed_address: str | None = None
self.record: AttemptRecord | None = None
self.status = SendStatus.NOT_OURS if ref is None and target is None else SendStatus.AVAILABLE
REPORTS_SWITCH = "ZAPRET_KVN_CLIENT_REPORTS"
def reports_enabled(environ: Mapping[str, str] | None = None, frozen: bool | None = None) -> bool:
"""Reports come only from the shipped app (the frozen exe) and only unless the environment says
``ZAPRET_KVN_CLIENT_REPORTS=off``.
A run from source (tests, the release gate, a developer's e2e) never sends: on 2026-11-20 two test processes
reached production with app errors of the dev tree (0.8.101 and 0.8.102 rows, empty core versions), because one
long test process gathers app errors before any reporter exists and hands them to the first real controller.
"""
if not (getattr(sys, "frozen", False) if frozen is None else frozen):
return False
value = (os.environ if environ is None else environ).get(REPORTS_SWITCH, "")
return value.strip().lower() not in ("off", "0", "false", "no")
class ClientReporter:
def __init__(
self,
root: Path,
app_version: str,
*,
tunnel_proxy_port: Callable[[], int | None],
direct_is_direct: Callable[[], bool] = lambda: True,
log: Callable[[str], None] = lambda _line: None,
now_ms: Callable[[], int] = lambda: int(time.time() * 1000),
opener: Callable[[bool, int | None], urllib.request.OpenerDirector] | None = None,
probe: BlockProbe | None = None,
flow_probe: Callable[[int | None], Check | None] = measure_flow,
no_data_probe: NoDataProbe | None = None,
on_failure_changed: Callable[[FailureReport | None], None] = lambda _report: None,
context: Callable[[], AttemptDetails] = AttemptDetails,
server_address: Callable[[str], str | None] = lambda _host: None,
enabled: bool | None = None,
) -> None:
self._root = Path(root)
# Test runs (release gate, e2e on local test servers) set ZAPRET_KVN_CLIENT_REPORTS=off: nothing is written to
# the queue and nothing leaves for the ingest; the notice and the path check still work.
self._enabled = reports_enabled() if enabled is None else enabled
if not self._enabled:
log(f"[client-report] off (a run from source or {REPORTS_SWITCH}=off): nothing is written or sent")
self._app = AppIdentity(APP_NAME, _safe_version(app_version), PLATFORM)
self._queue = ReportQueue(self._root / "queue.jsonl")
# «-2»: vpnbot's v2 (2026-11-19 19:37 UTC) no longer places a v1 value in any network, so one kept by an
# older version is left behind and a fresh one is asked (the value itself stays opaque).
self._tokens = NetworkTokenCache(self._root / "network-token-2")
self._ingest = IngestHostCache(self._root / "ingest-hosts")
self._random = random.SystemRandom()
self._failed_hosts: dict[str, int] = {}
self._tunnel_proxy_port = tunnel_proxy_port
# In TUN mode Windows has no per-socket bypass: a "direct" request follows the user's routing and may leave
# through the VPN server, so the ingest would record the server's network. Then only the tunnel path is used.
self._direct_is_direct = direct_is_direct
self._log = log
self._now_ms = now_ms
#: The ingest's clock from its replies: ``attempted_at`` goes in its time (vpnbot 2026-11-20).
self._clock = ReportClock()
#: report_id → the offset it was accepted with: its completion copy keeps the same attempted_at.
self._sent_offsets: dict[str, int] = {}
#: App errors not about a server (vpnbot «app_error live» 2026-11-20): counted, one send per code a day.
self._app_errors = AppErrorStore(Path(root) / "app-errors.json", now_ms)
self._opener = opener or _build_opener
self._lock = threading.Lock()
self._flush_lock = threading.Lock()
self._stopped_until_next = False
self._probe = probe
self._flow_probe = flow_probe
self._no_data_probe = no_data_probe
#: report_id -> vpnbot's verdict, for the notice (the last few only).
self._verdicts: dict[str, ServerVerdict] = {}
self._on_failure_changed = on_failure_changed
# Schema 2: what is the same for all attempts now (mode, masking, versions) and the address the start used.
self._context = context
self._server_address = server_address
self._probed_at: dict[str, int] = {}
self._flow_checked_at: dict[str, int] = {}
self._failure: _PendingFailure | None = None
self._failure_lock = threading.Lock()
# ── entry points (any thread; work goes to a daemon thread) ──
def on_attempt(
self,
ref: ServerRef | None,
scheme: str,
facts: AttemptFacts,
*,
elapsed_ms: int,
attempted_at_ms: int,
relay: tuple[int, str, str] | None = None,
target: ProbeTarget | None = None,
attempt: int = 0,
name: str = "",
) -> None:
"""One finished attempt; ``relay`` asks for the VLESS probe before the verdict. A report is written only for
a server of the service; the path check and the notice work for any server."""
if ref is None and (facts.succeeded or facts.cancelled) and relay is None:
if facts.succeeded:
self._clear_failure()
return
self._spawn(lambda: self._attempt(ref, scheme, facts, elapsed_ms, attempted_at_ms, relay, target, attempt, name))
def begin_attempt(self, attempt: int) -> None:
"""A new connection attempt: the previous failure leaves the notice."""
with self._failure_lock:
if self._failure is not None and self._failure.attempt != attempt:
self._failure = None
self._on_failure_changed(None)
def force_report(self) -> None:
"""The «Отправить отчёт» button: a failure without a report yet is written (``local_*`` when it is not about
the server) and the queue is sent now. The result arrives through ``on_failure_changed``."""
pending = self._failure
# A foreign server is sent in full (address, port, protocol, name); normally it is already written by itself,
# the button only sends at once.
if pending is None or (pending.ref is None and self._foreign_of(pending) is None):
return
pending.status = SendStatus.SENDING
self._publish(pending)
self._spawn(lambda: self._force(pending))
def _force(self, pending: _PendingFailure) -> None:
try:
if pending.record is None:
pending.record = self._write_failure(pending, flush=False)
self._stopped_until_next = False
self._flush(wait=True)
finally:
pending.status = self._status_of(pending)
self._log(f"[client-report] report by button: {'sent' if pending.status is SendStatus.SENT else 'queued'}")
self._publish(pending)
def flush(self) -> None:
if self._stopped_until_next or not self._enabled:
return
self._spawn(self._flush)
def prefetch_token(self) -> None:
"""Ask ``GET /v1/network`` now, while requests are really direct (no TUN session): app start, the start of a
connection, after a disconnect. In TUN mode the token cannot be asked later."""
if not self._enabled or not self._direct_is_direct() or not self._tokens.needs_refresh(self._now_ms()):
return
self._spawn(self._refresh_token)
# ── work ──
def _spawn(self, work: Callable[[], None]) -> None:
def run() -> None:
try:
work()
except Exception as exc: # never let a report break the app
self._log(f"[client-report] failed: {type(exc).__name__}")
threading.Thread(target=run, name="client-report", daemon=True).start()
def _attempt(
self,
ref: ServerRef | None,
scheme: str,
facts: AttemptFacts,
elapsed_ms: int,
attempted_at_ms: int,
relay: tuple[int, str, str] | None,
target: ProbeTarget | None = None,
attempt: int = 0,
name: str = "",
) -> None:
udp = str(scheme or "").lower() in UDP_SCHEMES
no_data: Relay | None = None
health: Check | None = None
if relay is not None:
started = time.monotonic()
result = probe_relay(*relay)
waited_ms = int((time.monotonic() - started) * 1000)
elapsed_ms += waited_ms
passed = result == k.ANSWERED
facts = AttemptFacts(succeeded=passed, traffic_failed=not passed)
no_data = None if passed else Relay(*relay)
if not passed:
health = Check(result, min(waited_ms, k.MAX_CHECK_MS))
self._log(f"[client-report] VLESS check through the server: {'passed' if passed else 'no answer'}")
facts = AttemptFacts(
cancelled=facts.cancelled, succeeded=facts.succeeded, core_code=facts.core_code,
network_lost=facts.network_lost, traffic_failed=facts.traffic_failed,
tcp_preflight_failed=facts.tcp_preflight_failed, udp_transport=udp,
)
if facts.cancelled or facts.succeeded:
found: dict[str, Check] = {}
if facts.succeeded:
flow = self._check_flow(target)
if flow is not None:
found["flow"] = flow
self._clear_failure()
if ref is not None:
self._write(ref, outcome_of(facts), elapsed_ms, attempted_at_ms, self._details(target, None),
carrier=self._carrier(target, scheme), core_code="", found=found)
return
pending = _PendingFailure(attempt, ref, scheme, facts, elapsed_ms, attempted_at_ms, target, name)
pending.relay = no_data
if health is not None and not attempt_core_code(facts):
# A fact the app already has (vpnbot 2026-11-20): it costs no traffic, so no rarity limit; it goes with
# the first copy.
pending.checks[k.HEALTH] = health
self._on_failure(pending)
def _on_failure(self, pending: _PendingFailure) -> None:
with self._failure_lock:
self._failure = pending
self._publish(pending)
target = pending.target
direct = self._direct_is_direct()
no_data = (pending.relay is not None and self._no_data_probe is not None and target is not None
and self._probe_slot(target))
path = (pending.relay is None and target is not None and self._probe is not None and direct
and needs_path_check(attempt_core_code(pending.facts)) and self._probe_slot(target))
# Completion (vpnbot «completion live» 2026-11-19): the core's part goes at once, so the panel sees the failure
# without waiting for the probes and the attempt is placed in the network it was made from; the same report
# whole follows after the check.
pending.record = self._write_failure(pending, complete=False if (no_data or path) else None)
pending.status = self._status_of(pending)
self._publish(pending)
if no_data:
# The tunnel was up and carried nothing: the path to the server is proven, only the no-data probes say
# more (owner 2026-11-19: no probe that cannot change the answer).
pending.checking = True
self._publish(pending)
try:
pending.checks.update(self._no_data_probe.run(pending.relay, direct_allowed=direct))
except Exception as exc: # a probe never breaks a report
self._log(f"[client-report] no-data check failed: {type(exc).__name__}")
# In TUN mode a "direct" socket follows the user's routing: the probes would test the VPN, not the path.
elif path:
pending.checking = True
self._publish(pending)
try:
path = self._probe.run(target, known_address=self._known_address(target))
except Exception as exc: # a probe never breaks a report
self._log(f"[client-report] path check failed: {type(exc).__name__}")
path = PathFacts()
pending.checks.update(path.checks)
pending.probed_address = path.probed_address
if path.probed_address:
self._take_slot(self._probed_at, "ip:" + path.probed_address, PATH_CHECK_PER_SERVER_MS)
if pending.checking:
pending.checking = False
pending.checked = True
self._log(f"[client-report] path check -> {_summary(pending.checks)}")
# Everything that can be sent goes by itself (owner 2026-11-19: «send everything we possibly can
# automatically, even without the button»; the button is for a user who wants to send it himself). A server
# of the user's own goes in full (address, port, protocol, name: owner's explicit choice the same day).
first = pending.record
if first is not None:
pending.record = self._write_failure(pending, complete=True, report_id=first.report_id)
pending.status = self._status_of(pending)
self._publish(pending)
def _write_failure(self, pending: _PendingFailure, *, flush: bool = True, complete: bool | None = None,
report_id: str = "") -> AttemptRecord | None:
foreign = self._foreign_of(pending) if pending.ref is None else None
if pending.ref is None and foreign is None:
return None
return self._write(
pending.ref, Outcome.FAILED, pending.elapsed_ms, pending.attempted_at_ms,
self._details(pending.target, pending.probed_address), foreign=foreign, flush=flush,
carrier=self._carrier(pending.target, pending.scheme), core_code=attempt_core_code(pending.facts),
found=pending.checks, complete=complete, report_id=report_id,
)
@staticmethod
def _carrier(target: ProbeTarget | None, scheme: str) -> str:
return target.carrier if target is not None else carrier_of(scheme)
def _known_address(self, target: ProbeTarget) -> str | None:
"""The address the start itself used: then no DNS probe is needed."""
try:
return self._server_address(target.host)
except Exception:
return None
def _probe_slot(self, target: ProbeTarget) -> bool:
"""At most one check per install in ``PATH_CHECK_PER_INSTALL_MS`` and per server host and server IP in
``PATH_CHECK_PER_SERVER_MS`` (rows sharing an IP, e.g. «через Россию», share it)."""
address = literal_address(target.host) or self._known_address(target)
keys = [("install", PATH_CHECK_PER_INSTALL_MS), ("host:" + target.host, PATH_CHECK_PER_SERVER_MS)]
if address:
keys.append(("ip:" + address, PATH_CHECK_PER_SERVER_MS))
now = self._now_ms()
with self._lock:
if any(now - self._probed_at[key] < interval for key, interval in keys if key in self._probed_at):
return False
for key, interval in keys:
self._take_slot(self._probed_at, key, interval)
return True
def _check_flow(self, target: ProbeTarget | None) -> Check | None:
"""``flow`` after a success: at most once per server per ``FLOW_CHECK_PER_SERVER_MS``, TCP lines only."""
if (target is None or target.carrier != "tcp" or self._probe is None
or not self._take_slot(self._flow_checked_at, target.host, FLOW_CHECK_PER_SERVER_MS)):
return None
port = self._tunnel_proxy_port()
try:
flow = self._flow_probe(int(port) if port else None)
except Exception:
flow = None
self._log("[client-report] flow check -> " + (f"{flow.r}, {flow.b} B, {flow.ms} ms" if flow else "not run"))
return flow
def _take_slot(self, stamps: dict[str, int], key: str, interval_ms: int) -> bool:
now = self._now_ms()
with self._lock:
last = stamps.get(key)
if last is not None and now - last < interval_ms:
return False
for stale in [name for name, at in stamps.items() if now - at >= PATH_CHECK_PER_SERVER_MS]:
stamps.pop(stale, None)
stamps[key] = now
return True
def _foreign_of(self, pending: _PendingFailure) -> ForeignServer | None:
target = pending.target
if target is None:
return None
return ForeignServer.of(target.host, target.protocol or pending.scheme, pending.name)
def _details(self, target: ProbeTarget | None, probed_address: str | None) -> AttemptDetails:
"""The report's details: the common part from ``context`` and this server's address, port, transport."""
try:
base = self._context()
except Exception:
base = AttemptDetails()
if target is None:
return base.cleaned()
from dataclasses import replace
try:
noted = self._server_address(target.host)
except Exception:
noted = None
return replace(
base,
server_ip=literal_address(target.host) or noted or probed_address,
port=target.port, transport=target.transport, security=target.security,
).cleaned()
def _status_of(self, pending: _PendingFailure) -> SendStatus:
if pending.record is None:
return SendStatus.NOT_OURS if pending.ref is None and self._foreign_of(pending) is None else SendStatus.AVAILABLE
return SendStatus.QUEUED if pending.record in self._queue.pending(self._now_ms()) else SendStatus.SENT
def _publish(self, pending: _PendingFailure) -> None:
if self._failure is not pending:
return
verdict = self._verdicts.get(pending.record.report_id) if pending.record is not None else None
if verdict is not None:
text, detail = verdict.text, verdict.detail
elif pending.record is not None and not pending.checking:
text, detail = WAITING_TEXT, None
else:
text, detail = None, None
self._on_failure_changed(FailureReport(
attempt=pending.attempt, checking=pending.checking, diagnosis_text=text, detail=detail,
status=pending.status,
))
def _clear_failure(self) -> None:
with self._failure_lock:
had = self._failure is not None
self._failure = None
if had:
self._on_failure_changed(None)
def _write(
self,
ref: ServerRef | None,
outcome: Outcome,
elapsed_ms: int,
attempted_at_ms: int,
details: AttemptDetails | None = None,
*,
foreign: ForeignServer | None = None,
flush: bool = True,
carrier: str = "tcp",
core_code: str = "",
found: dict[str, Check] | None = None,
complete: bool | None = None,
report_id: str = "",
) -> AttemptRecord | None:
if (ref is None and foreign is None) or not self._enabled:
return None
if self._tokens.needs_refresh(self._now_ms()):
self._refresh_token()
record = AttemptRecord(
server_id=ref.server_id if ref else "", product=ref.product if ref else "", outcome=outcome,
failed_step="", detail="",
elapsed_ms=max(0, min(int(elapsed_ms), MAX_ELAPSED_MS)),
attempted_at_ms=int(attempted_at_ms), network_token=self._tokens.current(self._now_ms()),
foreign=foreign, details=(details or AttemptDetails()).cleaned(), report_id=report_id or new_report_id(),
via=ref.via if ref else "", carrier=carrier if carrier in k.CARRIERS else "tcp",
core_code=k.core_code_of(core_code), checks=k.ordered(found or {}), complete=complete,
)
self._queue.add(record)
who = (f"{ref.product}/{ref.server_id}" + (f" via {ref.via}" if ref.via else "")) if ref else f"foreign {foreign.protocol if foreign else ''}"
code = f" {record.core_code}" if record.core_code else ""
part = {False: ", first copy", True: ", completion"}.get(complete, "")
self._log(f"[client-report] {who} {outcome.value}{code}, {len(record.checks)} checks{part}, queued {self._queue.size()}")
if flush:
self._stopped_until_next = False
self._flush()
return record
def _flush(self, wait: bool = False) -> None:
if not self._enabled or not self._flush_lock.acquire(blocking=wait):
return
try:
while True:
install_id = self._install_id()
batch = take_batch(
self._queue.pending(self._now_ms()), MAX_BATCH, MAX_BODY_BYTES,
lambda records: len(batch_json(install_id, self._app, records, SENT_DIRECT).encode("utf-8")),
)
if not batch:
self._flush_app_errors()
return
# A refused batch is split until the refused reports stand alone: the good ones are never lost.
stopped = deliver(batch, self._send_corrected, self._resolved)
if stopped is None:
continue
self._stopped_until_next = stopped is SendAction.KEEP_AND_STOP
return
finally:
self._flush_lock.release()
def _resolved(self, part: list[AttemptRecord], action: SendAction) -> None:
self._queue.remove(part)
if action is SendAction.DROP:
self._log(f"[client-report] the ingest refused {len(part)} report(s), removed")
def on_app_error(self, error: AppError, at_ms: int | None = None) -> None:
"""An app error not about any server (crash, start without a server, subscription, update, permission,
storage): a code and, for a crash, frames — never a message, path, address or URL. Goes with the next send.
Written at once (a crash may end the process); sent from a background thread."""
if not self._enabled:
return
try:
details = self._context()
except Exception:
details = AttemptDetails()
record = self._app_errors.add(error, self._app.version, self._tokens.current(self._now_ms()), details, at_ms)
if record is None:
return
self._log(f"[client-report] app error {error.code} ({error.step}), times: {record.count}")
self._stopped_until_next = False
self.flush()
def _flush_app_errors(self) -> None:
"""App errors after the reports, through the same names, clock and split; a stop leaves them for later."""
while True:
due = self._app_errors.due(APP_ERRORS_PER_BATCH)
if not due:
return
stopped = deliver(due, lambda part: self._send_corrected(part, self._send_app_errors_once),
self._app_errors_resolved)
if stopped is None:
continue
self._stopped_until_next = stopped is SendAction.KEEP_AND_STOP
return
def _app_errors_resolved(self, part: list[AppErrorRecord], action: SendAction) -> None:
self._app_errors.sent(part)
if action is SendAction.DROP:
self._log(f"[client-report] the ingest refused {len(part)} app error(s), removed")
def _send_app_errors_once(self, batch: list[AppErrorRecord]) -> SendAction | None:
install_id = self._install_id()
for via, proxy_port in self._routes():
offset = self._clock.applied()
items = [record_json(install_id, APP_NAME, PLATFORM, record, via, offset) for record in batch]
body = json.dumps({"reports": items}, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
for host in self._host_order()[:TRIES_PER_PATH]:
status, _reply = self._post(host, body, proxy_port)
action = after_response(status)
if action is SendAction.TRY_OTHER_PATH:
self._note_failed(host)
continue
self._log(f"[client-report] app errors {len(batch)}, {len(body)} B ({via}, {domain_of(host)}) -> HTTP {status}")
return action
return None
def _send_corrected(self, batch: list, once: Callable[[list], SendAction | None] | None = None) -> SendAction:
"""A refusal right after the ingest's clock was first learnt is sent once more in its time."""
once = once or self._send_once
used = self._clock.applied()
action = self._send(batch, once)
if action is not SendAction.DROP or self._clock.applied() == used:
return action
self._log(f"[client-report] the PC clock is {-self._clock.applied() // 1000} s off the ingest: resent in its time")
return self._send(batch, once)
def _send(self, batch: list, once: Callable[[list], SendAction | None] | None = None) -> SendAction:
"""Directly, then through the tunnel; up to ``TRIES_PER_PATH`` ingest names on each path. When no name
answered, the feed of names is asked once and the send repeated."""
once = once or self._send_once
if self._ingest.feed_due(self._now_ms(), nothing_answered=False):
self._refresh_feed()
action = once(batch)
if action is None and self._ingest.feed_due(self._now_ms(), nothing_answered=True) and self._refresh_feed():
action = once(batch)
if action is None:
self._log(f"[client-report] batch {len(batch)} not sent: no ingest name answered")
return SendAction.TRY_OTHER_PATH
return action
def _routes(self) -> list[tuple[str, int | None]]:
routes: list[tuple[str, int | None]] = [(SENT_DIRECT, None)] if self._direct_is_direct() else []
port = self._tunnel_proxy_port()
if port:
routes.append((SENT_TUNNEL, int(port)))
elif not self._direct_is_direct():
# TUN session without a local HTTP inbound: a request without a proxy is captured by the TUN itself.
# If the user's routing sends it out directly after all, it is still labelled tunnel, and the ingest takes
# no network from the source of a tunnel report: never a false network.
routes.append((SENT_TUNNEL, None))
return routes
def _send_once(self, batch: list[AttemptRecord]) -> SendAction | None:
install_id = self._install_id()
for via, proxy_port in self._routes():
offset = self._clock.applied()
with self._lock:
sent = dict(self._sent_offsets)
body = batch_json(install_id, self._app, batch, via, offset, sent).encode("utf-8")
for host in self._host_order()[:TRIES_PER_PATH]:
status, reply = self._post(host, body, proxy_port)
action = after_response(status)
if action is SendAction.TRY_OTHER_PATH:
self._note_failed(host)
continue
self._log(f"[client-report] batch {len(batch)}, {len(body)} B ({via}, {domain_of(host)}) -> HTTP {status}")
if action is SendAction.REMOVE:
self._note_sent_offsets(batch, offset, sent)
self._note_verdicts(batch, parse_reply(reply, len(batch)))
return action
return None
def _note_sent_offsets(self, batch: list[AttemptRecord], offset: int, sent: dict[str, int]) -> None:
with self._lock:
for record in batch:
if record.report_id:
self._sent_offsets[record.report_id] = sent.get(record.report_id, offset)
while len(self._sent_offsets) > 256:
self._sent_offsets.pop(next(iter(self._sent_offsets)))
def _note_verdicts(self, batch: list[AttemptRecord], verdicts: list[ServerVerdict | None]) -> None:
"""Keep vpnbot's verdicts by report_id and show the one of the failure on the notice."""
with self._lock:
for record, verdict in zip(batch, verdicts):
if verdict is not None and record.report_id:
self._verdicts[record.report_id] = verdict
while len(self._verdicts) > 64:
self._verdicts.pop(next(iter(self._verdicts)))
pending = self._failure
if pending is not None and pending.record is not None and pending.record.report_id in self._verdicts:
verdict = self._verdicts[pending.record.report_id]
self._log(f"[client-report] verdict: {verdict.detail} ({verdict.level}, {verdict.confidence})")
pending.status = self._status_of(pending)
self._publish(pending)
def _host_order(self) -> list[str]:
now = self._now_ms()
with self._lock:
for host in [h for h, at in self._failed_hosts.items() if now - at >= FAILED_FOR_MS]:
del self._failed_hosts[host]
failed = set(self._failed_hosts)
return ingest_order(self._ingest.hosts(), failed, self._random)
def _note_failed(self, host: str) -> None:
with self._lock:
self._failed_hosts[host] = self._now_ms()
def _refresh_feed(self) -> bool:
"""Ingest names from the zapret-links feed (entry pages on script.google.com and the gist), directly, then
through the tunnel. True when at least one name was learnt."""
self._ingest.note_feed_attempt(self._now_ms())
for _via, proxy_port in self._routes():
for feed in INGEST_FEEDS:
text = self._get(feed, proxy_port, MAX_FEED_BYTES)
hosts = parse_ingest_feed(text) if text is not None else []
if hosts:
self._ingest.store(hosts, self._now_ms())
self._log(f"[client-report] ingest names from the feed {urllib.parse.urlsplit(feed).hostname}: {len(hosts)}")
return True
self._log("[client-report] the feed of ingest names did not answer")
return False
def _get(self, url: str, proxy_port: int | None, max_bytes: int) -> bytes | None:
request = urllib.request.Request(url, headers={"Connection": "close"})
try:
with self._opener(proxy_port is not None, proxy_port).open(request, timeout=_TIMEOUT) as response:
if int(response.status) != 200:
return None
data = response.read(max_bytes + 1)
return data if len(data) <= max_bytes else None
except (OSError, ValueError, urllib.error.HTTPError):
return None
def _post(self, host: str, body: bytes, proxy_port: int | None) -> tuple[int | None, bytes | None]:
"""One short connection per request (urllib never reuses one; ``Connection: close`` says so to the server).
The status and, for an accepted batch, the answer (vpnbot's verdicts)."""
request = urllib.request.Request(
f"https://{host}/v1/reports", data=body, method="POST",
headers={"Content-Type": "application/json", "Connection": "close"},
)
try:
with self._opener(proxy_port is not None, proxy_port).open(request, timeout=_TIMEOUT) as response:
status = int(response.status)
self._learn_clock(response)
try:
reply = response.read(_MAX_REPLY_BYTES)
except OSError:
reply = None
return status, reply
except urllib.error.HTTPError as exc:
self._learn_clock(exc)
return int(exc.code), None
except (OSError, ValueError):
return None, None
def _learn_clock(self, response: Any) -> None:
headers = getattr(response, "headers", None)
try:
value = headers.get("Date") if headers is not None else None
except Exception:
value = None
self._clock.learn(date_header_ms(value), self._now_ms())
def _refresh_token(self) -> None:
"""``GET /v1/network`` only directly: through the tunnel the ingest would see the VPN server's network."""
if not self._direct_is_direct():
return
if self._ingest.feed_due(self._now_ms(), nothing_answered=False):
self._refresh_feed()
data: Any = None
for host in self._host_order()[:TRIES_PER_PATH]:
text = self._get(f"https://{host}/v1/network?install_id={self._install_id()}", None, _MAX_ANSWER_BYTES)
if text is None:
self._note_failed(host)
continue
try:
data = json.loads(text.decode("utf-8"))
except ValueError:
data = None
break
token = data.get("network_token") if isinstance(data, dict) else None
if isinstance(token, str) and is_network_token(token):
self._tokens.store(NetworkToken(token, self._now_ms()))
self._log("[client-report] network token received")
def _install_id(self) -> str:
with self._lock:
path = self._root / "install-id"
try:
value = path.read_text(encoding="utf-8").strip()
if is_install_id(value):
return value
except OSError:
pass
value = secrets.token_hex(16)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(value, encoding="utf-8")
return value
def _summary(found: dict[str, Check]) -> str:
"""The probes for the log: names, results and times, no addresses."""
if not found:
return "no probe"
return " ".join(f"{probe}={check.r}/{check.ms}ms" + (f"/{check.b}B" if check.b is not None else "")
for probe, check in k.ordered(found))
def _build_opener(via_proxy: bool, proxy_port: int | None) -> urllib.request.OpenerDirector:
if via_proxy and proxy_port:
address = f"http://127.0.0.1:{int(proxy_port)}"
return urllib.request.build_opener(urllib.request.ProxyHandler({"http": address, "https": address}))
return urllib.request.build_opener(urllib.request.ProxyHandler({}))
def _safe_version(version: str) -> str:
cleaned = "".join(ch for ch in str(version or "") if (ch.isascii() and ch.isalnum()) or ch in ".+-")[:32]
return cleaned or "0"