路由 — 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。 |
final | string | (unset) | <outbound tag> | 无规则命中时使用的默认出站。未设置时使用 outbounds 中的第一个出站。 |
find_process | bool | false | true | false | 为每条连接查找发起进程。process_name / process_path 规则需要它。 |
find_neighbor | bool | false | true | false | 仅 Linux / macOS。即使没有 source_mac_address / source_hostname 规则,也强制启用邻居解析(局域网客户端的 MAC 地址 / 主机名),用于日志。 |
dhcp_lease_files | badoption.Listable[string] | (auto-detected) | [<file path>] | 仅 Linux / macOS。用于把局域网客户端映射到主机名与 MAC 地址的 DHCP 租约文件。为空时自动探测 dnsmasq、odhcpd、ISC dhcpd 与 Kea。 |
auto_detect_interface | bool | false | true | false | 自动发现系统的默认出站接口 —— 供未指定 bind_interface 的 Direct 出站使用。 |
override_android_vpn | bool | false | true | false | 仅 Android —— 绕过系统 VPN 服务进行出站拨号。 |
default_interface | string | (auto) | <interface> | 覆盖默认出站接口。优先级高于 auto_detect_interface。 |
default_mark | FwMark | 0 | <uint32> | 出站套接字上设置的 Linux SO_MARK。 |
default_domain_resolver | *DomainResolveOptions | (none) | DomainResolveOptions | 未指定规则时目的域名的默认解析器。 |
default_network_strategy | *NetworkStrategy | (unset) | NetworkStrategy | Happy-Eyeballs / 蜂窝 vs Wi-Fi 拨号竞速使用的默认网络策略。 |
default_network_type | badoption.Listable[InterfaceType] | [] | <InterfaceType> | 默认出站偏好的网络类型。 |
default_fallback_network_type | badoption.Listable[InterfaceType] | [] | <InterfaceType> | 偏好类型失败后回退的网络类型。 |
default_fallback_delay | badoption.Duration | 0 | <duration> | 切换到回退网络前的延迟。 |
default_http_client | string | (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 | []string | tcp、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 | []string | CIDR 匹配。 |
ip_is_private / source_ip_is_private | bool | 匹配 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 | []string | Android 包名(UID 通过系统 API 解析)。package_name_regex 按正则表达式匹配。 |
source_mac_address / source_hostname | []string | 来源设备的 MAC 地址 / DHCP 主机名,经邻居解析获得(Linux、macOS,或 Android / macOS 图形客户端)。 |
user / user_id | []string / []int32 | 本地用户 / UID。 |
clash_mode | string | 仅当运行模式(通过 Clash API 控制)匹配该字符串时命中。 |
wifi_ssid / wifi_bssid | []string | Wi-Fi 感知路由(仅移动端)。 |
network_is_expensive / network_is_constrained | bool | iOS / macOS 网络类型标志。 |
rule_set | []string | 命中任一命名 rule-set 即匹配。 |
invert | bool | 反转整条规则的匹配结果。 |
逻辑规则
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
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
type | string | inline | inline | local | remote | rule-set 所在位置。inline 把规则直接写在该配置里;local 从文件路径读取;remote 从 URL 下载。 |
tag | badoption.Listable[string] | (required) | <string> | [<string>] | 供规则的 rule_set 匹配键引用的名称。可写成列表,一次定义多个共享其余选项的 rule-set;path、url 或 initial_path 中的 {tag} 会被替换为各个 tag(多 tag 时必需;不能与 type: inline 同用)。 |
format | string | (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)
