Skip to content

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
KeyMeaning
matchConditions. Without match the rule matches any connection
toA final action: where to send the connection. The rules after it are not checked
doAn 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 match must 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_range and port/port_range. For example, domain_suffix and ip_cidr in one match match if either of them matched.
yaml
- match:
    network: tcp
    port: [80, 443]
    domain_suffix: [example.org, example.net]
  to:
    outbound: proxy

Logic: any, all, not ​

Complex conditions are built with explicit nodes. A node stands alone in match: the other conditions are written inside it.

NodeMeaning
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/all and a not without conditions are an error.
  • rule_set can 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: proxy

If 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

GPL-3.0-or-later