ZaStoGram/Tools/check_wss_official_default.py
loop-uh eb435ec451
Some checks failed
Build three ZaStoGram APKs / build (arm64-v8a, ZaStoGram-standalone-arm64-v8a, Arm64, arm64) (push) Failing after 47s
Build three ZaStoGram APKs / build (armeabi-v7a, ZaStoGram-standalone-armeabi-v7a, Armv7, armv7) (push) Failing after 53s
ZaStoGram source guards / guards (push) Failing after 17s
Build three ZaStoGram APKs / build (x86, ZaStoGram-standalone-x86, X86, x86) (push) Failing after 48s
WSS: Cloudflare fronts send the zone as TLS server name, plain ws:// step, self-updating front list
Every front put kwsN.<zone> into the clear-text SNI, one pattern a network can
match to cut all of them at once. The zone is now the SNI and kwsN.<zone>
travels only in the encrypted Host header (101 on 40/40 front x DC2/DC4).

- A plain ws:// step (port 80) follows the TLS fronts, with its own health
  record (cdn-plain-kwsN); a pooled spare stays on its step.
- The front catalog is refreshed from the tg-ws-proxy list (GitHub, jsDelivr
  mirror) by WssFrontsUpdater at most daily into wss_fronts.txt; native
  validates it as a whole and a front without a baked address is dialled by name.
- naive-core pinned to 1f9bbfd (sni and plain fields); host tests cover TLS,
  plain and the catalog; guards updated for the new DC203 chain.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 12:41:26 -05:00

112 lines
7.1 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[1]
SHARED_CONFIG = ROOT / "TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java"
PROXY_LIST = ROOT / "TMessagesProj/src/main/java/org/telegram/ui/ProxyListActivity.java"
WSS_CPP = ROOT / "TMessagesProj/jni/tgnet/wss/WssSocket.cpp"
CONNECTION_CPP = ROOT / "TMessagesProj/jni/tgnet/Connection.cpp"
SOCKET_CPP = ROOT / "TMessagesProj/jni/tgnet/ConnectionSocket.cpp"
CONNECTIONS_JAVA = ROOT / "TMessagesProj/src/main/java/org/telegram/tgnet/ConnectionsManager.java"
FILE_LOADER = ROOT / "TMessagesProj/src/main/java/org/telegram/messenger/FileLoadOperation.java"
def require(condition: bool, message: str) -> None:
if not condition:
print(f"FAIL: {message}", file=sys.stderr)
sys.exit(1)
def main() -> None:
shared_config = SHARED_CONFIG.read_text(encoding="utf-8", errors="replace")
proxy_list = PROXY_LIST.read_text(encoding="utf-8", errors="replace")
wss_cpp = WSS_CPP.read_text(encoding="utf-8", errors="replace")
connection_cpp = CONNECTION_CPP.read_text(encoding="utf-8", errors="replace")
socket_cpp = SOCKET_CPP.read_text(encoding="utf-8", errors="replace")
connections_java = CONNECTIONS_JAVA.read_text(encoding="utf-8", errors="replace")
file_loader = FILE_LOADER.read_text(encoding="utf-8", errors="replace")
require('preferences.getBoolean("wssTransportEnabled", true)' in shared_config
and "WSS включён по умолчанию" in shared_config,
"WSS must be enabled by default while keeping an explicit user choice")
require('preferences.getInt("wssTransportMode"' in shared_config,
"legacy WSS mode must migrate to the checkbox")
require('.remove("wssHost")' in shared_config and '.remove("wssPath")' in shared_config,
"custom gateway keys must be removed during migration")
require("UseWssTransport" in proxy_list and "wssTransportEnabled" in proxy_list,
"the official transport must be exposed as a normal checkbox")
require('prefix = "kws" + std::to_string(dcId)' in wss_cpp,
"official WSS must generate Telegram DC1-DC5 relay names")
require("dcId < 1 || dcId > 5" in wss_cpp,
"official WSS relay catalog must cover exactly DC1-DC5")
require('mediaConnection ? "-1.web.telegram.org"' in wss_cpp,
"official WSS must select the media relay variant")
require("getDatacenterId(), isMediaConnection);" in connection_cpp
and "wssMediaRoute" not in connection_cpp,
"WSS relay selection must follow the selected auth realm; uploads use regular kwsN")
require("forceProxyLikeInitForWss" not in connection_cpp
and "if (useSecret != 0)" in connection_cpp,
"direct WSS must not inject the MTProxy-only DC marker")
require("dcId < 1 || dcId > 5" in wss_cpp and "testBackend" in wss_cpp,
"unsupported/test DCs must stay on their normal transport")
require("officialRelayIpsForDc" in wss_cpp
and '{"149.154.167.220", "149.154.167.99"}' in wss_cpp
and "result.relayHost = selectRelayAddress(result.network, dcId)" in wss_cpp
and "result.relayHostFallback = result.domain" in wss_cpp
and "result.viaFallback = preferFallback(result)" in wss_cpp,
"official WSS must use direct Telegram ingress with an automatic hostname fallback")
require("if (!moveToNextRelayAddress(routeConfig))" in wss_cpp
and "!isCdn(routeConfig) && !moveToNextRelayAddress(routeConfig)" in wss_cpp,
"a failed relay address must hand the DC to its other ingress before the DNS name")
require("dcId == kNoRelayDcId" in wss_cpp
and "return CdnRoute(network, dcId, route) || CdnRoute(network, dcId, route, false, true)\n"
" || TunnelRoute(network, dcAddress, route) || DirectRoute(network, dcId, route)\n"
" || CdnRoute(network, dcId, route, true);" in wss_cpp,
"DC203 must go fronts -> plain fronts -> tunnel -> guarded direct -> fronts ignoring suppression")
require("if (CdnRoute(result.network, dcId, route) || CdnRoute(result.network, dcId, route, false, true)\n"
" || TunnelRoute(result.network, dcAddress, route)\n"
" || DirectRoute(result.network, dcId, route)) {" in wss_cpp
and "return CdnRoute(result.network, dcId, route, true);" in wss_cpp,
"a DC with a closed relay must try guarded direct TCP only after the fronts and the tunnel, "
"and fall back to the fronts, never to unguarded direct TCP")
# A front is dialled with the zone as the TLS server name, never kwsN.<zone> (one pattern a network can
# match to cut every front); the kwsN name travels only in the encrypted Host header. The plain ws://
# step is a separate route with its own health record.
require("config.sni = frontSni.c_str();" in wss_cpp
and "isCdn(routeConfig) && !routeConfig.plain" in wss_cpp
and "config.plain = routeConfig.plain ? 1 : 0;" in wss_cpp,
"Cloudflare fronts must send the zone, not kwsN.<zone>, as the TLS server name")
require("result.relayPort = plain ? 80 : 443;" in wss_cpp
and '(plain ? "cdn-plain-" : "cdn-") + prefix' in wss_cpp,
"the plain ws:// front step must use port 80 and its own health record")
require("CdnRoute(stale.network, stale.cdnDcId, fresh, true, stale.plain)" in wss_cpp,
"a pooled spare must follow its front to the next slot of the SAME step (plain stays plain)")
require("wss_fronts.txt" in wss_cpp and "parseCdnCatalog" in wss_cpp,
"the front catalog must read the refreshed list and validate it as a whole")
require("if (routeSuppressed(network, name)) {" in wss_cpp
and '"direct-dc"' in wss_cpp
and "suppressLocked(route, health, now, failure);" in wss_cpp
and "kDirectProofBytes" in wss_cpp,
"direct TCP must be paused after one failure per DC and network and proven only by volume")
require("selectedWssRoute.direct" in socket_cpp
and "checkDirectStepTimeout(now)" in socket_cpp
and "WSS_TCP_CONNECT_TIMEOUT_MS" in socket_cpp
and "finishDirectStep(reason);" in socket_cpp
and "noteDirectStepSent((uint32_t) sentLength);" in socket_cpp,
"the direct step must get the WSS watchdogs and report one verdict when it closes")
require("if (isCurrentTransportWss())" in connection_cpp
and "useSecret = 0" in connection_cpp,
"direct WSS must not inherit a TCP dcOption or MTProxy secret")
require("supportsCdnFileRedirects()" in connections_java
and "return !isWssTransportActive()" in connections_java,
"network layer must disable CDN redirects when WSS cannot route CDN DC ids")
require("req.cdn_supported = ConnectionsManager.supportsCdnFileRedirects()" in file_loader,
"file downloads must advertise the active transport's CDN capability")
print("WSS official route guard passed.")
if __name__ == "__main__":
main()