Skip to content
Edit this page

TUN Transparent Proxy

ClashRS provides a built-in userspace network stack (smoltcp / watfaq_netstack) for L3 TUN transparent proxying across Windows, Linux, macOS, FreeBSD, Android, and iOS.

Requires the tun Cargo feature (enabled in standard / default builds).

Native Mode vs. Mihomo-Compatible Mode

ClashRS automatically enters Mihomo-compatible TUN translation whenever any of the following fields is set:

  • stack, dns-mode, auto-detect-interface, or auto-route is present
  • strict-route: true
  • route-address or route-exclude-address is non-empty

When Mihomo-compatible mode is active, defaults adjust automatically:

Field Native Default Mihomo-Mode Default
gateway 198.18.0.1/24 Address from dns.fake-ip-range with /30 prefix (e.g. 198.18.0.1/30)
gateway-v6 None fdfe:dcba:9876::1/126 when top-level ipv6: true
auto-route None (uses route-all) true
auto-detect-interface None true
dns-hijack Disabled ["any:53"] (+ TUN subnet DNS host)
dns-mode None "" (Auto: hijack when auto-route && !macos, else disabled)

All tun Fields

Key Alias Type Default Description
enable — bool false Enable the TUN inbound.
device-id device-url, device string "utun1989" Device specifier: fd://<N> (Unix pre-opened fd), dev://<NAME> (Windows also supports dev://NAME?guid=<uuid>), or bare <NAME>.
stack — mips | system | gvisor | mixed None Mihomo compatibility field. All values use the same native Rust watfaq_netstack stack.
dns-mode — "" (Auto) | disabled | native | hijack None (Auto in Mihomo mode) Controls TUN adapter DNS setup and WFP DNS blocking (especially on Windows).
auto-detect-interface — bool None (true in Mihomo mode) Auto-detect default physical outbound interface when top-level interface is unset. On Windows, true dynamically tracks default route changes (1s cache).
strict-route — bool false Strict anti-leak routing (effective only when auto-route resolves to true). Currently implemented via Windows Filtering Platform (WFP).
auto-redirect — bool false Mihomo compatibility field. Setting auto-redirect: true on Linux returns an InvalidConfig error (ClashRS has no Linux redirect backend). Accepted and ignored on other platforms.
exclude-interface — list[string] [] Windows-only compiled field accepted for shared-profile compatibility (no-op). Not defined on non-Windows platforms.
gateway — IPv4 CIDR 198.18.0.1/24 (or /30 in Mihomo mode) TUN IPv4 address and subnet.
gateway-v6 — IPv6 CIDR None (fdfe:dcba:9876::1/126 in Mihomo mode + ipv6: true) TUN IPv6 address and subnet (setting this enables TUN IPv6).
routes — list[CIDR] None Custom routes installed in native mode.
route-all — bool false Route all traffic through TUN in native mode.
auto-route — bool None (true in Mihomo mode) Mihomo-style automatic route installation. Falls back to route-all when unset.
route-address — list[CIDR] [] Subnets routed into TUN (requires auto-route: true). Defaults to 0.0.0.0/0 (and ::/0 with IPv6) when empty and route-exclude-address is set.
route-exclude-address — list[CIDR] [] Subnets carved out from route-address / full routes (requires auto-route: true).
mtu — u16 65535 (Windows), 1500 (other platforms) TUN interface MTU.
so-mark — SocketMark Inherits top-level routing-mark Linux/Android SO_MARK for policy-routing loop avoidance.
route-table — u32 2468 Linux policy routing table ID; bypass FIB number on FreeBSD.
dns-hijack — bool or list[string] Disabled (native) / ["any:53"] (Mihomo) DNS interception rules inside TUN. See below.

dns-hijack Formats

  • Boolean (true / false): true intercepts UDP port 53 only (not TCP 53).
  • List (e.g. ["any:53", "tcp://1.1.1.1:53"]): Strips any optional scheme:// prefix and matches both TCP and UDP. Accepts any:<port> or IP:port (bracketed [::1]:53 for IPv6). ClashRS also automatically appends the TUN subnet DNS host address (e.g. 198.18.0.2:53).

Unsupported Mihomo TUN Keys

inet4-address and inet6-address are not defined — use gateway and gateway-v6 instead. endpoint-independent-nat, gso, udp-timeout, include-uid, exclude-uid, include-package, and exclude-package are also not defined.

Platform Notes

  • Run as Administrator.
  • Uses Wintun: place matching wintun.dll beside clash-rs.exe, or compile with --features wintun-embed (MSVC).
  • Default MTU is 65535. Adapter GUID can be pinned with dev://ClashRS?guid=<uuid> (otherwise deterministically derived via UUIDv5 from the adapter name).
  • Full routing installs split /1 routes (0.0.0.0/1, 128.0.0.0/1, plus ::/1 and 8000::/1 when IPv6 is enabled).
  • With strict-route: true and auto-route: true, ClashRS installs session-scoped WFP (Windows Filtering Platform) filters that permit ClashRS and TUN traffic, block non-TUN port 53 when dns-mode resolves to hijack, and block non-TUN IPv6 (while preserving ::1 and ICMPv6 NDP) if gateway-v6 is disabled.
  • Requires root or CAP_NET_ADMIN and the ip (iproute2) command.
  • Full routing uses policy routing with route-table (default 2468) and so-mark (e.g. ip rule add not fwmark <mark> table 2468).
  • Attempts IFF_VNET_HDR TUN offload (TSO/USO) on newly created devices and falls back cleanly if unsupported.
  • auto-redirect: true is rejected with an error.
  • Named devices must start with utun (e.g. utun1989).
  • Full routing installs /1 routes plus -ifscope <default_iface> bypass routes to prevent loops.
  • dns-mode: auto resolves to disabled on macOS (system DNS is not automatically rewritten).
  • Uses multiple FIBs (setfib(2) + net.fibs): FIB 0 routes through TUN while the ClashRS process moves into the bypass FIB (route-table). State is saved to a temp state file for crash recovery.
  • Only fd://<N> is supported (passing a named device fails).
  • Routes are managed by the host VPN service (VpnService / NEPacketTunnelProvider).
  • In --service-mode, fd:// is disallowed.

Examples

ipv6: true

dns:
  enable: true
  ipv6: true
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  default-nameserver:
    - 223.5.5.5
    - 8.8.8.8
  nameserver:
    - https://1.1.1.1/dns-query

tun:
  enable: true
  stack: mixed
  auto-route: true
  auto-detect-interface: true
  strict-route: true
  dns-hijack:
    - any:53
  route-exclude-address:
    - 192.168.0.0/16
    - 10.0.0.0/8
routing-mark: 6666

tun:
  enable: true
  device-id: "dev://utun1989"
  gateway: 198.18.0.1/24
  route-all: true
  route-table: 2468
  so-mark: 6666
  dns-hijack:
    - any:53