Skip to content

路由 — sing-box

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

顶层选项

字段类型默认值允许值描述
geoip*GeoIPOptions(legacy)GeoIPOptions旧式 GeoIP 数据库位置与下载配置。已弃用,改用 rule-set。
geosite*GeositeOptions(legacy)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` 规则需要它。
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>切换到回退网络前的延迟。

源码: option/route.go:5-21 · 锚定版本 v1.13.11 (553cfa1)

规则

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

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

默认规则 —— 匹配字段

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

字段类型描述
inbound[]string按入站 tag 匹配。
network[]stringtcpudptcp,udp
protocol[]string嗅探出的应用协议。
domain / domain_suffix / domain_keyword / domain_regex[]string域名匹配器。
geosite[]stringGeoSite 类目 —— 现代配置请改用 rule-set。
geoip / source_geoip[]stringGeoIP 类目。
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:901024: 等)。
process_name / process_path / process_path_regex[]string进程匹配(需要 find_process: true)。
package_name[]stringAndroid 包名(UID 通过系统 API 解析)。
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": "..."
}

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

规则 action

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

Action含义
route(默认)送往 outbound。附加字段:override_addressoverride_portnetwork_strategyudp_*tls_fragment*
route-options后续 匹配应用 route 选项,但不离开规则链。
direct直接拨号 —— 完全绕过 outbounds(使用 default_interface 等)。
bypassroute 形态相同,单独命名便于日志区分。
reject丢弃连接。可配 method: "drop"method: "default"
hijack-dns把连接劫持为 DNS 流量,转入 DNS 引擎。
sniff对该连接执行协议嗅探。
resolve在后续规则执行前用命名 DNS 服务器解析目的域名。

Rule-sets

字段类型默认值允许值描述
typestringinlineinline | local | remoterule-set 所在位置。`inline` 把规则直接写在该配置里;`local` 从文件路径读取;`remote` 从 URL 下载。
tagstring(required)<string>供规则的 `rule_set` 匹配键引用的名称。
formatstring(inferred)source | binary文件格式。`source` 是 JSON;`binary` 是已编译的 `.srs` 格式。未设置时由文件扩展名推断。

源码: option/rule_set.go:20-27 · 锚定版本 v1.13.11 (553cfa1)

本地 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",
  "download_detour": "direct",
  "update_interval": "168h"
}

内联 rule-set

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

inline 形态中的 rulesHeadlessRule —— 结构与路由规则相同, 但没有 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"] },
    { "geoip": ["tor-exit"] }
  ],
  "outbound": "tor"
}

说明

  • 顶层 geoipgeosite 选项是 旧式。现代工作流是通过 rule_set(远程 .srs 文件)加载等价数据,并在规则的 rule_set 匹配字段中引用。
  • rule_set_ip_cidr_match_source(结构体中是 snake_case)控制 rule-set 的 IP-CIDR 规则是匹配源还是目的。旧式拼写 rule_set_ipcidr_match_source 已弃用。
  • sniffresolve 这两个 rule action 通常放在规则列表 开头, 以便后续规则能看到有用的元数据。DNS 引擎依赖 hijack-dns 截获 路由引擎想处理的 DNS 查询。
  • clash_mode 仅在 Clash API 启用时 生效 —— mode 来自那里。

跨内核说明

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

源码: option/route.go:5-21 · v1.13.11 (553cfa1)

由 Argsment 出品的 Core Tutorial