Skip to content

Bridge — sing-box ​

bridge 是 direct 出站在 L3 上的对应物。它不打开套接字,而是把来自 TUN 入站或其他 L3 端点(WireGuard、Tailscale)的 L3 流量——TCP、UDP 与 ICMP——直接从某个网络接口转发出去,无需先转换为 L4 连接。

权限与平台

bridge 需要提升的权限,且仅在 Linux、macOS、Windows(x86 / x64,通过 WinDivert)、已 root 的 Android 与已越狱的 iOS 上可用;在其他平台上该出站会在启动时报错。在图形客户端中,它需要带辅助服务的 macOS 独立版、Android 上的 root 权限,或已越狱的 iOS 设备。

出站选项 ​

outbounds[] 中的 type: "bridge":

字段类型默认值允许值描述
interfacestring(default interface)<interface name>转发流量的出口接口。未设置时使用默认接口;所选接口不可用期间,流量会被丢弃。
bridge_namestringbridge<name prefix>sing-box 创建的 bridge TUN 接口的名称前缀。在 Apple 平台上无效。
iproute2_table_indexint2200 + instance index<int>仅 Linux,且仅在设置了 interface 时生效:固定出口路由所用的 iproute2 路由表索引。
iproute2_rule_indexint100<int>仅 Linux:bridge 安装的 iproute2 规则的起始索引。

源码: option/bridge.go:3-8 · 锚定版本 v1.14.2 (af6e64c)

示例 ​

把 TUN 捕获的局域网流量通过 bridge 在 L3 转发,其余流量走 direct:

json
{
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "address": ["172.19.0.1/30"],
      "auto_route": true
    }
  ],
  "outbounds": [
    { "type": "direct", "tag": "direct" },
    { "type": "bridge", "tag": "bridge-out" }
  ],
  "route": {
    "rules": [
      {
        "ip_cidr": ["192.168.0.0/16"],
        "preferred_by": ["bridge-out"],
        "outbound": "bridge-out"
      }
    ],
    "final": "direct"
  }
}

说明 ​

  • bridge 只承载 L3 流量。它通过在 L3 入站(TUN、WireGuard、Tailscale)的 预匹配 阶段命中的 route 动作到达。来自 L4 入站(SOCKS、HTTP 等)的连接,或由仅在预匹配之后才命中的规则路由过来的连接,会以 only L3 traffic is supported by bridge 失败。
  • 本机的目标地址——回环、未指定地址,或分配给本机接口的任何地址——会被拒绝并记录警告。请在规则中排除它们。
  • 推荐使用 preferred_by: ["<bridge tag>"] 作为门控:对 bridge 而言,它匹配所有非本机目标,且只在预匹配中匹配,因此不会捕获 bridge 无法处理的流量。
  • bridge 也可以作为默认出站:预匹配中没有规则命中时,L3 转发依然生效(对于出站组,使用当前选中的成员)。到达它的 L4 连接会被拒绝。
  • FakeIP 目标必须在预匹配阶段通过 resolve 动作解析,否则连接会被拒绝。
  • 对 ICMP 而言,direct 也能在 L3 转发;对 TCP 与 UDP,只有 bridge 与 WireGuard / Tailscale 端点可以。

跨内核说明 ​

  • Xray-core 与 mihomo 没有 L3 转发出站:它们的 TUN 入站总会终结 TCP / UDP,再通过出站重新拨号(Xray 的 freedom、mihomo 的 DIRECT)。参见 TUN — Xray-core 与 TUN — mihomo。

源码: option/bridge.go:3-8 · v1.14.2 (af6e64c)

由 Argsment 出品的 Core Tutorial