Overview
TheProxyBackend implements a SOCKS5 proxy server that intercepts network connections and applies DPI bypass techniques through the TurkeyDPI engine pipeline. This is the primary backend for most use cases.
ProxyBackend
The main proxy backend structure that implements theBackend trait.
Structure
Fields
Atomic flag indicating whether the backend is currently running
Channel sender for graceful shutdown signaling
Current proxy configuration settings
Handle to the running proxy accept loop task
Counter tracking the number of active connections
Methods
new()
Creates a newProxyBackend instance.
ProxyBackend in a stopped state.
Example:
start()
Starts the proxy backend and begins accepting connections.Backend configuration including:
engine_config: DPI bypass engine configurationmax_queue_size: Maximum packet queue sizebackend_settings: Must beBackendSettings::Proxy(ProxySettings)
BackendHandle containing:
shutdown_tx: Channel for triggering shutdownstats: Reference to statistics collectorpipeline: Reference to the DPI bypass pipeline
BackendError::AlreadyRunning- Backend is already runningBackendError::NotSupported- Wrong backend settings type providedBackendError::BindFailed- Failed to bind to listen addressBackendError::Engine- Engine initialization failed
stop()
Stops the proxy backend and closes all connections.BackendError::NotRunning- Backend is not currently running
is_running()
Checks if the backend is currently running.true if the backend is running, false otherwise.
is_supported()
Checks if the proxy backend is supported on this platform.true (proxy backend is supported on all platforms).
name()
Returns the backend name."proxy"
ProxySettings
Configuration settings for the proxy backend.Fields
Address and port to listen on for incoming proxy connectionsDefault:
127.0.0.1:1080Type of proxy protocol to useDefault:
ProxyType::Socks5Maximum number of concurrent connections allowedDefault:
1000Connection timeout in secondsDefault:
300ProxyType
Supported proxy protocol types.Variants
SOCKS5 proxy protocol (fully implemented)
HTTP CONNECT proxy protocol (not yet implemented)
SOCKS5 Implementation
The proxy backend includes a full SOCKS5 server implementation that:- Handles authentication: Supports “no authentication” method (0x00)
- Supports all address types:
- IPv4 addresses (ATYP 0x01)
- Domain names (ATYP 0x03) with DNS resolution
- IPv6 addresses (ATYP 0x04)
- Processes CONNECT commands: Only CONNECT (0x01) is supported
- Applies DPI bypass: Outbound data flows through the pipeline
- Relays bidirectional traffic: Efficient tokio::select! based relay
Flow Key Generation
For each SOCKS5 connection, a flow key is generated:Connection Lifecycle
- Version negotiation: Validate SOCKS5 version (0x05)
- Method selection: Choose authentication method
- Request parsing: Parse CONNECT request and extract destination
- Connection establishment: Connect to destination address
- Response: Send success or error response
- Stream relay: Bidirectionally relay data with pipeline processing
Packet Processing
Outbound traffic (client → remote) is processed through the engine pipeline:Connection Management
TheConnectionGuard ensures accurate connection counting:
active_connections >= max_connections.
Error Handling
The proxy backend handles various error conditions:- Invalid SOCKS version: Returns method selection error (0xFF)
- Unsupported command: Returns command not supported (0x07)
- Unsupported address type: Returns address type not supported (0x08)
- Connection failed: Returns general failure (0x05) or host unreachable (0x04)
- Max connections: Silently rejects connection and logs warning
Example: Complete Usage
Performance Characteristics
- Async I/O: All operations use tokio async runtime
- Per-connection tasks: Each connection runs in its own tokio task
- Zero-copy where possible: Uses
BytesMutfor efficient buffer management - Automatic cleanup: Connection guards ensure proper resource cleanup
- Flow tracking: Pipeline maintains per-flow state for stateful DPI evasion
Platform Support
The proxy backend is supported on all platforms:- Linux
- macOS
- Windows