Xray-core icon indicating copy to clipboard operation
Xray-core copied to clipboard

[Feature Request] Automatic / Smart Selection of Network Egress Interface

Open osypai opened this issue 10 months ago • 7 comments

After the TUN inbound is implemented, there is an important detail users need to handle: for every outbound, sockopt.interface must be explicitly set to a physical egress network interface. Otherwise, traffic may fall back to the TUN inbound by default, causing a routing loop.

I’d like to propose adding a new high-level option to sockopt, for example:

sockopt.excludedInterface = "utun99"

This option would specify which network interfaces should be avoided. When it is set, the system would:

  1. Automatically retrieve the list of available network interfaces
  2. Sort them by routing priority / hop distance
  3. Exclude the specified interfaces (e.g. utun99)
  4. Select the highest-priority remaining interface (e.g. en0)

The end result would be functionally equivalent to setting:

sockopt.interface = "en0"

but without requiring users to hardcode the actual egress interface name.

As an alternative design, we could continue using sockopt.interface but extend its string syntax with rules, for example:

sockopt.interface = "excluded:utun99"

This would indicate that the outbound should automatically select an interface, excluding the specified one(s).

osypai avatar Jan 23 '26 07:01 osypai

我觉得核心提供一个接口就行了 别的乱七八糟的工作就给客户端吧 涉及到乱七八糟自动/智能处理 结果就把一个简单一个系统调用 又变复杂 今天auto interface 明天就auto route了 就算真要搞 也放在auto route的时候再搞吧(如果真的有)

Fangliding avatar Jan 23 '26 07:01 Fangliding

于我个人直接跑裸 core 的使用体验而言,core 自带 auto-interface、auto-route 什么的还是会方便不少的

RPRX avatar Jan 23 '26 13:01 RPRX

不过实现上可能不会改 sockopt interface 配置项,而是 tun 入站配置了 auto-route 后,出站没配置 interface 的就自动避开入站 tun

RPRX avatar Jan 23 '26 13:01 RPRX

https://github.com/XTLS/Xray-core/pull/5887 Windows 已支持 autoSystemRoutingTable 和 autoOutboundsInterface,其它系统 PR welcome

RPRX avatar Apr 13 '26 13:04 RPRX

现在 autoOutbuondsInterface 和 autoSystemRoutingTable 支持 macOS 和 Linux 了

Jasper344612 avatar Jun 24 '26 11:06 Jasper344612

还剩个 FreeBSD https://github.com/XTLS/Xray-core/pull/6366#issuecomment-4788510365

RPRX avatar Jun 24 '26 12:06 RPRX

{
  "tag": "tun-in",
  "protocol": "tun",
  "settings": {
    "name": "xray0",
    "mtu": 1500,
    "gateway": [
      "198.18.0.1",
      "198.18.0.2"
    ],
  "autoSystemRoutingTable": [
    "192.168.201.0/24"
  ],
  "autoOutboundsInterface": "auto"
  }
}

这样配置在alpine 3.22 x86_64上不能自动创建 192.168.201.0/24 到xray0的路由

yunhai20082008 avatar Sep 27 '26 10:09 yunhai20082008