Skip to content

Add a rule condition ​

A condition is a key in the match of a route rule, a DNS rule or a rule-set rule. A module adds it through Provide::Condition; the router finds the condition by key and does not know the module by name. The model is modules/routing/rule-process (process_name, process_path, user…).

Built-in conditions (domain*, ip_cidr, ports, network…) live in ours-mod-router itself under its features (rule-ip, rule-domain, …); a separate module is needed when a condition needs its own dependencies (here — ours-net-info and regex) or has to be turned off separately.

How the point works ​

  • ours-mod-router opens the conditions point (Provide::Point("conditions") in modules/routing/router/src/lib.rs).
  • Provide::Condition(Entry) takes the slot ("conditions", key) (crates/api/src/module.rs).
  • Entry (crates/api/src/rules.rs):
    • key — the name in match;
    • group — how the condition combines with its neighbours: Group::Each — on its own, must match; other groups (SourceAddress, DestinationPort…) — OR within the group, as in sing-box;
    • applies — where it is allowed: Place::Route, Place::Dns;
    • decode(node, scope) -> Result<Arc<dyn Condition>> — parse the value;
    • schema() -> Value — the JSON Schema of the value.
  • Condition: matches(&Subject) -> bool; describe() — as in logs and explain (process_name=[curl wget]); decide(&Probe) — the condition's value from what is known about a flow before the process sees it (address, port, domain — this is how auto_nft decides what to capture); Truth::Unknown by default leaves the flow to the process; needs() — what the router must find out before walking the rules (Needs { process, neighbour, network_type }).

Steps ​

  1. The crate modules/routing/rule-<name>, the package ours-mod-rule-<name> — following module.md. Dependencies: ours-api, ours-codec, ours-json and whatever the condition needs. There is no need to depend on ours-mod-router: everything goes through the contract.

  2. The condition type and impl ours_api::rules::Condition. Connection data comes from subject.metadata; if the condition needs something that is looked up separately (the process, the neighbour), declare it in needs(), otherwise the field stays empty:

    rust
    fn needs(&self) -> Needs {
        Needs { process: true, ..Needs::default() }
    }
  3. The decoder: the value is a string or a list (Listable<T> from ours_codec::types); an error — with a position (ours_config::error(node.span, …)). describe is built once at decoding and repeats sing-box (including its abbreviations).

  4. MODULE:

    rust
    const EVERYWHERE: &[Place] = &[Place::Route, Place::Dns];
    
    pub const MODULE: ours_api::Module = ours_api::Module {
        name: "rule-process",
        requires: &["router"],
        provides: &[
            Provide::Condition(Entry { key: "process_name", group: Group::Each,
                applies: EVERYWHERE, decode: process_name, schema: strings }),
            // ...
        ],
    };

    requires: &["router"] — the conditions point is opened by the router.

  5. The binary: the feature rule-<name> = ["router", "dep:ours-mod-rule-<name>"], if needed include it in the umbrella rule-conditions (like rule-process), a line in LIST — see module.md.

Tests ​

modules/routing/rule-process/tests/rules.rs: the conditions are checked through the real router without the kernel — ours_route_api::rules::Detached(&[&MODULE]) gives a scope in which a rule finds the module's conditions, the rules are compiled by ours_mod_router::compile_with and walked by ours_route_api::walk::walk. ours-mod-router with the needed features — in [dev-dependencies].

GPL-3.0-or-later