路由 — 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`。 |
final | string | (unset) | <outbound tag> | 无规则命中时使用的默认出站。未设置时使用 `outbounds` 中的第一个出站。 |
find_process | bool | false | true | false | 为每条连接查找发起进程。`process_name` / `process_path` 规则需要它。 |
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> | 切换到回退网络前的延迟。 |
源码: option/route.go:5-21 · 锚定版本 v1.13.11 (553cfa1)
规则
rules[] 中每个条目都是多态的 Rule 对象。type 字段决定形态:
type: "default"(或省略)—— 扁平的RawDefaultRule,匹配字段- 一个
RuleAction。
- 一个
type: "logical"—— 对嵌套规则做布尔组合。
默认规则 —— 匹配字段
RawDefaultRule 有 41 个字段,全部可选;多个设置时按 AND 组合。 最常用的:
| 字段 | 类型 | 描述 |
|---|---|---|
inbound | []string | 按入站 tag 匹配。 |
network | []string | tcp、udp 或 tcp,udp。 |
protocol | []string | 嗅探出的应用协议。 |
domain / domain_suffix / domain_keyword / domain_regex | []string | 域名匹配器。 |
geosite | []string | GeoSite 类目 —— 现代配置请改用 rule-set。 |
geoip / source_geoip | []string | GeoIP 类目。 |
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 解析)。 |
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*。 |
route-options | 对 后续 匹配应用 route 选项,但不离开规则链。 |
direct | 直接拨号 —— 完全绕过 outbounds(使用 default_interface 等)。 |
bypass | 与 route 形态相同,单独命名便于日志区分。 |
reject | 丢弃连接。可配 method: "drop" 或 method: "default"。 |
hijack-dns | 把连接劫持为 DNS 流量,转入 DNS 引擎。 |
sniff | 对该连接执行协议嗅探。 |
resolve | 在后续规则执行前用命名 DNS 服务器解析目的域名。 |
Rule-sets
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
type | string | inline | inline | local | remote | rule-set 所在位置。`inline` 把规则直接写在该配置里;`local` 从文件路径读取;`remote` 从 URL 下载。 |
tag | string | (required) | <string> | 供规则的 `rule_set` 匹配键引用的名称。 |
format | string | (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 形态中的 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"] },
{ "geoip": ["tor-exit"] }
],
"outbound": "tor"
}说明
- 顶层
geoip与geosite选项是 旧式。现代工作流是通过rule_set(远程.srs文件)加载等价数据,并在规则的rule_set匹配字段中引用。 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-21 · v1.13.11 (553cfa1)
