TUN — mihomo
mihomo 的 tun: 是顶层配置块(不是 listeners: 条目)。schema 与 sing-box 的 TUN 入站几乎相同(mihomo 的实现派生自 sing-box),并新增 mihomo 独有的几项:显式 dns-hijack、MAC 地址过滤、ICMP 丢弃开关, 以及 Darwin 上的 recvmsg_x / sendmsg_x 优化标志。
顶层 tun: 块
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
enable | bool | false | true | false | 总开关。为 false 时块内其他字段被忽略。 |
device | string | (auto) | <interface name> | TUN 设备名。 |
stack | C.TUNStack | mixed | system | gvisor | mixed | 与 sing-box 一致:选择 TCP/IP 协议栈。 |
dns-hijack | []string | [] | [<address>] | 应当被劫持为 DNS 的地址 —— 到这些端点的出站 UDP 会被重定向到内部 DNS 引擎。标准取值:`[any:53]`(全部捕获)。 |
auto-route | bool | false | true | false | 自动安装操作系统路由。 |
auto-detect-interface | bool | false | true | false | 自动发现默认出站接口 —— 用于把 TUN 绑定流量从 TUN 中排除掉以免回环。 |
mtu | uint32 | 0 (auto) | <bytes> | 设备 MTU。0 选择合适的默认值(多数平台为 9000)。 |
gso | bool | false | true | false | Generic Segmentation Offload(Linux)。内核支持时可提升吞吐。 |
gso-max-size | uint32 | 65536 | <bytes> | GSO 启用时的最大段大小。 |
inet6-address | []netip.Prefix | [] | <CIDR> | 分配给设备的 IPv6 地址。IPv4 使用顶层 `inet4-address` 字段(当前在源码中被注释 —— 见 `config/config.go:278`)。 |
iproute2-table-index | int | 2022 | <int> | Linux iproute2 表索引。 |
iproute2-rule-index | int | 9000 | <int> | Linux iproute2 规则索引。 |
auto-redirect | bool | false | true | false | Linux NFTables 自动重定向(热路径上比 auto-route 更快)。 |
auto-redirect-input-mark | uint32 | 0 | <uint32> | 送入 TUN 的数据包附加的 fwmark。 |
auto-redirect-output-mark | uint32 | 0 | <uint32> | TUN 出口数据包附加的 fwmark。 |
auto-redirect-iproute2-fallback-rule-index | int | 0 | <int> | auto-redirect 无法安装时使用的回退规则索引。 |
loopback-address | []netip.Addr | [] | <IP> | 视作 loopback 的地址。 |
strict-route | bool | false | true | false | 阻止本应绕过 TUN 的流量(kill-switch 语义)。 |
route-address | []netip.Prefix | [] | <CIDR> | 启用 `auto-route` 时仅把这些 CIDR 经 TUN。 |
route-address-set | []string | [] | <rule-provider name> | 按规则提供方的包含项路由。 |
route-exclude-address | []netip.Prefix | [] | <CIDR> | 保留在默认接口上的 CIDR。 |
route-exclude-address-set | []string | [] | <rule-provider name> | 按规则提供方的排除项。 |
include-interface | []string | [] | <interface> | 仅附着到这些接口。 |
exclude-interface | []string | [] | <interface> | 排除这些接口。 |
include-uid | []uint32 | [] | <uid> | 按 UID 包含。 |
include-uid-range | []string | [] | <from:to> | 按 UID 范围包含。 |
exclude-uid | []uint32 | [] | <uid> | 按 UID 排除。 |
exclude-uid-range | []string | [] | <from:to> | 按 UID 范围排除。 |
exclude-src-port | []uint16 | [] | <port> | 按源端口排除。 |
exclude-src-port-range | []string | [] | <from:to> | 按源端口范围排除。 |
exclude-dst-port | []uint16 | [] | <port> | 按目的端口排除。 |
exclude-dst-port-range | []string | [] | <from:to> | 按目的端口范围排除。 |
include-android-user | []int | [] | <user id> | Android 多用户包含。 |
include-package | []string | [] | <package name> | Android 包名包含。 |
exclude-package | []string | [] | <package name> | Android 包名排除。 |
include-mac-address | []string | [] | <MAC> | 基于 MAC 的包含(mihomo 独有,sing-box 没有)。 |
exclude-mac-address | []string | [] | <MAC> | 基于 MAC 的排除。 |
endpoint-independent-nat | bool | false | true | false | 对 UDP 启用 endpoint-independent NAT。部分游戏 / VoIP 应用需要。 |
udp-timeout | int64 | 300 | <seconds> | UDP 空闲超时(秒)。 |
disable-icmp-forwarding | bool | false | true | false | 在 TUN 层丢弃 ICMP 包(mihomo 独有)。 |
file-descriptor | int | 0 | <fd> | 使用预先打开的文件描述符而非自行创建 TUN 设备。在由操作系统交付 fd 的 VPN 服务集成中有用。 |
inet4-route-address | []netip.Prefix | — | — | |
inet6-route-address | []netip.Prefix | — | — | |
inet4-route-exclude-address | []netip.Prefix | — | — | |
inet6-route-exclude-address | []netip.Prefix | — | — | |
recvmsgx | bool | false | true | false | 仅 Darwin —— 使用 recvmsg_x 系统调用做批处理。 |
sendmsgx | bool | false | true | false | 仅 Darwin —— 使用 sendmsg_x。 |
源码: config/config.go:268-321 · 锚定版本 v1.19.27 (5184081)
协议栈选择
与 sing-box 一致:
system—— 内核 TCP/IP。最快,需要内核 TUN 支持。gvisor—— 用户态栈。较慢但可移植。mixed—— TCP 走 gVisor、UDP 走系统。默认。
DNS 劫持
dns-hijack 是 mihomo 把 DNS 查询路由到内部解析器的惯用方式:
yaml
tun:
enable: true
dns-hijack:
- any:53 # 所有 UDP/53 流量
- tcp://any:53 # 以及 TCP/53
- 8.8.8.8:53 # 仅特定服务器实现位于 TUN 层 —— DNS 包在进入路由规则前就被截获,比在 53 端口 跑 UDP 入站再回到规则更快。
示例
桌面透明代理:
yaml
tun:
enable: true
device: mihomo-tun
stack: mixed
mtu: 9000
dns-hijack:
- any:53
- tcp://any:53
auto-route: true
auto-detect-interface: true
strict-route: true
route-address:
- 0.0.0.0/1
- 128.0.0.0/1
- ::/1
- 8000::/1
route-exclude-address:
- 192.168.0.0/16
- 10.0.0.0/8Linux NFTables 自动重定向:
yaml
tun:
enable: true
device: utun0
stack: system
dns-hijack: [any:53]
auto-redirect: true
auto-redirect-input-mark: 0x100
auto-redirect-output-mark: 0x200Android 应用分流 —— 只代理特定应用:
yaml
tun:
enable: true
stack: system
mtu: 9000
dns-hijack: [any:53]
auto-route: true
include-package:
- com.netflix.mediaclient
- com.spotify.music
exclude-package:
- com.google.android.gms说明
inet4-address在源代码中被注释(config/config.go:278)—— 当前 mihomo 使用硬编码的 IPv4 默认段(198.18.0.1/30)。IPv6 仍由显式 的inet6-address字段决定。dns-hijack接受几种特殊形式:any:53同时匹配 TCP 与 UDP 端口 53。- 用
tcp://或udp://前缀限定协议。 - 裸
IP:port表示特定上游解析器。
auto-route与auto-redirect互斥。按你的内核选其一:支持 NFTables 的内核出于性能考虑优选 auto-redirect。recvmsgx/sendmsgx是仅 macOS 的优化,使用按系统调用批处理的 变体。在 macOS 上启用可提升吞吐。disable-icmp-forwarding是 mihomo 独有 —— 当你已有独立的 ICMP 路径(如基于路由表的 ping 器)、不希望 TUN 捕获 ping 时有用。
跨内核说明
- Xray-core 提供最小化的 TUN 入站。参见 TUN — Xray-core。
- sing-box 在
inbounds[]中以type: "tun"提供同样的特性 集合,字段名为 snake_case。参见 TUN — sing-box。
源码: config/config.go:268-321 · v1.19.27 (5184081)
