Skip to content

Points and extensions ​

Provide::Point, Provide::Extension, Provide::Block; Extensions — crates/api/src/module.rs.

A point is a place to which other modules add variants, while the owning module defines their type and meaning. This way modules extend each other without the owner knowing about the variants.

rust
Provide::Point("transport")                                     // open a point
Provide::Block { block: "transport", key: "ws", decode }        // a variant of an entity block
Provide::Extension { point: "conditions", key: "dns", value: &X }  // a value variant

Rules (Modules::new): a point has exactly one owner (the slot ("points", name)); only an open point can be extended; someone else's point can be extended only with its owner in requires; the pair (point, key) is unique in a build.

Block — variants of entity blocks ​

An entity (inbound/outbound) block decoded by the owner module of the point with the same name:

Point (owner)VariantsHow it is written
transport (transport)ws, http, httpupgrade, grpc, xhttp — the transport-* modulestransport: { ws: {...} } — the variant key
mux (mux)h2mux, smux, yamux, xray (feature cool), inboundon an outbound — mux: { smux: {...} }; on an inbound — mux: {...} without variants (key inbound)
tls (tls)inbound, outboundtls: {...} — without variants, the key is the side
dial (dial)outbounddial: {...}

A block with variants (transport, mux on an outbound) is decoded by the variant key; a block without variants (tls, dial, mux on an inbound) — by the entity's side (Decoders::decode_side). An inbound has no dial block. The decision is made by the loader — Blocks and variants.

A variant's Decoded value is of the type the owner expects. For example, the transport module provides the helper ours_transport_api::variant::decode(node, &KIND): variants put into the block a Configured with closures that build the client and server sides, and the protocol takes it through variant::client(blocks, setup, caps) (crates/transport-api/src/variant.rs), without knowing which transport is built in.

The key of a Block is both the key in the slot and the variant in messages like "transport ws is not built in".

Extension — values for the owner ​

value: &'static (dyn Any + Sync) — a static value of a type defined by the owner. The consumer gets it with a type check:

  • at run time — Extensions::get::<T>(point, key), Extensions::keys(point); Extensions is assembled from all modules of the build and published as a service (Arc<Extensions>), and is also available from Modules::extensions();
  • during loading — Scope::extension(point, key) and Decoders::extension(s).

Provide::Condition(Entry) is a shorthand for Extension { point: "conditions", key: entry.key, value: &entry } (Rules and conditions). Currently Extension in explicit form is used by router for the dns condition in its own conditions point, by transport for quic, by mux for smux-server (trojan-go's smux server, ours_api::mux::Sessions) and by adblock for skip_adblock in the dns_filter point of dns.

A Block is a variant of its point too: Extensions::get::<BlockDecoder>(block, key) gives its decoder, so that a module builds the variant from options of its own (shadowsocks v2ray-plugin decodes a transport.ws block it makes of plugin_opts).

The build's points ​

PointOwnerExtended by
transporttransporttransport itself (quic, feature quic), transport-ws, transport-http, transport-httpupgrade, transport-grpc, transport-xhttp
tlstlstls itself (inbound, outbound)
muxmuxmux itself (and smux-server)
dialdialdial itself (outbound)
dns_filterdnsadblock (skip_adblock)
conditionsrouterrouter (dns), rule-process (process_name, process_path, process_path_regex, package_name, package_name_regex, user, user_id)

GPL-3.0-or-later