Skip to content

Entities ​

Code: crates/native-config/src/entity.rs (inbound, outbound, group, split_entity, merge, synthetic), crates/native-config/src/rules.rs (lower_rule_sets), crates/config/src/blocks.rs (listener, dialer).

An entity is a named object of a list: an inbound, an outbound, a group, a rule-set. In Config it is a pair (name, Blocks): the name and the decoded blocks, one of which is the protocol block.

Form ​

yaml
outbounds:
  proxy:              # the name is the map key
    vless:            # the protocol block: key = type
      server: ...
      tls: {...}      # blocks that not every type takes go inside the protocol
      transport: {ws: {...}}
    dial: {...}       # entity blocks go next to the protocol
inbounds:
  in:
    socks:
      listen: ...     # the address is in the protocol
    listener: {...}   # the inbound's socket goes next to it, like dial for an outbound

split_entity splits the entity's keys: everything not in ENTITY_BLOCKS is a candidate for the protocol block; there must be exactly one. Then it takes tls, transport, mux (PROTOCOL_BLOCKS) out of the protocol block and puts them with the entity blocks; the same keys at the entity level are the error `tls` belongs inside the `vless` block. Without a protocol or with two — no protocol block / one protocol block expected, found .... There are no type and tag fields: the type is the block's key, the name is the entity's key.

The protocol block ​

It is decoded by the type's module: Provide::Inbound { key, decode, build } / Provide::Outbound { key, decode, build }, the call is Decoders::decode_protocol(section, kind, ...). An unknown type — unknown outbound type `x`; available: ... or "not built in".

Protocol modules usually use the ours_config helpers (crates/config):

  • listener::<T>(node) — for an inbound: listen: host:port → listen + listen_port (split_address), a multiplex key inside the protocol is the error "belongs in the mux block" (refuse); sing-box fields replaced by route actions (sniff*, domain_strategy, udp_disable_domain_unmapping, proxy_protocol*) are the error "x is not an inbound field: …" with a hint (LISTEN_REMOVED);
  • listener_with_users::<T>(node) — the same, and in addition it moves users out of the cold form (Decoded::cold): the inbound applies a change of users without a rebuild (hot_changed);
  • dialer::<T>(node) — for an outbound: server: host:port, udp_over_tcp is enabled by presence (enable: writes in enabled: true, an explicit enabled is an error), the DialerOptions fields are the error "belongs in the dial block".

Both decode into the sing-box options type (T) with the exact field names (Decoded::of).

An inbound's socket settings are the listener block at the entity level. The loader (listener in entity.rs) rejects socket fields in the protocol block (`interface` belongs in the inbound's `listener` block), expands the block (config::listener_block: interface/fwmark → bind_interface/routing_mark, tcp, udp) and appends the result to the protocol's members before decoding: every protocol's options type carries sing-box's ListenOptions anyway, so the protocol decoders know nothing about the block. The rules of form — Nesting and shared blocks.

A protocol that has fields that make sense only together writes its own decoder from parts: listener_members/dialer_members, then ungroup(members, block, fields, shorthand) — expands a block (hop: {ports, interval} in hysteria*, encryption: {key, cipher} in VLESS) into flat option names and rejects flat names next to it (`hop_interval` is `hop.interval`); unkind(members, key) — the kind as a key (obfs: {salamander: {password}} → {type: salamander, …}); decode_members::<T> and, for an inbound, hot_users. Their schema is adjusted by schema::grouped and schema::kinded.

Other blocks ​

Besides the protocol block an entity may carry tls, transport, mux, dial (outbound only) and reload. Each is accepted only if the type declares it in Provide::Blocks; reload is always accepted. In detail — blocks.

outbounds.defaults ​

A reserved name in outbounds. merge(own, defaults, top) before decoding:

  • at the entity's top level, dial from defaults is added to everyone, other blocks — only to those outbounds that already have that block;
  • inside blocks the merge is field by field, deep into maps; one's own value wins.

defaults also applies to subscription servers (Context::defaults); not to groups (defaults in groups is an error). An entity is recorded in Context::written already merged — the modules' checks see the result.

Groups ​

A group is written in groups without a protocol block; the loader:

  1. resolves members (names and match: <pattern>) into a list of names — see names and references;
  2. checks rank: {manual: {default: x}} (chosen_by_default): a subscription server that is not among the members right now is replaced with the first member, with a warning;
  3. for a group with members from subscriptions it appends a hidden allow_empty: true (an allow_empty written by the user is an error);
  4. assembles the protocol block group: {members: [...], ...} and hands it to the module of the outbound type group (Provide::Outbound { key: "group" }, the group module); the only allowed block is reload.

group: or block: as a type in outbounds is an error with a hint (groups, to: block).

Rule-sets ​

lower_rule_sets: the source is exactly one of the keys remote, file, inline; the loader appends tag and hands the node to Decoders::decode_rule_set(source, ...) (Provide::RuleSet { key, decode }, the router module). In Config::rule_sets the entity gets Blocks::of(kind, decoded), where kind is remote, local (for file) or inline. A rule-set is an entity without its own factory: they are built by Provide::RuleSets.

Entities not added by the user ​

  • synthetic(...) — a protocol block without a written node: the block outbound for final: block. The kernel makes the default direct the same way (Instance), calling Decoders::decode("outbounds", "direct", ...).
  • Source entities (Provide::Source): the dns.listen inbounds (dns), subscription servers and groups (subscriptions). They go through the same inbound/outbound/ group as the written ones.

A check without consequences ​

entity::accepts (through Scope::accepts) tries to lower an entity and rolls back natives/written: this way a source throws out in advance a server that will not load, instead of bringing down the whole configuration.

GPL-3.0-or-later