TUN — sing-box
sing-box 的 TUN 入站是 Go 语言生态的 规范实现 —— 多数其他实现 (mihomo、Clash-Meta 等)都派生自它。入站创建 TUN 设备,在系统栈 或 gVisor 栈中解析原始数据包,并把得到的连接交给路由引擎处理。
入站
type: "tun":
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
interface_name | string | (auto) | <interface name> | TUN 设备名。为空时按操作系统默认选取(`tun0`、`utun5` 等)。 |
mtu | uint32 | 9000 | <bytes> | 设备 MTU。sing-box 的默认值是 9000 —— 偏高,便于摊薄每包开销。 |
address | badoption.Listable[netip.Prefix] | [] | <CIDR> | 接口地址(典型为一个 IPv4 + 一个 IPv6)。取代已弃用的 `inet4_address` / `inet6_address` 对。 |
auto_route | bool | false | true | false | 自动安装操作系统路由,把所有流量导向 TUN。 |
iproute2_table_index | int | 2022 | <int> | `auto_route` 使用的 Linux iproute2 表索引。 |
iproute2_rule_index | int | 9000 | <int> | Linux iproute2 规则索引。 |
auto_redirect | bool | false | true | false | Linux NFTables 自动重定向 —— 不改路由就把流量接入 TUN。在热 loopback 路径上比 auto_route 更快。 |
auto_redirect_input_mark | FwMark | 0 | <uint32> | 送入 TUN 的数据包附加的 fwmark。 |
auto_redirect_output_mark | FwMark | 0 | <uint32> | TUN 出口数据包附加的 fwmark。 |
auto_redirect_reset_mark | FwMark | 0 | <uint32> | 处理完成后清除的 fwmark。 |
auto_redirect_nfqueue | uint16 | 0 | <uint16> | auto-redirect 路径使用的 NFQUEUE 编号。 |
auto_redirect_iproute2_fallback_rule_index | int | 0 | <int> | auto-redirect 无法安装时使用的回退规则索引。 |
exclude_mptcp | bool | false | true | false | 跳过 MPTCP 流(让其走普通内核路径)。 |
loopback_address | badoption.Listable[netip.Addr] | [] | <IP> | 视作 loopback 的地址(不经 TUN)。 |
strict_route | bool | false | true | false | 阻止流量绕过 TUN 设备(在默认路由边界添加 DROP 规则)。 |
route_address | badoption.Listable[netip.Prefix] | [] | <CIDR> | 启用 `auto_route` 时,仅把这些 CIDR 经 TUN。默认全量。 |
route_address_set | badoption.Listable[string] | [] | <rule-set tag> | 按 rule-set 的 IP-CIDR 条目而非显式列表路由。 |
route_exclude_address | badoption.Listable[netip.Prefix] | [] | <CIDR> | 保留在默认接口上的 CIDR(逃生口)。 |
route_exclude_address_set | badoption.Listable[string] | [] | <rule-set tag> | 按 rule-set 的排除项。 |
include_interface | badoption.Listable[string] | [] | <interface> | 仅附着到这些接口(移动端多网卡)。 |
exclude_interface | badoption.Listable[string] | [] | <interface> | 排除这些接口。 |
include_uid | badoption.Listable[uint32] | [] | <uid> | Linux / macOS 上按 UID 包含。 |
include_uid_range | badoption.Listable[string] | [] | <from:to> | 按 UID 范围包含。 |
exclude_uid | badoption.Listable[uint32] | [] | <uid> | 按 UID 排除。 |
exclude_uid_range | badoption.Listable[string] | [] | <from:to> | 按 UID 范围排除。 |
include_android_user | badoption.Listable[int] | [] | <user id> | Android 多用户包含。 |
include_package | badoption.Listable[string] | [] | <package name> | Android 包名包含。 |
exclude_package | badoption.Listable[string] | [] | <package name> | Android 包名排除。 |
udp_timeout | UDPTimeoutCompat | 5m | <duration or seconds> | UDP 流的空闲超时。 |
stack | string | mixed | system | gvisor | mixed | TCP/IP 栈实现。`system` 使用内核网络栈;`gvisor` 跑用户态栈;`mixed` TCP 用 gVisor、UDP 用系统。 |
platform | *TunPlatformOptions | (unset) | TunPlatformOptions | 平台专属覆盖(当前仅 `http_proxy`)。 |
gso | bool | — | — | Deprecated: removed |
inet4_address | badoption.Listable[netip.Prefix] | — | — | Deprecated: merged to Address |
inet6_address | badoption.Listable[netip.Prefix] | — | — | Deprecated: merged to Address |
inet4_route_address | badoption.Listable[netip.Prefix] | — | — | Deprecated: merged to RouteAddress |
inet6_route_address | badoption.Listable[netip.Prefix] | — | — | Deprecated: merged to RouteAddress |
inet4_route_exclude_address | badoption.Listable[netip.Prefix] | — | — | Deprecated: merged to RouteExcludeAddress |
inet6_route_exclude_address | badoption.Listable[netip.Prefix] | — | — | Deprecated: merged to RouteExcludeAddress |
endpoint_independent_nat | bool | — | — | Deprecated: removed |
源码: option/tun.go:13-63 · 锚定版本 v1.13.11 (553cfa1)
该结构体还内嵌 InboundOptions(sniff、sniff_override_dest、 domain_strategy 等)。
协议栈选择
system—— 内核原生 TCP/IP。最快。需要 OS 暴露相应的 TUN ioctl (Linux 是;macOS utun 是;Windows 通过 wintun DLL)。gvisor—— Google 的用户态 TCP/IP 栈。较慢但可移植;在内核 TUN 支持不佳的平台上是唯一选择。mixed—— TCP 走 gVisor(这里用户态状态机的 bug 影响较小), UDP 走系统(这里更要紧)。是默认值,也是大多数用户的选择。
路由模式
sing-box 的 TUN 支持两种路由方式:
auto_route: true(跨平台)。安装操作系统路由把所有流量 导向 TUN。Linux 上使用 iproute2 表 + 规则索引;macOS 上调用route add;Windows 上直接编程路由表。auto_redirect: true(仅 Linux)。安装 NFTables 重定向规则代替改路由。在热 loopback 路径上更快(少了一跳)。 需要nft与匹配的内核模块。
strict_route: true 在外围接口添加 DROP 规则,防止流量绕过 TUN 泄露 —— 对 kill-switch 语义很重要。
示例
标准桌面(Linux / macOS):
json
{
"inbounds": [{
"type": "tun",
"tag": "tun-in",
"interface_name": "sing-tun",
"mtu": 9000,
"address": ["172.16.0.1/30", "fdfe:dcba:9876::1/126"],
"auto_route": true,
"strict_route": true,
"stack": "mixed",
"sniff": true,
"sniff_override_dest": true
}],
"route": {
"auto_detect_interface": true,
"rules": [
{ "action": "sniff" },
{ "protocol": "dns", "action": "hijack-dns" },
{ "ip_is_private": true, "outbound": "direct" }
],
"final": "proxy"
}
}Android 应用过滤 —— 只代理列出的包:
json
{
"inbounds": [{
"type": "tun",
"interface_name": "tun0",
"mtu": 9000,
"address": ["172.16.0.1/30"],
"auto_route": true,
"stack": "system",
"include_package": ["com.netflix.mediaclient", "com.spotify.music"]
}]
}繁忙主机上推荐使用的 Linux NFTables 自动重定向:
json
{
"inbounds": [{
"type": "tun",
"auto_redirect": true,
"auto_redirect_input_mark": "0x100",
"auto_redirect_output_mark": "0x200",
"address": ["172.16.0.1/30"]
}]
}说明
address此前是两个字段(inet4_address/inet6_address)。 两者仍可解析但会触发弃用警告 —— 配置请改用新的合并address列 表。endpoint_independent_nat已 移除。在 route 规则 action 中 通过udp_disable_domain_unmapping设置等效行为。gso已 移除 —— 内核 GSO 现在自动检测。auto_route与auto_redirect互斥。只能选一个。- Linux 上
strict_route优先使用 NFTables,必要时回退到 iptables。 两者都要求 root。
跨内核说明
- Xray-core 的 TUN 入站是最小化的 —— 没有自动路由、DNS 劫持 与应用过滤。参见 TUN — Xray-core。
- mihomo 在顶层
tun:块下提供几乎相同的特性集,字段名为 kebab-case,并多了一个dns-hijack列表。参见 TUN — mihomo。
源码: option/tun.go:13-63 · v1.13.11 (553cfa1)
