Router-Level Clash Setup: OpenWrt + Clash for Whole-Home Proxy
Table of Contents
Why Deploy Clash at the Router Level
Deploying a proxy client on individual devices is tedious and often impossible for IoT gadgets, smart TVs, and gaming consoles. By configuring a clash router setup on OpenWrt, you create a transparent proxy that intercepts all network traffic at the gateway level. This approach functions similarly to a network-wide TUN interface, providing a seamless experience without requiring client software on end devices. If you are familiar with how local clients handle traffic, you can read our TUN mode global proxy guide to understand the underlying mechanics of virtual network interfaces and how they differ from traditional HTTP/SOCKS proxies.
Router-level deployment ensures that every byte of traffic leaving your LAN is evaluated by Clash rules. This is particularly useful for enforcing ad-blocking, routing specific streaming services through dedicated nodes, and maintaining privacy for devices that do not support custom proxy configurations.
Hardware and OpenWrt Prerequisites
Before initiating the clash router setup, you must verify that your hardware can handle the cryptographic overhead of modern proxy protocols like VLESS, TUIC, and Reality. Routers typically feature low-power ARM or MIPS processors, making efficiency paramount.
- CPU: Dual-core ARM64 (e.g., Cortex-A53) or equivalent MIPS. Single-core devices will struggle with AES-GCM or ChaCha20 decryption.
- RAM: 256MB minimum, 512MB recommended. Clash Meta requires memory for connection tracking and rule evaluation.
- Storage: At least 100MB of free space on the overlay filesystem for the binary, GeoIP databases, and configuration files.
- Firmware: OpenWrt 22.03 or 23.05 stable releases.
If your router has only 16MB of flash storage, you cannot install Clash Meta directly. You must either expand your storage using a USB drive formatted as ext4, or use a router with 32MB+ flash. Running out of space during an update will brick your configuration.
Installing Clash Meta (mihomo) on OpenWrt
The original Clash core is archived. We will use mihomo, the community-driven successor. The installation process on OpenWrt is virtually identical to a standard headless server. If you need a refresher on downloading and managing the binary on a standard Linux environment, refer to our Clash Linux CLI setup tutorial.
Connect to your router via SSH. Determine your architecture using uname -m. For most modern routers, this will be aarch64 or x86_64. Download the appropriate release.
cd /tmp
wget https://github.com/MetaCubeX/mihomo/releases/download/v1.18.10/mihomo-linux-arm64-v1.18.10.gz
gunzip mihomo-linux-arm64-v1.18.10.gz
chmod +x mihomo-linux-arm64-v1.18.10
Move the binary to a persistent location and create the configuration directory.
mkdir -p /etc/clash
mv mihomo-linux-arm64-v1.18.10 /etc/clash/mihomo
mkdir -p /etc/clash/config
Configuring Transparent Proxy and Firewall Rules
This is the most critical phase of the clash router setup. We must instruct the Linux kernel to redirect TCP traffic and use TPROXY for UDP traffic, ensuring that gaming and VoIP protocols are not broken.
Create a script at /etc/clash/clash_fw.sh to handle the iptables rules. OpenWrt 23.05 uses nftables by default, but iptables-nft is fully compatible and widely used for transparent proxy scripts.
#!/bin/sh
# Flush existing rules
iptables -t nat -F CLASH_TCP 2>/dev/null
iptables -t mangle -F CLASH_UDP 2>/dev/null
# Create custom chains
iptables -t nat -N CLASH_TCP
iptables -t mangle -N CLASH_UDP
# Ignore LAN subnets to prevent routing loops
iptables -t nat -A CLASH_TCP -d 192.168.0.0/16 -j RETURN
iptables -t nat -A CLASH_TCP -d 10.0.0.0/8 -j RETURN
iptables -t nat -A CLASH_TCP -d 127.0.0.0/8 -j RETURN
# Redirect TCP to Clash mixed port
iptables -t nat -A CLASH_TCP -p tcp -j REDIRECT --to-ports 7892
# TPROXY UDP to Clash mixed port
iptables -t mangle -A CLASH_UDP -p udp -j TPROXY --on-port 7892 --tproxy-mark 0x01/0x01
# Apply chains to PREROUTING
iptables -t nat -A PREROUTING -p tcp -j CLASH_TCP
iptables -t mangle -A PREROUTING -p udp -j CLASH_UDP
Make the script executable and ensure it runs on boot by adding it to /etc/rc.local before the exit 0 line.
Advanced DNS Hijacking and Resolution
If DNS queries bypass Clash, your device will resolve proxy domains to blocked or incorrect IP addresses, causing connection failures. We must hijack port 53 traffic and forward it to the Clash internal DNS server.
Edit /etc/dnsmasq.conf to redirect all DNS queries to the Clash DNS listener.
no-resolv
server=127.0.0.1#7891
listen-address=127.0.0.1
Next, configure your config.yaml to handle DNS resolution efficiently. For a router, you should enable fake-ip mode to reduce latency and prevent DNS leaks.
dns:
enable: true
listen: 0.0.0.0:7891
enhanced-mode: fake-ip
fake-ip-range: 198.18.0.1/16
nameserver:
- 223.5.5.5
- 119.29.29.29
fallback:
- tls://8.8.8.8
- tls://1.1.1.1
If you use local services like Home Assistant or a NAS, fake-ip mode might break them. You must exclude local domains in your Clash config using fake-ip-filter. For complex routing scenarios where you need to bypass the proxy for specific local subnets while proxying others, consult our split routing rules guide to structure your YAML efficiently.
Optimizing Performance and Resource Management
Running a full proxy stack on a router requires aggressive optimization. Every megabyte of RAM and CPU cycle counts.
- Disable Process Matching: Set
find-process-mode: offin your global config. Matching processes on a router is impossible and consumes massive CPU resources. - Optimize TUN Stack: If using TUN mode instead of transparent proxy, set
tun.stack: system. Thegvisorstack introduces unnecessary overhead on low-power ARM CPUs. - Reduce Log Output: Set
log-level: warning. Writing continuous info logs to flash storage will degrade your router's NAND chip over time. - Use Geodata instead of GeoIP: Download the
GeoLite2-Country.mmdbandgeoip.datfiles. Mihomo's geodata loader is highly optimized and uses less memory than parsing massive text-based rule providers.
Troubleshooting Common Router Proxy Issues
Even with a perfect configuration, router-level proxies can encounter edge cases. Here is how to resolve the most frequent issues.
If your router fails to boot after applying firewall rules, it is likely due to a syntax error in clash_fw.sh causing iptables to hang. Always test your firewall script manually in SSH before adding it to rc.local. If you are locked out, boot into OpenWrt failsafe mode and delete the script.
If you suspect DNS leaks, run tcpdump -i br-lan port 53 on the router. You should only see DNS queries originating from the router itself (directed to 127.0.0.1:7891). If you see queries from client IPs going directly to external DNS servers, your dnsmasq configuration is incorrect or clients have hardcoded DNS settings.
By following these advanced configurations, your OpenWrt router will serve as a robust, high-performance gateway, providing a unified and secure network environment for every device in your home.