English
Routing rules
route.rules is a list of rules. A rule is a match condition and exactly one action: do or to.
yaml
route:
rules:
- match:
domain_suffix: example.org
port: 443
to:
outbound: proxy
final:
outbound: direct| Key | Meaning |
|---|---|
match | Conditions. Without match the rule matches any connection |
to | A final action: where to send the connection. The rules after it are not checked |
do | An intermediate action: it is performed, and the check continues from the next rule |
do and to in one rule are an error, as is a rule without either. A rule has no other keys: action, outbound, type, mode, invert from sing-box are an error with a hint.
A to rule without match takes every connection, so the rules after it and final never apply: check and startup warn about it. Such a rule is final: write its to there.
Actions are in Actions, conditions in Conditions.
How conditions combine
- Different conditions of one
matchmust all hold (AND). - Several values of one condition need only one to match (OR):
port: [80, 443]. - Some conditions form groups combined with OR, as in sing-box: the destination address (
domain*,ip_cidr,ip_is_private),source_ip_cidr/source_ip_is_private,source_port/source_port_rangeandport/port_range. For example,domain_suffixandip_cidrin onematchmatch if either of them matched.
yaml
- match:
network: tcp
port: [80, 443]
domain_suffix: [example.org, example.net]
to:
outbound: proxyLogic: any, all, not
Complex conditions are built with explicit nodes. A node stands alone in match: the other conditions are written inside it.
| Node | Meaning |
|---|---|
any: [ … ] | At least one of the conditions (OR) |
all: [ … ] | All conditions (AND) |
not: { … } | Negation |
yaml
- match:
any:
- port: 853
- all:
- network: udp
- port: 443
- not:
domain_suffix: example.org
to: block- Nodes nest into each other to any depth.
- An empty
any/alland anotwithout conditions are an error. rule_setcan be written inside any node.
do: options before to
Connection parameters (do: options, see Actions) can be applied to the route of one rule: write do: options and right after it a rule with to and the very same match. Such a pair is one rule. Its to must be outbound, group or bypass; with block or hijack-dns it is a load error.
yaml
- match:
domain_suffix: example.org
do:
options:
udp:
timeout: 5m
- match:
domain_suffix: example.org
to:
outbound: proxyIf the next rule is not a to or its match differs, do: options is an ordinary intermediate action: the parameters are remembered and apply to all the rules below until the connection gets a route.
Order and numbers
The rules are checked in the order written. In explain, the log and the dashboard a rule is called rule[N], its number in the list the router goes through: a do: options + to pair counts as one rule, and if dns.listen is enabled, one more rule stands before yours, redirecting queries from its inbounds to hijack-dns.
Example
yaml
groups:
main:
members: [proxy-de, proxy-nl, direct]
rule_sets:
ru:
inline:
- domain_suffix: [ru, su, рф]
route:
rules:
- match:
inbound: local
do:
sniff:
timeout: 300ms
- match:
any:
- protocol: dns
- port: 53
to: hijack-dns
- match:
protocol: bittorrent
to:
outbound: direct
- match:
rule_set: ru
to:
outbound: direct
- match:
ip_is_private: true
to:
outbound: direct
final:
group: main
inbounds:
local:
mixed:
listen: 127.0.0.1:1080
outbounds:
direct:
direct:
proxy-de:
trojan:
server: de.example.com:443
password: secret
tls:
sni: de.example.com
proxy-nl:
trojan:
server: nl.example.com:443
password: secret
tls:
sni: nl.example.com