ZaStoGram_desktop/Telegram/SourceFiles/tests/test_mtproto_layer_cycles.py
loop-uh 744109cc78 Pace MTProxy dials instead of steering them
Every session dialled a proxy on its own with nothing in between, so a cold
start or a proxy switch opened one socket per session per account in the same
millisecond. A public mtproxy answers about two parallel handshakes and
silently drops the rest, which the client read back as
client_hello_sent_no_server_hello or connected_no_mtproto_data across the
whole batch - including on a dd-secret proxy that sends no ClientHello at all
and therefore cannot be fingerprint-blocked.

Replace the machinery that was supposed to prevent this with a rate limiter
that actually does. ProxyDialLease keeps at most two unproven handshakes in
flight per proxy server, spaces the rest apart and stretches the spacing
after a run of attempts that proved nothing, so a blackholed proxy is no
longer redialled by every session on its own eight second timer.

Everything that reacted to failure by changing its own behaviour is gone:
EndpointAdmissionArbiter, EndpointLivePool, ConnectionBroker,
session_proxy_adapter, SessionProxyPort, HandshakeGate, open_scheduler,
endpoint_health, adaptive_policy and ProxyRotationManager. A fingerprint
filter is deterministic, so rotating emulated ClientHello profiles only
hands the other side more of them, and escalated recipes (fragmentation,
pacing) make the flow less browser-like rather than more - while the signal
that drove the escalation could not tell a DPI box from a proxy refusing
extra connections. The ClientHello templates themselves are untouched and
the profile is now whatever the user configured, fixed.

Also: ServerHello budget 2.5s -> 5s, mtproxy status reduces through the same
ProxyConnectionStatus path as every other proxy type, and route memory is
wired back up so an address that answered is dialled first and one that
failed is dialled last.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-25 16:58:05 +03:00

55 lines
1.6 KiB
Python

from pathlib import Path
SOURCE_DIR = Path(__file__).resolve().parents[1]
MTPROTO_DIR = SOURCE_DIR / "mtproto"
TESTS_DIR = SOURCE_DIR / "tests"
def read(path):
assert path.exists(), f"missing expected source file: {path}"
return path.read_text(encoding="utf-8")
def assert_no_tokens(path, tokens):
text = read(path)
for token in tokens:
assert token not in text, f"{path.relative_to(SOURCE_DIR)} contains {token}"
def test_runtime_headers_do_not_include_proxy_layer_headers():
for path in (
MTPROTO_DIR / "runtime" / "connection_status.h",
MTPROTO_DIR / "runtime" / "runtime_environment.h"):
assert_no_tokens(path, (
'#include "mtproto/proxy/',
))
def test_transport_details_do_not_include_proxy_socket_subclasses():
assert_no_tokens(
MTPROTO_DIR
/ "transport"
/ "details"
/ "mtproto_abstract_socket.cpp",
(
'#include "mtproto/proxy/mtproxy/tls_socket.h"',
'#include "mtproto/proxy/wss/socket.h"',
"std::make_unique<TlsSocket>",
"std::make_unique<WssSocket>",
))
def test_existing_source_guards_do_not_lock_old_cycles():
for path in (
TESTS_DIR / "test_proxy_connection_status.py",):
assert_no_tokens(path, (
'assert \'#include "mtproto/proxy/status.h"\' in abstract_socket_h',
))
if __name__ == "__main__":
test_runtime_headers_do_not_include_proxy_layer_headers()
test_transport_details_do_not_include_proxy_socket_subclasses()
test_existing_source_guards_do_not_lock_old_cycles()