Skip to content

rule_sets — sets of conditions ​

A rule-set is a named set of conditions that route and dns rules refer to with the condition rule_set: <name>. The set matches if at least one of its rules matched.

yaml
rule_sets:
  ads:
    remote:
      url: https://example.org/ads.srs
      update: 24h
      via: proxy
  local-nets:
    file: local-nets.json
  ru:
    inline:
      - domain_suffix: [ru, su, рф]
      - ip_cidr: 77.88.8.0/24

route:
  rules:
    - match:
        rule_set: ads
      to: block
    - match:
        rule_set: [ru, local-nets]
      to:
        outbound: direct
  final:
    outbound: proxy

The key is the name of the set; there is no tag field. The source is exactly one of the keys remote, file, inline. Fields that only one source has are written inside its block: update next to remote is the error `update` belongs inside the `remote` block.

Sources ​

inline ​

A list of conditions right in the configuration. Each element is a separate rule of the set, and inside it conditions combine by the same rules as in match, including any, all, not:

yaml
rule_sets:
  work:
    inline:
      - domain_suffix: corp.example
      - all:
          - ip_cidr: 10.20.0.0/16
          - port: [443, 8443]

inline has no format field.

file ​

A file on disk: file: <path> or a block file: {path, format, category}. When the file changes the set is reread without reloading the configuration; a file rewritten with the same bytes is not an update. If the new content cannot be read, the previous rules stay.

remote ​

A URL the set is downloaded from: remote: <url> or a block with url and download fields.

Field of the remote blockMeaningDefault
urlThe address of the setrequired
formatSee belowby the ending of url
categoryCodes of a .dat file, see belowrequired for dat
updateHow often to download again24h
viaThe outbound or group to download throughsee below
http_clientThe name of a client from http_clients, or its description in place
initial_pathA file with content for the first start, while nothing has been downloaded

What the download goes through, in order: http_client, then via, then route.default_http_client (or, without it, the first client of http_clients), otherwise directly through the outbound of route.final. via and http_client together are an error.

A downloaded set is saved in cache if it is enabled. At startup the set is taken from the cache, and the next download waits until update has passed since the previous one. If there is no cache, the rules are taken from initial_path (if given) and the set is downloaded right away. A repeated download asks the server whether the file has changed (ETag). A failed download keeps the previous rules and is retried after 5 s, 30 s, 5 min, then every 30 min (but no less often than update), and every download is cut off if the server is silent for 30 s. If at startup there is neither a cache nor initial_path and the set cannot be downloaded, ours starts anyway: the set is empty (matches nothing) until a download succeeds. sing-box does not start in this case.

Format: format ​

ValueWhat it is
binaryThe sing-box binary format (.srs)
sourceThe sing-box rule-set JSON format ({"version": …, "rules": […]})
datA V2Ray/Xray geosite.dat or geoip.dat database; the rules are taken from the category codes

For file and remote the format is derived from the ending of the path: .srs is binary, .json is source, .dat is dat. Otherwise it has to be given explicitly:

yaml
rule_sets:
  ads:
    remote:
      url: https://example.org/lists/ads
      format: binary

V2Ray/Xray databases (.dat) ​

geosite.dat and geoip.dat (v2fly routercommon protobuf, which Xray reads too) are lists of codes: a geosite code has domains, a geoip code address prefixes. The set takes the codes listed in category from the file and matches with the same engine as .srs:

yaml
rule_sets:
  geosite-cn:
    file:
      path: /usr/share/xray/geosite.dat
      category: cn
  ads:
    remote:
      url: https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat
      format: dat
      category: [category-ads-all, google@ads]
  geoip-private:
    file:
      path: /usr/share/xray/geoip.dat
      category: private

route:
  rules:
    - match:
        geosite: cn        # the same as rule_set: geosite-cn
      to:
        outbound: direct

category is a string or a list. Each code is written as in Xray after geosite: and geoip::

EntryWhat is taken
cnAll entries of the code; case does not matter
google@cnThe domains of the code that have the attribute cn; google@cn@ads — both attributes
!cnFor geoip: addresses outside the code's prefixes (like geoip:!cn in Xray)

geosite domains become conditions of the set by their type: full is domain, domain is domain_suffix, keyword is domain_keyword, regexp is domain_regex (Rust regex syntax, as for domain_regex). All domains and prefixes of the codes are collected into one rule; a geoip code with ! (or with reverse_match in the file) is a separate rule with invert. A code that is not in the file is a load error; attributes that match nothing give an empty set.

The shorthands geosite: <x> and geoip: <x> in match refer to the set geosite-<x> and geoip-<x>; it still has to be declared in rule_sets.

A file is read whole on every load, and a remote set keeps the whole downloaded .dat in the cache. For many codes of one database a single file is more convenient, or converting to .srs in advance with ours rule-set convert -t dat (Command line).

Conditions inside a set ​

ConditionMeaning
domain, domain_suffix, domain_keyword, domain_regexThe destination domain
ip_cidr, source_ip_cidrThe destination and source IP
port, port_range, source_port, source_port_rangePorts
networktcp or udp
query_typeThe DNS query type (applies in dns.rules)
process_name, process_path, process_path_regexThe local process
network_typeThe type of the current network

The descriptions are in Conditions. There is nothing else in a set: inbound, protocol, rule_set inside a set are an error, and so are sing-box's Android and Wi-Fi conditions.

rule_set_ip_cidr_match_source: true in a rule that refers to a set makes the set's ip_cidr check the source IP.

Updates and live connections ​

When a remote or file set changes, the connections whose route depended on that set are checked again and handled according to the reload policy: by default, those that would now go to another outbound are closed (TCP) or moved (UDP).

GPL-3.0-or-later