English
Inbound and outbound
Provide::Inbound, Provide::Outbound, Provide::Blocks — crates/api/src/module.rs; the traits — crates/api/src/adapter.rs.
Entity type
rust
Provide::Inbound { key: &'static str, decode: BlockDecoder, build: InboundFactory }
Provide::Outbound { key: &'static str, decode: BlockDecoder, build: OutboundFactory }key is the entity type and the key of its protocol block in YAML (vless: inside an inbound). The loader decodes the protocol block through decode (fn(&Node) -> Result<Decoded>); the entity's other blocks (tls, transport, mux, dial) — through the decoders of their modules (Points and extensions). All of it together is the entity's Blocks; Blocks::kind() is the key of the protocol block.
Typical decoders are in ours_config (crates/config/src/blocks.rs): listener::<T>, listener_with_users::<T> and dialer::<T>; what each does is in Add a protocol.
Factories:
rust
type InboundFactory = fn(tag: &str, blocks: &Blocks, cx: &Context<'_>) -> Result<Arc<dyn Inbound>, String>;
type OutboundFactory = fn(tag: &str, blocks: &Blocks, cx: &Context<'_>) -> Result<Arc<dyn Outbound>, String>;A factory gets its own part through blocks.protocol::<T>() and blocks.get::<T>("tls"), services — through cx.services (Runtime types). It opens nothing: sockets are opened in Inbound::start.
Which blocks a type takes
rust
Provide::Blocks { section: "inbounds" | "outbounds", key, blocks: &["tls", "transport", "dial", ...] }The list of blocks a type accepts besides the protocol one. A type that declared nothing takes no module blocks: the loader answers "kind takes no tls block". The reload block belongs to the kernel and is allowed for all. The list may depend on features (vless takes mux only with the mux feature).
The Inbound trait
| Method | When | What |
|---|---|---|
start(dispatcher, shard, sockets) | on every shard, inside its runtime | open the listeners (or take the handed-over ones, if !shard.owned), put copies into Handover, accept and hand to the dispatcher; an error leaves nothing open on the shard |
socket_identity() | reload | what the listening sockets are bound with (SocketIdentity); the kernel hands the sockets to a successor that reports the same; None by default |
close() | closing/replacement | stop accepting on all shards; the future completes when the sockets are closed; accepted connections keep living |
attach(cx, dial_table) | right after the build | for inbounds that dial themselves |
validate_hot(blocks) / hot_changed(blocks) | reload without a rebuild | check and apply "hot" changes (users and so on) — only for new connections |
inject(dispatcher, stream, source) | another inbound named this one as its detour | accept the stream as if from client source; Unsupported by default |
inject_packet(dispatcher, conn, source) | the same, for a packet flow | accept the datagrams of conn as if client source sent them to the address each carries, replying through conn; Unsupported by default |
Socket hand-over — Runtime and shards; hot changes — Reload.
The Outbound trait
Mandatory: kind(), tag(), connect_stream(&Metadata), connect_packet(&Metadata). The rest have a default implementation:
supports(network),transparent()(direct without its own settings),multiplex();as_group()returns theGroupinterface for member selection, probes, freezing a route for one connection, and carrying a manual choice across reload;connection_member()identifies the member of an already resolved route for tracking;attach_services(&Services),start(), andclose()manage the lifecycle. An outbound retained by reload keeps running;host_cell()exposes the host settings cell shared between the dialer and kernel.
During construction the dialer obtains the DialTable from services. A successful reload publishes the table and updates retained outbounds' HostCell. Previews and failed reloads leave those values intact. See Instance.
Endpoint
rust
Provide::Endpoint { key: &'static str, decode: BlockDecoder, build: EndpointFactory }
type EndpointFactory = fn(tag: &str, blocks: &Blocks, cx: &Context<'_>) -> Result<Endpoint, String>;
pub struct Endpoint { pub inbound: Arc<dyn Inbound>, pub outbound: Arc<dyn Outbound> }The sing-box endpoints section: one entity that is both an outbound and an inbound. The loader puts it into Config::endpoints; the name goes into the outbound namespace (to: {outbound: wg}, detour) and into the inbound names (the inbound condition). The entity's blocks are those of an outbound: dial, if the type declared it (Provide::Blocks { section: "endpoints", ... }), and reload.
The kernel builds an endpoint once and installs both sides: the outbound — into the outbound table under the endpoint's name (a name matching an outbound is an error), the inbound — into the inbound list (Inbound::start on every shard). In the component graph an endpoint is two components, outbound[tag] and inbound[tag], with the same blocks: reload keeps or rebuilds both sides together. The only implementation is wireguard.
The "not built in" error
unavailable(kind, type_name, implemented) produces "kind type x is not built in (feature x)" or "is not implemented yet"; Modules::inbound/outbound adds "available: ...".