English
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-routeropens theconditionspoint (Provide::Point("conditions")inmodules/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 inmatch;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 howauto_nftdecides what to capture);Truth::Unknownby default leaves the flow to the process;needs()— what the router must find out before walking the rules (Needs { process, neighbour, network_type }).
Steps
The crate
modules/routing/rule-<name>, the packageours-mod-rule-<name>— following module.md. Dependencies:ours-api,ours-codec,ours-jsonand whatever the condition needs. There is no need to depend onours-mod-router: everything goes through the contract.The condition type and
impl ours_api::rules::Condition. Connection data comes fromsubject.metadata; if the condition needs something that is looked up separately (the process, the neighbour), declare it inneeds(), otherwise the field stays empty:rustfn needs(&self) -> Needs { Needs { process: true, ..Needs::default() } }The decoder: the value is a string or a list (
Listable<T>fromours_codec::types); an error — with a position (ours_config::error(node.span, …)).describeis built once at decoding and repeats sing-box (including its abbreviations).MODULE:rustconst 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"]— theconditionspoint is opened by the router.The binary: the feature
rule-<name> = ["router", "dep:ours-mod-rule-<name>"], if needed include it in the umbrellarule-conditions(likerule-process), a line inLIST— 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].