ZaStoGram/TMessagesProj/jni/tgnet/pathtest/test/pathtest_host_test.cpp
loop-uh a918b73f48
Some checks failed
Build three ZaStoGram APKs / build (armeabi-v7a, ZaStoGram-standalone-armeabi-v7a, Armv7, armv7) (push) Failing after 49s
Build three ZaStoGram APKs / build (arm64-v8a, ZaStoGram-standalone-arm64-v8a, Arm64, arm64) (push) Failing after 51s
ZaStoGram source guards / guards (push) Failing after 40s
Build three ZaStoGram APKs / build (x86, ZaStoGram-standalone-x86, X86, x86) (push) Failing after 1m8s
Check connection: a path test that runs the real client code and writes one log
New module (jni/tgnet/pathtest, org.telegram.zastogram.pathtest): for every proxy of the list, the official WSS route
and two control hosts it makes 12 attempts 1.5 s apart through the REAL client code and writes one text log with a
JSONL twin into the logs folder (so the logs screen and its zip export carry it): per-attempt lines (server_hello,
alert, rst, fin, silent, connect_failed with time to first byte, DNS and TCP times), a summary table and a plain
verdict per host and profile.

The probe is not a second client: Naive entries go through naive::CreateFor, the WSS route through the real
wss::Socket (setDiagnosticProbe switches off route-health writes), FakeTLS profiles through composeFakeTlsClientHello,
now the single place ConnectionSocket also builds its hello. The probe peeks the first server bytes to classify the
answer. Secrets are never read (random FakeTLS key, throw-away Naive credentials, only the cover domain of a secret),
hosts are ep-<8 hex of sha256(host:port)> unless real names are ticked, every log field is a closed-vocabulary token.

UI: a row in the proxy settings, an offer after a failed proxy check, share button. Tests: JVM tests of the pure
logic, a native host test against the real WssSocket.cpp and local servers (silent, fin, rst, alert, http, overloaded),
guards updated for the shared hello composer. Verified on an x86_64 emulator against lab shapes and the real relay.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-29 12:36:51 +00:00

115 lines
4.7 KiB
C++

// Host test of the "Check connection" probe against the REAL tgnet/wss/WssSocket.cpp (libzsgnaive handshake) and local
// servers that behave like the cases the module exists to tell apart (pathtest_servers.py).
//
// pathtest_host_test wss PORT one attempt of the WSS client (probe mode), prints the encoded result
// pathtest_host_test raw PORT HELLO one attempt that sends the bytes in HELLO and classifies the answer
// pathtest_host_test health PORT the probe must not teach the client anything (see below)
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include "WssSocket.h"
#include "pathtest/PathTestProbe.h"
namespace {
sockaddr_in Local(int port) {
sockaddr_in a{};
a.sin_family = AF_INET;
a.sin_port = htons(static_cast<uint16_t>(port));
inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
return a;
}
tgnet::wss::Route LabRoute(int port) {
tgnet::wss::Route route;
route.relayHost = "proxy.test";
route.connectHost = "127.0.0.1";
route.relayPort = static_cast<uint16_t>(port);
route.domain = "proxy.test";
route.path = "/apiws";
route.healthDomain = "proxy.test";
return route;
}
// The probe drives the same socket the client uses; running it must leave route health exactly as it was, while the
// same failures counted by an ordinary socket do suppress the route (otherwise this test proves nothing).
int Health(int closedPort) {
tgnet::wss::SetNetworkType(1);
tgnet::wss::Route before;
if (!tgnet::wss::OfficialRoute(2, false, false, "149.154.167.51", &before)) {
std::printf("health: no official route\n");
return 2;
}
auto routeOf = [&](tgnet::wss::Route r) {
r.connectHost = "127.0.0.1";
r.relayPort = static_cast<uint16_t>(closedPort);
return r;
};
sockaddr_in a = Local(closedPort);
for (int i = 0; i < 12; i++) {
tgnet::wss::Socket probe(routeOf(before));
probe.setDiagnosticProbe(true);
tgnet::pathtest::ProbeTransport(probe, reinterpret_cast<sockaddr *>(&a), sizeof(a), 2000, 0);
}
tgnet::wss::Route afterProbe;
tgnet::wss::OfficialRoute(2, false, false, "149.154.167.51", &afterProbe);
const bool unchanged = afterProbe.connectHost == before.connectHost && afterProbe.domain == before.domain
&& afterProbe.cdnSlot == before.cdnSlot && afterProbe.tunnel == before.tunnel
&& tgnet::wss::RouteUsable(before);
std::printf("health: after 12 probe failures route unchanged=%s usable=%d\n", unchanged ? "YES" : "NO",
tgnet::wss::RouteUsable(before) ? 1 : 0);
// The same failures through an ordinary socket must change something.
for (int i = 0; i < 12; i++) {
tgnet::wss::Socket ordinary(routeOf(before));
std::string diagnostic;
if (ordinary.open(reinterpret_cast<sockaddr *>(&a), sizeof(a), &diagnostic)) {
std::vector<std::vector<uint8_t>> payloads;
for (int k = 0; k < 20; k++) {
if (!ordinary.onEvent(0x1 | 0x4 | 0x8, payloads, &diagnostic)) break;
}
}
}
tgnet::wss::Route afterOrdinary;
tgnet::wss::OfficialRoute(2, false, false, "149.154.167.51", &afterOrdinary);
const bool changed = afterOrdinary.connectHost != before.connectHost || afterOrdinary.domain != before.domain
|| afterOrdinary.viaFallback != before.viaFallback || afterOrdinary.cdnSlot != before.cdnSlot
|| afterOrdinary.tunnel || afterOrdinary.direct || !tgnet::wss::RouteUsable(before);
std::printf("health: after 12 ordinary failures route changed=%s\n", changed ? "YES" : "NO");
return unchanged && changed ? 0 : 1;
}
} // namespace
int main(int argc, char **argv) {
if (argc < 3) {
return 64;
}
const std::string mode = argv[1];
const int port = std::atoi(argv[2]);
if (mode == "health") {
return Health(port);
}
sockaddr_in a = Local(port);
if (mode == "wss") {
tgnet::wss::Socket socket(LabRoute(port));
socket.setDiagnosticProbe(true);
std::printf("%s\n", tgnet::pathtest::Encode(tgnet::pathtest::ProbeTransport(
socket, reinterpret_cast<sockaddr *>(&a), sizeof(a), 4000, 1500)).c_str());
return 0;
}
if (mode == "raw" && argc >= 4) {
std::ifstream f(argv[3], std::ios::binary);
std::vector<uint8_t> hello((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
std::printf("%s\n", tgnet::pathtest::Encode(tgnet::pathtest::ProbeRawHello(
reinterpret_cast<sockaddr *>(&a), sizeof(a), hello.data(), static_cast<uint32_t>(hello.size()), 2500)).c_str());
return 0;
}
return 64;
}