English
router
Route rules compiled for fast matching, the rule-sets they are matched against, DNS rules (their conditions are the same) and rule analysis for auto_nft.
| Crate | ours-mod-router, modules/routing/router |
| Binary feature | router; the sources rule-set-inline, rule-set-local, rule-set-remote, rule-set-binary, rule-set-dat; the conditions rule-conditions (= all rule-* below + rule-process), rule-metadata, rule-ip, rule-domain, rule-dns, rule-port, rule-network, rule-neighbor |
| Crate features | the same names; rule-set-remote → ours-http + ours-tls-options; rule-set-binary, rule-ip, rule-domain → ours-srs (+ regex); rule-set-dat → ours-geodat + rule-set-binary; sniff → ours-sniff/all |
A condition or source turned off by a feature is rejected at load time with a clear error (the test omitted_conditions_fail_after_full_schema_decoding in src/lib.rs).
requires
tls— remote rule-sets are downloaded by an HTTP client with TLS from theours_tls_options::Tlsfactory the module publishes; the crate does not depend onours-mod-tls.cache— a remote rule-set is stored in the cache file (<scope>/rule-set/<tag>) taken through the contract (ours_api::cache::of); the crate does not depend onours-mod-cache.
provides
Provide::Rules(compile_section)— compilingroute.rules(with the host: without it a rule with an interface condition is rejected — a rule that never matches is worse than an error).Provide::RuleSets(build_rule_set)— building rule-sets;Provide::RuleSet { key: "remote" | "file" | "inline" }— the sources.Provide::Unit(DnsRules)— puts into the servicesours_dns_types::rules::Compiler(the DNS rule compiler — used by thednsmodule) and, withrule-set-remote, the download HTTP clients (remote::Clients).Provide::Point("conditions")— rule conditions; modules add their own (rule-process).Provide::Extension { point: "conditions", key: "dns" }— reading DNS conditions.- The sections
http_clients,route.rules,route.default_http_client; the schemasroute,rule_sets.
Syntax: route, rule_sets, http_clients.
Dependencies
ours-srs (the .srs format and matchers), ours-sniff, ours-dns-types, ours-http, ours-net-info, ours-net-sys, ours-tls-options, ours-api, ours-codec, ours-json, arc-swap, regex.
Design
src/lib.rs—compile*,Router(implementsours_api::Rules). Conditions are grouped as inrule_abstract.go: OR within a group, AND across groups; destination domains and addresses are one group (domain_suffixORip_cidr). A rule-set of one rule is merged into the groups of the referring rule.RuleNeeds— what to look up before the walk (the process, the neighbour and so on).src/item.rs— conditions: domains in a succinct trie, addresses in sorted ranges, ports in a bitmap.src/rule_set.rs— aRuleSetbehind a swap: an updated rule-set changes the rules in place, and the rules referring to it are not rebuilt; a local one is reread when the file changes; a loading error keeps the previous rules.Metadata— which kinds of conditions are in the rule-set (DNS chooses its mode by it), andValidatordoes not let an update change that mode.src/remote.rs— remote: at start from the cache orinitial_path, otherwise a download through an outbound; then everyupdate_intervalwith anETag.src/dns.rs— DNS rules (rule_dns.goand the mode checks ofdns/router.go).src/bypass.rs— the analysis forauto_nft: rules are evaluated in three-valued logic over cells of the address space;capture— what to capture,analyze— what can be bypassed,Answers(RealAnswers) — which DNS answers to give as real ones withfakeip_for_direct: false. Used by theours-auto-nftcrate and the binary.src/dat.rs— rule-sets from V2Ray/Xraygeosite.datandgeoip.dat(format: dat): the selected categories become the rules of a binary rule-set.src/adguard.rs— AdGuard DNS filters as headless rules, asrule-set convert --type adguardwrites them.src/files.rs— rule-set files for therule-setcommand: source to binary and back, AdGuard and dat to binary, which rules of a file match a domain or an address.src/syntax.rs,src/schema.rs,src/options/— the notation in the native configuration, the schema, the option types.
Tests
modules/routing/router/tests/: rules.rs, rule_sets.rs, dns_rules.rs, dns_router.rs (sing-box cases), live.rs, rule_set_updates.rs, remote_rule_sets.rs, dns_live.rs, dns_fakeip_label.rs, dns_reload.rs, rule_set_files.rs, references.rs, bench_cost.rs (--ignored); the example examples/rules_walk.rs.