Skip to content

TUN — Xray-core ​

Xray-core ships a TUN inbound at proxy/tun. It creates a TUN device, reads raw packets, and terminates them in Xray-core's own lightweight TCP/IP stack before dispatching the resulting connections to outbounds. Opt-in helpers — gateway, dns, autoSystemRoutingTable, autoOutboundsInterface, autoSystemDnsToGateway and autoSystemWfpBlockLeak — let Xray install system routing-table entries, bind outbounds to a physical interface, point the system resolver into the TUN and block traffic that would bypass it; without them you set up routes, DNS, and firewall rules yourself.

It is still lighter-weight than the sing-box / mihomo TUN stacks: there is no strict-route, auto-redirect, or UID/package filtering. For those, use sing-box or mihomo as a TUN front-end and route their outbound through Xray.

Inbound ​

settings for "protocol": "tun":

FieldTypeDefaultAllowed valuesDescription
namestring(auto-generated)<interface name>Name of the TUN device, created at startup. When empty, an unused utun<N> name (N in 10–1024) is picked automatically.
descstringWintun<string>Device description passed to the Wintun adapter on creation (Windows only). Defaults to "Wintun" when empty.
mtuuint321500<bytes>Maximum transmission unit for the device.
gateway[]string—<IP/prefix> | …Gateway address(es) assigned to the TUN interface, in prefix form (172.19.0.1/30).
dns[]string—<ip address> | …DNS server address(es) configured on the TUN adapter. Windows only — other systems ignore it (on Linux / FreeBSD see autoSystemDnsToGateway).
userLeveluint320<uint32>Default policy level for connections received on this inbound.
autoSystemRoutingTable[]string—<CIDR> | …Destination prefixes (0.0.0.0/0, ::/0, …) for which Xray installs system routes into the device while it runs. Supplying this list enables auto-routing.
autoOutboundsInterface*stringauto<interface name> | autoPhysical interface that outbound connections bind to, preventing routing loops. Defaults to "auto" when autoSystemRoutingTable is set.
autoSystemDnsToGatewayboolfalsetrue | falseLinux / FreeBSD: point the system resolver into the TUN so that system lookups are answered by Xray. Needs gateway, and a routing rule sending the TUN's port 53 to a dns outbound — see Notes.
autoSystemWfpBlockLeak[]string[]dns | windnsclient | misconfigtun | all | allexceptlan | icmp | tcp[/<ports>] | udp[/<ports>] | <port>[-<port>] | <CIDR>:<value>Windows: Windows Filtering Platform filters that let the listed traffic of every program but Xray leave only through the TUN. Needs autoSystemRoutingTable; values that keep the system's DNS inside the TUN also need dns — see Notes.

Source: infra/conf/tun.go:16-27 · pinned at v26.11.9 (676eaf6)

Example ​

json
{
  "inbounds": [{
    "tag": "tun-in",
    "protocol": "tun",
    "settings": {
      "name": "xray0",
      "mtu": 9000
    }
  }]
}

To let Xray manage routing itself, add autoSystemRoutingTable (and optionally pin the outbound interface):

json
{
  "inbounds": [{
    "tag": "tun-in",
    "protocol": "tun",
    "settings": {
      "name": "xray0",
      "mtu": 1500,
      "autoSystemRoutingTable": ["0.0.0.0/0", "::/0"],
      "autoOutboundsInterface": "auto"
    }
  }]
}

To hand the system resolver to Xray as well (Linux / FreeBSD), add autoSystemDnsToGateway with a gateway, and route the TUN's port 53 to a dns outbound:

json
{
  "dns": { "servers": ["https://1.1.1.1/dns-query"] },
  "inbounds": [{
    "tag": "tun-in",
    "protocol": "tun",
    "settings": {
      "name": "xray0",
      "gateway": ["172.19.0.1/30"],
      "autoSystemRoutingTable": ["0.0.0.0/0", "::/0"],
      "autoSystemDnsToGateway": true
    }
  }],
  "outbounds": [
    { "tag": "proxy", "protocol": "vless", "settings": { /* ... */ } },
    { "tag": "dns-out", "protocol": "dns" }
  ],
  "routing": {
    "rules": [
      { "type": "field", "inboundTag": ["tun-in"], "port": 53, "outboundTag": "dns-out" }
    ]
  }
}

On Windows, set the adapter's dns instead (keeping the same routing rule) and block leaks around the TUN:

json
{
  "inbounds": [{
    "tag": "tun-in",
    "protocol": "tun",
    "settings": {
      "gateway": ["172.19.0.1/30"],
      "dns": ["172.19.0.2"],
      "autoSystemRoutingTable": ["0.0.0.0/0", "::/0"],
      "autoSystemWfpBlockLeak": ["dns", "misconfigtun"]
    }
  }]
}

Or configure the system networking manually instead:

sh
# Linux example
ip addr add 198.18.0.1/30 dev xray0
ip link set xray0 up

# Route everything through xray0 (skip your management subnet)
ip route add 1/1 dev xray0
ip route add 128/1 dev xray0

# DNS hijack
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-port 5353
# (then have an Xray dokodemo-door inbound on 5353)

Notes ​

  • Packets are handled by Xray-core's own lightweight TCP/IP stack: TCP and UDP become connections that are dispatched to outbounds like any other inbound's, and ICMP echo requests (ping) are answered locally.
  • Common usage on Linux: pair with dokodemo-door inbounds and a hand-crafted iptables ruleset for the transparent-proxy parts, or set autoSystemRoutingTable to have Xray install the routes itself.
  • autoSystemRoutingTable makes Xray manage system routing-table entries automatically; when it is present and autoOutboundsInterface is omitted, the outbound interface defaults to auto. gateway and dns set the address and (on Windows) the resolver(s) on the device.
  • On Windows/macOS, the Xray TUN inbound creates a wintun / utun device. With autoSystemRoutingTable it can wire up routing for you; otherwise script it yourself. FreeBSD is supported too, including autoSystemRoutingTable / autoOutboundsInterface. Many users on those platforms prefer sing-box or mihomo for the richer TUN feature set.
  • autoSystemDnsToGateway (Linux / FreeBSD) hands the system resolver to Xray. On Linux it goes through systemd-resolved (resolvectl, systemd 240 or newer) and points it at the address right after the first gateway address (172.19.0.1/30 → 172.19.0.2); on FreeBSD it goes through resolvconf and points it at the gateway address itself. Xray does not start when this would break or loop name resolution: a query from the TUN to that address on port 53 must be routed to a dns outbound, and the dns block must list servers, none of them localhost. The setting is reverted when Xray stops (not after SIGKILL — then run resolvectl revert <interface>).
  • autoSystemWfpBlockLeak (Windows) adds Windows Filtering Platform filters so that, for every program except Xray itself, the listed traffic can only leave through the TUN. Values (case-insensitive): dns (TCP / UDP ports 53 and 853, plus windnsclient); windnsclient (the Windows DNS Client service, which on Windows 11 / Server 2022 may also query over HTTPS or TLS on any port); misconfigtun (all traffic of an IP version that autoSystemRoutingTable has no route for); all (everything, both directions); allexceptlan (like all, but private, link-local, multicast and broadcast addresses stay reachable — includes dns); icmp; tcp / udp, alone or with /<port> or /<low>-<high>; or a bare port / port range for both TCP and UDP. A CIDR in front limits a value to those addresses (1.1.1.0/24:53, [::/0]:all). Loopback, DHCP and IPv6 neighbor discovery always stay allowed, and if the filters cannot be added Xray does not start. It needs autoSystemRoutingTable, and values that keep the system's DNS inside the TUN (all, allexceptlan, dns, windnsclient, anything covering port 53) also need dns. Upstream recommends at least ["dns", "misconfigtun"].
  • On Windows, when IPv6 is turned off through the DisabledComponents registry value, errors setting up IPv6 for gateway, dns and autoSystemRoutingTable are only logged and the TUN still starts (misconfigtun then blocks IPv6 outside the TUN whatever the routes); otherwise such errors stop the TUN from starting.

Cross-core notes ​

  • sing-box has a feature-complete TUN inbound with auto_route, auto_redirect, strict_route, route_address, UID/package filtering, and per-platform stack choice (gVisor / system / mixed). See TUN — sing-box.
  • mihomo has an equivalent feature-complete TUN inbound at the top-level tun: block with the same option set under kebab-case names. See TUN — mihomo.

Source: infra/conf/tun.go:16-27 · v26.11.9 (676eaf6)

Core Tutorial by Argsment