Clash Verge TUN Mode: Avoiding the Performance Pitfalls of Layer-3 Tunnels
In the previous article, “Set Up an Install-Free IKEv2 Layer-3 Tunnel to Bypass Cursor Region Restrictions”, we introduced how to use an IKEv2 layer-3 tunnel to bypass geo-restrictions of software like Cursor. Although the IKEv2 solution has the advantage of not requiring a client installation, layer-3 tunnels themselves have some inherent performance issues.
This article will introduce a more efficient alternative: using Clash Verge’s TUN mode together with the VLESS protocol, which keeps things transparent to applications while avoiding the performance overhead brought by layer-3 tunnels.
Performance Pitfalls of Layer-3 Tunnels
The IKEv2 + VLESS/WebSocket architecture from the previous article has three main performance issues:
- TCP over TCP: application-layer TCP is encapsulated and transported inside the tunnel’s TCP (WebSocket), so two layers of TCP state machines interfere with each other
- Head-of-Line Blocking: multiple application connections are multiplexed over the same tunnel; packet loss on one connection blocks all connections
- QoS Limits on Long Connections: a single long-lived connection is easily throttled by middleboxes on the network
The TCP over TCP Problem
In the previous article’s architecture, we used VLESS over WebSocket over TLS to encapsulate IKEv2’s UDP traffic. Although IKEv2 itself uses UDP, when upper-layer applications use TCP, the entire data path ends up with TCP over TCP:
1 | 应用层 TCP 连接(如 Cursor 的 HTTP/2) |
This nested TCP connection leads to serious performance issues:
Retransmission Storms: when packet loss occurs on the underlying network, both the outer TCP (WebSocket connection) and the inner TCP (application connection) trigger their retransmission mechanisms. Packets retransmitted by the outer TCP may include data that the inner TCP has already successfully transmitted, causing大量无效重传.
Conflicting Congestion Control: the two TCP layers each maintain independent congestion windows and RTT estimates. When network conditions change, their congestion control algorithms may make conflicting decisions, reducing bandwidth utilization.
Accumulated Latency: each TCP layer has its own acknowledgment mechanism and timeout-based retransmission, and the resulting latency stacks up layer by layer.
Head-of-Line Blocking Problem
When multiple upper-layer connections are multiplexed over a single tunnel, Head-of-Line Blocking occurs:
1 | 连接 A 的数据包: [A1] [A2] [A3] |
If a packet in the tunnel (say A2) is lost, the tunnel’s TCP connection pauses while waiting for retransmission. Even if B1, C1, A3, and B2 have already arrived, they cannot be read by the upper-layer applications, because TCP guarantees in-order delivery. This means:
- Connections B and C are blocked by packet loss on connection A
- Even if the data of connections B and C is intact, they still have to wait
This problem is especially severe on high-latency, high-loss cross-border links.
QoS Limits on Long Connections
Middleboxes on the network (such as ISP traffic management devices, firewalls, etc.) often apply QoS (Quality of Service) throttling to connections that have existed for a long time or that transmit large amounts of data:
Connection Duration Detection: some middleboxes mark connections that live beyond a threshold (e.g., 30 minutes), and then lower their priority or limit their bandwidth.
Traffic Pattern Identification: a single connection with sustained high bandwidth is easily identified as “abnormal traffic”, triggering throttling policies.
Fairness Scheduling: to ensure fairness, ISPs may limit how much bandwidth a single connection can occupy.
Layer-3 tunnels usually maintain one or just a few long-lived connections to carry all traffic, which makes them particularly prone to hitting these limits.
Design Principles of Clash Verge TUN Mode
Clash Verge’s TUN mode offers an elegant solution: it keeps things transparent to applications while avoiding the aforementioned performance issues.
What Is TUN Mode?
TUN (network tunnel) is a virtual network device provided by the operating system. When TUN mode is enabled:
- Clash creates a virtual network interface (e.g.,
utunortun0) - The system routing table is modified to direct traffic to this virtual interface
- All traffic passing through this virtual interface is captured and processed by Clash
This means any application’s network traffic is transparently proxied, without needing to configure browser or per-application proxy settings.
Key Design: User-Space Protocol Stack
Clash’s TUN mode uses a user-space TCP/IP protocol stack (such as gVisor’s netstack), which is the key to avoiding the TCP over TCP problem:
1 | 传统三层隧道: |
One-to-One Connection Mapping
Another key design in Clash TUN mode is the one-to-one mapping between upper-layer connections and underlying tunnel connections:
- Each application TCP connection corresponds to an independent proxy connection
- Each application UDP session corresponds to an independent proxy connection
This design brings significant benefits:
Eliminates Head-of-Line Blocking: packet loss on connection A does not affect connections B and C, because they use independent underlying connections.
Avoids TCP over TCP: the user-space protocol stack terminates the application’s TCP connection locally, extracts the application-layer data, and then sends it over a new TCP connection (VLESS/WebSocket). Although the underlying transport is still TCP, the application’s TCP state machine (sequence numbers, acknowledgments, retransmissions) is not encapsulated and transported, so there is no interference between two TCP state machines.
Evades Long-Connection Limits: each application connection exists independently, and its lifecycle matches application behavior, so there is no single ultra-long-lived connection.
Better Congestion Control: another important benefit of terminating the application’s TCP connection is that the long cross-border path is split into multiple shorter segments. In a layer-3 tunnel, the application’s TCP congestion control has to deal with the entire path from the client to the US server and then to the target website, resulting in a long RTT and poor bandwidth utilization. In Clash TUN mode, since Clash terminates the application-layer TCP connection, the RTT on each segment is shorter, congestion control can converge to optimal bandwidth more quickly, and overall throughput is higher.
Transparent to Applications
Unlike traditional HTTP/SOCKS proxies, TUN mode is completely transparent to applications:
| Feature | HTTP/SOCKS Proxy | TUN Mode |
|---|---|---|
| Requires app support | Yes | No |
| Requires proxy configuration | Yes | No |
| Supports TCP | Yes | Yes |
| Supports UDP | Partial (SOCKS5) | Yes |
| Supports ICMP | No | Yes |
| Supports HTTP/2 | Depends on app | Yes |
This means various hard-to-configure applications can work normally:
- Cursor / Claude Code: these AI coding tools use HTTP/2 long connections and do not offer proxy configuration options
- Docker Container: when developing with Dev Container mode, containers need to download dependencies from GitHub, npm, PyPI, etc., and configuring proxies is very troublesome
- Command-line Tools: tools like
git clone,npm install, andpip installcan be configured to use a proxy via environment variables, but each tool has different configuration methods and it’s easy to miss some
TUN mode operates at the system network layer, is transparent to all applications, and requires no per-app configuration.
How DNS Works
DNS handling is another key component of Clash TUN mode.
Why Does DNS Need Special Handling?
In traditional HTTP/SOCKS proxies, applications usually perform DNS resolution first, then send the resolved IP address to the proxy server. This leads to two problems:
- DNS Leakage: DNS queries are sent directly to the local DNS server (usually the ISP’s), exposing the domains you access
- DNS Poisoning: some domains get incorrect IP addresses, making them inaccessible
Fake IP Mode
Clash’s TUN mode uses Fake IP mode by default to solve these problems:
1 | 1. 应用请求解析 google.com |
Advantages of this design:
- Zero DNS leakage: no real DNS resolution happens locally
- Bypass DNS poisoning: DNS resolution is performed in the proxy server’s network environment
- Supports domain-based routing: even if the application only sends IP addresses, Clash still knows the original domain name
- Reduced latency: eliminates the local DNS resolution round-trip
DNS Hijacking
To make Fake IP mode work, Clash needs to hijack system DNS requests. In TUN mode, this is done as follows:
- The TUN virtual interface takes over all network traffic
- Any request sent to a DNS server (port 53) is intercepted by Clash
- Clash’s built-in DNS server handles these requests
This is why, after enabling TUN mode, DNS requests are always handled by Clash regardless of the system’s DNS settings.
Architecture Design
Following the idea from “Cleverly Using Hong Kong Relay to Build a Smooth and Stable China–US Layer-3 Multi-Hop Tunnel”, we also adopt a three-hop architecture here, but using the VLESS protocol end-to-end:
1 | +---------------------------+ |
Why Use Three Hops?
Just like with the IKEv2 solution, the three-hop architecture has the following advantages:
- Mainland China -> Hong Kong: leverage cloud providers’ optimized routes, with low latency and high stability
- Hong Kong -> United States: Hong Kong is a major network hub in Asia and has high-quality international bandwidth
- US Exit: the final IP is in the United States, which bypasses geo-restrictions
Why use VLESS end-to-end?
- Unified protocol: Simplifies configuration and maintenance
- Great performance: VLESS is lightweight with low overhead
- WebSocket transport: Masquerades as normal HTTPS traffic, strong penetration capability
- TLS encryption: End-to-end encryption, high security
Preparation
1. Server requirements
- Mainland China server (in this article we use
cn-gw.01.meas an example) - Hong Kong server (in this article we use
hk.01.meas an example) - US server (in this article we use
us-west.01.meas an example)
2. Domain names and certificates
Prepare a domain name and request an SSL certificate for each server:
1 | # 在每台服务器上安装 certbot 并申请证书 |
3. Generate UUID
Generate a UUID for each hop’s VLESS connection:
1 | export UUID_CLIENT_CN=$(uuidgen) |
Step 1: Configure the US egress server
1.1 Install Xray
1 | sudo apt update |
1.2 Configure Xray
Create /usr/local/etc/xray/config.json:
1 | sudo tee /usr/local/etc/xray/config.json > /dev/null << EOF |
1.3 Start the service
1 | sudo systemctl enable --now xray |
Step 2: Configure the Hong Kong relay server
2.1 Install Xray
1 | sudo apt update |
2.2 Configure Xray
Create /usr/local/etc/xray/config.json:
1 | sudo tee /usr/local/etc/xray/config.json > /dev/null << EOF |
2.3 Start the service
1 | sudo systemctl enable --now xray |
Step 3: Configure the mainland China ingress server
3.1 Install Xray
1 | sudo apt update |
3.2 Configure Xray
Create /usr/local/etc/xray/config.json:
1 | sudo tee /usr/local/etc/xray/config.json > /dev/null << EOF |
3.3 Start the service
1 | sudo systemctl enable --now xray |
Step 4: Configure the Clash Verge client
4.1 Install Clash Verge
Download and install the version suitable for your OS from Clash Verge Rev.
4.2 Create the configuration file
The Clash Verge configuration is very simple: you only need to define the proxy servers and rules. TUN mode, DNS, and other settings can be enabled in the GUI and don’t need to be written into the config file.
1 | proxies: |
Configuration notes:
- type: vless: Use the VLESS protocol
- tls: true: Enable TLS encryption
- udp: true: Support UDP forwarding (needed for DNS, QUIC, etc.)
- network: ws: Use WebSocket transport
- rules: MATCH,Auto: Send all traffic through the proxy
If you need direct access for domestic traffic, you can add split-routing rules:
1 | rules: |
4.3 Enable TUN mode
- Open Clash Verge
- Import the above configuration file
- In Settings, enable TUN Mode (Clash Verge will automatically configure DNS hijacking and Fake IP)
- macOS/Linux users need to grant administrator privileges
- Windows users need to run as administrator
After enabling TUN mode, Clash Verge will automatically:
- Create a TUN virtual network interface
- Configure the system routing table
- Enable DNS hijacking and Fake IP mode
Verification and testing
1 | # 检查出口 IP |
Comparison with the IKEv2 solution
| Feature | IKEv2 + VLESS/WS tunnel | Clash Verge TUN + VLESS |
|---|---|---|
| Client installation | Not required | Required |
| TCP over TCP | Exists (app TCP over IKEv2 over WebSocket) | None (user-space stack terminates app TCP) |
| HOL Blocking | Exists (multiple connections multiplexed over a single tunnel) | None (one-to-one connection mapping) |
| Long-connection limits | Easily triggered (single long connection) | Harder to trigger (independent short connections) |
| Congestion control | End-to-end long path, high RTT | Segmented short paths, low RTT, higher bandwidth |
| HTTP/2 support | Yes | Yes |
| UDP support | Yes | Yes |
| Transparent to apps | Yes | Yes |
| Traffic splitting rules | Not supported | Supported |
| Suitable scenarios | No-client-install scenarios | High performance |
Summary
The Clash Verge TUN mode + VLESS solution, through its user-space protocol stack and one-to-one connection mapping, cleverly avoids the traditional L3 tunnel issues of TCP over TCP and head-of-line blocking. More importantly, by terminating application-layer TCP, it splits a long cross-border path into multiple shorter segments, each with independent congestion control, shorter RTT, and better bandwidth utilization. At the same time, independent short connections are less likely to trigger QoS limits imposed by middleboxes.
Compared with the IKEv2 solution, this approach requires installing a client, but in return provides better performance and flexible traffic-splitting rule support. For developers who need high-performance proxies (especially users of tools like Cursor), this is a solution worth considering.
Each solution has its pros and cons:
- IKEv2: Suitable when you don’t want to install extra software
- Clash Verge TUN: Suitable when you pursue performance and need split-routing rules
Choose the solution that best matches your actual needs.