TUN inbound: Refine gateway and autoSystemRoutingTable on Linux#6398
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request extends the Linux TUN inbound implementation to optionally configure OS-level interface addresses (from gateway) in addition to the existing system route setup, and updates the TUN README to reflect the new behavior and OS responsibilities.
Changes:
- Add Linux interface address configuration (
gateway) with rollback on startup failure and cleanup on close. - Update startup/teardown flow to ensure addresses are removed when routes fail to apply or when the TUN closes.
- Refresh
proxy/tun/README.mdto document Linux/Windows address + route behavior and clarify DNS handling on Linux.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| proxy/tun/tun_linux.go | Adds Linux-side interface address add/remove logic tied into Start/Close lifecycle. |
| proxy/tun/README.md | Updates documentation to match Linux/Windows address+route capabilities and Linux DNS behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
隔壁 v2rayN 说 macOS 的也没通 2dust/v2rayN#9649 (comment) , |
|
macOS 上可能有问题,Linux 上肯定没问题。 |
|
|
|
macOS 在 NE 模式下工作非常好,一点儿毛病没有。 |
gateway and autoSystemRoutingTable on Linux
|
|
mac TUN 模式提权确实麻烦 |
麻烦你测试一下吧,我裸核用了很久了,还没遇到bug |
补充 Linux TUN 对
gateway和autoSystemRoutingTable的支持。这里给 Linux TUN 设置 IP,不是因为 gVisor/Xray 的 TUN 包收发在协议层必须依赖这个地址。Linux 上只把 TUN link up,再添加 dev route,在一些场景下也可以工作。设置 IP 的主要目的,是让 Xray-core 可以完整接管“创建 TUN 接口、配置接口地址、配置系统路由、关闭时清理”的生命周期。
在这个配置里,
gateway更准确地理解为 TUN 接口地址,例如198.18.0.1/15或fc00::1/64。把它写到 TUN 设备后,系统状态会更明确:ip addr show <tun>能看到接口地址,autoSystemRoutingTable添加的路由也可以直接指向这个 TUN link。这里不会派生隐藏的下一跳地址,也不会把 route 写成via 198.18.0.2之类的形式;路由仍然是 on-link route,只绑定到对应的LinkIndex。这样做还有两个实际收益:
gateway作为 TUN 接口地址写入,Linux 补齐后,同一份 TUN inbound 配置在 Windows/Linux 上语义一致。ip addr add的 wrapper 逻辑。对于直接使用官方 Xray 二进制的桌面客户端,这可以减少应用层对系统路由的重复处理。这次改动的边界比较明确:Linux 不接管系统 DNS,不修改
resolv.conf、systemd-resolved 或 NetworkManager;route 不设置Gw,也不调整 metric;如果 TUN 是外部 fd 传入的,ownsTun == false时不会修改外部接口。关闭时只清理本次由 Xray 添加的地址和路由。AI Coding by GPT-5.5 Extra High
Tested on Linux