Skip to content

路由 — sing-box ​

sing-box 的 route 块承载规则、rule-set 以及若干默认接口 / 进程查找开关。规则是多态的(默认 + 逻辑)并显式携带一个 action 选择八种行为之一。

顶层选项 ​

字段类型默认值允许值描述
geoip*GeoIPOptions(ignored)GeoIPOptions不支持 GeoIP 数据库:该块仍会被解析但会被忽略;请改用 rule-set。
geosite*GeositeOptions(ignored)GeositeOptions不支持 GeoSite 数据库:该块仍会被解析但会被忽略;请改用 rule-set。
rules[]Rule[][Rule]路由规则,按顺序求值。
rule_set[]RuleSet[][RuleSet]命名 rule-set —— 在规则中通过 rule_set 匹配键引用。有三种类型:inline、local、remote。
finalstring(unset)<outbound tag>无规则命中时使用的默认出站。未设置时使用 outbounds 中的第一个出站。
find_processboolfalsetrue | false为每条连接查找发起进程。process_name / process_path 规则需要它。
find_neighborboolfalsetrue | false仅 Linux / macOS。即使没有 source_mac_address / source_hostname 规则,也强制启用邻居解析(局域网客户端的 MAC 地址 / 主机名),用于日志。
dhcp_lease_filesbadoption.Listable[string](auto-detected)[<file path>]仅 Linux / macOS。用于把局域网客户端映射到主机名与 MAC 地址的 DHCP 租约文件。为空时自动探测 dnsmasq、odhcpd、ISC dhcpd 与 Kea。
auto_detect_interfaceboolfalsetrue | false自动发现系统的默认出站接口 —— 供未指定 bind_interface 的 Direct 出站使用。
override_android_vpnboolfalsetrue | false仅 Android —— 绕过系统 VPN 服务进行出站拨号。
default_interfacestring(auto)<interface>覆盖默认出站接口。优先级高于 auto_detect_interface。
default_markFwMark0<uint32>出站套接字上设置的 Linux SO_MARK。
default_domain_resolver*DomainResolveOptions(none)DomainResolveOptions未指定规则时目的域名的默认解析器。
default_network_strategy*NetworkStrategy(unset)NetworkStrategyHappy-Eyeballs / 蜂窝 vs Wi-Fi 拨号竞速使用的默认网络策略。
default_network_typebadoption.Listable[InterfaceType][]<InterfaceType>默认出站偏好的网络类型。
default_fallback_network_typebadoption.Listable[InterfaceType][]<InterfaceType>偏好类型失败后回退的网络类型。
default_fallback_delaybadoption.Duration0<duration>切换到回退网络前的延迟。
default_http_clientstring(first http_clients entry)<http client tag>未设置 http_client 的远程 rule-set 所用的顶层 http_clients 条目 tag。完全没有 http_clients 时,会使用一个经默认出站拨号的隐式客户端(已弃用)。

源码: option/route.go:5-24 · 锚定版本 v1.14.2 (af6e64c)

规则 ​

rules[] 中每个条目都是多态的 Rule 对象。type 字段决定形态:

  • type: "default"(或省略)—— 扁平的 RawDefaultRule,匹配字段
    • 一个 RuleAction。
  • type: "logical" —— 对嵌套规则做布尔组合。

默认规则 —— 匹配字段 ​

RawDefaultRule 有 44 个字段,全部可选;多个设置时按 AND 组合。最常用的:

字段类型描述
inbound[]string按入站 tag 匹配。
network[]stringtcp、udp 或 tcp,udp。
protocol[]string嗅探出的应用协议。
domain / domain_suffix / domain_keyword / domain_regex[]string域名匹配器。
geosite / geoip / source_geoip[]string不支持 —— 使用它们的规则会在启动时失败。请改用 rule_set。
ip_cidr / source_ip_cidr[]stringCIDR 匹配。
ip_is_private / source_ip_is_privatebool匹配 RFC1918 / 链路本地 / loopback。
port / source_port[]uint16离散端口匹配。
port_range / source_port_range[]string端口范围匹配(80:90、1024: 等)。
process_name / process_path / process_path_regex[]string进程匹配(需要 find_process: true)。
package_name[]stringAndroid 包名(UID 通过系统 API 解析)。package_name_regex 按正则表达式匹配。
source_mac_address / source_hostname[]string来源设备的 MAC 地址 / DHCP 主机名,经邻居解析获得(Linux、macOS,或 Android / macOS 图形客户端)。
user / user_id[]string / []int32本地用户 / UID。
clash_modestring仅当运行模式(通过 Clash API 控制)匹配该字符串时命中。
wifi_ssid / wifi_bssid[]stringWi-Fi 感知路由(仅移动端)。
network_is_expensive / network_is_constrainedbooliOS / macOS 网络类型标志。
rule_set[]string命中任一命名 rule-set 即匹配。
invertbool反转整条规则的匹配结果。

逻辑规则 ​

json
{
  "type": "logical",
  "mode": "and",
  "rules": [ <Rule>, <Rule>, … ],
  "invert": false,
  "action": "..."
}

mode 为 and(默认)或 or。嵌套规则本身也可以是逻辑规则。

规则 action ​

每条规则携带一个 action 决定命中后的行为。八种取值:

Action含义
route(默认)送往 outbound。附加字段:override_address、override_port、network_strategy、udp_*、tls_fragment*、tls_spoof / tls_spoof_method。
route-options对 后续 匹配应用 route 选项,但不离开规则链。
direct直接拨号 —— 完全绕过 outbounds(使用 default_interface 等)。
bypass与 route 形态相同,单独命名便于日志区分。
reject丢弃连接。可配 method: "drop" 或 method: "default"。
hijack-dns把连接劫持为 DNS 流量,转入 DNS 引擎。
sniff对该连接执行协议嗅探。对来自 L3 入站(TUN、WireGuard、Tailscale)的 UDP 连接也会在预匹配阶段基于首个数据包执行。
resolve在后续规则执行前用命名 DNS 服务器解析目的域名。可选 timeout 与 disable_optimistic_cache。

Rule-sets ​

字段类型默认值允许值描述
typestringinlineinline | local | remoterule-set 所在位置。inline 把规则直接写在该配置里;local 从文件路径读取;remote 从 URL 下载。
tagbadoption.Listable[string](required)<string> | [<string>]供规则的 rule_set 匹配键引用的名称。可写成列表,一次定义多个共享其余选项的 rule-set;path、url 或 initial_path 中的 {tag} 会被替换为各个 tag(多 tag 时必需;不能与 type: inline 同用)。
formatstring(inferred)source | binary文件格式。source 是 JSON;binary 是已编译的 .srs 格式。未设置时由文件扩展名推断。

源码: option/rule_set.go:22-29 · 锚定版本 v1.14.2 (af6e64c)

本地 rule-set ​

json
{ "type": "local", "tag": "cn", "format": "binary", "path": "geosite-cn.srs" }

远程 rule-set ​

json
{
  "type": "remote",
  "tag": "cn",
  "format": "binary",
  "url": "https://example.com/geosite-cn.srs",
  "http_client": { "detour": "direct" },
  "update_interval": "168h"
}

内联 rule-set ​

json
{
  "type": "inline",
  "tag": "block",
  "rules": [
    { "domain_keyword": ["ads", "tracker"] }
  ]
}

inline 形态中的 rules 是 HeadlessRule —— 结构与路由规则相同,但没有 action 字段(动作由 引用方 规则决定)。

示例 ​

CN 直连 + 其余走 proxy:

json
{
  "route": {
    "rule_set": [
      { "type": "remote", "tag": "geoip-cn", "format": "binary",
        "url": "https://github.com/SagerNet/sing-geoip/raw/rule-set/geoip-cn.srs" },
      { "type": "remote", "tag": "geosite-cn", "format": "binary",
        "url": "https://github.com/SagerNet/sing-geosite/raw/rule-set/geosite-cn.srs" }
    ],
    "rules": [
      { "ip_is_private": true, "outbound": "direct" },
      { "rule_set": ["geoip-cn", "geosite-cn"], "outbound": "direct" },
      { "action": "sniff" },
      { "protocol": "dns", "action": "hijack-dns" }
    ],
    "final": "proxy",
    "find_process": false,
    "auto_detect_interface": true
  }
}

用 inline rule-set 拒绝广告:

json
{
  "route": {
    "rule_set": [
      {
        "type": "inline",
        "tag": "ads",
        "rules": [
          { "domain_keyword": ["doubleclick", "googlesyndication"] }
        ]
      }
    ],
    "rules": [
      { "rule_set": "ads", "action": "reject" }
    ]
  }
}

逻辑 OR 规则:

json
{
  "type": "logical",
  "mode": "or",
  "rules": [
    { "domain_suffix": [".onion"] },
    { "rule_set": ["tor-exit"] }
  ],
  "outbound": "tor"
}

说明 ​

  • 不支持 geoip / geosite:顶层块仍会被解析但被忽略,规则中的 geoip / geosite / source_geoip 会使该规则在启动时失败。请通过 rule_set(远程 .srs 文件)加载等价数据,并在规则的 rule_set 匹配字段中引用。
  • 远程 rule-set 通过 HTTP 客户端下载:http_client 可写内联对象(引擎、TLS 以及 detour 等拨号字段),或顶层 http_clients 条目的 tag;不写时使用 route.default_http_client 或第一个 http_clients 条目。download_detour 已弃用,改用 http_client。initial_path 用本地文件预置 rule-set 内容,使启动不被首次下载阻塞。
  • rule-set 的匹配语义:只有当被引用的 rule-set 恰好只含一条不带 invert 的 default 规则时,其字段才会合并进引用它的规则;其他任何 rule-set 都作为独立条件求值 —— 只要其中任意一条规则命中即为命中。
  • source_mac_address / source_hostname 依赖邻居解析;存在此类规则(或 local DNS 服务器设置了 neighbor_domain)时会自动启用。find_neighbor 可为日志强制启用,dhcp_lease_files 在 Linux / macOS 上提供主机名。
  • rule_set_ip_cidr_match_source(结构体中是 snake_case)控制 rule-set 的 IP-CIDR 规则是匹配源还是目的。别名 rule_set_ipcidr_match_source 已弃用。
  • sniff 与 resolve 这两个 rule action 通常放在规则列表 开头,以便后续规则能看到有用的元数据。DNS 引擎依赖 hijack-dns 截获路由引擎想处理的 DNS 查询。
  • clash_mode 仅在 Clash API 启用时生效 —— mode 来自那里。

跨内核说明 ​

  • Xray-core 使用单一多态规则形态,字段名为 camelCase,且匹配键集合小得多。没有 action 枚举 —— 每条规则都路由到 outboundTag 或 balancerTag。参见 Routing — Xray-core。
  • mihomo 使用紧凑一行式字符串规则(DOMAIN-SUFFIX,example.com,proxy),并有独立的 rule-providers: 机制承接远端规则列表。参见 Routing — mihomo。

源码: option/route.go:5-24 · v1.14.2 (af6e64c)

由 Argsment 出品的 Core Tutorial