English
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 outboundsplit_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), amultiplexkey inside the protocol is the error "belongs in themuxblock" (refuse); sing-box fields replaced by route actions (sniff*,domain_strategy,udp_disable_domain_unmapping,proxy_protocol*) are the error "xis not an inbound field: …" with a hint (LISTEN_REMOVED);listener_with_users::<T>(node)— the same, and in addition it movesusersout 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_tcpis enabled by presence (enable: writes inenabled: true, an explicitenabledis an error), theDialerOptionsfields are the error "belongs in thedialblock".
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,
dialfromdefaultsis 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:
- resolves
members(names andmatch: <pattern>) into a list of names — see names and references; - 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; - for a group with members from subscriptions it appends a hidden
allow_empty: true(anallow_emptywritten by the user is an error); - assembles the protocol block
group: {members: [...], ...}and hands it to the module of the outbound typegroup(Provide::Outbound { key: "group" }, the group module); the only allowed block isreload.
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: theblockoutbound forfinal: block. The kernel makes the defaultdirectthe same way (Instance), callingDecoders::decode("outbounds", "direct", ...).- Source entities (
Provide::Source): thedns.listeninbounds (dns), subscription servers and groups (subscriptions). They go through the sameinbound/outbound/groupas 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.