Skip to content

TUN inbound: Refine gateway and autoSystemRoutingTable on Linux#6398

Merged
RPRX merged 2 commits into
XTLS:mainfrom
OneXray:main
Jul 4, 2026
Merged

TUN inbound: Refine gateway and autoSystemRoutingTable on Linux#6398
RPRX merged 2 commits into
XTLS:mainfrom
OneXray:main

Conversation

@yiguodev

@yiguodev yiguodev commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

补充 Linux TUN 对 gatewayautoSystemRoutingTable 的支持。

这里给 Linux TUN 设置 IP,不是因为 gVisor/Xray 的 TUN 包收发在协议层必须依赖这个地址。Linux 上只把 TUN link up,再添加 dev route,在一些场景下也可以工作。设置 IP 的主要目的,是让 Xray-core 可以完整接管“创建 TUN 接口、配置接口地址、配置系统路由、关闭时清理”的生命周期。

在这个配置里,gateway 更准确地理解为 TUN 接口地址,例如 198.18.0.1/15fc00::1/64。把它写到 TUN 设备后,系统状态会更明确:ip addr show <tun> 能看到接口地址,autoSystemRoutingTable 添加的路由也可以直接指向这个 TUN link。这里不会派生隐藏的下一跳地址,也不会把 route 写成 via 198.18.0.2 之类的形式;路由仍然是 on-link route,只绑定到对应的 LinkIndex

这样做还有两个实际收益:

  1. 和 Windows 语义对齐。Windows 侧已经把 gateway 作为 TUN 接口地址写入,Linux 补齐后,同一份 TUN inbound 配置在 Windows/Linux 上语义一致。
  2. 客户端不再需要额外维护一套 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

Copilot AI review requested due to automatic review settings June 29, 2026 07:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.md to 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.

Comment thread proxy/tun/tun_linux.go Outdated
@RPRX

RPRX commented Jul 3, 2026

Copy link
Copy Markdown
Member

隔壁 v2rayN 说 macOS 的也没通 2dust/v2rayN#9649 (comment)我们都没机器只能你来测了

@yiguodev

yiguodev commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

macOS 上可能有问题,Linux 上肯定没问题。可能是 v2rayN 的使用方法不对? OneXray 从 4 月的版本开始就已经移除了 tun2socks ,改为 Xray-only 模式。Xray config 需要在运行时修正 tun 的配置参数。

@RPRX

RPRX commented Jul 4, 2026

Copy link
Copy Markdown
Member

用 Linux 当桌面有点浪费生命所以我也不用,你测过没问题就行,先合了

话说你不是开发 iOS APP 吗,不是必须要 Xcode 嘛,你没有 macOS 机器测一下 TUN?关于 macOS 的 @ 你几次了都没下文

@yiguodev

yiguodev commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

macOS 在 NE 模式下工作非常好,一点儿毛病没有。
如果裸核用的话,必须要 sudo 启动,对 GUI 开发简直就是灾难 。之前工作确实忙,好在最近没工作了😭 。总之我最近研究一下吧。

@RPRX RPRX changed the title Add Linux TUN route configuration support TUN inbound: Refine gateway and autoSystemRoutingTable on Linux Jul 4, 2026
@RPRX

RPRX commented Jul 4, 2026

Copy link
Copy Markdown
Member

NE 模式相当于 OS 把接口和路由表都弄好了就比较简单,然而那个是从 iOS 移植的手机 APP 模式吧,桌面模式更自由一些

@RPRX RPRX merged commit 65f6f0a into XTLS:main Jul 4, 2026
45 of 46 checks passed
@Jasper344612

Copy link
Copy Markdown
Contributor

macOS 在 NE 模式下工作非常好,一点儿毛病没有。 如果裸核用的话,必须要 sudo 启动,对 GUI 开发简直就是灾难 。之前工作确实忙,好在最近没工作了😭 。总之我最近研究一下吧。

mac TUN 模式提权确实麻烦

@Jasper344612

Copy link
Copy Markdown
Contributor

macOS 上可能有问题,Linux 上肯定没问题。可能是 v2rayN 的使用方法不对? OneXray 从 4 月的版本开始就已经移除了 tun2socks ,改为 Xray-only 模式。Xray config 需要在运行时修正 tun 的配置参数。

麻烦你测试一下吧,我裸核用了很久了,还没遇到bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants