Skip to content

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.

Crateours-mod-router, modules/routing/router
Binary featurerouter; 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 featuresthe 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 the ours_tls_options::Tls factory the module publishes; the crate does not depend on ours-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 on ours-mod-cache.

provides ​

  • Provide::Rules(compile_section) — compiling route.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 services ours_dns_types::rules::Compiler (the DNS rule compiler — used by the dns module) and, with rule-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 schemas route, 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 (implements ours_api::Rules). Conditions are grouped as in rule_abstract.go: OR within a group, AND across groups; destination domains and addresses are one group (domain_suffix OR ip_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 — a RuleSet behind 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), and Validator does not let an update change that mode.
  • src/remote.rs — remote: at start from the cache or initial_path, otherwise a download through an outbound; then every update_interval with an ETag.
  • src/dns.rs — DNS rules (rule_dns.go and the mode checks of dns/router.go).
  • src/bypass.rs — the analysis for auto_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 with fakeip_for_direct: false. Used by the ours-auto-nft crate and the binary.
  • src/dat.rs — rule-sets from V2Ray/Xray geosite.dat and geoip.dat (format: dat): the selected categories become the rules of a binary rule-set.
  • src/adguard.rs — AdGuard DNS filters as headless rules, as rule-set convert --type adguard writes them.
  • src/files.rs — rule-set files for the rule-set command: 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.

GPL-3.0-or-later