How trojan-gfw/trojan works
Project type and purpose
• This is a C++ network proxy tool (called “Trojan”) designed to bypass the Great Firewall by encapsulating traffic in TLS and mimicking common protocols.
• It’s neither a traditional web app nor a database-backed service; it’s a high-performance client/server networking application with pluggable protocols and optional MySQL-based authentication.
File structure
• Top-level build and config: CMakeLists.txt, cmake/, Dockerfile, azure-pipelines.yml, .gitpod.*
• Source code under src/ subdivided into:
• core/ – configuration, logging, versioning, generic service abstractions
• proto/ – protocol definitions (Trojan requests, SOCKS5, UDP payloads)
• session/ – concrete session handlers (client, server, forward, NAT, UDP)
• ssl/ – TLS session abstraction with default parameters
• main.cpp – CLI entrypoint
• examples/ – JSON template files for client/server configuration
• docs/ – user and protocol documentation
• tests/ – smoke tests invoking client/server configurations
README for additional insights
• The tool supports multiple “protocols over TLS,” uses Boost.Asio, OpenSSL, and optionally MySQL for authentication.
• It’s built with CMake and tested via shell scripts (LinuxSmokeTest).
Components
a. Main components:
• Client binary (Trojan client)
• Server binary (Trojan server)
• Core library (config loader, logger, service manager)
• Protocol layer (Trojan protocol, SOCKS5, UDP)
• Session layer (ClientSession, ServerSession, ForwardSession, NATSession, UDPForwardSession)
• TLS/SSL layer (SSLSession, policy defaults)
• External services: MySQL (optional), DNS, network endpoints
b. Relationships and interactions:
• Client → TLS handshake → Server
• Within each binary: main() invokes core::Service to spawn listener(s)
• Incoming TLS records passed to ssl::SSLSession → parsed by proto layer → handed off to appropriate session
• Session uses core::Config for routing and log for diagnostics
• Optional auth module queries MySQL over secure channel
c. Architectural patterns / principles:
• Layered architecture: Transport (TCP/TLS) → Protocol → Session → Service orchestration
• Plugin-like handling of different session types (polymorphic Session interface)
• Configuration-driven: examples/*.json define listener, upstream targets, passwords, plugin paths
d. Key technologies:
• Boost.Asio (async I/O)
• OpenSSL (TLS)
• CMake (build)
• Optional: libmysqlclient