English
Loading
Code: app/ours/src/main.rs, app/ours/src/subscriptions.rs, crates/native-config/src/lib.rs, crates/native-config/src/entity.rs.
Where the text comes from
Pass either -c or -C once. Commands such as check and run also accept a positional file path.
| Flag | Meaning |
|---|---|
-c, --config | a YAML file. Use - or stdin for standard input. The default is config.yaml |
-C, --config-directory | a directory holding exactly one *.yaml or *.yml file |
-D, --directory | the working directory. The process changes to it before reading and resolves relative paths from it |
read_config_at accepts the extensions .yaml and .yml and passes the text to subscriptions::load. If -C selects zero or multiple files, native_options rejects the configuration. With --watch, the process watches the selected directory. The dashboard saves edits only when a single file path is given (saved_file).
Two passes for the sake of subscriptions
subscriptions::load(text):
ours_native_config::load_with(text, &decoders);- in a build with
subscriptions:ours_mod_subscriptions::runtime::prime(&config.sections)reads into the store the servers that are not there yet (fromfileor the cache file, without the network); if anything was read, loading is repeated so that the subscriptions'Provide::Sourcegives out these servers; - the subscription warnings are added to the loader's warnings.
The same load is used by reload on a signal/--watch, by reload when subscription servers change (reload_subscriptions rereads the active text) and by the dashboard (ui::Host::load).
The loader's steps
load_with = ours_yaml::parse + entity::lower. The order in lower matters: it determines what each step already knows.
version::migrate(crates/native-config/src/version.rs) takesversion. TodayCURRENTis 1 andSTEPSis empty. A version newer thanCURRENTis an error on its value before anything else (sections of a future version may mean something else); without the field —CURRENT. A document of an earlier version goes through the stepsSTEPS[n - 1](versionn→n + 1), each writes a warning about what was rewritten. Then — renames within the version (experimental.cache_file→cache, with a warning; both at once is an error). Another version number is needed when an old name stops being read or a field's meaning changes: then a step is added toSTEPSandCURRENTgrows. Thenroot.known— a top-level key not fromTOP_LEVELis an "unknown field" error on that key right away, before any section.outbounds,groupsandendpointsare taken from the root; all their names are entered intoNamesbefore decoding — forward references work. The namedefaultsinoutboundsis reserved.subscriptionsand the names ofrule_setsare taken (only the names: rules checkrule_set: namebefore the rule-sets are decoded).- A
Contextis built — the implementation ofScopefor modules (see types). subscriptions: the section is decoded by the module, thenDecoders::expandgives out the servers (outbounds) and groups; subscription groups are appended after the written groups.- The sections
log,certificate,ntp,network_namespaces,http_clients,adblock. inbounds— every entity; thenendpoints— every entity.outbounds(withdefaults), then the subscription servers; then the groups: members are resolved, cycles are checked, every group is decoded as an outbound of typegroup.dns: the section is decoded by the DNS module; thedomain_resolverblocks it gives are appended to the outbounds;Decoders::expand("dns")gives out thedns.listeninbounds and the hijacking rule.route: the rules (the hijacker first, a non-outboundfinallast),final, rule-sets, the modules'route.<key>subsections, the rest ofroute— to the kernel.- Vanished subscription groups get a substitute leading to
final. Decoders::check— the modules' checks over everything decoded (View).- The listen-address collision check (
shared_listen); thencache,reload,runtime,admin. root.finish— a top-level key that no step took is an error.
A top-level key not in TOP_LEVEL is rejected at step 0, before any section. A section whose module is not in the build fails at its own step ("not built in"), so an error in an earlier section is shown before it.
The order of entities in Config
ordered lays out the decoded entities in listing order:
inbounds: the written ones, then thedns.listenlisteners;outbounds: groups (the written ones, then subscription groups), the written outbounds, subscription servers, the syntheticblockfromfinal: blockanddirectnamedbypassfromfinal: bypass, substitutes for vanished groups;endpoints: in the order written;rule_sets: in the order written.
Groups first — as in typical sing-box configurations; route.final is mandatory, so the kernel's "first outbound by default" decides nothing.
The result
rust
pub struct Loaded { pub config: Config, pub origins: Vec<Origin>, pub warnings: Vec<String> }load returns only Config, load_located — Config and origins. origins are needed by the dashboard (dashboard view), warnings — by the log and check (errors).