通过 `default_interface` 固定出站网卡后,切换无关的默认网卡仍会断开现有连接

Aperta
#4,749 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go, linux
Ambito
networking

Direzione di ricerca

Start by reading route/network.go around the default-interface update path, especially ResetNetwork and the unconditional connectionManager.CloseAll() call, then trace how route.default_interface is applied to connections. Reproduce the issue with the provided network-namespace setup and minimal SOCKS configuration, changing only the unrelated default route. The expected result is that connections bound to the explicitly configured interface remain open when that interface has not changed.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

操作系统

Linux

系统版本

Arch Linux (Linux 7.3.1-1-cachyos x86_64)

安装类型

sing-box 原始命令行程序

如果您使用图形客户端程序,请提供该程序版本。

No response

版本
sing-box version 1.14.4

Environment: go1.26.10 linux/amd64
Tags: with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,with_ocm,with_cloudflared,with_naive_outbound,with_usbip,with_openvpn,with_openconnect,badlinkname,tfogo_checklinkname0,with_purego
Revision: c005f35300ce76427dc285481ec5695f5b179cd2
CGO: disabled
描述

通过 route.default_interface 将出站显式绑定到某张固定网卡后,当系统默认路由在其他无关网卡之间切换时,已建立、经 sing-box 转发并绑定在该未变动网卡上的连接仍会中断。

重现方式

首先,创建一个新的网络命名空间以进行复现。进入 bash 后,查看一下 PID:

$ unshare -Urn bash
[root@host ~]# echo $$
<pid>
[root@host ~]#

另开一些终端来执行其他复现指令,均通过 nsenter、使用刚刚打印的 PID,加入同一个网络命名空间:

nsenter -U --preserve-credentials -n -t <pid> bash

用下面的命令在命名空间内配置三张网卡:fixed0、uplink0 和 uplink1。我们会把 sing-box 的出站固定绑定在 fixed0 上,而系统默认网卡会在 uplink0 和 uplink1 之间切换:

ip link set lo up
ip link add fixed0 type dummy && ip addr add 192.0.2.1/24 dev fixed0 && ip link set fixed0 up
ip link add uplink0 type dummy && ip link set uplink0 up
ip link add uplink1 type dummy && ip link set uplink1 up
ip route add default dev uplink0
启动 sing-box

现在创建下面的最小配置 ./config.json。这个配置只有一个简单的 SOCKS 入站,并将网卡固定为 fixed0。然后,启动 sing-box:

{
  "inbounds": [
    {
      "type": "socks",
      "listen": "127.0.0.1",
      "listen_port": 1080
    }
  ],
  "route": {
    "default_interface": "fixed0"
  }
}
sing-box run -c config.json
通过 sing-box 建立连接

接着分别用 nc 启动一个监听,再用另一个 nc 经由 sing-box 连接上去:

nc -l 18481
nc -X 5 -x 127.0.0.1:1080 192.0.2.1 18481

目前一切正常,连接会顺利建立。

切换默认网卡

这个时候把网络命名空间里的默认网卡从 uplink0 切换到 uplink1:

ip route replace default dev uplink1

考虑到配置中并未启用 auto_detect_interface,而且我们指定了 route.default_interface 为 fixed0,那么,fixed0 本身没有任何变动,这条已建立的 TCP 连接理应不受影响;但切换后连接却立刻断开了——你会发现 nc -l 18481 因为连接断开而退出。

从下面 sing-box 的运行日志可以看到,这是 sing-box 检测到了默认网卡变化,主动关闭了所有连接。尽管这可能在 sing-box 处于自动选择网卡模式下有一定用处,但是,在我们指定了网卡的情况下,不相关网卡的变化不应该导致 sing-box 断开所有连接。

Root cause

https://github.com/SagerNet/sing-box/blob/c005f35300ce76427dc285481ec5695f5b179cd2/route/network.go#L556-L570

默认网卡更新时会执行 ResetNetwork 并调用 connectionManager.CloseAll(),直接无条件关闭所有现有连接。

我用过一段时间 sing-box,当然,我肯定不完全熟悉 sing-box。不过,对于此问题,我建议:

  • 可以考虑在 ConnectionManager 里面记录一下各个连接是否是自动选择网卡,如果不是自动,也记录所绑定的网卡;从而在网卡更新时,只重置自动选择网卡的连接、以及所绑定网卡发生变化的连接。

  • 更进一步地,可以考虑引入类似现有 interrupt_exist_connections 的选项,允许用户配置,选择在切换网卡时主动断开现有连接,或者保留现有连接、仅让后续新建连接使用新的默认网卡。

Btw

#3676 与 #4678 也提到了由 ResetNetwork 引发的类似断连问题。不过那两个场景中的变动多少还发生在当前正在使用的默认网卡上;而在这里的例子中,连接已经显式绑定在另一张全程未变的网卡上,与发生切换的默认网卡完全无关,却依然被一并断开。相较于前两个 Issue,这种情形下的行为显得更加不合理,更像是一个 bug 而不是潜在的改进。

日志
DEBUG[0000] dns/local: systemd-resolved service is available
INFO[0000] inbound/socks[0]: tcp server started at 127.0.0.1:1080
INFO[0000] network: updated default interface uplink0, index 3
INFO[0000] sing-box started (0.00s)
INFO[0007] [2378887871 0ms] inbound/socks[0]: inbound connection from 127.0.0.1:46918
INFO[0007] [2378887871 0ms] inbound/socks[0]: inbound connection to 192.0.2.1:18481
INFO[0007] [2378887871 0ms] outbound/direct: outbound connection to 192.0.2.1:18481
INFO[0062] network: updated default interface uplink1, index 4
TRACE[0062] [2378887871 55.39s] connection: connection upload closed
TRACE[0062] [2378887871 55.39s] connection: connection download closed
支持我们
完整性要求
  • 我保证阅读了文档,了解所有我编写的配置文件项的含义,而不是大量堆砌看似有用的选项或默认值。
  • 我保证提供了可以在本地重现该问题的服务器、客户端配置文件与流程,而不是一个脱敏的复杂客户端配置文件。
  • 我保证提供了可用于重现我报告的错误的最简配置,而不是依赖远程服务器、TUN、图形界面客户端或者其他闭源软件。
  • 我保证提供了完整的配置文件与日志,而不是出于对自身智力的自信而仅提供了部分认为有用的部分。
Lingua principale
Go
Stelle
40.4k
Fork
5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SagerNet/sing-box

Tutte le issue di SagerNet/sing-box

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.