Skip to content

TUN — Xray-core ​

Xray-core 在 proxy/tun 提供 TUN 入站:创建 TUN 设备、读取原始数据包,由 Xray-core 自带的轻量 TCP/IP 协议栈处理它们,再把得到的连接分发给出站。可选的辅助项 —— gateway、dns、autoSystemRoutingTable、autoOutboundsInterface、autoSystemDnsToGateway 与 autoSystemWfpBlockLeak —— 让 Xray 自动安装系统路由表条目、把出站绑定到某个物理接口、把系统解析器指向 TUN,并拦截绕过 TUN 的流量;不使用它们时,需要你自行设置路由、DNS 与防火墙规则。

它仍然比 sing-box / mihomo 的 TUN 栈更轻量:没有 strict-route、auto-redirect,也没有 UID / 包过滤。若需要这些能力,请用 sing-box 或 mihomo 作为 TUN 前端,再把它们的出站经 Xray 转出。

入站 ​

"protocol": "tun" 的 settings:

字段类型默认值允许值描述
namestring(auto-generated)<interface name>TUN 设备名称,启动时创建。为空时会自动挑选一个未使用的 utun<N> 名称(N 取 10–1024)。
descstringWintun<string>创建时传给 Wintun 适配器的设备描述(仅 Windows)。为空时默认为 "Wintun"。
mtuuint321500<bytes>设备的最大传输单元(MTU)。
gateway[]string—<IP/prefix> | …分配给 TUN 接口的网关地址,采用前缀形式(172.19.0.1/30)。
dns[]string—<ip address> | …在 TUN 适配器上配置的 DNS 服务器地址。仅限 Windows —— 其他系统会忽略它(Linux / FreeBSD 见 autoSystemDnsToGateway)。
userLeveluint320<uint32>经此入站收到的连接的默认策略等级。
autoSystemRoutingTable[]string—<CIDR> | …目的前缀(0.0.0.0/0、::/0 等):Xray 运行期间为它们安装指向该设备的系统路由。提供该列表即启用自动路由。
autoOutboundsInterface*stringauto<interface name> | auto出站连接绑定的物理接口,用于防止路由环路。设置了 autoSystemRoutingTable 时默认为 "auto"。
autoSystemDnsToGatewayboolfalsetrue | falseLinux / FreeBSD:把系统解析器指向 TUN,使系统的域名查询由 Xray 应答。需要 gateway,以及一条把 TUN 的 53 端口送往 dns 出站的路由规则 —— 见“说明”。
autoSystemWfpBlockLeak[]string[]dns | windnsclient | misconfigtun | all | allexceptlan | icmp | tcp[/<ports>] | udp[/<ports>] | <port>[-<port>] | <CIDR>:<value>Windows:Windows 筛选平台(WFP)过滤器,使除 Xray 外所有程序的所列流量只能经 TUN 离开。需要 autoSystemRoutingTable;把系统 DNS 留在 TUN 内的取值还需要 dns —— 见“说明”。

源码: infra/conf/tun.go:16-27 · 锚定版本 v26.11.9 (676eaf6)

示例 ​

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

若想让 Xray 自行管理路由,加上 autoSystemRoutingTable(并可选地固定出站接口):

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

若还要把系统解析器交给 Xray(Linux / FreeBSD),加上 autoSystemDnsToGateway 与 gateway,并把 TUN 的 53 端口路由到 dns 出站:

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" }
    ]
  }
}

在 Windows 上则改为设置适配器的 dns(保留同样的路由规则),并拦截绕过 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"]
    }
  }]
}

或者改为手动配置系统网络:

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

# 全部走 xray0(管理子网除外)
ip route add 1/1 dev xray0
ip route add 128/1 dev xray0

# DNS 劫持
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-port 5353
# (再在 5353 上放一个 Xray dokodemo-door 入站)

说明 ​

  • 数据包由 Xray-core 自带的轻量 TCP/IP 协议栈处理:TCP 与 UDP 变成连接,像其他入站的连接一样分发给出站;ICMP 回显请求(ping)在本地应答。
  • Linux 上常见用法:搭配 dokodemo-door 入站 + 手写的 iptables 规则集来实现透明代理,或者设置 autoSystemRoutingTable 让 Xray 自行安装路由。
  • autoSystemRoutingTable 让 Xray 自动管理系统路由表条目;当它存在而 autoOutboundsInterface 省略时,出站接口默认为 auto。gateway 与 dns 设置设备上的地址与(Windows 上的)解析器。
  • Windows / macOS 上 Xray TUN 入站会创建 wintun / utun 设备。带 autoSystemRoutingTable 时它能为你接好路由;否则需自行脚本化。FreeBSD 同样受支持,包括 autoSystemRoutingTable / autoOutboundsInterface。这些平台上许多用户更倾向使用 sing-box 或 mihomo —— 它们的 TUN 功能集更丰富。
  • autoSystemDnsToGateway(Linux / FreeBSD)把系统解析器交给 Xray。Linux 上经由 systemd-resolved(resolvectl,systemd 240 或更新),指向第一个 gateway 地址的下一个地址(172.19.0.1/30 → 172.19.0.2);FreeBSD 上经由 resolvconf,直接指向网关地址本身。若这样做会让域名解析中断或形成环路,Xray 不会启动:从 TUN 发往该地址 53 端口的查询必须被路由到 dns 出站,且 dns 块必须列出服务器、其中不能有 localhost。Xray 停止时会还原该设置(SIGKILL 之后不会 —— 此时请执行 resolvectl revert <interface>)。
  • autoSystemWfpBlockLeak(Windows)添加 Windows 筛选平台(WFP)过滤器,使除 Xray 自身外所有程序的所列流量只能经 TUN 离开。取值(不区分大小写):dns(TCP / UDP 的 53 与 853 端口,外加 windnsclient);windnsclient(Windows 的 DNS Client 服务,在 Windows 11 / Server 2022 上它也可能经 HTTPS 或 TLS 在任意端口查询);misconfigtun(autoSystemRoutingTable 中没有路由的那个 IP 版本的全部流量);all(全部流量,双向);allexceptlan(同 all,但私有、链路本地、组播与广播地址仍可访问 —— 包含 dns);icmp;tcp / udp,可单独使用或带 /<port>、/<low>-<high>;或者单独的端口 / 端口范围,同时适用于 TCP 与 UDP。在取值前加 CIDR 可将其限定到这些地址(1.1.1.0/24:53、[::/0]:all)。回环、DHCP 与 IPv6 邻居发现始终允许;若无法添加过滤器,Xray 不会启动。该选项需要 autoSystemRoutingTable,而把系统 DNS 留在 TUN 内的取值(all、allexceptlan、dns、windnsclient、任何覆盖 53 端口的取值)还需要 dns。上游建议至少使用 ["dns", "misconfigtun"]。
  • 在 Windows 上,若已通过注册表值 DisabledComponents 关闭 IPv6,为 gateway、dns 与 autoSystemRoutingTable 设置 IPv6 时出现的错误只会被记录,TUN 仍会启动(此时 misconfigtun 会不论路由如何都拦截 TUN 之外的 IPv6);其他情况下这类错误会使 TUN 无法启动。

跨内核说明 ​

  • sing-box 提供功能齐全的 TUN 入站,含 auto_route、auto_redirect、strict_route、route_address、UID / 包过滤,以及按平台的协议栈选择(gVisor / system / mixed)。参见 TUN — sing-box。
  • mihomo 在顶层 tun: 块提供同等齐全的 TUN 入站,选项集合相同,但字段名采用 kebab-case。参见 TUN — mihomo。

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

由 Argsment 出品的 Core Tutorial