Routing — Xray-core
routing 块是决定连接走哪个出站的引擎。它承载一份按顺序匹配(先命中 先赢)的规则列表、一个全局 domainStrategy,以及可被规则引用的命名 负载均衡器 balancers。
顶层选项
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
rules | []json.RawMessage | [] | <rule object array> | 路由规则,按声明顺序逐条求值,命中第一条即停。 |
domainStrategy | *string | AsIs | AsIs | IpIfNonMatch | IpOnDemand | 目的域名在规则匹配中如何处理。`AsIs` 把域名当作字符串(只有 domain 规则匹配);`IpIfNonMatch` 在没有 domain 规则命中时再解析为 IP;`IpOnDemand` 只要存在 IP 规则就提前把每个域名解析为 IP。 |
balancers | []*BalancingRule | [] | [BalancingRule] | 命名的负载均衡器,可在规则目标中通过 `balancerTag` 引用。 |
源码: infra/conf/router.go:71-75 · 锚定版本 v26.6.1 (94ffd50)
rules[] —— 规则对象
每条规则都是一个 JSON 对象:匹配键 选出一组连接,目标键 决定 它们的去向。结构是多态的;解析器会根据出现的匹配键自行判断。
匹配键(全部可选,AND 组合):
| 字段 | 类型 | 描述 |
|---|---|---|
type | string | 必须为 "field"(V2Ray 中曾有 "chain" 等其他类型,现在仅支持 field)。 |
domain / domains | []string | 按目的域名匹配。支持前缀:full:example.com、domain:example.com、regexp:.+\\.com$、keyword:google、geosite:cn。 |
ip | []string | 按目的 IP 匹配。支持裸 IP、CIDR、geoip:cn。 |
source | []string | 按来源 IP 匹配(语法同 ip)。 |
port | string | 按目的端口匹配。80、80-90、80,443,8080-8090。 |
sourcePort | string | 按来源端口匹配(语法同上)。 |
network | string | 按传输协议匹配。tcp、udp、tcp,udp。 |
user | []string | 按入站用户的 email 标签匹配。 |
inboundTag | []string | 按入站 tag 匹配。 |
protocol | []string | 按嗅探出的应用协议匹配。http、tls、bittorrent、quic。要求入站启用了嗅探。 |
attrs | map[string]string | 按嗅探出的属性(如 Host)匹配。 |
domainMatcher | string | 域名匹配器实现。hybrid(默认,更快)或 linear。 |
目标键(二选一):
| 字段 | 描述 |
|---|---|
outboundTag | 把匹配到的流量送到该出站。 |
balancerTag | 把匹配到的流量送到负载均衡器(再由其在池内挑选出站)。 |
以及元数据字段:
| 字段 | 描述 |
|---|---|
ruleTag | 规则的可读名称。会出现在 API 与日志中。 |
balancers[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
tag | string | (required) | <string> | 负载均衡器名。被路由规则的 `balancerTag` 字段引用。 |
selector | StringList | (required) | [<outbound-tag prefix>] | 出站 tag 前缀列表。tag 以其中任一前缀开头的出站会进入该均衡器的池中。 |
strategy | StrategyConfig | {type: "random"} | {type: "random|leastLoad|leastPing|roundRobin", settings?: {...}} | 选择策略。`random` 在池中任选;`roundRobin` 轮询;`leastPing` 选延迟最低的(需要启用 [`observatory`](/zh/xray/observatory));`leastLoad` 选负载最低的。 |
fallbackTag | string | (unset) | <outbound tag> | 当均衡池为空或所有成员失败时使用的出站。 |
源码: infra/conf/router.go:21-26 · 锚定版本 v26.6.1 (94ffd50)
策略变体
random—— 均匀随机挑选。roundRobin—— 按池中顺序轮换。leastPing—— 选延迟最低的成员。需要启用observatory或burstObservatory,以便每个出站都有 已知延迟。leastLoad—— 选负载最低的成员。配套有strategyLeastLoadConfig(见infra/conf/router_strategy.go), 含健康检查调优字段。
示例
国内直连(典型「在中国大陆」模式):
json
{
"routing": {
"domainStrategy": "IpIfNonMatch",
"rules": [
{ "type": "field", "ip": ["geoip:private"], "outboundTag": "direct" },
{ "type": "field", "domain": ["geosite:cn"], "outboundTag": "direct" },
{ "type": "field", "ip": ["geoip:cn"], "outboundTag": "direct" },
{ "type": "field", "outboundTag": "proxy" }
]
}
}带延迟感知的负载均衡路由:
json
{
"routing": {
"rules": [
{ "type": "field", "outboundTag": "direct", "domain": ["geosite:cn"] },
{ "type": "field", "balancerTag": "proxy-balance" }
],
"balancers": [
{
"tag": "proxy-balance",
"selector": ["proxy-"],
"strategy": { "type": "leastPing" },
"fallbackTag": "direct"
}
]
},
"observatory": {
"subjectSelector": ["proxy-"],
"probeURL": "http://cp.cloudflare.com/generate_204",
"probeInterval": "30s"
}
}按应用协议匹配(要求入站开启嗅探):
json
{
"inbounds": [{
"port": 1080,
"protocol": "socks",
"sniffing": { "enabled": true, "destOverride": ["http", "tls"] }
}],
"routing": {
"rules": [
{ "type": "field", "protocol": ["bittorrent"], "outboundTag": "block" }
]
}
}说明
- 规则 按声明顺序 求值,命中第一条即停;后续规则只能看到未命中 的流量。
- 只存在 IP 规则时,
domainStrategy决定行为:AsIs—— 域名目的地跳过 IP 规则。最廉价。IpIfNonMatch—— 没有 domain 规则命中时再把域名解析为 IP,折中 选项。IpOnDemand—— 只要存在 IP 规则,就提前把每个域名解析为 IP。 最精确,开销也最大。
- 域名前缀(
full:、domain:、regexp:、keyword:、geosite:) 在匹配器内部解析。不加前缀时默认按domain:(后缀匹配)。 geoip:与geosite:规则从外部数据文件读取 (geoip.dat/geosite.dat),通常放在 Xray 二进制旁边。 类目名(cn、private、apple、google等)由这些文件定义。- 这些数据文件现在可以 自动更新:顶层
geodata块会按 cron 计划、经选定的出站下载新的geoip.dat/geosite.dat。参见 Geodata。 - 入站 嗅探 为
protocol与attrs匹配键提供输入。入站sniffing块接受destOverride,外加排除列表domainsExcluded以及近期 Xray 新增的ipsExcluded,对匹配的域名 / IP 跳过目的地 覆盖(连同metadataOnly与routeOnly)。 - 策略为
leastPing的均衡器只在配置了对应的 observatory 或 burst-observatory 时才工作。 ruleTag启用通过 gRPC RoutingService 的运行时规则操作(见 API)。
跨内核说明
- sing-box 使用 snake_case 的结构化规则,按条件分字段 (
domain_suffix、ip_cidr、process_name等)。规则可携带action(route、direct、reject、hijack-dns、sniff、resolve、 route-options)而不仅是出站 tag。参见 Routing — sing-box。 - mihomo 使用紧凑字符串形式的规则 (
DOMAIN-SUFFIX,example.com,proxy),按顺序求值;另有独立的rule-providers:机制承接远端规则列表。参见 Routing — mihomo。
源码: infra/conf/router.go:21-75 · v26.6.1 (94ffd50)
