English
Blocks and variants
Code: crates/native-config/src/entity.rs (taken, variant_block, policy), crates/api/src/config.rs (Decoders::decode, decode_side, takes), crates/kernel/src/modules.rs (Modules::new, Modules::decoders).
A block is a part of an entity other than the protocol: tls, dial, mux, transport, reload. A block belongs not to the entity's type but to the module that opens the point of the same name; the type only declares which blocks it takes.
Three declarations
rust
// the tls module opens the point and decodes both sides
Provide::Point("tls"),
Provide::Block { block: "tls", key: "inbound", decode: block::decode_inbound },
Provide::Block { block: "tls", key: "outbound", decode: block::decode_outbound },
// the ws module adds a variant to someone else's point
Provide::Block { block: "transport", key: "ws", decode: ... },
// the vless type says which blocks it takes
Provide::Blocks { section: "outbounds", key: "vless", blocks: &["tls", "transport", "mux", "dial"] },Modules::new checks: someone opens the point block (Provide::Point), and the module adding a variant requires the opening one (requires). The pair (block, key) is unique in the build (Provide::slot). Modules::decoders puts all Provide::Block, Provide::Inbound and Provide::Outbound into one table (block, key, BlockDecoder): the protocol block is an entry with block = "inbounds"/"outbounds".
A block without variants and a block with variants
- Side (
decode_side(block, side, ...)): the entry's key isinboundoroutbound. This is howtls,dial(onlyoutbound),muxon an inbound andreloadare decoded. An empty block (tls:without a value) is decoded as an empty map. - Variant (
variant_block): the block is a map with one key, and the key selects the variant:transport: {ws: {...}},mux: {smux: {...}}. Atypefield inside the variant is forbidden (the kind is the key, no `type`). The canonical form is wrapped in{<variant>: ...}so that changing the variant changes the fingerprint.
Who opens and who fills the points:
| Point | Opened by | Variants / sides |
|---|---|---|
tls | tls | inbound, outbound |
dial | dial | outbound |
mux | mux | inbound; h2mux, smux, yamux |
transport | transport | ws, grpc, http, httpupgrade, xhttp — the transport-* modules |
reload | the kernel (own, not a point) | inbound, outbound |
reload is not a point: the kernel registers it through Decoders::and_block, bypassing Modules::new, and the loader accepts it for any type without asking takes.
Checks before decoding
taken(cx, section, name, kind, block, variants):
Decoders::takes(section, kind, block); a type that did not declareProvide::Blockstakes nothing; a type unknown to the build is skipped (it will fail as unknown by itself);- the variant or side is looked up in the table; if absent —
unknown transport `quic`; available: ...or... is not built in (feature `transport-ws`).
A block missing from the build and a type refusing it are different errors: the first is about the build, the second about the configuration.
What a block's module gets and gives
A block decoder is fn(&Node) -> Result<Decoded>; it does not need a Scope: a block does not check names. The exception is dial.detour: the loader checks it in Names itself before calling the module (except for subscription servers: their detour is written by the subscriptions module from via and the links between servers, and it throws out servers whose target has gone itself — drop_dependents).
The block's value is later taken by the entity's factory: blocks.get::<T>("tls"), and the protocol block — blocks.protocol::<T>(). The type T is known only to the owning module; the point's module gets the variants of other modules through Extensions/Scope::extension (see the contract).
A block given by a section
An entity may get a block not from its own text: the DNS module gives outbounds domain_resolver through Decoded::gives, and the loader inserts it with Context::add_block (dns). For the factory such a block is no different from a written one.
Adding a block
For entities to get a new block, you need: a module with Provide::Point and Provide::Block; the block's name in ENTITY_BLOCKS (an entity block) or PROTOCOL_BLOCKS (a block that not every protocol takes — written inside the protocol block) and a branch in the loader's inbound/outbound (the list of blocks is closed in the same place as the top level); Provide::Blocks on the types that take it; a schema part. A new variant of an existing point requires only a module.