English
Rules
Code: modules/routing/router/src/syntax.rs, crates/api/src/rules.rs, crates/route-api/src/rules.rs, crates/config/src/lib.rs (fuse_options), crates/native-config/src/rules.rs, modules/dns/dns/src/syntax.rs, modules/routing/rule-process/src/lib.rs.
Routing rules (route.rules), DNS rules (dns.rules) and rule-set rules are read by the router module; the loader only assembles the list and remembers positions. The result is sing-box option types (Rule, DNSRule, HeadlessRule) with default/logical logic.
The form of a rule
yaml
- match: {domain_suffix: [example.com], process_name: [curl]}
to: {group: main} # or do: sniff | resolve | {options: {...}}route_rule: only the keys match, do, to (RULE_KEYS); exactly one of do/to.
to— a final action:outbound: x/group: x(→route),blockorblock: drop|reply|default(→reject),hijack-dns,bypass. A bare outbound name is an error with a hint (use `group: main`).do— an intermediate one:sniff(protocols→sniffer),resolve(withoutserver: the upstream is chosen bydns.rules),options(→route-options). The options are read byroute_optionsinrouter/src/syntax.rs:override: host:port(split_address), the blocksinterfaces(INTERFACES, as indial),udpandtls: {fragment, spoof}are expanded byungroupinto flat sing-box names,fallback_delayis a duration converted to milliseconds.do: options+ a following rule with the samematchand atoare one rule: the route options are attached toto(fuse_options, comparingmatchwithout positions —same). Such a rule takes two list elements;Decoded::writtenstores the span of the first one.
match
conditions::<R>(scope, node):
any: [...],all: [...],not: {...}— logic; no other keys next to them;notinverts the nested one (invert ^= true). The sing-box fieldsinvert,type,mode,rules,actioninmatchare errors with a hint.rule_set,geoip,geosite— rule-set names, checked byScope::has_rule_set.outboundin a DNS rule — names/patterns throughScope::select; a group is forbidden (it resolves nothing),*selects only outbounds. If only subscription servers are selected and there are none right now — a rule that matches nothing (an empty one withinvert).- a key that a module added to the
conditionspoint for this place (Place::Route/Place::Dns) is read by that module; - a key of a module not built into the binary —
condition `x` is not built in (feature ...); - the rest — fields of the rule type (
RawDefaultRule,RawDefaultDNSRule,DefaultHeadlessRule), strictly by name.
Conditions as the conditions point
router opens Provide::Point("conditions"); a module adds a condition with Provide::Condition(Entry { key, group, applies, decode, schema }) — an extension of the conditions point with the key key (Provide::extension).
rust
pub struct Entry {
pub key: &'static str, // the name in match
pub group: Group, // the sing-box condition group (ruleMatchState)
pub applies: &'static [Place], // Route, Dns
pub decode: fn(&Node, &dyn Scope) -> Result<Arc<dyn Condition>>,
pub schema: fn() -> Value, // the JSON Schema part of the value
}Condition — matches(&Subject), describe, decide(&Probe) -> Truth (a decision before the process), needs() (what to look up before the rule walk). group: Group::Each — a condition on its own; the other groups are alternatives within the group, as in sing-box.
Example — the rule-process module (requires: ["router"]): process_name, process_path, process_path_regex, user, user_id and package_name* (only to say that this is Android). The router does not know about them; without the module in the build the key gets "not built in".
Written and flat
Module conditions live in the rule type's field extensions: Written. For every condition Written stores the key, the node as written and the compiled condition with its group.
- Native reading puts a condition in already read (
Written::push). - Reading through
Decode(sing-box JSON, tests) puts in only the nodes;Written::resolve(scope, place)(throughRule::resolve) finishes reading them with the modules before compilation. Encodewrites conditions without positions,PartialEqcompares them bysame: rule equality does not depend on who read them and how.flat(rule)lifts the members ofextensionsto the level of the other conditions: in the canonical form, incheck --viewand in the dashboard a module's condition looks like an ordinary one (process_namenext todomain_suffix). That is whyextensionsas amatchkey is forbidden.
A rule without a configuration (a rule-set, a test) is read with ours_route_api::rules::Detached(&[modules]): it knows the extensions of the listed modules and does not know names.
route.rules as a whole
The loader (rules::route) assembles one list: the dns.listen hijacking rule (if any), the written rules, the rule from final if it is not an outbound. route_rules returns Decoded::new(Vec<Rule>) with written; the section is declared with kind Route. From written the loader computes the Origin of every rule: the position of the first element and the number of elements (2 for do: options + to); the loader's own rules get the path dns.listen or route.final.
route.final is mandatory (route: `final` is required):
{outbound: x}/{group: x}— the name is checked inNamesand passed to the kernel asroute.final;blockandbypass— the loader adds a synthetic outbound (block, ordirectnamedbypass; if the name is taken,block-1,bypass-1, …) and makes itfinal: whatever ends up at the default outbound is refused or goes direct too;- any other value — the last rule
{to: <value>}after the written ones.
dns.rules
The DNS module does not depend on the form of match directly: the router puts into the conditions point under the key dns the function dns_conditions (ReadConditions from crates/dns-types), and the DNS module gets it through Scope::extension("conditions", "dns"). DNS rule actions, upstreams and order — in dns.
Rule-sets
remote/file/inline are the router's Provide::RuleSet (remote_rule_set, file_rule_set, inline_rule_set). source_block expands the source block: a string is the url/path, a map — url/path and the fields only this source has (for remote: format, update, via, http_client, initial_path; for file: format); the same fields next to the source are the error "belongs inside the remote block". Native names are translated into sing-box ones (update → update_interval, via → download_detour), format is inferred from the extension, inline is a list of match without actions (HeadlessRule). Every rule-set is a rule-set component (Part); a remote one without a client and via uses the default HTTP client (Part::defaults).
See also the router and rule-process modules.