Skip to content

Rules, rule-sets, conditions, DNS ​

Provide::Rules, Provide::RuleSets, Provide::Condition, Provide::Dns — crates/api/src/module.rs; the traits — route.rs, rules.rs, adapter.rs, dns.rs in crates/api/src. Singletons: at most one of each in a build. Currently Rules, RuleSets — the router module, Dns — the dns module. Without them the kernel substitutes stubs (Instance).

Rules ​

rust
type RulesFactory = fn(sections: &Blocks, rule_sets: &HashMap<String, Arc<dyn RuleSet>>,
                       host: Option<&Arc<dyn Host>>) -> Result<Arc<dyn Rules>, String>;

Compiles the route.rules section (which the same module decoded) with ready rule-sets. The Rules trait: len, matches(index, metadata, why), action(index), describe(index), uses_rule_set(index, tag) (for a small reload on a rule-set update), needs(metadata) (what to look up before the walk: the process, the neighbour, the interface type), dependencies() (what is needed at all — the kernel checks the host at build time), as_any.

The rule walk is not part of the module: step and walk in crates/route-api/src/walk.rs are the same for the dispatcher, reload and explain. Actions (Action): Route, RouteOptions, Bypass, Reject, Sniff, Resolve, HijackDns; the decision (Decision): Default, Outbound(tag), Reject(method), HijackDns. In detail — Data path.

RuleSets and RuleSet ​

rust
type RuleSetFactory = fn(tag: &str, blocks: &Blocks, outbounds: &Outbounds<'_>,
                         cx: &Context<'_>) -> Result<Arc<dyn RuleSet>, String>;

Builds a rule-set from what the source decoded (Provide::RuleSet, Sections). outbounds — for downloading a remote one through a detour.

The RuleSet trait: as_any (the router that built it knows the type), start(updated) — bring it up to date (I/O) and keep it so until close, calling updated after every change; it is started on a shard; close; attach_services (the cache). The kernel passes as updated a callback that calls DispatchControl::rule_set_updated.

Condition — rule conditions from other modules ​

rust
Provide::Condition(Entry { key, group, applies, decode, schema })

A variant of the router module's conditions point (Points): the key in match, the group (Group: Each — a condition of its own that must match; the source and destination address/port groups are alternatives within the group, like sing-box's ruleMatchState), where it is allowed (Place::Route, Place::Dns), a decoder of the value into Arc<dyn Condition> and the value's JSON Schema.

The Condition trait: matches(&Subject) (connection metadata and/or a DNS query), describe, decide(&Probe) — a three-valued decision for analysis without a connection, needs(). The router finds the contribution through ours_route_api::rules::entry(scope, key). Example — rule-process (modules/routing/rule-process/src/lib.rs).

Dns ​

rust
type DnsFactory = fn(sections: &Blocks, outbounds: &Outbounds<'_>,
                     rule_sets: &HashMap<String, Arc<dyn RuleSet>>, previous: Option<&Arc<dyn Dns>>,
                     plan: &Plan, context: &DnsContext) -> Result<Arc<dyn Dns>, String>;

It gets the previous Dns and the plan and decides itself what to reuse (servers are dns-server[tag] components, the DNS router is dns). DnsContext: a weak reference to DnsCell, Host, the configuration's services.

The Dns trait: start(); lookup(domain, Lookup) — for connections and outbounds; exchange(query, metadata, udp) — the answer to a DNS query from a connection (hijack-dns); fake_ip(ip), reverse(ip) — fake-ip and reverse mapping; attach_services; set_real_answers — for auto_nft. Other modules see DNS through DnsCell (Runtime types).

GPL-3.0-or-later