fix(detect): never bind USB gadget iface as proxy egress

设备把自己当 USB 网卡挂给上游主机时会出现 usb0(驱动 configfs-gadget.g1,
IP 192.168.68.1)。该链路只通向 PC,被当成出口后所有出站都 context
deadline exceeded,客户端看到 socks connect failed rep=0x01。

误选原因:score_iface_as_cellular 给 usb0 与真出口 wwan0 都打 175 分。
usb0 靠名称命中 patterns 里的 usb(+80)与非默认路由(+40);wwan0 的
bam-dmux 驱动不在白名单里,白丢 +100。detect_cellular_iface 用 -gt 比较,
list_ifaces 按名排序让 usb0 先入选,平分下无法被顶替。

改动:
- lib.sh 新增 iface_is_usb_gadget(),按驱动名与 gadget 总线路径识别,
  并并入 is_virtual_or_skip_iface(usb0 评分 175 -> 0)
- 驱动白名单补 bam-dmux/bam_dmux/qcom-ipa/ipa_wan;名称权重 80 -> 40,
  确保驱动证据始终压过名称猜测
- 新增 detect_cellular_iface_via_mm(),把 ModemManager bearer 的
  interface: 作为第 0 步权威来源
- detect_cellular_iface / resolve_cellular 全链路拒绝 gadget 网卡,
  取不到真出口时按 REQUIRE_CELLULAR_IFACE 直接失败而不是绑错
- generate/install/upgrade/watch/detect/verify 各入口独立设闸,
  verify.sh 发现出口是 gadget 时直接 exit 4 并给出修复命令
- 默认 CELLULAR_IFACE_PATTERNS 去掉 usb/enx;upgrade.sh 与根 install.sh
  就地迁移已有 settings.conf,旧机器升级即修复
- 新增 tests/detect-gadget.sh:18 条离线断言,无需真机
This commit is contained in:
Hermes
2026-08-23 18:39:34 +08:00
parent 659ad6f950
commit 88f2ff44aa
11 changed files with 447 additions and 30 deletions
+16 -3
View File
@@ -255,6 +255,16 @@ need_refresh() {
return 0
fi
if iface_is_usb_gadget "$cell"; then
logw "CELLULAR_IFACE=$cell is a USB gadget link (host-facing), never a data egress → rebind"
return 0
fi
if [[ -n "$cfg_iface" ]] && iface_is_usb_gadget "$cfg_iface"; then
logw "config bind_interface=$cfg_iface is a USB gadget link → rebind"
return 0
fi
live_src="$(detect_source_ip "$cell" || true)"
if [[ -z "$live_src" ]]; then
# 网卡在但无 IPv4:可能刚重拨;不立刻 die,等下一轮或 MM 恢复路径
@@ -290,10 +300,13 @@ do_refresh() {
local src=""
local reason="${1:-manual}"
if [[ -z "$cell" ]] || ! iface_exists "$cell"; then
if [[ -z "$cell" ]] || ! iface_exists "$cell" || iface_is_usb_gadget "$cell"; then
if [[ -n "$cell" ]] && iface_is_usb_gadget "$cell"; then
logw "refuse USB gadget iface $cell as egress; re-detecting"
fi
cell="$(detect_cellular_iface "" || true)"
if [[ -z "$cell" ]]; then
logw "detect failed; skip reason=$reason"
if [[ -z "$cell" ]] || iface_is_usb_gadget "$cell"; then
logw "detect failed or gadget-only; skip reason=$reason"
return 1
fi
fi