Desktop twin of the Android module (zsg_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 log, tdata/pathtest/path-test-YYYYMMDD-HHMMSS.txt with a JSONL twin: 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. Same format and same ep-<8 hex> host ids as Android (golden files from the Java implementation are reproduced byte for byte by tests/run_pathtest_core_test.sh). The probe is not a second client: WssClientConfig and Zsg::Naive::NaiveClientConfig are now the single builders of the zsgnaive config for both the real sockets and the probe, and FakeTLS profiles go through PrepareClientHelloRules + PrepareClientHello, the calls TlsSocket::sendClientHello makes. Secrets are never read (random FakeTLS key, throw-away Naive credentials, only the cover domain of a secret); every log field is a closed-vocabulary token. UI: a section in the proxy box, an offer once in 30 minutes when the retry link appears. Guards: test_zsg_pathtest.py, test_proxy_wss_default.py updated for the shared config builder. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
317 lines
11 KiB
C++
317 lines
11 KiB
C++
/*
|
|
This file is part of Telegram Desktop,
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
For license and copyright information please follow this link:
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
*/
|
|
#include "zsg_naive/zsg_naive_api.h"
|
|
|
|
#include "logs.h"
|
|
#include "settings.h"
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
#include <QtCore/QCryptographicHash>
|
|
#include <QtCore/QDir>
|
|
#include <QtCore/QFile>
|
|
#include <QtCore/QLibrary>
|
|
#include <QtCore/QSaveFile>
|
|
#include <QtNetwork/QSslCertificate>
|
|
#include <QtNetwork/QSslConfiguration>
|
|
|
|
#include <type_traits>
|
|
|
|
#ifdef Q_OS_WIN
|
|
#include <windows.h>
|
|
#endif // Q_OS_WIN
|
|
|
|
// Generated by CMake next to the Windows build (the SHA-256 of the embedded
|
|
// library); other platforms have nothing embedded.
|
|
#if __has_include("zsg_naive_embedded.h")
|
|
#include "zsg_naive_embedded.h"
|
|
#endif
|
|
#ifndef ZSG_NAIVE_EMBEDDED_SHA256
|
|
#define ZSG_NAIVE_EMBEDDED_SHA256 ""
|
|
#endif
|
|
|
|
namespace Zsg::Naive {
|
|
namespace {
|
|
|
|
// ISRG Root X1 (Let's Encrypt), SHA-256 96BCEC06...BDDF08C6.
|
|
constexpr auto kIsrgRootX1 = R"PEM(-----BEGIN CERTIFICATE-----
|
|
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
|
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
|
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
|
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
|
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
|
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
|
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
|
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
|
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
|
|
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
|
|
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
|
|
B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
|
|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nvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
|
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
|
-----END CERTIFICATE-----
|
|
)PEM";
|
|
|
|
struct Loaded {
|
|
Api api = {};
|
|
bool ok = false;
|
|
QString error;
|
|
};
|
|
|
|
// The library built into the executable as a resource (see CMakeLists.txt):
|
|
// checked against the hash recorded at build time and unpacked once per
|
|
// version into tdata/zsg/. Empty path: nothing is embedded (not an error).
|
|
[[nodiscard]] QString EmbeddedLibraryPath(QString *error) {
|
|
#ifdef Q_OS_WIN
|
|
if (sizeof(ZSG_NAIVE_EMBEDDED_SHA256) <= 1) {
|
|
return QString();
|
|
}
|
|
const auto resource = FindResourceW(nullptr, L"ZSGNAIVE", RT_RCDATA);
|
|
if (!resource) {
|
|
return QString();
|
|
}
|
|
const auto loaded = LoadResource(nullptr, resource);
|
|
const auto size = SizeofResource(nullptr, resource);
|
|
const auto data = loaded
|
|
? static_cast<const char*>(LockResource(loaded))
|
|
: nullptr;
|
|
if (!data || !size) {
|
|
*error = u"embedded zsgnaive resource is unreadable"_q;
|
|
return QString();
|
|
}
|
|
const auto bytes = QByteArray::fromRawData(data, int(size));
|
|
const auto hash = QCryptographicHash::hash(
|
|
bytes,
|
|
QCryptographicHash::Sha256).toHex();
|
|
if (hash != QByteArrayLiteral(ZSG_NAIVE_EMBEDDED_SHA256)) {
|
|
*error = u"embedded zsgnaive resource does not match its hash"_q;
|
|
return QString();
|
|
}
|
|
const auto directory = cWorkingDir() + u"tdata/zsg/"_q;
|
|
if (!QDir().mkpath(directory)) {
|
|
*error = u"cannot create "_q + directory;
|
|
return QString();
|
|
}
|
|
const auto path = directory
|
|
+ u"zsgnaive-"_q
|
|
+ QString::fromLatin1(hash.left(16))
|
|
+ u".dll"_q;
|
|
const auto present = [&] {
|
|
auto file = QFile(path);
|
|
return file.open(QIODevice::ReadOnly)
|
|
&& (QCryptographicHash::hash(
|
|
file.readAll(),
|
|
QCryptographicHash::Sha256).toHex() == hash);
|
|
};
|
|
if (!present()) {
|
|
auto file = QSaveFile(path);
|
|
if (!file.open(QIODevice::WriteOnly)
|
|
|| (file.write(bytes) != bytes.size())
|
|
|| !file.commit()) {
|
|
*error = u"cannot write "_q + path;
|
|
return QString();
|
|
}
|
|
}
|
|
// Older versions' copies; one that another running instance still has
|
|
// loaded just stays until it is closed.
|
|
const auto old = QDir(directory).entryInfoList(
|
|
{ u"zsgnaive-*.dll"_q },
|
|
QDir::Files);
|
|
for (const auto &entry : old) {
|
|
if (entry.absoluteFilePath() != QFileInfo(path).absoluteFilePath()) {
|
|
QFile::remove(entry.absoluteFilePath());
|
|
}
|
|
}
|
|
return path;
|
|
#else // Q_OS_WIN
|
|
return QString();
|
|
#endif // Q_OS_WIN
|
|
}
|
|
|
|
[[nodiscard]] Loaded Load() {
|
|
auto result = Loaded();
|
|
static QLibrary library;
|
|
auto embeddedError = QString();
|
|
const auto embedded = EmbeddedLibraryPath(&embeddedError);
|
|
if (!embeddedError.isEmpty()) {
|
|
result.error = embeddedError;
|
|
return result;
|
|
}
|
|
library.setFileName(embedded.isEmpty()
|
|
? (QCoreApplication::applicationDirPath() + u"/zsgnaive"_q)
|
|
: embedded);
|
|
if (!library.load()) {
|
|
result.error = u"zsgnaive library is not available: "_q
|
|
+ library.errorString();
|
|
return result;
|
|
}
|
|
auto missing = QString();
|
|
const auto resolve = [&](auto &slot, const char *name) {
|
|
slot = reinterpret_cast<std::remove_reference_t<decltype(slot)>>(
|
|
library.resolve(name));
|
|
if (!slot) {
|
|
missing += QString::fromLatin1(name) + u' ';
|
|
}
|
|
};
|
|
auto &api = result.api;
|
|
resolve(api.naiveNew, "zsg_naive_new");
|
|
resolve(api.naiveFree, "zsg_naive_free");
|
|
resolve(api.naiveState, "zsg_naive_state");
|
|
resolve(api.naiveError, "zsg_naive_error");
|
|
resolve(api.naiveFeedNetwork, "zsg_naive_feed_network");
|
|
resolve(api.naiveTakeNetwork, "zsg_naive_take_network");
|
|
resolve(api.naiveOpenTunnel, "zsg_naive_open_tunnel");
|
|
resolve(api.naiveTunnelState, "zsg_naive_tunnel_state");
|
|
resolve(api.naiveTunnelHttpStatus, "zsg_naive_tunnel_http_status");
|
|
resolve(api.naiveTunnelWrite, "zsg_naive_tunnel_write");
|
|
resolve(api.naiveTunnelRead, "zsg_naive_tunnel_read");
|
|
resolve(api.naiveTunnelEof, "zsg_naive_tunnel_eof");
|
|
resolve(api.naiveCloseTunnel, "zsg_naive_close_tunnel");
|
|
resolve(api.wsNew, "zsg_ws_new");
|
|
resolve(api.wsFree, "zsg_ws_free");
|
|
resolve(api.wsState, "zsg_ws_state");
|
|
resolve(api.wsError, "zsg_ws_error");
|
|
resolve(api.wsUpgradeStatus, "zsg_ws_upgrade_status");
|
|
resolve(api.wsUpgradeRetryable, "zsg_ws_upgrade_retryable");
|
|
resolve(api.wsRetryUpgrade, "zsg_ws_retry_upgrade");
|
|
resolve(api.wsFeedNetwork, "zsg_ws_feed_network");
|
|
resolve(api.wsTakeNetwork, "zsg_ws_take_network");
|
|
resolve(api.wsSend, "zsg_ws_send");
|
|
resolve(api.wsNextMessageSize, "zsg_ws_next_message_size");
|
|
resolve(api.wsPopMessage, "zsg_ws_pop_message");
|
|
resolve(api.wsClose, "zsg_ws_close");
|
|
resolve(api.wsDeflateNegotiated, "zsg_ws_deflate_negotiated");
|
|
resolve(api.profileVersion, "zsg_naive_profile_version");
|
|
// Optional: the WARP entry hop exists only in a library built after it was added.
|
|
api.warpStart = reinterpret_cast<decltype(api.warpStart)>(library.resolve("zsg_warp_start"));
|
|
api.warpState = reinterpret_cast<decltype(api.warpState)>(library.resolve("zsg_warp_state"));
|
|
api.warpStop = reinterpret_cast<decltype(api.warpStop)>(library.resolve("zsg_warp_stop"));
|
|
api.warpStats = reinterpret_cast<decltype(api.warpStats)>(library.resolve("zsg_warp_stats"));
|
|
api.warpKeypair = reinterpret_cast<decltype(api.warpKeypair)>(library.resolve("zsg_warp_keypair"));
|
|
api.warpStart2 = reinterpret_cast<decltype(api.warpStart2)>(library.resolve("zsg_warp_start2"));
|
|
api.warpMasqueKeypair = reinterpret_cast<decltype(api.warpMasqueKeypair)>(library.resolve("zsg_warp_masque_keypair"));
|
|
api.warpTransport = reinterpret_cast<decltype(api.warpTransport)>(library.resolve("zsg_warp_transport"));
|
|
api.warpScanNow = reinterpret_cast<decltype(api.warpScanNow)>(library.resolve("zsg_warp_scan_now"));
|
|
api.warpScanStatus = reinterpret_cast<decltype(api.warpScanStatus)>(library.resolve("zsg_warp_scan_status"));
|
|
api.warpEndpoint = reinterpret_cast<decltype(api.warpEndpoint)>(library.resolve("zsg_warp_endpoint"));
|
|
if (!missing.isEmpty()) {
|
|
result.error = u"zsgnaive library lacks: "_q + missing;
|
|
return result;
|
|
}
|
|
result.ok = true;
|
|
LOG(("Naive: library loaded, Chrome profile %1.").arg(
|
|
QString::fromLatin1(api.profileVersion())));
|
|
return result;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
const Api *LoadApi(QString *error) {
|
|
static const auto loaded = Load();
|
|
if (!loaded.ok && error) {
|
|
*error = loaded.error;
|
|
}
|
|
return loaded.ok ? &loaded.api : nullptr;
|
|
}
|
|
|
|
const Trust &SystemTrust() {
|
|
static const auto trust = [] {
|
|
auto result = Trust();
|
|
const auto system = QSslConfiguration::systemCaCertificates();
|
|
for (const auto &certificate : system) {
|
|
auto der = certificate.toDer();
|
|
if (!der.isEmpty()) {
|
|
result.der.push_back(std::move(der));
|
|
}
|
|
}
|
|
for (const auto &der : result.der) {
|
|
result.certs.push_back({
|
|
reinterpret_cast<const uint8_t*>(der.constData()),
|
|
size_t(der.size()),
|
|
});
|
|
}
|
|
result.pem = QByteArray(kIsrgRootX1);
|
|
LOG(("Naive: %1 system roots + ISRG Root X1.").arg(
|
|
result.der.size()));
|
|
return result;
|
|
}();
|
|
return trust;
|
|
}
|
|
|
|
NaiveClientConfig::NaiveClientConfig(
|
|
const QString &host,
|
|
int port,
|
|
const QString &user,
|
|
const QString &password)
|
|
: _host(host.toUtf8())
|
|
, _user(user.toUtf8())
|
|
, _password(password.toUtf8()) {
|
|
const auto &trust = SystemTrust();
|
|
const auto &identity = BrowserIdentity();
|
|
_config = NaiveConfig();
|
|
_config.host = _host.constData();
|
|
_config.port = uint16_t(port ? port : 443);
|
|
_config.user = _user.constData();
|
|
_config.password = _password.constData();
|
|
_config.extra_roots_pem = trust.pem.constData();
|
|
_config.extra_roots_der = trust.certs.data();
|
|
_config.extra_roots_der_count = trust.certs.size();
|
|
_config.user_agent = identity.userAgent;
|
|
_config.sec_ch_ua = identity.secChUa;
|
|
_config.sec_ch_ua_mobile = identity.secChUaMobile;
|
|
_config.sec_ch_ua_platform = identity.secChUaPlatform;
|
|
_config.send_preamble = 1;
|
|
}
|
|
|
|
const Identity &BrowserIdentity() {
|
|
#if defined Q_OS_WIN
|
|
static const auto result = Identity{
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
|
"(KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36",
|
|
"\"Chromium\";v=\"158\", \"Not.A/Brand\";v=\"24\", "
|
|
"\"Google Chrome\";v=\"158\"",
|
|
"?0",
|
|
"\"Windows\"",
|
|
};
|
|
#elif defined Q_OS_MAC
|
|
static const auto result = Identity{
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
|
|
"(KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36",
|
|
"\"Chromium\";v=\"158\", \"Not.A/Brand\";v=\"24\", "
|
|
"\"Google Chrome\";v=\"158\"",
|
|
"?0",
|
|
"\"macOS\"",
|
|
};
|
|
#else
|
|
static const auto result = Identity{
|
|
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
|
|
"(KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36",
|
|
"\"Chromium\";v=\"158\", \"Not.A/Brand\";v=\"24\", "
|
|
"\"Google Chrome\";v=\"158\"",
|
|
"?0",
|
|
"\"Linux\"",
|
|
};
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
} // namespace Zsg::Naive
|