Skip to content

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.

FlagMeaning
-c, --configa YAML file. Use - or stdin for standard input. The default is config.yaml
-C, --config-directorya directory holding exactly one *.yaml or *.yml file
-D, --directorythe 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):

  1. ours_native_config::load_with(text, &decoders);
  2. in a build with subscriptions: ours_mod_subscriptions::runtime::prime(&config.sections) reads into the store the servers that are not there yet (from file or the cache file, without the network); if anything was read, loading is repeated so that the subscriptions' Provide::Source gives out these servers;
  3. 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.

  1. version::migrate (crates/native-config/src/version.rs) takes version. Today CURRENT is 1 and STEPS is empty. A version newer than CURRENT is 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 steps STEPS[n - 1] (version n → 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 to STEPS and CURRENT grows. Then root.known — a top-level key not from TOP_LEVEL is an "unknown field" error on that key right away, before any section.
  2. outbounds, groups and endpoints are taken from the root; all their names are entered into Names before decoding — forward references work. The name defaults in outbounds is reserved.
  3. subscriptions and the names of rule_sets are taken (only the names: rules check rule_set: name before the rule-sets are decoded).
  4. A Context is built — the implementation of Scope for modules (see types).
  5. subscriptions: the section is decoded by the module, then Decoders::expand gives out the servers (outbounds) and groups; subscription groups are appended after the written groups.
  6. The sections log, certificate, ntp, network_namespaces, http_clients, adblock.
  7. inbounds — every entity; then endpoints — every entity.
  8. outbounds (with defaults), then the subscription servers; then the groups: members are resolved, cycles are checked, every group is decoded as an outbound of type group.
  9. dns: the section is decoded by the DNS module; the domain_resolver blocks it gives are appended to the outbounds; Decoders::expand("dns") gives out the dns.listen inbounds and the hijacking rule.
  10. route: the rules (the hijacker first, a non-outbound final last), final, rule-sets, the modules' route.<key> subsections, the rest of route — to the kernel.
  11. Vanished subscription groups get a substitute leading to final.
  12. Decoders::check — the modules' checks over everything decoded (View).
  13. The listen-address collision check (shared_listen); then cache, reload, runtime, admin.
  14. 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 the dns.listen listeners;
  • outbounds: groups (the written ones, then subscription groups), the written outbounds, subscription servers, the synthetic block from final: block and direct named bypass from final: 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).

GPL-3.0-or-later