$ show Flowseal/tg-ws-proxy
This repository implements a local MTProto proxy for Telegram Desktop, designed to bypass network censorship by wrapping Telegram's protocol in WebSocket connections, often routed through Cloudflare. It is a fully-featured desktop application with CLI and GUI modes, providing a robust solution for circumventing DPI-based blocking. The architecture is a monolithic asyncio application with a clear separation of concerns: the `proxy/` package handles the core protocol translation (MTProto to WebSocket, Fake TLS, HTTP/2 multiplexing), the `ui/` package provides a cross-platform tray interface using CustomTkinter, and `utils/` contains supporting infrastructure like update management and diagnostics. The system employs a sophisticated fallback chain (WebSocket pool -> Cloudflare Proxy -> Cloudflare Worker -> Direct TCP) to ensure reliability. The codebase demonstrates high engineering craftsmanship. It features several genuinely reusable components, including a custom `httpx` HTTP/2 transport, a robust connection pool with exponential backoff, and an atomic self-update mechanism with rollback. While tightly coupled to Telegram's specific protocol, the underlying patterns for network resource management, protocol obfuscation, and desktop application lifecycle are well-isolated and could be extracted for other projects. The extensive test suite and professional CI/CD pipeline further attest to its quality.
The LLM identified 5 files worth highlighting for reuse:
Implements a custom httpx.AsyncBaseTransport using the h2 library. Handles HTTP/2 framing, flow control, stream multiplexing, and cancellation. Demonstrates deep integration between httpx and h2, providing a robust foundation for custom HTTP/2 clients. Highly reusable for any project requiring low-level HTTP/2 control.
A generic connection pool with warmup, rotation, stale-connection eviction, and exponential backoff. Manages WebSocket and Cloudflare Worker connections. The core pooling logic is protocol-agnostic and represents a production-grade pattern for managing reusable network resources.
Implements CfH2Pool which multiplexes many native TCP connections over a single HTTP/2 connection to Cloudflare. Features _HttpLane and _HttpChannel for capacity management, request replay with bounded attempts, and retry budgets. A sophisticated async state machine for high-performance proxying.
A complete self-update mechanism. Fetches release info from GitHub API, downloads assets with mandatory SHA-256 verification, performs atomic file replacement, and implements rollback on failure. The rollback logic is a particularly strong pattern for desktop application updates.
An AES-CTR implementation that prefers the cryptography library but falls back to ctypes-wrapped OpenSSL libcrypto. This solves a common dependency issue in embedded or restricted environments. A clean, practical, and highly reusable utility module.