Core Principles · Deep Analysis

How does Clash work?

Explaining the complete proxy link from traffic entry to rule matching and then to protocol outbound—helping you truly understand the mechanisms behind rule routing, TUN transparent proxy, and DNS leak protection, rather than just knowing how to paste configurations.

How is a request processed in Clash?

When you open a URL in a browser, the traffic is not sent directly. Clash listens on one or more ports locally, intercepts all outbound connections, and after judgment by the rule engine, decides whether to take "Direct" or "Proxy Outbound."

The entire link is divided into five stages, each with a clear point of intervention by Clash:

  1. Traffic Interception—The application initiates a connection, and the system proxy or TUN virtual network card forwards the traffic to the Clash process.
  2. DNS Resolution—Clash's built-in DNS module intervenes and decides whether to use Fake-IP or real IP resolution based on configuration.
  3. Rule Matching—Conditions such as domain, IP, and process name are compared one by one from top to bottom according to the rules list.
  4. Policy Group Scheduling—After hitting a rule, it is handed over to the corresponding policy group (Proxy / DIRECT / REJECT) for processing.
  5. Protocol Outbound—Uses the protocol of the selected node (SS / VMess / Trojan...) to complete encrypted transmission.

Clash Rule Engine: Priorities, Match Types, and Execution Order

The rule engine is Clash's most core capability. It matches line-by-line from the first to the last in the rules list. The first rule that matches takes effect immediately, and subsequent rules are no longer executed—order determines everything.

Domain Rules

The most commonly used type, accurate to the domain or suffix level, suitable for split-routing specific websites.

DOMAINgoogle.comProxy
DOMAIN-SUFFIXgithub.comProxy
DOMAIN-KEYWORDyoutubeProxy
  • DOMAIN:Exact match, only hits the complete domain name.
  • DOMAIN-SUFFIX:Suffix match, simultaneously hits all subdomains.
  • DOMAIN-KEYWORD:Keyword match, hits if the domain name contains the word.

IP / Geographic Rules

Based on target IP address or GeoIP database judgment, it is the core rule for "Domestic Direct, Foreign Proxy."

IP-CIDR192.168.0.0/16DIRECT
IP-CIDR6fe80::/10DIRECT
GEOIPCNDIRECT
  • IP-CIDR:Exact match of IPv4 subnet.
  • IP-CIDR6:Exact match of IPv6 subnet.
  • GEOIP:Calls the Country.mmdb library to query the attribution.

Process / Port Rules

Routes traffic according to application process name or port number, suitable for separately configuring exits for specific software (such as development tools, game clients).

PROCESS-NAMEchromeProxy
SRC-PORT7890DIRECT
DST-PORT443Proxy
  • PROCESS-NAME:Matches by process name (better support on macOS / Windows).
  • DST-PORT:Matches by target port, commonly used for 80 / 443.

Rule Set (RULE-SET)

Packaging hundreds or thousands of rules into a remote subscription file, which the client pulls for periodic updates, is the standard practice for large-scale routing solutions.

# rule-providers define rule sets
RULE-SETgfwProxy
RULE-SETcn_domainDIRECT
  • Rule sets support YAML / text formats.
  • interval can be set for automatic periodic updates.
  • Common in the community: rule sets maintained by Loyalsoldier / blackmatrix7, etc.

Rule order determines routing results

Rules match sequentially from top to bottom. The first match takes effect, and subsequent rules are not executed. Common mistake: putting MATCH,DIRECT before GEOIP will cause all traffic to connect directly. Recommended order:

  1. Local / Private addresses (IP-CIDR 192.168.0.0/16) → DIRECT
  2. Domains clearly needing proxy / RULE-SET → Proxy policy group
  3. Domains clearly needing direct connection / RULE-SET → DIRECT
  4. GEOIP,CN,DIRECT—Direct if IP belongs to China.
  5. MATCH,Proxy—Fallback, unmatched traffic goes through proxy.

TUN Mode vs System Proxy: Differences and Use Cases

Clash supports two ways to take over traffic. Choosing the wrong mode is a common root cause for "certain apps not using the proxy":

System Proxy Mode
TUN Transparent Proxy
Principle
Registers an HTTP/SOCKS5 proxy address in the system, and applications actively connect to that address.
Creates a virtual network card to intercept all IP traffic at the network layer.
Coverage
Only supports apps with system proxy settings (browsers, most apps).
All outbound traffic from all processes, including command-line tools and game clients.
Permissions Required
Ordinary user permissions
Administrator / root permissions (to create virtual network card)
Use Cases
Daily browsing, simple proxy needs
Game acceleration, command-line tool proxy, global traffic takeover

TUN Mode Configuration Diagram

config.yaml
tun:
  enable: true
  stack: system # or gvisor
  auto-route: true
  auto-detect-interface: true
  dns-hijack:
    - any:53

stack optionsystem uses the system network stack for higher performance; gvisor user-space network stack has better compatibility. Mihomo kernel also supports mixed hybrid mode.

Clash DNS Working Principle: Fake-IP, Redir-Host, and DNS Leak Protection

DNS leak is the security blind spot proxy users most easily overlook—your traffic goes through the proxy, but DNS queries are sent in plaintext to your local ISP, exposing the domains you visit. Clash has a complete built-in DNS module to address this.

Fake-IP Mode

Recommended

After receiving a DNS request, Clash immediately returns a fake IP (such as 198.18.x.x), allowing the app to initiate a connection as quickly as possible. The actual DNS resolution is deferred until the proxy side, avoiding early domain leakage locally.

1 App asks: What is google.com's IP?
2 Clash returns fake IP 198.18.0.1
3 App connects to the fake IP; Clash intercepts and records the real domain.
4 Node side re-resolves and connects to the real server.
  • Faster connection (no need to wait for real DNS response)
  • Good leak prevention, domain doesn't pass through local DNS
  • Smoothest integration with the rule engine

Redir-Host Mode

Compatibility Mode

Real DNS resolution is completed locally first, and after the domain is resolved to a real IP, it's handed over to the rule engine for GEOIP matching. Better compatibility, but the DNS query itself may leak to the local ISP.

1 App asks: What is google.com's IP?
2 Clash queries upstream DNS for real IP
3 Real IP is returned; rule engine uses IP for GEOIP judgment.
4 Determines direct or proxy according to rules.
  • Better compatibility with special network environments
  • More accurate GEOIP rule matching (based on real IP)
  • DNS queries occur locally, leak risk exists.

DNS Configuration Reference

config.yaml — dns block
dns:
  enable: true
  enhanced-mode: fake-ip # or redir-host
  fake-ip-range: 198.18.0.1/16
  nameserver:
    - 8.8.8.8
    - 114.114.114.114
  fallback: # used when GEOIP != CN
    - tls://1.1.1.1:853
    - https://dns.google/dns-query
  fallback-filter:
    geoip: true
    geoip-code: CN

fallback with fallback-filter: When a domain resolves to a foreign IP, it's automatically re-resolved using encrypted DoT / DoH servers to prevent poisoning.

Proxy Protocols Supported by Clash: Principles and Selection Advice

The protocol determines the traffic encryption and obfuscation strategy. Clash natively supports multiple protocols, and the Mihomo (Clash Meta) kernel further extends support for next-generation protocols.

Shadowsocks(SS)

Classic & Stable

Uses symmetric encryption (AES / ChaCha20) to encrypt traffic, making it look like random data to resist Deep Packet Inspection (DPI). Simple protocol design, widest client support.

EncryptionAES-256-GCM / ChaCha20-Poly1305
Transport LayerTCP / UDP
Best ForDaily proxy, widespread nodes, best for entry-level

VMess

V2Ray Ecosystem

Proprietary protocol designed by the V2Ray project, adding timestamp verification and obfuscation capabilities beyond encryption. Can be used with WebSocket + TLS to disguise as HTTPS traffic, with strong anti-blocking capabilities.

EncryptionAES-128-GCM / ChaCha20-Poly1305
Transport LayerTCP / WebSocket / gRPC / HTTP/2
Best ForHigh-blocking environments, works well with CDN

Trojan

TLS Obfuscation

Directly transmits proxy data in the TLS layer, looking exactly like normal HTTPS without extra obfuscation. The server also provides real web responses to further hide proxy characteristics.

EncryptionTLS 1.3 (Certificate Validation)
Transport LayerTCP / WebSocket
Best ForRequires TLS obfuscation, traffic characteristics closest to HTTPS

Hysteria2

Mihomo Support

A new generation of high-performance protocols based on QUIC (UDP), significantly outperforming TCP protocol families in weak networks and high-packet-loss environments. Designed specifically for high-latency, unstable network scenarios.

Transport LayerQUIC (UDP-based)
AdvantagesFaster in weak network environments, strong anti-packet-loss capability
RequirementsRequires Mihomo (Clash Meta) Kernel

TUIC

Mihomo Support

Also based on QUIC, focusing on low latency and connection reuse. The 0-RTT handshake design reduces first-packet latency, suitable for response-speed sensitive scenarios (such as gaming, real-time audio/video).

Transport LayerQUIC (UDP) + 0-RTT
AdvantagesUltra-low first-packet latency, high connection reuse efficiency
RequirementsRequires Mihomo (Clash Meta) Kernel

WireGuard

Mihomo Support

Modern VPN protocol with minimal code and simple auditing, using ChaCha20 + Poly1305 encryption, and extremely high performance with kernel-level implementation. Mihomo supports using WireGuard as an outbound protocol.

EncryptionChaCha20-Poly1305 / Curve25519
Transport LayerUDP
RequirementsRequires Mihomo (Clash Meta) Kernel

Policy Groups Explained: URL-Test Auto-Speed, Fallback Failover, and Load Balance

Policy groups are the core mechanism for "packaging" rules and "scheduling" nodes. After a rule is hit, traffic is not sent directly to a node, but to a policy group—the policy group decides which node to use, enabling capabilities like automatic speed testing and failover.

select · Manual Selection

Users manually pick the current node from the node list, usually displayed directly on the client interface. Suitable for users wanting precise control over the exit.

type: select
proxies: [HK-01, SG-02, JP-03]

url-test · Auto Best Latency

Regularly sends HTTP probe requests to all nodes and automatically selects the node with the lowest response latency. It only switches when node latency exceeds the tolerance threshold, avoiding frequent jitter.

type: url-test
url: http://www.gstatic.com/generate_204
interval: 300
tolerance: 50

fallback · Auto Failover

Uses the first node in order; when it fails the health check, it automatically switches to the second, and so on. Suitable for scenarios requiring guaranteed stability.

type: fallback
url: http://www.gstatic.com/generate_204
interval: 180

load-balance · Load Balancing

Multiple nodes take turns carrying traffic, spreading connections across different nodes to improve overall throughput. Mihomo supports consistent-hashing to ensure the same session uses the same node.

type: load-balance
strategy: consistent-hashing
url: http://www.gstatic.com/generate_204

Clash Classic vs Mihomo (Clash Meta): Which One to Use?

Clash currently has two main branches, often used interchangeably in daily conversation, but with significant capability differences—especially in protocol support and TUN enhancement features.

Clash (Classic)

Dreamacro/clash
  • Shadowsocks / VMess / Trojan / SNELL
  • HTTP / SOCKS5 Proxy
  • Rule / Global / Direct Modes
  • Fake-IP / Redir-Host DNS
  • TUN Mode (Basic)
  • Hysteria2 / TUIC / WireGuard
  • Enhanced TUN (gVisor / mixed stack)
  • rule-providers Advanced Capabilities
VS

Mihomo(Clash Meta)

metacubex/mihomo
  • Shadowsocks / VMess / Trojan / SNELL
  • Hysteria2 / TUIC / WireGuard / VLESS
  • Rule / Global / Direct / Script Modes
  • Fake-IP + fallback-filter Enhanced DNS
  • Enhanced TUN (system / gVisor / mixed)
  • rule-providers Remote Rule Set Subscription
  • Sub-Rule, GeoSite Database
  • Multi-architecture: x86_64 / ARM / MIPS

Conclusion: New users should directly choose clients based on the Mihomo (Clash Meta) kernel (such as Clash Verge Rev, Mihomo Party, Clash Meta for Android) to get more complete protocol support and TUN capabilities without compromise. Classic Clash kernel clients remain stable, but continuous updates to the protocol ecosystem are concentrated in the Mihomo branch.

Core Principles FAQ

Why is Clash's rule order so important?
The rule engine matches line-by-line from top to bottom; the first hit takes effect and subsequent ones are not executed. If a broad MATCH is placed at the top, all rules below it will never be triggered. A reasonable order should be: Local Network → Explicit Proxy Domains → Explicit Direct Domains → GEOIP → MATCH Fallback.
Can Fake-IP cause problems with certain applications?
A few applications (such as certain games and LAN services) may handle the 198.18.x.x IP range specially, leading to anomalies. Clash provides a fake-ip-filter configuration item to exclude specific domains from Fake-IP, letting them use normal DNS resolution.
Why does the system slow down after TUN mode is enabled?
TUN mode intercepts all traffic and processes it in user-space, which introduces an extra layer of overhead compared to going directly through the kernel network stack. Mihomo's system stack has higher performance than gvisor. If machine performance is insufficient, you can enable system proxy for specific apps instead of enabling TUN globally.
Which is safer, VMess or Shadowsocks?
Both are comparable at the encryption level (both use AEAD encryption). The difference lies in traffic characteristics: Shadowsocks has relatively simple characteristics, while VMess paired with WebSocket+TLS can completely disguise as normal HTTPS, making it harder to identify and block in high-intensity detection environments.
What are the requirements for the url-test policy group's speed test URL?
The speed test URL should be fast and stable. Commonly used are Google's http://www.gstatic.com/generate_204 (returns an empty 204 response) or http://cp.cloudflare.com/generate_204. It is recommended to use an overseas address accessible via proxy to avoid false positives caused by domestic addresses.
What is Country.mmdb and how often should it be updated?
Country.mmdb is an IP attribution database in MaxMind GeoIP2 format. GEOIP rules rely on it to map IP addresses to country codes. It is recommended to update every 1-3 months. Some clients support automatic updates. You can get more accurate CN IP coverage from refined versions maintained by communities like Loyalsoldier.

Next Step: Download and Configure Clash

Choose the client that fits your platform, complete installation and subscription configuration in three steps, and experience smart routing immediately.