MTProto Proxy Architecture
An architectural guide for a Zig-based MTProto proxy, detailing its event-driven networking core and relay logic.
Install
mkdir -p .claude/skills/mtproto-proxy-architecture && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/14135" && unzip -o skill.zip -d .claude/skills/mtproto-proxy-architecture && rm skill.zipInstalls to .claude/skills/mtproto-proxy-architecture
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Current architecture and design rules for the Linux epoll-based Zig MTProto proxy.Key capabilities
- →Handle socket I/O with a single-threaded network core
- →Parse TLS ClientHello records incrementally
- →Validate TLS-auth HMAC and SNI
- →Build and send fake ServerHello messages
- →Resolve routes to direct DC or MiddleProxy
- →Perform C2S and S2C relay pipeline transformations
How it works
The proxy accepts client sockets, incrementally parses TLS and MTProto handshakes, validates security parameters, resolves routing, and then transforms and relays traffic between the client and the destination.
Inputs & outputs
When to use MTProto Proxy Architecture
- →Reviewing proxy architecture
- →Configuring proxy binaries
- →Debugging relay pipelines
- →Deploying MTProto infrastructure
About this skill
MTProto Proxy Architecture (Current)
This project is a production MTProto proxy in Zig with FakeTLS fronting, active anti-replay protection, and Linux-first deployment.
Build Artifacts
The project produces two binaries via build.zig:
| Binary | Source | Install Path | Purpose |
|---|---|---|---|
mtproto-proxy | src/main.zig | /opt/mtproto-proxy/mtproto-proxy | The proxy server |
mtbuddy | src/ctl/main.zig | /usr/local/bin/mtbuddy | Installer & control panel (TUI) |
Cross-compile for production: make build (or zig build -Doptimize=ReleaseFast -Dtarget=x86_64-linux -Dcpu=x86_64_v3).
Runtime Model
- Single-threaded network core: one Linux
epollevent loop handles socket I/O. - No thread-per-connection: connection handling is state-machine driven.
- Connection slots are allocated lazily and reused.
- Per-connection heavy buffers are on-demand, not permanently embedded in idle slots.
Primary file: src/proxy/proxy.zig.
Core Flow
- Accept client socket (non-blocking).
- Parse TLS ClientHello record header/body incrementally.
- Validate TLS-auth HMAC.
- Validate SNI against configured
tls_domain. - Build/send fake
ServerHello(+ optional split/desync behavior). - Read 64-byte MTProto obfuscation handshake.
- Resolve route: direct DC or MiddleProxy route.
- Enter relay mode (C2S/S2C transform pipeline).
Relay Pipeline
C2S
- TLS unwrap
- client AES-CTR decrypt
- transport encapsulation:
- direct DC: AES-CTR encrypt for DC
- MiddleProxy:
RPC_PROXY_REQframing + CBC layer
S2C
- transport decapsulation/decrypt
- client-side AES-CTR encrypt (unless fast-mode path)
- TLS application record wrapping
MiddleProxy
- Runtime path is non-blocking and event-loop integrated.
- Legacy blocking handshake helpers were removed.
- Endpoint/secret metadata refresh is periodic.
Anti-Replay
- Handshake digest is validated with timestamp skew window.
- Replay cache key uses canonical HMAC value from validation path.
Message Queue Strategy
- Write path uses chained blocks +
writevflush. - Queue head uses index progression (not repeated
orderedRemove(0)hot-path shifts).
mtbuddy (Installer & Control Panel)
Source tree: src/ctl/. Interactive TUI with raw terminal mode, arrow-key navigation, and Unicode box-drawing.
Key modules:
| Module | Purpose |
|---|---|
main.zig | CLI arg dispatch + interactive menu |
tui.zig | Terminal UI engine (raw mode, rendering) |
install.zig | Fresh proxy installation |
update.zig | Self-update from GitHub releases |
tunnel.zig | AmneziaWG tunnel + network namespace setup |
dashboard.zig | Monitoring dashboard installer |
recovery.zig | Service recovery & masking health |
uninstall.zig | Clean uninstall |
i18n.zig | English / Russian localization |
Deployment Layout (Server)
/opt/mtproto-proxy/
├── mtproto-proxy # proxy binary
├── config.toml # runtime configuration
├── env.sh # optional env vars (TAG, etc.)
└── monitor/ # dashboard assets (optional)
/usr/local/bin/mtbuddy # installer/control binary
/etc/systemd/system/mtproto-proxy.service
Platform Scope
- Linux-only runtime target.
- macOS is supported for development/cross-compile, not for production runtime.
Design Principles
- Keep the hot path non-blocking.
- Favor explicit state transitions over hidden control flow.
- Keep security checks in the handshake path strict and cheap.
- Avoid stale parallel implementations of the same protocol path.
Limitations
- →Linux-only runtime target
How it compares
This proxy uses a single-threaded epoll network core and state-machine driven connection handling for Linux performance, unlike traditional thread-per-connection models.
Compared to similar skills
MTProto Proxy Architecture side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| MTProto Proxy Architecture (this skill) | 0 | 4mo | No flags | Advanced |
| domain-cloud-native | 1 | 6mo | No flags | Advanced |
| simulator-management | 0 | 6mo | No flags | Intermediate |
| senior-devops | 7 | 7mo | Review | Advanced |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
domain-cloud-native
actionbook
Use when building cloud-native apps. Keywords: kubernetes, k8s, docker, container, grpc, tonic, microservice, service mesh, observability, tracing, metrics, health check, cloud, deployment, 云原生, 微服务, 容器
simulator-management
pmaojo
description: Capabilities for managing and troubleshooting the API Simulator.
senior-devops
davila7
Comprehensive DevOps skill for CI/CD, infrastructure automation, containerization, and cloud platforms (AWS, GCP, Azure). Includes pipeline setup, infrastructure as code, deployment automation, and monitoring. Use when setting up pipelines, deploying applications, managing infrastructure, implementing monitoring, or optimizing deployment processes.
server-management
davila7
Server management principles and decision-making. Process management, monitoring strategy, and scaling decisions. Teaches thinking, not commands.
azure-cloud-services
ComeOnOliver
This skill provides expert guidance for Azure Cloud Services. Covers troubleshooting, best practices, decision making, limits & quotas, security, configuration, integrations & coding patterns, and deployment. It combines local quick-reference content with remote documentation fet
mimir
grafana
>