Skip to content

Names and references ​

Code: crates/native-config/src/tree.rs (Names, glob_match), crates/native-config/src/entity.rs (names, group_members, find_cycle), libs/codec/src/refs.rs, crates/kernel/src/graph.rs.

References are checked in two layers: the loader — by the written names and at load time (an error with a position), the kernel's graph — by the References of the decoded values (dependency edges for reload).

Namespaces ​

NamespaceWhereCheck
outbounds + groups + endpointsNamesshared: an outbound and a group cannot have the same name
subscription serversNames (servers)<subscription>/<server>, unique in advance
inboundsContext::inbound_namesfor Scope::unique
rule-setsContext::rule_setsScope::has_rule_set
DNS upstreamsthe dns modulewithin the section

check_name: an empty name, the pattern characters * ? [ ] { } and / are forbidden in written names. / is reserved for subscription servers. Inbound, rule-set and upstream names may use pattern characters but not / (checked by check_name). A repeat — `x` is already the name of an outbound (first at line:column).

All names of outbounds and groups are entered into Names before anything is decoded — forward and mutual references work regardless of the order in the text.

Kinds of references in the text ​

  • Names::of_kind(node, Kind) — a reference with a kind: {outbound: x} or {group: x} in route.final, to. The wrong kind — `x` is a group: use `group: x`.
  • Names::any(node, what) — an outbound or a group: dial.detour, members, via.
  • A subscription server cannot be reached by name from these places (reach it through a group): servers come and go, and only a group may hold on to them.
  • A reference to a rule-set is rule_set: name in match; geoip: x/geosite: x is the rule-set geoip-x/geosite-x.

Modules check references through Scope (of_kind, any, select, has_rule_set), without seeing Names directly.

Patterns ​

Names::select(pattern, span): a string without pattern characters is the name itself (it must exist); otherwise {a,b} is expanded (without nesting), each alternative is a glob (*, ?, [...] with !/^ and ranges), matches come in definition order (Names::order: outbounds, groups, subscription servers). An empty result is the error matches no outbound or group.

select_soft — the same for places that may concern subscriptions: a pattern with / or a name from a subscription namespace that selected nothing gives an empty list and a warning (matches no server now, is not a server now) instead of an error.

Group members ​

group_members for every group:

  1. members — a list of names and {match: pattern} elements, or a single {match: ...};
  2. a name → any (or select_soft, if it is in a subscription namespace); a pattern → select_soft, the group itself is excluded from the result;
  3. duplicates are dropped, keeping the order;
  4. a group that concerns subscriptions is marked "open" (open_groups): it may be empty, gets allow_empty, and an empty one gives the warning is empty: it refuses new connections;
  5. find_cycle — a DFS over the already resolved names; a cycle is an error on the first group of the cycle: group `a` contains itself: a -> b -> a.

Resolving patterns is the loader's business, not the group module's: the module gets a ready list of members, and the pattern is recomputed on every load (including when subscription servers change).

RefKind and References ​

After decoding, references live in the values. A field with #[config(reference = "outbound")] (derive) or a hand-written References implementation puts Reference { kind, tag } into Decoded::references/Part::references.

rust
enum RefKind { Inbound, Outbound, DnsServer, RuleSet, NetworkNamespace, HttpClient }

The kernel's graph (Graph::build) turns references into component dependencies: a reference to a missing component or to itself is unresolved. Besides explicit references:

  • Part::defaults: Vec<RefKind> — the component uses the default of a kind (a rule-set without http_client and via depends on the default HTTP client);
  • Decoded::defaults: Vec<Reference> — a section assigns the default of a kind (route.default_http_client); for outbounds the default is route.final; without an assignment — the first component of the kind.

The graph and the reload plan in detail — kernel.

Vanished subscription groups ​

A reference group: <subscription>/<group> to a group the subscription does not currently describe is not an error: of_kind remembers it in Names::gone, and the loader creates a substitute — see subscriptions.

GPL-3.0-or-later