- Rust 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| src | ||
| tests | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| LICENSE | ||
| README.md | ||
mtproxy-rs
A Rust implementation of Telegram's MTProto proxy protocol.
Features
- Single-threaded daemon server with Tokio.
- Obfuscated MTProto client connections with 16-byte proxy secrets.
- Abridged, Intermediate, and Padded Intermediate transports.
- Multiple client secrets through repeated
--mtproto-secretoptions. - Telegram
proxy-secretandproxy-multi.confinput compatibility. - IPv4 and IPv6 upstream key derivation and routing.
- Optional local-to-public address translation with
--nat-info. - One reusable upstream RPC connection per data center.
- Upstream readiness timeout, periodic ping/pong, and a 30-second idle reuse window.
- Timestamped, colored structured logs with
RUST_LOGfiltering. - Ctrl-C shutdown handling.
FakeTLS is not implemented.
Requirements
- Rust 1.95 or newer.
- A Telegram upstream proxy secret file containing 32 to 256 bytes.
- A Telegram
proxy-multi.confroute file. - At least one random 16-byte client secret encoded as 32 hexadecimal digits.
Download Telegram's current upstream inputs:
curl -fsS https://core.telegram.org/getProxySecret -o proxy-secret
curl -fsS https://core.telegram.org/getProxyConfig -o proxy-multi.conf
Generate a client secret with:
openssl rand -hex 16
Telegram can update the route configuration; refresh proxy-multi.conf operationally before restarting the daemon.
Keep both client and upstream secrets private. Client secrets are passed on the command line and may be visible to other users through process inspection on some operating systems.
Build
cargo build --release
The executable is written to target/release/mtproxy.
Run
target/release/mtproxy \
--port 8443 \
--mtproto-secret 00112233445566778899aabbccddeeff \
--proxy-secret proxy-secret \
--nat-info 192.168.0.1:203.0.113.10 \
proxy-multi.conf
--aes-pwd is accepted as an alias for --proxy-secret.
Command-line options
Usage: mtproxy [OPTIONS] --mtproto-secret <CLIENT_SECRETS> --proxy-secret <PROXY_SECRET> <PROXY_MULTI_CONF>
Arguments:
<PROXY_MULTI_CONF> Telegram data-center route configuration
Options:
-p, --port <PORT> Listening port [default: 8443]
--listen <LISTEN> Listening IP address [default: ::]
-S, --mtproto-secret <CLIENT_SECRETS> 32-hex-digit client secret; repeatable
--proxy-secret <PROXY_SECRET> Telegram upstream secret file [alias: --aes-pwd]
--nat-info <LOCAL:GLOBAL> Address translation; repeatable
-h, --help Print help
The two addresses in --nat-info must use the same IP family. Repeat the option to configure more than one mapping.
Configuration files
Client secrets
Each --mtproto-secret value must contain exactly 32 hexadecimal digits. Repeat the option to accept multiple secrets:
mtproxy \
-S 00112233445566778899aabbccddeeff \
-S ffeeddccbbaa99887766554433221100 \
--proxy-secret proxy-secret \
proxy-multi.conf
Upstream secret
--proxy-secret names the binary secret used for encrypted RPC communication with Telegram data centers. The implementation accepts files from 32 through 256 bytes and redacts the secret from Debug output.
Route configuration
The route file uses Telegram's semicolon-delimited proxy-multi.conf syntax. The implementation requires at least one default directive and one proxy_for directive:
default 2;
proxy_for 2 149.154.167.50:8888;
proxy_for -2 [2001:0b28:f23d:f001::a]:8888;
Host names are resolved during startup. Duplicate routes, unknown directives, malformed addresses, missing defaults, and route files larger than 1 MiB are rejected.
The compatibility directives timeout, min_connections, max_connections, and force_probability are currently parsed but ignored. Pool timing is controlled by fixed implementation defaults.
Runtime behavior
The daemon uses a current-thread Tokio runtime and a local task set. Client sessions sharing a data center also share its upstream RPC actor and connection.
Current timing defaults are:
- 10 seconds to receive a client's initial MTProto packet.
- 3 seconds for an upstream connection and handshake to become ready.
- A ping every 5 seconds while an upstream connection is active.
- 30 seconds before an unused upstream connection is closed, allowing a new client to reuse it.
Configuration files and DNS results are loaded once at startup. Live configuration reload is not implemented.
Logging
Logs are written to standard error with timestamps and ANSI colors. The default filter is info.
Use RUST_LOG to change verbosity:
RUST_LOG=warn target/release/mtproxy [OPTIONS] proxy-multi.conf
RUST_LOG=mtproxy=debug target/release/mtproxy [OPTIONS] proxy-multi.conf
RUST_LOG=off target/release/mtproxy [OPTIONS] proxy-multi.conf
Connection logs include client addresses, data-center identifiers, connection generations, and errors. Secrets, encryption keys, and packet payloads are not logged.
Library
The mtproxy crate exposes the implementation modules through src/lib.rs, including client cryptography, configuration, pooling, protocol codecs, relay logic, DH, and upstream cryptography. Integration tests use these public interfaces.
Local verification
The repository intentionally contains only local unit and integration tests. It does not include tests that connect to official Telegram servers.
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test --all-targets
The local suites cover client obfuscation, all supported transports, configuration parsing, RPC control and records, proxy RPC layouts, upstream cryptography, connection pooling, timeouts, and keepalives.
Limitations
- No FakeTLS transport.
- No live configuration or DNS reload.
- No built-in privilege dropping, daemonization, metrics endpoint, or HTTP administration interface.
- One current-thread runtime; expensive or blocking work must not be added to the async execution path.
- Some
proxy-multi.confpool-policy directives are accepted for compatibility but not applied.
License
Licensed under the Apache License 2.0. See LICENSE.