Skip to content

The spec: desired state

Source files: 66 · checked against Etemenanki 46c12e4 · katana v6.1.0 · shinku-quic v0.1.0
  • Etemenanki/supervisor/src/topology/spec_plan/mod.rs
  • Etemenanki/supervisor/src/topology/spec_plan/inbound.rs
  • Etemenanki/supervisor/src/topology/spec_plan/outbound.rs
  • Etemenanki/supervisor/src/topology/spec_plan/transport.rs
  • Etemenanki/supervisor/src/topology/spec_plan/route.rs
  • Etemenanki/supervisor/src/topology/spec_plan/dns.rs
  • Etemenanki/supervisor/src/topology/spec_plan/plan.rs
  • Etemenanki/supervisor/src/topology/inbound/mod.rs
  • Etemenanki/supervisor/src/topology/balancer.rs
  • Etemenanki/supervisor/src/entity/user.rs
  • Etemenanki/supervisor/src/entity/id.rs
  • Etemenanki/supervisor/src/policy.rs
  • Etemenanki/supervisor/src/guard.rs
  • Etemenanki/supervisor/src/carrier.rs
  • Etemenanki/supervisor/src/build/validate.rs
  • Etemenanki/supervisor/src/build/apply.rs
  • Etemenanki/supervisor/src/build/users.rs
  • Etemenanki/supervisor/src/build/inbound.rs
  • Etemenanki/supervisor/src/build/outbound.rs
  • Etemenanki/supervisor/src/build/dns.rs
  • Etemenanki/supervisor/src/build/route.rs
  • Etemenanki/supervisor/src/supervisor.rs
  • Etemenanki/supervisor/src/system/listener.rs
  • Etemenanki/concepts/src/net.rs
  • Etemenanki/environment/src/routing.rs
  • Etemenanki/protocols/src/helpers/address_family.rs
  • Etemenanki/protocols/src/transports/tls/config.rs
  • Etemenanki/protocols/src/transports/ws/endpoint.rs
  • Etemenanki/protocols/src/tun/device.rs
  • Etemenanki/protocols/src/tun/config.rs
  • Etemenanki/protocols/src/dns/mod.rs
  • Etemenanki/protocols/src/hysteria/server/masquerade.rs
  • Etemenanki/protocols/src/hysteria/server/endpoint.rs
  • Etemenanki/protocols/src/hysteria/server/config.rs
  • Etemenanki/protocols/src/hysteria/config.rs
  • Etemenanki/protocols/src/wireguard/config.rs
  • Etemenanki/protocols/src/ss_2022/crypto.rs
  • Etemenanki/protocols/src/ss_2022/users.rs
  • Etemenanki/protocols/src/vless/addons.rs
  • Etemenanki/protocols/src/anytls/padding.rs
  • Etemenanki/protocols/src/anytls/client/mod.rs
  • Etemenanki/protocols/src/sss/server.rs
  • Etemenanki/protocols/src/sss/client.rs
  • Etemenanki/protocols/src/sss/transform/mod.rs
  • Etemenanki/protocols/src/sss/transform/stream.rs
  • Etemenanki/protocols/src/transports/reality.rs
  • Etemenanki/app/src/lower.rs
  • Etemenanki/app/src/config.rs
  • Etemenanki/app/src/transport.rs
  • Etemenanki/ffi/src/client.rs
  • Etemenanki/app/tests/unit/lower.rs
  • Etemenanki/supervisor/tests/unit/validate.rs
  • Etemenanki/supervisor/tests/unit/plan.rs
  • Etemenanki/supervisor/tests/hot_swap.rs
  • Etemenanki/supervisor/tests/handoff.rs
  • Etemenanki/environment/tests/unit/routing.rs
  • Etemenanki/ffi/tests/proxy.rs
  • Etemenanki/supervisor/tests/anytls.rs
  • Etemenanki/supervisor/tests/sss.rs
  • katana/src/lower/mod.rs
  • katana/src/lower/inbound.rs
  • katana/src/api/helium.rs
  • katana/src/api/hysteria.rs
  • katana/src/api/newv2board.rs
  • katana/src/manager/node.rs
  • shinku-quic/shinku-quic/src/hop.rs

A Spec<U> is everything a supervisor is asked to run, written as typed data: the listeners and the protocols served on them, the upstreams, the balancers, the route table, name resolution, the user sets that inbounds admit, and the policies for live connections and for clients that fail to authenticate. A front end (etemenanki-app’s TOML loader, katana’s panel lowering, the FFI client) lowers its own format into a spec and hands it to Supervisor::start, apply or update. The supervisor validates it, plans a reconcile against the spec it already runs, and builds only what changed.

This page documents every type under supervisor/src/topology/spec_plan/, and the types they are made of from topology/inbound/mod.rs, entity/user.rs and policy.rs: what each field means, what the builders do with it, which rules apply to it, and how two specs are compared. It is for authors of a front end, and for contributors who add a field, a protocol or a transport. The rules and their full error texts are on Validation. What a difference between two specs leads to (versions, swaps, drains) is on Reconcile.

Paths without a crate directory, such as build/validate.rs or supervisor.rs, are under supervisor/src/. Paths in other crates are written from the repository root, such as app/src/lower.rs.

A spec:

  • names every resource by a tag, and every reference between resources is a tag: a route names an outbound or a balancer, a balancer names outbounds, an inbound names a user set;
  • carries every value typed. It holds a Destination rather than host text, a Uuid, a method enum, decoded key bytes, PEM bytes and a Bandwidth rate, never the string they were parsed from. So two specs compare with ==, and plan can tell a resource to keep from one to rebuild without asking where either spec came from;
  • holds its secret values in Secret<T> (the fields are listed under Secret<T> below), which compares by value and prints as Secret(..);
  • is plain data with public fields. Its only constructors are Secret::new and From<T> for Secret<T>, and its only fallible helpers are Strategy::parse and DomainRegexSet::new, which a front end calls while it lowers. A spec that breaks a rule can be built; applying it refuses it whole.

A spec does not:

  • read files. The front end reads certificates, keys and CA bundles and passes their bytes. When the supervisor builds from a spec, two fields lead it to read a file: RouteSpec.geoip and geosite are paths that build/route.rs → compile_routes reads, and DnsSpec::Split { use_hosts: true } has build/dns.rs → build_dns read the system hosts file.
  • check its own rules. The types carry what they can: a WireGuard key is a [u8; 32], a speed limit a NonZeroU64, a device limit a NonZeroU32. The rules about how a spec’s values fit together (which fields go together, which tags exist, which values are in range) live in build/validate.rs → validate, which is pure: no sockets, no files, no clock. A front end checks what exists only in its own format, such as a key that is set while the feature it belongs to is off.
  • hold runtime state. The only resource a spec can hold is a supplied TUN descriptor (SuppliedTun, an Arc<OwnedFd>), which exists only on Unix.
  • know how it was written. Nothing in it records a file name, a line number or a panel field.
Front end Lowering U What it sets
etemenanki-app app/src/lower.rs → lower, after the subscribe file is merged lower::UserKey, the app’s type alias of UserName One user set per inbound that takes users, tagged with the inbound’s own tag. Every user’s speed_limit and device_limit are None, every inbound’s proxy_protocol is false, and its real_ip and handoff are None: the app has no config key for any of them. Policies::default() and no per-inbound or per-outbound override. Hysteria2UserAuth::Account. Every outbound’s WebSocket host, gRPC authority and SNI filled in. VMess clock_check, the Hysteria 2 bandwidth, congestion, quic and hop settings and TUN ipv6 come from the config, with the defaults given under each type below.
katana src/lower/mod.rs → node_spec, for each node Uid, the panel’s numeric user id, named UserName::Username with the id in decimal One user set tagged users, with each user’s credentials derived from their UUID, and their device_limit from the panel while [node.controller] enable_device_limit is on. Policies with ip_ban and carrier_max_failures from [node.controller] (src/lower/mod.rs → policies). proxy_protocol on its TCP binds. real_ip from [node.controller] real_ip_header and real_ip_from on a node whose stream transport is WebSocket, and None on every other node. handoff from [node.controller] handoff_dir and handoff_group on a TCP bind: <handoff_dir>/<port>.sock, or <handoff_dir>/<listen_ip>-<port>.sock when listen_ip is one address rather than all, mode 0o660, and the group’s gid, read from /etc/group; None without handoff_dir and on a Hysteria 2 node. Hysteria2UserAuth::Password, unless [node.hysteria] credential = "user_pass", a key a helium node cannot set.
etemenanki-ffi ffi/src/client.rs → lowering: the app’s lower, then refuse_servers, then supply_tun the app’s lower::UserKey The app’s spec, with every server inbound refused and the TUN inbound’s bind replaced by TunSource::Fd over the descriptor the platform supplies.

The FFI client runs as a client on a phone, so refuse_servers refuses any inbound that serves a proxy protocol to other hosts: Trojan, VLESS, VMess, Shadowsocks, Shadowsocks 2022, Hysteria 2, AnyTLS, sss, and SOCKS or HTTP bound anywhere but a Unix socket, localhost or a loopback address. InboundProtocolSpec is #[non_exhaustive], so its match ends in a wildcard arm, which refuses a protocol added later as a server protocol until it is listed. The refusal reads inbound "<tag>" serves <protocol>, a server protocol; a client runs only a tun inbound and local socks or http inbounds, where <protocol> is, for example, trojan or socks beyond this device. supply_tun then serves the platform’s descriptor:

  • A config with one TUN inbound keeps it, with its MTU, UDP relay, flow limit and sniffing. The platform owns the interface, so a configured name, addresses or routes are ignored with a WARN: inbound <tag>: the platform owns the tun interface, so its name, addresses and routes in the config are ignored.
  • A config with none gets one tagged tun (TUN_TAG), with sniffing on, udp on and the defaults of protocols/src/tun/config.rs.
  • A config with more than one is refused: the config has more than one tun inbound; the platform supplies one device.

How etemenanki-app lowers its TOML is on etemenanki-app: from TOML to a spec. How katana lowers a node is on Lowering a node into a spec. How the FFI client rewrites the app’s spec is on Mobile library (etemenanki-ffi).

supervisor/src/topology/spec_plan/mod.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Spec<U: UserId> {
pub inbounds: Vec<InboundSpec>,
pub outbounds: Vec<OutboundSpec>,
pub balancers: Vec<BalancerSpec>,
pub route: RouteSpec,
pub dns: DnsSpec,
/// The users inbounds admit, as their own resource: a user set can be
/// swapped without reconciling anything else.
pub user_sets: Vec<UserSet<U>>,
/// The supervisor-wide defaults, which an inbound or outbound may
/// override.
pub policies: Policies,
}

U is the key the front end already uses for its users (see UserId and UserName). It appears only in user_sets; everything else is the same for every front end. Spec has no Default, and neither have RouteSpec and DnsSpec, so a front end always names the default route target and a DNS backend itself.

Field Identity inside the spec Built by Page
inbounds tag unique among inbounds; bind unique among inbounds build/inbound.rs → build_handler, system/listener.rs → bind Serving
outbounds tag unique among outbounds and balancers build/outbound.rs → build_outbound Outbounds
balancers tag unique among outbounds and balancers topology/balancer.rs → Balancer::new Outbounds
route one per spec build/route.rs → compile_routes Routing plane
dns one per spec build/dns.rs → build_dns DNS
user_sets tag unique among user sets build/users.rs → admit, build/inbound.rs → user_table Users and sessions
policies one per spec read when a drain or a removal is resolved; ip_ban and carrier_max_failures handed to the guard (guard.rs → Guard::set_policy) at every commit Policies

The public paths are etemenanki_supervisor::topology::spec_plan for Spec, Secret, ObfsSpec, everything re-exported from its private submodules inbound, outbound, transport, route and dns, and the protocols crate’s types that VLESS and sss specs hold: VlessFlow, and sss’s ServerSettings, ClientSettings, Chain, ParseError and Reject, re-exported as SssServerSettings, SssClientSettings, SssChain, SssChainError and SssReject; etemenanki_supervisor::topology::inbound for BindSpec, HandoffSpec, TunSource and, on Unix only, SuppliedTun; etemenanki_supervisor::entity::user for the user types; etemenanki_supervisor::policy for the policies and IpBanPolicy; and etemenanki_supervisor::topology::balancer::Strategy. The planner lives beside the spec, in the public submodule spec_plan::plan.

Types from other crates that a spec holds:

Type Path Held by
Destination, DialNetwork, Remote etemenanki_concepts::net ProxyUpstream.server, Hysteria2OutboundSpec.server, WireguardSpec.endpoint, RealityServerSpec.dest
AddressFamilyStrategy etemenanki_protocols::helpers::address_family Freedom, OutboundTransportSpec, Hysteria2OutboundSpec, WireguardSpec (twice)
Method (imported as SsMethod) etemenanki_protocols::ss_legacy Shadowsocks inbound and outbound
Method (imported as Ss2022Method) etemenanki_protocols::ss_2022 Shadowsocks 2022 inbound and outbound
VerifyMode etemenanki_protocols::transports::tls ClientTlsSpec, Hysteria2OutboundSpec
Security etemenanki_protocols::vmess VMess outbound
VlessFlow etemenanki_protocols::vless (defined in vless/addons.rs), re-exported from spec_plan VLESS inbound and outbound
ServerSettings, ClientSettings (re-exported as SssServerSettings, SssClientSettings) etemenanki_protocols::sss sss inbound and outbound
BandwidthConfig, QuicOptions etemenanki_protocols::hysteria::config Hysteria2InboundSpec, Hysteria2OutboundSpec
Fallback etemenanki_protocols::hysteria::config, re-exported from shinku_quic::congestion Hysteria2InboundSpec, Hysteria2OutboundSpec
PortHopping etemenanki_protocols::hysteria::config Hysteria2OutboundSpec
Bandwidth, HopInterval etemenanki_protocols::hysteria::config, re-exported from shinku_quic inside BandwidthConfig and PortHopping
DeviceSpec etemenanki_protocols::tun TunSource::Create
Backend etemenanki_protocols::dns DnsSpec
RouteMatch etemenanki_environment::routing RouteRuleSpec
Uuid uuid Credentials.uuid, VLESS, VMess and sss outbounds
CompactString compact_str every tag, Account.user, UserName

Tags name resources, and they are how resources refer to each other:

flowchart LR
  inbound["InboundSpec"]
  set["UserSet"]
  rule["RouteRuleSpec"]
  dflt["RouteSpec default"]
  balancer["BalancerSpec"]
  outbound["OutboundSpec"]
  inbound -->|"users: set tag"| set
  rule -->|"outbound: tag"| outbound
  rule -->|"outbound: tag"| balancer
  dflt -->|"tag"| outbound
  dflt -->|"tag"| balancer
  balancer -->|"members: outbound tags"| outbound
  • Three namespaces. Inbound tags, user-set tags, and outbound plus balancer tags. A route names an outbound and a balancer the same way, so those two kinds share one namespace, and a balancer whose tag an outbound already has is refused as duplicate balancer tag <tag>. An inbound or a user set may carry an outbound’s tag. etemenanki-app relies on the separation: it tags each inbound’s user set with the inbound’s own tag.
  • No empty tag. The empty tag is the supervisor’s own. It names the target through which its DNS service is reached (supervisor.rs → internal_id) and the blackhole of the empty plane a supervisor starts with, so no spec tag can collide with them. An empty tag is refused as ApplyError::Invalid.
  • Every reference resolves. A route rule or the route default must name an outbound or a balancer (route references unknown outbound missing). A balancer member must name an outbound, never another balancer (balancer outer references unknown outbound inner). An inbound’s users must name a user set (inbound socks references unknown user set missing).

The error texts above are ApplyError Display forms. Every rule, in the order validate checks it, is on Validation.

supervisor/src/topology/spec_plan/mod.rs
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Secret<T>(T);
impl<T> Secret<T> {
pub fn new(value: T) -> Self;
/// The secret itself, for the code that has to put it on the wire.
pub fn expose(&self) -> &T;
}
impl<T> From<T> for Secret<T>;
impl<T> fmt::Debug for Secret<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Secret(..)")
}
}
  • Clone, PartialEq, Eq and Hash are derived, so a secret compares and hashes as its contents. A changed password is a changed spec, and the resource holding it is rebuilt.
  • Debug is written by hand and prints Secret(..): neither the value nor its length. Secret has no Display.
  • The inner value is private, and expose() is the only way to read it. In the supervisor, validate calls it to check lengths, emptiness and collisions, and the builders call it where the value goes into a protocol’s own configuration.

Where Secret appears in a spec:

Field T
InboundProtocolSpec::Shadowsocks.password, OutboundProtocolSpec::Trojan.password, OutboundProtocolSpec::Shadowsocks.password String
InboundProtocolSpec::Ss2022.psk, OutboundProtocolSpec::Ss2022.psk and each of its identity_psks Vec<u8>
Hysteria2InboundSpec.shared_password, Hysteria2OutboundSpec.password String
AnytlsOutboundSpec.password String
ServerTlsSpec.key_pem Vec<u8>
ObfsSpec::Salamander.psk Vec<u8>
WireguardSpec.private_key, WireguardSpec.preshared_key [u8; 32]
RealityServerSpec.private_key [u8; 32]
Account.pass String
Credentials.password, Credential::Password String
Credentials.ss2022_psk, Credential::Ss2022Psk Vec<u8>

A Trojan listener over WebSocket and TLS, and a SOCKS listener on a Unix socket (served only on Unix), both admitting one user set, with everything sent direct except one blocked domain:

Illustrative
use std::collections::BTreeMap;
use std::time::Duration;
use etemenanki_environment::routing::RouteMatch;
use etemenanki_protocols::dns::Backend;
use etemenanki_protocols::helpers::address_family::AddressFamilyStrategy;
use etemenanki_supervisor::entity::user::{Account, Credentials, UserName, UserSet, UserSpec};
use etemenanki_supervisor::policy::{Policies, UserRemovalPolicy};
use etemenanki_supervisor::topology::inbound::BindSpec;
use etemenanki_supervisor::topology::spec_plan::*;
use etemenanki_supervisor::Supervisor;
// The front end reads the files; the spec carries bytes.
let cert_pem = std::fs::read("/etc/etemenanki/cert.pem")?;
let key_pem = Secret::new(std::fs::read("/etc/etemenanki/key.pem")?);
// One user with two credentials: Trojan admits the password, SOCKS the account.
let alice = UserSpec {
credentials: Credentials {
password: Some(Secret::new("replace-with-a-long-random-password".into())),
account: Some(Account {
user: "alice".into(),
pass: Secret::new("replace-with-a-long-random-password".into()),
}),
..Credentials::default()
},
speed_limit: None,
device_limit: None,
};
let spec: Spec<UserName> = Spec {
inbounds: vec![
InboundSpec {
tag: "trojan-in".into(),
bind: BindSpec::Tcp { host: "0.0.0.0".into(), port: 443 },
handoff: None,
sniff: true,
protocol: InboundProtocolSpec::Trojan {
transport: InboundTransportSpec {
shape: StreamShape::Ws { path: "/t".into(), host: None, tls: true },
tls: Some(ServerTlsSpec { cert_pem, key_pem }),
reality: None,
},
},
users: Some("people".into()),
user_removal: None,
proxy_protocol: false,
real_ip: None,
},
InboundSpec {
tag: "socks-in".into(),
bind: BindSpec::Unix("/run/etemenanki/socks.sock".into()),
handoff: None,
sniff: false,
protocol: InboundProtocolSpec::Socks {
udp: false,
udp_bind: None,
// A Unix listener carries no transport: plain TCP.
transport: InboundTransportSpec { shape: StreamShape::Tcp, tls: None, reality: None },
},
users: Some("people".into()),
user_removal: Some(UserRemovalPolicy::CloseAfter(Duration::from_secs(30))),
proxy_protocol: false,
real_ip: None,
},
],
outbounds: vec![
OutboundSpec {
tag: "direct".into(),
protocol: OutboundProtocolSpec::Freedom { address_family: AddressFamilyStrategy::Auto },
drain: None,
},
OutboundSpec { tag: "block".into(), protocol: OutboundProtocolSpec::Blackhole, drain: None },
],
balancers: Vec::new(),
route: RouteSpec {
rules: vec![RouteRuleSpec {
matchers: vec![RouteMatch::DomainSuffix("ads.example.com".into())],
outbound: "block".into(),
}],
default: "direct".into(),
geoip: None,
geosite: None,
},
dns: DnsSpec::Single(Backend::System),
user_sets: vec![UserSet {
tag: "people".into(),
users: BTreeMap::from([(UserName::Email("alice@example.com".into()), alice)]),
}],
policies: Policies::default(),
};
let (supervisor, _report) = Supervisor::start(spec).await?;
// Later: an edit of the running spec, validated and planned like any apply.
supervisor.update(|spec| spec.route.default = "block".into()).await?;
flowchart LR
  fe["front end: TOML, panel API, FFI"]
  spec["Spec"]
  validate["validate"]
  plan["plan against RunningState"]
  stage["stage users and handlers"]
  prepare["prepare: DNS, outbounds, routes, binds"]
  commit["commit"]
  running["RunningState.spec"]
  users["set_users, upsert_user, remove_user"]
  fe -->|"lower"| spec
  spec --> validate --> plan --> stage --> prepare --> commit --> running
  running -->|"compared with =="| plan
  running -->|"update: clone, then edit"| spec
  users -->|"edit one set, store the edited spec"| running
  1. Lowering. The front end parses its format, reads files, fills defaults and gaps, and builds a Spec. Everything that can fail on syntax fails here, as the front end’s own error.
  2. Hand-over. SupervisorBuilder::start and Supervisor::start take the first spec, apply and apply_with take a whole new one, and update and update_with take a closure FnOnce(&mut Spec<U>). The actor clones the running spec and runs the closure on the clone on the blocking pool (Actor::edited), and applies the result like any other spec. check takes &Spec<U> and builds everything without binding.
  3. Validation. plan(running, desired) calls validate(desired) first. A spec that breaks a rule is refused whole, and nothing is planned (an_invalid_spec_is_refused_without_a_plan).
  4. Planning. plan compares the desired spec with RunningState.spec, resource by resource (see Equality semantics), and emits Build, Reuse and the other steps. Validation and planning run on the blocking pool, inside Actor::stage. The actor refuses a plan with a Disrupt step unless the apply allows disruption.
  5. Staging. Still on the blocking pool, stage_users admits each inbound’s users and builds what the users decide: build_handler for each inbound the plan builds, which parses the certificate and key and builds the inbound’s user table with user_table, or a new user table for a running inbound whose admitted users changed. It also works out the user limits (user_limits).
  6. Prepare and commit. Prepare builds the rest of what the plan says from the spec: build_dns, build_outbound and compile_routes (which reads the geo files). It reports a staged handler or table that failed to build, in inbound order, and binds the listeners of new binds. Commit makes it live and stores the desired spec as RunningState.spec.
  7. User edits. set_users, upsert_user and remove_user change one set in a clone of the running spec, staged on the blocking pool (stage_user_edit). For each inbound that names the set, it runs validate_admission, not the whole validate, and rebuilds that inbound’s user table. Only when every table is built does the actor store them and the edited clone as the running spec. A remove_user for a user the set does not hold returns false and changes nothing. A later update starts from the edited set, and a later apply compares its sets with the edited ones.

What each phase does in full is on Reconcile.

supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InboundSpec {
pub tag: CompactString,
pub bind: BindSpec,
pub handoff: Option<HandoffSpec>,
pub sniff: bool,
pub protocol: InboundProtocolSpec,
pub users: Option<CompactString>,
pub user_removal: Option<UserRemovalPolicy>,
pub proxy_protocol: bool,
pub real_ip: Option<RealIpSpec>,
}
impl InboundSpec {
pub fn binds(&self) -> impl Iterator<Item = BindSpec> + '_;
}
Field Meaning
tag Names the inbound. Routes match it with RouteMatch::InboundTag, and sessions belong to it.
bind What is bound: the listener’s identity across applies. An inbound whose bind is unchanged keeps its socket or device, and the handler behind it is swapped.
handoff Also serve the TCP connections a relay running on the same host hands over on this Unix socket, as if bind had accepted them. See HandoffSpec. Only a stream protocol on a Tcp bind may set it: only a stream inbound on a tcp listener takes a handoff socket. None binds no second listener.
sniff Whether to read a domain out of a flow addressed by IP. The builder passes it to every kind of handler: the stream inbound, the SOCKS inbound, the Hysteria 2 connection config, and the TUN inbound (without_sniffing when it is false). See Sniffing.
protocol The protocol served, with its settings. See InboundProtocolSpec.
users The tag of the UserSet this inbound admits. Each user is admitted by the one credential kind the protocol takes, and a user without it is skipped here. None is the protocol’s open or shared mode.
user_removal Overrides Policies.user_removal for the sessions of this inbound’s users.
proxy_protocol Whether the listener reads a PROXY protocol v2 header at the start of each TCP connection. When a connection sends one, the client’s address is taken from it; a connection without one keeps its socket peer as its client. The header read has a timeout. Only a Tcp bind may set it: only a tcp listener reads a PROXY protocol header.
real_ip Take a WebSocket connection’s client from a header a reverse proxy in front, such as nginx, adds to each upgrade request, when the connection comes from a proxy trusted to set it. See RealIpSpec. Only a WebSocket transport on a Tcp bind may set it. None leaves the client as the connection, or its PROXY header, names it.
supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RealIpSpec {
pub header: CompactString,
pub trusted: Vec<cidr::IpCidr>,
}
impl RealIpSpec {
pub fn trusts(&self, ip: IpAddr) -> bool;
}

Where a WebSocket inbound behind a reverse proxy finds its client, as nginx’s realip module does. spec_plan exports it beside InboundSpec.

Field Meaning
header The upgrade request header that names the client: X-Real-IP, or a list such as X-Forwarded-For, where each proxy appends the address it heard from. In a list the client is the rightmost address outside trusted, or the leftmost when every one is inside. It must be a valid HTTP header name: the real-ip header "<h>" is not a header name.
trusted The networks of the proxies trusted to set the header, as cidr::IpCidr values. At least one: a real-ip header needs at least one trusted proxy.

trusts(ip) canonicalises ip (an IPv4-mapped IPv6 address becomes IPv4) and answers whether any network in trusted holds it. The header counts only on a connection from a proxy in the trusted networks; from anyone else it is ignored, so a client cannot name itself someone else. A trusted proxy that sends no header, or one that is not a list of addresses, leaves the client as the connection names it. The transport reads at most 32 addresses (MAX_REAL_IP_ADDRESSES in protocols/src/transports/ws/endpoint.rs); a longer list counts as malformed. How the listener applies it, and what the named client is then held to (bans, the device limit, routing), is on Serving.

real_ip is accepted only where validation finds a WebSocket transport (StreamShape::Ws) on a Tcp bind, whichever protocol InboundProtocolSpec::transport() returns it for (SOCKS and Shadowsocks included): only a websocket transport on a tcp listener reads a real-ip header. etemenanki-app and the FFI client always set None. katana lowers it from [node.controller]: real_ip_header and real_ip_from must be set together, and it refuses a half setting, a header that is not a header name, or a network that does not parse before any panel names a WebSocket node (Lowering inbounds and outbounds).

supervisor/src/topology/inbound/mod.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandoffSpec {
pub path: PathBuf,
pub mode: u32,
pub gid: Option<u32>,
}

A Unix socket on which a relay running on the same host hands over TCP connections it accepted for the inbound, which serves each exactly as one it accepted itself: its transport, then its protocol. topology::inbound exports it beside BindSpec, and InboundSpec::binds() yields it as BindSpec::Handoff after the inbound’s own bind. Only Unix serves it.

Field Meaning
path Where the socket is bound: absolute, naming a file, and at most 107 bytes, which is what sun_path holds beside its terminating NUL. A missing directory is created, mode 0750 and in gid.
mode The socket file’s permission bits, such as 0o660: who may connect, and so hand connections over. Permission bits only, so a set-id or sticky bit is refused. They are set before the socket listens.
gid The group the socket file, and a directory created for it, are given, so a relay running as another user in that group can connect. None leaves the process’s own group.

Each connection on the socket carries one message: the TCP socket as SCM_RIGHTS, exactly one descriptor, and a whole PROXY protocol v2 header naming its client, at most MAX_V2_LEN (4,096) bytes with nothing after it. A LOCAL header leaves the socket’s own peer as the client. The relay is answered with one byte, 0x00 once the socket is taken and 0x01 when it is refused and still the relay’s alone, and the control connection is closed. The whole exchange is on Serving.

A changed mode or gid is a new bind: the socket is bound afresh at the same path, under a staging name beside it, and moved over the path at commit, so a refused apply leaves the running socket where it is. A changed path moves it. Either way the inbound’s TCP listener keeps its socket, and the connections handed over before run on. The TCP listener and the handoff socket count against one set of admission caps, the inbound’s (Serving).

Validation (validate_inbound → handoff_socket) refuses, in this order, a handoff socket off Unix, one beside anything but a Tcp bind, a relative path or one that names no file, a path longer than 107 bytes, and a mode beyond 0o777. A socket path is one place, so a handoff socket collides with any other bind at its path: a Unix listener’s, or another handoff socket’s with a different mode. The texts are on Validation.

etemenanki-app and the FFI client always set None. katana lowers it from [node.controller]: handoff_dir, an absolute directory, and handoff_group, a group name it looks up in /etc/group. Every stream node on a TCP bind gets a socket in handoff_dir with mode 0o660, named by handoff_file(listen_ip, port): <port>.sock when listen_ip is 0.0.0.0, :: or empty, and <listen_ip>-<port>.sock otherwise (an IP address as it parses and prints, for example 2001:db8::1-443.sock), so two nodes on one port and different addresses get different paths. The directory, a / and the node’s longest name, handoff_file(listen_ip, 65535), must fit in MAX_SOCKET_PATH, 107 bytes. katana refuses handoff_group without handoff_dir, a relative or too long directory, and a group /etc/group does not list, before any panel names a node’s port (Lowering inbounds and outbounds).

The file topology/inbound/mod.rs also defines what an inbound spec is built into: InboundHandler, StreamInbound, StreamProtocol, Ss2022Users, Hy2Handler and TunHandler. Those runtime types are on Serving.

supervisor/src/topology/inbound/mod.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BindSpec {
Tcp { host: String, port: u16 },
Udp { host: String, port: u16 },
Unix(PathBuf),
Handoff(HandoffSpec),
Tun(TunSource),
}
impl fmt::Display for BindSpec;
Variant Display Serves
Tcp { host, port } host:port, for example 127.0.0.1:1080 SOCKS, HTTP, Trojan, VLESS, VMess, Shadowsocks, Shadowsocks 2022, AnyTLS, sss
Udp { host, port } udp host:port, for example udp 0.0.0.0:8443 Hysteria 2
Unix(path) unix: and the path, for example unix:/run/etemenanki/proxy.sock The same stream protocols as Tcp, without a transport layer, so not AnyTLS, which needs TLS. Served only on Unix; elsewhere validation refuses it
Handoff(spec) handoff: and the path, for example handoff:/run/etemenanki/handoff/443.sock Never an inbound’s own bind (the protocol cannot be served on handoff:<path>): it is the second listener of an inbound whose handoff is set, yielded by InboundSpec::binds()
Tun(TunSource::Create(spec)) tun and the interface name, or tun auto when spec.name is None TUN
Tun(TunSource::Fd(device)) tun fd and the raw descriptor number TUN. The variant exists only on Unix

The Display form is what the errors about a bind print:

inbound b: 127.0.0.1:1080 is bound by another inbound
inbound h2: udp 0.0.0.0:8443 is bound by another inbound
inbound b: unix:/run/etemenanki/proxy.sock is bound by another inbound
inbound other: handoff:/run/etemenanki/handoff/443.sock is bound by another inbound
inbound t2: tun ete0 is bound by another inbound

Equality is derived, so two binds are the same bind exactly when every field is equal:

  • TCP and UDP on the same port are different binds. A Hysteria 2 listener may share a port number with a SOCKS listener (the_same_port_over_udp_is_another_bind).
  • A socket path is one place: two binds at one path collide however else they differ, a Unix bind and a handoff socket, or two handoff sockets with different modes (two_binds_may_not_share_a_socket_path). Across applies, a handoff socket whose mode or group changed is another bind, bound afresh at its path.
  • Two TUN inbounds that both leave the interface name to the kernel and ask for the same MTU, addresses and routes are the same bind. etemenanki-app refuses such a config with inbound t2: tun auto is bound by another inbound.
supervisor/src/topology/inbound/mod.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TunSource {
/// Created, addressed and routed by the inbound.
Create(DeviceSpec),
/// Handed over already configured. Unix only.
#[cfg(unix)]
Fd(SuppliedTun),
}
impl TunSource {
pub fn mtu(&self) -> u16;
/// The addresses the inbound assigns the device; none for a supplied one.
pub fn addresses(&self) -> impl Iterator<Item = IpAddr> + '_;
}
#[cfg(unix)]
#[derive(Debug, Clone)]
pub struct SuppliedTun {
pub fd: Arc<OwnedFd>,
/// What the platform configured the interface with; at least 1280.
pub mtu: u16,
}
#[cfg(unix)]
impl PartialEq for SuppliedTun {
fn eq(&self, other: &Self) -> bool {
self.fd.as_raw_fd() == other.fd.as_raw_fd() && self.mtu == other.mtu
}
}
#[cfg(unix)]
impl Eq for SuppliedTun {}
protocols/src/tun/device.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceSpec {
/// Interface name; `None` lets the kernel pick one. macOS takes only
/// `utun<N>`.
pub name: Option<String>,
pub mtu: u16,
/// `(address, prefix length)` pairs assigned to the interface.
pub addresses: Vec<(IpAddr, u8)>,
/// `(network, prefix length)` pairs routed into the interface.
pub routes: Vec<(IpAddr, u8)>,
}
  • Create. The inbound creates the interface, assigns addresses and installs routes (protocols/src/tun/device.rs → open, install_routes). That needs the privilege to create an interface: root or CAP_NET_ADMIN on Linux, root on macOS, an elevated process on Windows. The device is a tun device on Linux, a utun device on macOS and a Wintun adapter on Windows, which needs wintun.dll beside the executable. Routes are installed on Linux (through rtnetlink), macOS and Windows (through route_manager, as on-link routes into the interface). They are never removed explicitly: the system drops the interface and its routes when the device’s last handle closes. check_platform refuses what the platform cannot honour. On any platform but Linux, macOS and Windows, a non-empty routes gives tun routes are installed only on Linux, macOS and Windows; add them with the OS route tool. On macOS, a name that is not utun followed by digits gives tun name "<name>": macOS names a tun device utun<N>, or leave it unset. On Android and iOS tun::open refuses every device: tun devices are created by the system VPN API here; adopt its descriptor.
  • Fd. Unix only. A mobile VPN API’s descriptor. The platform owns the interface, its addresses and its routes, so check_platform does not apply. The inbound serves duplicates of fd (tun::adopt) and closes them when it stops; fd itself closes when its last holder drops it, and the spec is one of its holders. adopt makes its duplicate non-blocking, and a duplicate shares the descriptor’s file status flags, so fd becomes non-blocking too.
  • Equality of a supplied device. OwnedFd has no equality, so SuppliedTun compares the raw descriptor number and the MTU. The code’s reason: an open descriptor’s number is unique while it is open, and both values hold theirs open. An apply therefore keeps the device only while its spec names that same descriptor.
  • MTU. TunSource::mtu reads either source. Both must be at least MIN_TUN_MTU = 1280, the smallest MTU the userspace stack serves (inbound t: tun mtu must be at least 1280).
  • Addresses. TunSource::addresses yields the addresses a Create device is assigned, and nothing for Fd, whose platform assigned them. The TUN handler keeps them as its own addresses (see TunSpec).

A change to a TUN inbound is a disruptive change: the plan gives it a Disrupt step. etemenanki-app refuses a TUN change, and a restart applies it. The TUN stack itself is on TUN.

supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum InboundProtocolSpec {
Socks { udp: bool, udp_bind: Option<IpAddr>, transport: InboundTransportSpec },
Http { allow_transparent: bool, transport: InboundTransportSpec },
Trojan { transport: InboundTransportSpec },
Vless { transport: InboundTransportSpec, flow: VlessFlow },
Vmess { transport: InboundTransportSpec, clock_check: bool },
Shadowsocks { method: SsMethod, password: Secret<String>, transport: InboundTransportSpec },
Ss2022 { method: Ss2022Method, psk: Secret<Vec<u8>>, transport: InboundTransportSpec },
Hysteria2(Hysteria2InboundSpec),
Tun(TunSpec),
Anytls { transport: InboundTransportSpec, padding_scheme: Option<Vec<String>> },
Sss { transport: InboundTransportSpec, settings: SssServerSettings },
}
impl InboundProtocolSpec {
/// The transport this protocol runs over, for every protocol that takes
/// one: the one place that says which do. Hysteria 2 and TUN have none.
pub fn transport(&self) -> Option<&InboundTransportSpec>;
}

The enum is #[non_exhaustive], so a new protocol is a new variant and not a breaking change: a match outside the crate keeps a wildcard arm, as etemenanki-app’s and the FFI client’s do. Inside the crate the attribute does not apply, so the compiler still lists the matches to extend, apart from the catch-all arm listed under Changing the spec. transport() is the one list of the protocols that take a transport: the builder, the real-IP rule and the transport rules of validation all read it, so a protocol added later meets those rules without calling them.

Every variant pairs with one kind of bind, and every variant admits users by one credential kind (build/users.rs → credential_kind):

Variant Bind Transport Admits users by users = None means
Socks Tcp or Unix InboundTransportSpec account no authentication
Http Tcp or Unix InboundTransportSpec account no accounts: the open proxy mode
Trojan Tcp or Unix InboundTransportSpec password refused: no open mode
Vless Tcp or Unix InboundTransportSpec, the only one that may be REALITY uuid refused: no open mode
Vmess Tcp or Unix InboundTransportSpec uuid refused: no open mode
Shadowsocks Tcp or Unix InboundTransportSpec, carrying TCP only password only the server password
Ss2022 Tcp or Unix InboundTransportSpec, carrying TCP only ss2022_psk only the server psk
Hysteria2 Udp QUIC, with its own TLS account, or password under Hysteria2UserAuth::Password only shared_password
Tun Tun none none: a TUN device admits no users the only mode
Anytls Tcp InboundTransportSpec, whose shape must be Tls password refused: no open mode
Sss Tcp or Unix InboundTransportSpec uuid refused: no open mode

A protocol on the wrong kind of bind is refused with inbound <tag>: the protocol cannot be served on <bind> (each_protocol_is_served_only_on_its_kind_of_bind). Trojan, VLESS, VMess, AnyTLS and sss without a set are refused with the protocol has no open mode and needs a user set, because nobody could connect (trojan_vless_and_vmess_need_a_user_set, anytls_needs_tls_a_user_set_and_a_scheme_that_parses, and validation_refuses_bad_limits in supervisor/tests/sss.rs).

A Unix listener is local, so it carries no transport. A stream protocol’s shape on one must be plain StreamShape::Tcp without TLS (a unix socket carries no transport; its shape must be plain tcp), and the builder ignores the transport of any inbound bound to Unix. AnyTLS, whose shape must be Tls, can therefore never be served on one. Unix sockets are served only on Unix: elsewhere, an inbound bound to Unix is refused with unix sockets are not served on <os>, where <os> is std::env::consts::OS, such as windows.

The transport rules are checked once for every protocol transport() names, in build/validate.rs → inbound_transport (every_protocol_meets_the_same_transport_rules): plain TCP on a Unix listener, TLS material exactly with a shape that layers TLS, REALITY settings exactly with the REALITY shape, and that shape under VLESS only. So a REALITY transport under any other protocol is refused with reality is for vless only (reality_is_vless_only).

Field Meaning Rule
Socks.udp Whether UDP ASSOCIATE is served. Only over a plain StreamShape::Tcp transport: the relay’s datagrams go to a UDP socket of their own beside the listener, in the clear, so under TLS they would name every target the TLS hides, and behind a WebSocket or gRPC front the client would reach the front, not the relay. socks serves udp associate over plain tcp only; turn udp off to wrap it in a transport (socks_relays_udp_over_plain_tcp_only).
Socks.udp_bind The IP reported to the client, and bound, for the UDP relay. None uses the listener’s local IP. A Unix listener has no local IP, so SOCKS on a Unix socket with udp = true needs a udp_bind: socks over a unix socket has no local IP for UDP associate; set udp_bind or turn udp off.
Socks.transport The transport the control connection runs over, such as SOCKS5 inside TLS. The transport rules above.
Http.allow_transparent Whether an origin-form request target is served, its target taken from the Host header. none

etemenanki-app reads a SOCKS inbound’s [inbound.stream] like any other and leaves udp at its default, so a SOCKS inbound wrapped in a transport must set udp = false. katana lowers a panel’s SOCKS node with udp: false. The protocol pages are SOCKS and HTTP.

These three carry a transport, and always need a user set. Trojan admits by password, VLESS and VMess by uuid.

VLESS also carries flow, the XTLS flow it serves: VlessFlow::None is plain VLESS, and VlessFlow::Vision (xtls-rprx-vision) wraps each connection in the XTLS Vision stream adapter. Vision frames the body and hands the raw socket over mid-stream, which only REALITY’s record layer can do, so a Vision inbound whose shape is anything but StreamShape::Reality is refused with vless vision (xtls-rprx-vision) rides only over reality. etemenanki-app reads flow from a VLESS inbound’s settings ("xtls-rprx-vision" or unset); katana lowers it from the node config’s VLESS flow, which is Vision or none, and its newV2board client refuses any other flow a UniProxy panel names. How Vision frames the stream is on XTLS Vision.

VMess also carries clock_check. true requires the timestamp in a client’s auth id to lie within ±120 s of the server clock. false admits any plausible timestamp, so a client whose clock is wrong still connects, at the cost of replay protection: a handshake replayed more than REPLAY_TTL (240 s) after it first arrived is admitted again (protocols/src/vmess/accounts.rs → AccountValidator::with_clock_check). build_handler keeps the check for anything but an explicit false. etemenanki-app reads clock_check from a VMess inbound’s settings, default true (app/src/config.rs → VmessInboundSettings); katana lowers clock_check = !disable_vmess_clock_check from [node.controller]. The check lives in the VMess validator, so a change builds a new handler and swaps it without disruption (a_vmess_clock_check_change_swaps_the_handler_without_disruption).

See Trojan, VLESS and VMess wire format.

Shadowsocks Ss2022
Methods SsMethod: Aes128Gcm, Aes256Gcm, ChaCha20Poly1305, XChaCha20Poly1305 Ss2022Method: Blake3Aes128Gcm (2022-blake3-aes-128-gcm, 16-byte key), Blake3Aes256Gcm (2022-blake3-aes-256-gcm, 32), Blake3ChaCha20Poly1305 (2022-blake3-chacha20-poly1305, 32)
Server secret password, admitted besides the users psk, decoded and exactly method.key_len() bytes. While the inbound admits no user it is the one session key; once it admits users, it is the identity PSK (iPSK) of the Extended Identity Header, and each user’s own key is their session key
User credential password ss2022_psk, decoded, sized by each admitting inbound
Users need any method an AES-GCM method

The server key of Ss2022 must be exactly the method’s key length, or the spec is refused with the shadowsocks-2022 key must be 32 bytes for this method (a_shadowsocks_2022_server_key_is_sized_to_its_method). etemenanki-app decodes the server key (ss_2022::users::decode_psk: standard base64 of the trimmed text, failing with decode PSK: <error>) and runs it through ss_2022::users::normalise_psk before it builds the spec. A longer key is folded to the method’s length, keeping the first key_len bytes of its SHA-256 (ss_2022::crypto::fold_key); a shorter one is refused by the app: inbound ss: shadowsocks-2022: PSK too short (16 < 32).

A user’s ss2022_psk is only decoded. Each inbound that admits the user normalises it to its own method, since that is where the size comes from: validate_admission checks it and user_table folds it. A longer key is folded down; a shorter one is refused, naming the user and not the key: inbound ss: user a@example.com: shadowsocks-2022: PSK too short (8 < 16) (a_shadowsocks_2022_user_key_must_normalise_to_the_method).

The Extended Identity Header is defined only for the AES-GCM methods, so an Ss2022 inbound with Blake3ChaCha20Poly1305 cannot admit users. The protocols crate refuses such a user table when it is built (protocols/src/ss_2022/users.rs → Validator::from_config), so the apply fails as a build error: building inbound ss failed: shadowsocks-2022: multi-user requires an aes-gcm method.

Both carry a transport, so a server offers Shadowsocks of either kind over TLS, WebSocket or gRPC, with or without TLS, in its shared mode as well as with users (shadowsocks_runs_over_a_transport). The transport carries the TCP side alone: Shadowsocks carries no UDP here over any transport, as in Xray, whose client sends a Shadowsocks datagram as a UDP packet of its own and never inside its transport. See Shadowsocks and Shadowsocks 2022.

Field Meaning Rule
Anytls.transport The TLS the protocol runs inside. AnyTLS adds no encryption of its own. The shape must be StreamShape::Tls: anytls runs over tls only; its shape must be tls.
Anytls.padding_scheme The padding scheme pushed to clients whose own differs, as its lines, joined with \n as sing-box and Xboard write it. None is the reference default every client starts with. Parsed with protocols/src/anytls/padding.rs → PaddingScheme::from_lines, whose error is the reason, for example padding scheme line 2: an item is neither c nor min-max with 1 <= min <= max <= 1048576. At most MAX_SCHEME_LEN (2,048 bytes) once joined.

AnyTLS admits users by password, as Trojan does, and has no open mode. A padding scheme change builds a new handler and swaps it: live connections keep the scheme they started with, and new ones get the new one (a_padding_change_leaves_live_sessions_alone in supervisor/tests/anytls.rs). The protocol is on AnyTLS.

Sss.settings is the protocols crate’s ServerSettings, re-exported as SssServerSettings: the two transform chains up and down (SssChain, already parsed and bounded), max_datagram (U), header_timeout (T_h), reject (SssReject::Drain or Fallback(addr)) and flush_delay (t_c). sss admits users by uuid and has no open mode. Validation holds U to at least 1, T_h above zero and t_c to at most 10 ms (MAX_FLUSH_DELAY); the texts are on Validation. A settings change swaps the handler, and live streams keep the settings they started with. The settings, the gate they drive and the chains are on sss and sss transforms.

supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hysteria2InboundSpec {
pub tls: ServerTlsSpec,
pub shared_password: Option<Secret<String>>,
pub masquerade: Option<MasqueradeSpec>,
pub obfs: Option<ObfsSpec>,
pub udp_idle_timeout: Option<Duration>,
pub max_connections: usize,
pub max_circuits: usize,
pub user_auth: Hysteria2UserAuth,
pub bandwidth: BandwidthConfig,
pub ignore_client_bandwidth: bool,
pub congestion: Fallback,
pub quic: QuicOptions,
}
Field Meaning Rule
tls The certificate chain and key. QUIC always carries TLS; there is no plaintext Hysteria. Parsed at build by protocols/src/hysteria/server/endpoint.rs → server_config.
shared_password The one password every client presents, when the inbound admits no user set. Exactly one of this and InboundSpec.users is set, and it is not empty.
masquerade The answer to an unauthenticated request. None answers as Go’s http.NotFound does. See MasqueradeSpec below.
obfs Salamander obfuscation beneath QUIC. Key at least 4 bytes. A change is disruptive.
udp_idle_timeout How long a UDP association may stay quiet. None disables UDP relay. Between 2 and 600 seconds when set: udp_idle_timeout must be between 2 and 600 seconds.
max_connections Client connections served at once. At least 1: max_connections and max_circuits must be at least 1.
max_circuits Live circuits (proxied streams) across the whole listener. At least 1, with the same text.
user_auth Which credential the user set is admitted by. Ignored when shared_password is set. See Hysteria2UserAuth below.
bandwidth This server’s rates: down is answered to every client as the rate the server receives at, and up caps what it sends to one client. none; a Bandwidth is never zero
ignore_client_bandwidth Upstream’s ignoreClientBandwidth: answer auto, so neither end sends at a fixed rate, whatever a client declares. none
congestion The congestion controller used when no fixed rate is agreed: Fallback::Cubic, NewReno or Bbr. none
quic The QUIC transport settings of each client’s connection. The hy2_quic limits; see Limits.
  • Rates and congestion. When a client and the server agree on a send rate, the connection sends at it with Brutal; otherwise it uses the congestion fallback. The controllers and the switch between them are on Congestion control, and Bandwidth on shinku-quic overview.
  • Both or neither. Were a shared password and a user set both set, a credential could have two answers: a shared password and a user set cannot both be set; a credential would have two answers. Neither: hysteria2 needs a shared password or a user set. An empty shared password: the shared password must not be empty.
  • Idle range. HY2_UDP_IDLE = 2..=600 seconds, checked against Duration::as_secs, which drops any fraction of a second: 1.5 s counts as 1 and is refused, 600.9 s counts as 600 and is accepted. The code’s reason for the range: below it a busy association is swept between packets, above it a dead one holds its socket for ten minutes. None needs no bound, because it turns UDP off.
  • Front-end defaults. DEFAULT_MAX_CONNECTIONS (262,144) and DEFAULT_MAX_CIRCUITS (4,194,304), from protocols/src/hysteria/server/config.rs, are what katana always lowers and what etemenanki-app lowers when the config sets no max_connections or max_circuits. Both front ends default the UDP idle timeout to 60 seconds when UDP is on, a literal unwrap_or(60) in each rather than a named constant; on a helium node katana takes the idle timeout the panel sends. Both default congestion to Fallback::Cubic and start quic from QuicOptions::server(); katana always lowers QuicOptions::server(). etemenanki-app reads bandwidth, congestion and quic from the inbound’s settings, and leaves ignore_client_bandwidth, when the config does not set it, true while no rate is given and false, upstream’s default, once one is. katana takes the rates from the panel’s up_mbps and down_mbps, each overridden by [node.hysteria] when set there, and ignore_client_bandwidth from [node.hysteria], then the panel, else true. A helium node can set none of those three keys, so its rates and ignore_client_bandwidth are the panel’s. The spec itself has no default.
  • Front-end checks. A spec cannot express a timeout for a relay that is off, so both front ends refuse that combination themselves: udp_idle_timeout is set but udp is not enabled. katana checks the 2 to 600 second range as it reads [node.hysteria] (src/api/hysteria.rs → udp), with the supervisor’s reason text, and holds the masquerade to helium-node-api’s rules there (a status from 100 to 999, never 233) and to Masquerade::new in its lowering (src/lower/inbound.rs → masquerade_spec), so a [node.hysteria] block that breaks either fails before a spec exists. A helium panel’s node config is held to the same rules as it is read.
  • Changes on a running listener. A masquerade change swaps the handler without disrupting anyone (a_hysteria2_masquerade_change_swaps_without_disrupting), and so does a change to bandwidth, ignore_client_bandwidth, congestion or quic: swap_disruption names none of them. An obfs change is a Disrupt step with the reason the hysteria2 obfuscation changed; connected clients cannot follow the new key; an apply that does not allow disruption is refused with inbound <tag>: the hysteria2 obfuscation changed; connected clients cannot follow the new key; this ends its live connections and needs allow_disruptive. etemenanki-app refuses it, katana allows it. katana itself closes every session of a node after an apply that changes its obfuscation, port or masquerade, or that turns its UDP relay on or off, though not after one that changes only its rates or UDP idle timeout (src/manager/node.rs → disruptive_part), and after any edit to [node.hysteria] (Hysteria 2: server). A max_circuits change gives the new handler a fresh circuit budget; when max_circuits is unchanged, the new handler shares the running listener’s budget, so live circuits keep counting against it (supervisor.rs → same_circuits). See Reconcile.

The listener itself is on Hysteria 2: server.

supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Hysteria2UserAuth {
/// Upstream's `user:pass`, from each user's `account`.
#[default]
Account,
/// The whole auth string is the user's `password`.
Password,
}
Variant Credential Admission rules
Account (default) The user’s account, presented as user:pass Usernames are compared case-insensitively on the wire, so two accounts that differ only in case collide: users <a> and <b> present the same username. The wire form splits on a colon, so a name holding : could never be presented, and an empty part is no credential: user <name>: a hysteria2 account needs a name without ':' and a password.
Password The user’s password, as the whole auth string Must not be empty: user <name>: a hysteria2 password must not be empty.

etemenanki-app always lowers Account. katana lowers Password by default, and gives each user their UUID as that password. The code’s reason: the node agents that panels are built for key their users by the UUID itself, and a panel’s user list carries no password field for the user:pass form. [node.hysteria] credential = "user_pass" selects Account, and katana refuses any other value than "", "uuid" and "user_pass". A helium node cannot set credential at all, so its users always present their UUID.

supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MasqueradeSpec {
/// Never a success status: that would tell the prober it had
/// authenticated.
pub status: u16,
pub body: String,
pub content_type: String,
}

validate builds a Masquerade from it (protocols/src/hysteria/server/masquerade.rs → Masquerade::new) to find out whether it is acceptable, so both refusals come from the protocols crate:

  • hysteria2: 233 is the authentication success status and cannot be used for the masquerade, printed by etemenanki-app as inbound h1: hysteria2: 233 is the authentication success status and cannot be used for the masquerade;
  • hysteria2: <status> is not an HTTP status code, for a status outside 100 to 999, which is what http::StatusCode::from_u16 accepts: inbound h1: hysteria2: 99 is not an HTTP status code.

The response carries the status, a Content-Type of content_type and a Content-Length of the body’s length. None builds Masquerade::default(): status 404, body 404 page not found\n, content type text/plain; charset=utf-8, byte for byte what an unconfigured upstream server answers. When a config sets only some masquerade keys, both front ends fill the rest with these same values.

supervisor/src/topology/spec_plan/inbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TunSpec {
/// Relay UDP flows as well as TCP connections.
pub udp: bool,
/// How long a UDP flow may stay quiet before it is retired.
pub udp_idle_timeout: Duration,
/// Flows tracked at once.
pub max_flows: usize,
/// Relay IPv6; when not, IPv6 connections are refused at the handshake.
pub ipv6: bool,
}

The device itself, MTU included, is the inbound’s BindSpec::Tun, so TunSpec holds only what the stack does with it. build_handler builds a TunInbound from TunConfig { user: Principal::anonymous(), mtu: device.mtu(), udp, udp_idle_timeout, max_flows }: a TUN flow carries no credential, so every flow is anonymous. It adds without_sniffing() when sniff is false and without_ipv6() when ipv6 is false, and the handler records the device’s own addresses (TunHandler.own_addresses, from TunSource::addresses).

  • ipv6 = false. Every IPv6 TCP connection is refused at its handshake and every other IPv6 packet is dropped, so a client falls back to IPv4 at once.
  • ipv6 = true. A runtime still refuses an IPv6 TCP connection that no egress it may take can reach: before the stack answers it, the handler asks the plane (topology/plane.rs → Plane::ipv6_unreachable) whether every target the route table may send it to lacks IPv6. The device’s own addresses count as no way out. See Routing plane.

etemenanki-app fills what the config leaves out, and the FFI client’s own TUN inbound uses, the defaults of protocols/src/tun/config.rs: DEFAULT_MTU = 1500, DEFAULT_UDP_IDLE_TIMEOUT = 60 s and DEFAULT_MAX_FLOWS = 65,536, with udp on. etemenanki-app also defaults ipv6 to true, and the FFI client’s own TUN inbound sets it.

supervisor/src/topology/spec_plan/mod.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ObfsSpec {
Salamander { psk: Secret<Vec<u8>> },
}

Salamander XORs each packet beneath QUIC with BLAKE2b-256(psk || salt). One type serves both sides: Hysteria2InboundSpec.obfs and Hysteria2OutboundSpec.obfs. The key is at least MIN_SALAMANDER_PSK = 4 bytes on either side (salamander obfs key must be at least 4 bytes, pinned by a_salamander_key_is_at_least_4_bytes_on_either_side). build/outbound.rs → obfs turns it into the protocols crate’s Obfs::Salamander for the listener and the connector alike. The wire side is on Hysteria 2: client.

supervisor/src/topology/spec_plan/transport.rs
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum StreamShape {
/// Plain TCP.
Tcp,
/// TLS over TCP: this project's own `network = "tls"`.
Tls,
Ws {
path: String,
/// The `Host` the config named, if any. Each side applies its own
/// fallback: an outbound may borrow `server`, an inbound has nothing
/// to borrow.
host: Option<String>,
tls: bool,
},
Grpc { service: String, authority: Option<String>, tls: bool },
/// REALITY over TCP: a TLS 1.3 of its own, with no ServerTlsSpec.
Reality,
}
impl StreamShape {
/// Whether this project's TLS is layered under this shape, and so
/// whether the TLS material beside it must be present.
pub fn uses_tls(&self) -> bool;
}

Inbounds and outbounds share this one shape. A rule about which fields a network requires therefore cannot end up on one side only. In etemenanki-app, app/src/transport.rs → resolve_stream is the one function that derives a shape from a [.stream] block, for both sides. Like the protocol enums, StreamShape is #[non_exhaustive], so a new transport is an addition and a match outside the crate keeps a wildcard arm.

uses_tls() is false for Tcp and Reality, true for Tls, and the tls flag for Ws and Grpc. REALITY is not this project’s TLS: the etemenanki-reality crate runs a TLS 1.3 of its own that passes the server off as another site, so a Reality shape carries no TLS material and its settings sit in the transport’s reality field instead. It rides under VLESS only, on both sides (reality is for vless only). What each shape is built into:

Shape Inbound (build/inbound.rs → build_transport) Outbound (build/outbound.rs → transport_connector) ALPN
Tcp InboundTransport::Tcp TransportKind::Tcp none
Tls InboundTransport::Tls TransportKind::Tls none
Ws InboundTransport::ws(path, host, tls) TransportKind::ws(host, path, tls) http/1.1 when tls
Grpc InboundTransport::grpc(service, tls) TransportKind::grpc(authority, service, tls) h2 when tls
Reality InboundTransport::Reality(server), the server built by build_reality from RealityServerSpec TransportKind::reality(client) from RealityClientSpec, with .vision(uuid) for a VLESS outbound whose flow is Vision n/a: REALITY’s own TLS

host and authority hold what the config named, if anything, and each side applies its own fallback. An inbound’s WebSocket listener accepts an upgrade only on its path, normalised as Xray does it: an ed= query parameter (Xray’s early-data setting) is removed, an empty path becomes /, and a path without a leading / gets one (protocols/src/transports/ws/endpoint.rs → normalize_path). Its host, when set, makes it accept only a request whose Host matches, case-insensitively and without its port (host_matches); None accepts any Host. A server does not check the authority a gRPC client sends, so an inbound’s authority has no meaning. The transports themselves are on TCP and TLS and WebSocket and gRPC.

supervisor/src/topology/spec_plan/transport.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InboundTransportSpec {
pub shape: StreamShape,
pub tls: Option<ServerTlsSpec>,
pub reality: Option<RealityServerSpec>,
}
/// What a server presents: a certificate chain and its private key, both PEM.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerTlsSpec {
pub cert_pem: Vec<u8>,
pub key_pem: Secret<Vec<u8>>,
}
  • tls is Some exactly when shape.uses_tls() holds. The fields are public, so this is checked on apply, both ways: the transport layers tls but carries no tls settings, or the transport carries tls settings but does not layer tls (an_inbound_carries_tls_material_exactly_when_its_shape_layers_tls).
  • reality is Some exactly when the shape is StreamShape::Reality, checked the same way: a reality transport carries no reality settings, or the transport carries reality settings but its shape is not reality (reality_field_must_match_its_shape). A Reality shape layers none of this project’s TLS, so tls must be None beside it.
  • An inbound’s shape may have gaps: a WebSocket shape without host and a gRPC shape without authority are valid (an_inbound_needs_no_ws_host_or_grpc_authority). A server has nothing to borrow them from and needs neither.
  • Validation never parses PEM; its fixtures use b"cert" and b"key". The builders do: TlsServerConfig::from_pem(cert, key, alpn) for a stream transport, which uses OpenSSL’s mozilla_intermediate_v5 profile with a TLS 1.2 minimum, so TLS 1.2 and TLS 1.3 only, and hysteria::server::endpoint::server_config for Hysteria 2. A PEM that does not parse is a build error, not a validation error: building inbound t failed: no certificate in PEM bundle, or for Hysteria 2 building inbound h1 failed: hysteria2: the certificate file contains no certificates.
supervisor/src/topology/spec_plan/transport.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RealityServerSpec {
pub private_key: Secret<[u8; 32]>,
pub server_names: Vec<String>,
pub short_ids: Vec<[u8; 8]>,
pub dest: Destination,
pub max_time_diff: Option<Duration>,
pub min_client_version: Option<[u8; 3]>,
pub max_client_version: Option<[u8; 3]>,
pub require_hybrid: bool,
}

What a REALITY inbound lets in, and the site it passes itself off as. Every value is decoded and shape-checked when the front end lowers the spec, so two specs compare with == and a bad key or shortId fails before an apply: the key is 32 bytes by its type, and each shortId is the 8 bytes its hex text decodes to, zero-padded.

Field Meaning Rule (build/validate.rs → reality_settings)
private_key The server’s X25519 private key 32 bytes by its type
server_names The SNIs answered as REALITY, matched exactly At least one, none empty and none holding *: reality needs at least one server name, invalid reality server name "<name>"
short_ids The shortIds let in; the all-zero one is allowed At least one: reality needs at least one shortId
dest The site dialed over TCP for anyone who is not a client, and passed off as A non-zero port and, for a domain, a non-empty host: reality dest needs a non-zero port, reality dest needs a host
max_time_diff Refuse a client whose clock is further than this from the server’s; None does not check (Xray’s maxTimeDiff) none
min_client_version, max_client_version Inclusive bounds on the client version, three bytes compared as one number (Xray’s minClientVer, maxClientVer); None leaves that end open When both are set, min is not above max: reality min_client_version is above max_client_version
require_hybrid Require the X25519MLKEM768 key share before any X25519 one; off by default, because sing-box and mihomo send X25519 alone none

reality_settings_fail_closed breaks each rule in turn. build/inbound.rs → build_reality builds the protocols crate’s RealityServer from it with RealityServer::from_bytes, and gives it a dialer for dest: Dialer::new(socket) under the supervisor’s socket policy, the system resolver (Resolver::default()) and AddressFamilyStrategy::default(). dest is the operator’s cover site, not proxied traffic, so it is dialed directly, outside the routing plane and the configured DNS, as Xray’s own REALITY does. An error from from_bytes is a build error, reported in prepare, so the apply is refused and what runs keeps running. A changed RealityServerSpec is an ordinary handler swap, like a certificate change. etemenanki-app lowers it from [inbound.stream.reality] over network = "tcp"; katana from a panel node’s REALITY settings, with its one server name as both the SNI and the host of dest. The server is on REALITY transport.

OutboundTransportSpec, ClientTlsSpec and VerifyMode

Section titled “OutboundTransportSpec, ClientTlsSpec and VerifyMode”
supervisor/src/topology/spec_plan/transport.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutboundTransportSpec {
pub shape: StreamShape,
pub tls: Option<ClientTlsSpec>,
/// Present exactly when the shape is StreamShape::Reality.
pub reality: Option<RealityClientSpec>,
/// Which family of the upstream's addresses is dialed.
pub address_family: AddressFamilyStrategy,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RealityClientSpec {
/// The SNI sent, matched against the server's names.
pub server_name: String,
/// The server's X25519 public key.
pub public_key: [u8; 32],
/// The shortId presented, the 8 bytes a hex string decodes to.
pub short_id: [u8; 8],
/// The client version sealed into the hello; None uses the crate's
/// default.
pub client_version: Option<[u8; 3]>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientTlsSpec {
/// The SNI sent, and the name the certificate is checked against.
pub server_name: String,
/// Which roots the certificate is verified against, if it is at all.
pub verify: VerifyMode,
}
protocols/src/transports/tls/config.rs
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum VerifyMode {
/// Verify with the platform/OpenSSL default trust store.
System,
/// Verify with the platform/OpenSSL default trust store plus these PEM CAs.
CustomCa(Vec<u8>),
/// Disable certificate chain and hostname verification.
Insecure,
}

Unlike an inbound’s, an outbound’s shape carries no gaps: a WebSocket host and a gRPC authority must be Some. A gap that reaches the supervisor is the front end’s bug, and it is refused rather than guessed at: ws transport needs a host, grpc transport needs an authority (an_outbound_needs_a_ws_host_and_a_grpc_authority). tls is Some exactly when the shape uses TLS, with the same two texts as inbounds (an_outbound_carries_tls_settings_exactly_when_its_shape_layers_tls). The builder turns ClientTlsSpec into ClientConfig::with_verify_mode(server_name, verify, alpn), which sets a TLS 1.2 minimum. CustomCa adds the bundle’s certificates to the default trust store rather than replacing it, and a bundle holding no certificate fails the build: building outbound p@v1 failed: no certificate in CA PEM bundle. Insecure turns off both chain and host-name verification. The default trust store is OpenSSL’s default verify paths, plus the platform’s own roots on Android, iOS, Windows and macOS, where an OpenSSL built from source would find none (see TCP and TLS transports).

etemenanki-app fills the gaps in app/src/lower.rs → outbound_transport: the WebSocket host from ws.host, then tls.server_name, then server; the gRPC authority from grpc.authority along the same chain; and the SNI from tls.server_name, then server. A CA file is read into VerifyMode::CustomCa as bytes, so the spec never names it.

reality is the client half of REALITY, present exactly when the shape is StreamShape::Reality (a reality transport carries no reality settings, the transport carries reality settings but its shape is not reality), and then with no tls beside it (a reality transport carries no tls settings). Its server_name must be non-empty and hold no *: invalid reality server name "<name>". a_reality_outbound_validates_and_its_field_must_match pins the pairing. Like the inbound, a REALITY transport rides under VLESS only: outbound <tag>@v0: reality is for vless only (a_reality_outbound_is_vless_only), so a mistyped security = "reality" under another protocol fails closed. The builder turns it into RealityClientConfig::from_bytes(server_name, public_key, short_id), adds with_client_version when one is set, and wraps it as TransportKind::reality(client). etemenanki-app lowers [outbound.stream.reality]: its public_key, its first server name as the SNI (else the TLS name, else server), its first shortId (all zeros when none is given) and no client version. The client is on REALITY transport.

address_family chooses among the upstream server’s addresses, which the transport connector looks up with the supervisor’s server resolver (Dns::servers).

supervisor/src/topology/spec_plan/outbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutboundSpec {
pub tag: CompactString,
pub protocol: OutboundProtocolSpec,
/// Overrides Policies::drain for the live flows of this outbound once it
/// is removed or changed.
pub drain: Option<DrainPolicy>,
}
/// Where a proxy client dials, and over what.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProxyUpstream {
/// The proxy server, always addressed over TCP.
pub server: Destination,
pub transport: OutboundTransportSpec,
}
concepts/src/net.rs
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Destination {
pub network: DialNetwork, // Unknown = 0, Tcp = 1, Udp = 2, Unix = 3
pub remote: Remote, // IpAddr(IpAddr) or Domain(CompactString)
pub port: u16,
}

The tag is what routes and balancers name. A changed spec under the same tag is a new version of that outbound, an OutboundId printed as tag@vN, and the flows of the version it replaces are handled by the drain policy (see Reconcile).

ProxyUpstream.server is always dialled over TCP: build/outbound.rs → tcp replaces its network with DialNetwork::Tcp whatever the spec says. etemenanki-app writes DialNetwork::Tcp there too, so its specs compare equal (see What a front end owes the comparison). A remote that is a domain is resolved when the transport dials. See Links and types for Destination.

supervisor/src/topology/spec_plan/outbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum OutboundProtocolSpec {
Freedom { address_family: AddressFamilyStrategy },
Blackhole,
Socks { upstream: ProxyUpstream, account: Option<Account> },
Http { upstream: ProxyUpstream, account: Option<Account> },
Trojan { upstream: ProxyUpstream, password: Secret<String> },
Vless { upstream: ProxyUpstream, id: Uuid, flow: VlessFlow },
Vmess { upstream: ProxyUpstream, id: Uuid, security: Security },
Shadowsocks { upstream: ProxyUpstream, method: SsMethod, password: Secret<String> },
Ss2022 {
upstream: ProxyUpstream,
method: Ss2022Method,
identity_psks: Vec<Secret<Vec<u8>>>,
psk: Secret<Vec<u8>>,
},
Hysteria2(Hysteria2OutboundSpec),
Wireguard(WireguardSpec),
Anytls(AnytlsOutboundSpec),
Sss { upstream: ProxyUpstream, id: Uuid, settings: SssClientSettings },
}

The enum is #[non_exhaustive], as InboundProtocolSpec is.

Variant What it reaches Balancer member (TCP probe) Resolver it holds
Freedom The destination itself, from this host; address_family picks among the destination’s addresses no: no upstream Dns::destinations
Blackhole Nothing: EOF on read, packets swallowed no: no upstream none
Socks, Http upstream, with an optional Account yes: upstream.server Dns::servers
Trojan upstream; the password is hashed at build (trojan::protocol::password_hash) yes Dns::servers
Vless, Vmess upstream; id is the user’s UUID. VLESS adds flow, which the client announces; VlessFlow::Vision frames the body and rides only over a REALITY transport, and its UDP rides XUDP over mux.cool, since the datagram codec is built with the flow too. VMess adds security (Aes128Gcm or ChaCha20Poly1305) yes Dns::servers
Shadowsocks upstream; the key is derived from the password at build (evp_bytes_to_key(password, method.key_len())) yes Dns::servers
Ss2022 upstream; identity_psks in the order iPSK:…:uPSK writes them, empty for a single-user server, and psk the user’s own yes Dns::servers
Hysteria2 Its own QUIC connection, not a transport no: it listens on UDP Dns::servers
Wireguard Its own tunnel, not a transport no: it listens on UDP Dns::servers for the endpoint, Dns::destinations inside the tunnel
Anytls upstream, over TLS only, with a pool of sessions; see AnytlsOutboundSpec below yes: upstream.server Dns::servers
Sss upstream; id is the user’s UUID, and settings (SssClientSettings) the chains up and down, max_datagram and flush_delay it shares with the server yes Dns::servers
  • Probe targets. A balancer selects on health, and a TCP connect to the upstream is how health is learned. build/validate.rs → probe_target names the upstream for the nine proxy variants and nothing for the other four; WireGuard and Hysteria 2 listen on UDP, so a TCP connect would mark them down forever. A member without a probe target is refused: balancer pool: outbound direct has no upstream a TCP health probe can reach.
  • Resolvers. Every outbound but a blackhole holds a resolver built from the DnsSpec, which is why a changed DnsSpec rebuilds every outbound except the blackholes (a_dns_change_rebuilds_every_outbound_but_a_blackhole).
  • Shadowsocks 2022 keys. Every key, the user’s and each identity key, must be exactly method.key_len() bytes: shadowsocks-2022 keys must be 16 bytes for this method (shadowsocks_2022_client_keys_are_sized_to_their_method). Unlike an inbound’s user key, nothing folds them on the supervisor side. etemenanki-app splits the configured password on : (iPSK:…:uPSK), decodes each part and runs it through normalise_psk while it lowers; the last part becomes psk and the others identity_psks.
  • VLESS flow. VlessFlow::Vision on a transport whose shape is not StreamShape::Reality is refused with vless vision (xtls-rprx-vision) rides only over reality, the inbound’s rule mirrored.
  • sss limits. max_datagram at least 1 and flush_delay at most 10 ms, as on the inbound: sss udp_max_datagram must be at least 1, sss flush_delay must be at most 10ms.

How each variant becomes an outbound handler, including its UDP support, is on Outbounds.

supervisor/src/topology/spec_plan/outbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnytlsOutboundSpec {
/// The server, dialed over TLS: the transport's shape is StreamShape::Tls.
pub upstream: ProxyUpstream,
pub password: Secret<String>,
/// What the `client` setting announces. Empty keeps the software unnamed.
pub client: String,
/// How often idle sessions are checked. At or below 5 s it is 30 s.
pub idle_session_check_interval: Duration,
/// How long a session may stay idle before a check closes it. At or
/// below 5 s it is 30 s.
pub idle_session_timeout: Duration,
/// Idle sessions a check keeps however long they have been idle.
pub min_idle_session: usize,
}

An AnyTLS client keeps a pool of sessions to one server, each carrying one flow at a time. The shape of upstream.transport must be StreamShape::Tls (anytls runs over tls only; its shape must be tls), and its TLS settings must be present as for any client, and password must not be empty (anytls password must not be empty); an_anytls_outbound_needs_tls_and_a_password pins both. The three pool settings go to the protocols crate’s AnyTlsClientConfig unchanged; the 5 s floor is the crate’s own. The client is on AnyTLS.

supervisor/src/topology/spec_plan/outbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hysteria2OutboundSpec {
/// The server's UDP endpoint.
pub server: Destination,
/// The SNI sent, and the name the certificate is checked against.
pub server_name: String,
pub password: Secret<String>,
pub verify: VerifyMode,
pub obfs: Option<ObfsSpec>,
/// Concurrent proxy streams on the connection. At least 1.
pub max_concurrent_streams: usize,
/// Which family of the server's addresses is dialed.
pub address_family: AddressFamilyStrategy,
/// Spread the connection over a set of server ports; the port of
/// `server` is then not used.
pub hop: Option<PortHopping>,
/// This client's rates.
pub bandwidth: BandwidthConfig,
/// The congestion control used when no fixed rate is agreed.
pub congestion: Fallback,
/// QUIC transport settings.
pub quic: QuicOptions,
}
protocols/src/hysteria/config.rs
pub struct PortHopping {
/// Every port the server answers on. Not empty, no zero.
pub ports: Vec<u16>,
/// How long the client stays on one port.
pub interval: HopInterval,
}
  • server is dialled over UDP whatever its network says (build/outbound.rs → udp), and its name is looked up with Dns::servers, filtered and ordered by address_family.
  • password must not be empty (hysteria2 password must not be empty), and max_concurrent_streams must be at least 1 (max_concurrent_streams must be at least 1). Both are pinned by a_hysteria2_client_needs_a_password_and_a_stream.
  • hop turns on port hopping: the connection runs over a HoppingSocket that binds a fresh socket at every hop and moves to another port of ports, so the port of server is not used. HopInterval is a fixed interval or a random one between a minimum and a maximum; the port_hopping rules refuse an empty port list, port 0 and a minimum below 5 s (see Validation). The socket is on Port hopping.
  • bandwidth is this client’s rates: down is told to the server at authentication, and with up set, a send rate agreed with the server switches the connection to Brutal. Otherwise the connection uses congestion (Congestion control). quic is checked by the same hy2_quic limits as an inbound’s.
  • The builder starts from Hy2Config::new(server, server_name, password), which fills upstream’s defaults, and then sets every other field from the spec (build/outbound.rs → build_outbound).
  • When the config sets none, etemenanki-app fills max_concurrent_streams with DEFAULT_MAX_CONCURRENT_STREAMS = 102,400 from protocols/src/hysteria/config.rs, server_name with the outbound’s server, congestion with Fallback::Cubic and quic with QuicOptions::client(). It lowers hop from ports and a hop interval (hop_interval, or min_hop_interval with max_hop_interval; HopInterval::default(), 30 s, when neither is set), and with hopping and no port it gives server the first port of the set.
  • An unchanged spec is carried over by Arc with its QUIC connection, so a flow opened after an apply rides the connection an earlier flow opened. A changed one is a new version that dials a connection of its own (an_unchanged_hysteria2_outbound_keeps_its_quic_connection_across_apply).
supervisor/src/topology/spec_plan/outbound.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WireguardSpec {
pub private_key: Secret<[u8; 32]>,
pub peer_public_key: [u8; 32],
pub preshared_key: Option<Secret<[u8; 32]>>,
pub endpoint: Destination,
pub endpoint_address_family: AddressFamilyStrategy,
pub addresses: Vec<IpAddr>,
pub address_family: AddressFamilyStrategy,
pub mtu: usize,
pub keepalive: Option<u16>,
pub reserved: Option<[u8; 3]>,
}
Field Meaning
private_key, preshared_key This side’s key and the optional pre-shared key, 32 bytes each, as Secrets
peer_public_key The peer’s public key, 32 bytes, not secret
endpoint The peer’s UDP endpoint, a proxy server like any other: dialled over UDP whatever its network says
endpoint_address_family Which family of the endpoint’s addresses is dialled; the endpoint is looked up with Dns::servers
addresses The tunnel-local addresses
address_family Which family is used inside the tunnel, for destinations looked up with Dns::destinations
mtu The tunnel MTU, at the IP layer. When the config sets none, etemenanki-app fills DEFAULT_MTU = 1420 from protocols/src/wireguard/config.rs
keepalive The persistent keepalive interval, in seconds
reserved Xray’s 3-byte reserved header field

The two address-family fields answer different questions: endpoint_address_family is about reaching the peer over the host’s network, and address_family is about what is reached inside the tunnel. When address_family asks for one family only (Ipv4Only or Ipv6Only), an address of that family must be among addresses, because the userspace stack has no other source address to use: wireguard address_family ipv6_only needs an IPv6 address (a_wireguard_address_family_needs_an_address_of_that_family). The prefer_* strategies and Auto need nothing. The tunnel is on WireGuard.

protocols/src/helpers/address_family.rs
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum AddressFamilyStrategy {
#[default]
Auto,
Ipv4Only,
Ipv6Only,
PreferIpv4,
PreferIpv6,
}
Variant as_str Addresses used
Auto (default) auto All, in the resolver’s order, which already reflects the host’s address-selection rules
Ipv4Only ipv4_only IPv4 only
Ipv6Only ipv6_only IPv6 only
PreferIpv4 prefer_ipv4 All, IPv4 first (a stable sort, so the order within each family is kept)
PreferIpv6 prefer_ipv6 All, IPv6 first

It appears five times in a spec: Freedom.address_family, OutboundTransportSpec.address_family, Hysteria2OutboundSpec.address_family, and WireguardSpec.endpoint_address_family and address_family. The dialers that apply it are on Dialers.

A front end parses it with its FromStr, which trims the text, lower-cases it and reads - as _:

Accepted text Variant
empty, auto Auto
ipv4, v4, 4, ipv4_only, ipv4only Ipv4Only
ipv6, v6, 6, ipv6_only, ipv6only Ipv6Only
prefer_ipv4, prefer_v4, ipv4_prefer, v4_prefer PreferIpv4
prefer_ipv6, prefer_v6, ipv6_prefer, v6_prefer PreferIpv6

Anything else fails with unknown address family strategy.

supervisor/src/topology/spec_plan/route.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouteSpec {
pub rules: Vec<RouteRuleSpec>,
/// The outbound or balancer tag for a flow no rule matches.
pub default: CompactString,
/// The geoip `.dat` file `GeoIp` matchers look up.
pub geoip: Option<PathBuf>,
/// The geosite `.dat` file `GeoSite` matchers look up.
pub geosite: Option<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouteRuleSpec {
pub matchers: Vec<RouteMatch>,
/// The outbound or balancer tag.
pub outbound: CompactString,
}
  • First match. Rules are tried in order. The first rule with any matcher that matches wins, and a flow no rule matches takes default. A rule is tested with Iterator::any over its matchers: its matchers are alternatives, not conditions that must all hold. A rule whose matchers is empty matches no flow. A rule is identified by its index in rules (entity/id.rs → RuleId, a u32), so reordering the rules renumbers them.
  • Geo data. geoip and geosite are paths to .dat files rather than bytes. The code’s reason: a .dat file runs to megabytes, and only the sets the rules reference are loaded from it. build/route.rs → compile_routes collects the codes of every GeoSite and GeoIp matcher and passes them, with the two paths, to environment/src/routing.rs → build_geo_data, which reads and decodes a file only when some matcher needs it. See the geo codes and errors below.
  • Targets by tag. outbound and default name an outbound or a balancer. The compiled table names slots rather than outbounds, one per distinct tag, so an outbound can be rebuilt without recompiling the table (see Routing plane).

etemenanki-app lowers each [[route.rule]] into one RouteRuleSpec, with its matchers in a fixed order of kinds: domain_suffix, domain_keyword, domain_full, domain_regex (one set for the whole list), cidr, source_cidr, inbound_tag, network, port, geosite, geoip. Every key of the rule goes into that one list, so a rule that sets both domain_suffix and port matches a flow that satisfies either, not only one that satisfies both. It lower-cases the three plain domain kinds. When [route].default is absent, default is the first outbound’s tag.

The geo codes a matcher names:

Matcher Code Meaning
GeoSite code The geosite entry code, compared case-insensitively
GeoSite code@attr Only the domains of entry code that carry the attribute attr
GeoIp code The geoip entry code, compared case-insensitively
GeoIp !code Every IP destination outside the entry code

The loaded sets are keyed by the matcher’s text as written, such as cn, google@ads or !cn. A geo matcher without its file, an entry that does not exist or a file that does not decode fails the route’s build:

  • a geosite matcher is used but no geosite file is configured, printed as building route failed: a geosite matcher is used but no geosite file is configured, and the same for geoip;
  • geosite code not found: <code>, and geoip code not found: <code>;
  • geosite decode: <error>, and geoip decode: <error>, when the file is not the expected protobuf.

RouteMatch belongs to environment/src/routing.rs, the route model shared with katana:

Variant A flow matches when
GeoSite(code) Its domain is in the geosite set code
DomainSuffix(parent) Its domain is parent or a subdomain of it: example.com matches a.example.com, not notexample.com
DomainKeyword(needle) Its domain contains needle
DomainFull(name) Its domain is exactly name
DomainRegex(DomainRegexSet) Its domain matches any of the set’s patterns
GeoIp(code) Its destination is an IP in the geoip set code
Cidr(IpCidr) Its destination is an IP in the range
SourceCidr(IpCidr) The client’s own address is in the range
PortRange(lo, hi) Its destination port is between lo and hi, both included
Network(TargetNetwork) It is TCP, or UDP
InboundTag(tag) It arrived on the inbound tag

Domain matchers try both the domain the request named and a domain sniffed from the payload, each lower-cased once per flow. The DomainSuffix, DomainKeyword and DomainFull values are compared as written, so a front end writes them in lower case, as etemenanki-app does. A destination given as an IP matches no domain matcher unless a domain was sniffed, and Cidr and GeoIp never match a destination given as a domain. Each matcher reads one field of the router’s RouteTarget (environment/src/routing.rs): SourceCidr reads source, Network reads network and InboundTag reads inbound_tag, and a matcher whose field the router was not given does not match.

DomainRegexSet compiles a rule’s patterns into one regex::RegexSet, or several when one set would exceed the engine’s size limit (regexes_too_big_for_one_set_still_build). DomainRegexSet::new rejects a set with an invalid pattern and names it: invalid domain regex "(unclosed": regex parse error:, followed by the regex crate’s own explanation. RegexSet has no equality, so DomainRegexSet defines it on the source patterns: two rules are the same rule when they were spelled the same way, in the same order (domain_regex_equality_is_by_pattern). The matching rules are on Routing.

supervisor/src/topology/spec_plan/route.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BalancerSpec {
pub tag: CompactString,
/// Outbound tags, in priority order.
pub members: Vec<CompactString>,
pub strategy: Strategy,
/// How often each member is probed.
pub probe_interval: Duration,
/// How long one probe may take.
pub probe_timeout: Duration,
}
supervisor/src/topology/balancer.rs
pub const DEFAULT_PROBE_INTERVAL: Duration = Duration::from_secs(30);
pub const DEFAULT_PROBE_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
/// The first healthy member in configured order. Order is the priority.
Failover,
/// Each healthy member in turn.
RoundRobin,
}
impl Strategy {
pub fn parse(s: &str) -> io::Result<Self>;
}
  • A balancer’s tag shares the outbound namespace: a route names a balancer exactly as it names an outbound.
  • members are outbound tags only, in priority order. Each must exist and have a TCP probe target (see OutboundProtocolSpec), and the list must not be empty (balancer <tag> has no members).
  • Strategy::parse accepts failover and round_robin, and refuses anything else with unknown balancer strategy "random" (expected "failover" or "round_robin"). etemenanki-app prefixes it with the balancer: balancer pool: unknown balancer strategy "random" (expected "failover" or "round_robin"). It lowers Failover when the config names no strategy.
  • DEFAULT_PROBE_INTERVAL (30 s) and DEFAULT_PROBE_TIMEOUT (5 s) are what a front end fills when its source names no interval or timeout. The spec has no default.
  • Prepare builds a balancer from its spec with Member::new(tag, target, probe) for each member, where target is the member’s built outbound and probe its probe_target, and then Balancer::new(members, strategy). Balancer::new refuses an empty list (a balancer needs at least one outbound), which validation has already refused as balancer <tag> has no members.

Probing, selection and what happens when every member is down are on Outbounds.

supervisor/src/topology/spec_plan/dns.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DnsSpec {
/// One resolver for everything: the proxy servers the outbounds dial and
/// the destinations this host reaches itself. Applications' DNS is
/// routed like any other flow.
Single(Backend),
/// Resolution split by what is being looked up, with applications' DNS
/// answered by the supervisor itself.
Split {
/// Asked directly. They look up the proxy servers, and the
/// `through_proxy` servers' own names.
pre_proxy: Vec<Backend>,
/// Reached through the route table. They look up everything else,
/// including every query applications send. Empty means the
/// `pre_proxy` servers answer those too.
through_proxy: Vec<Backend>,
/// Answer from the system hosts file before asking any server.
use_hosts: bool,
/// Whether `AAAA` answers are returned to applications.
resolve_ipv6: bool,
},
}
protocols/src/dns/mod.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Backend {
/// The host resolver (getaddrinfo). The default.
System,
Udp(ServerAddr),
Tls { server: ServerAddr, server_name: CompactString, ca_pem: Option<Vec<u8>> },
Https { server: ServerAddr, host: CompactString, path: CompactString, ca_pem: Option<Vec<u8>> },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ServerAddr {
Ip(SocketAddr),
Name { host: CompactString, port: u16 },
}

build/dns.rs → build_dns turns a DnsSpec into two resolvers and, for Split, a DNS service:

Looked up Single(backend) Split
The proxy servers the outbounds dial (Dns::servers) backend the pre_proxy servers, asked directly
The destinations this process reaches itself, through freedom and inside a WireGuard tunnel (Dns::destinations) the same resolver, so the outbounds share one cache the through_proxy servers, reached through the route table; the pre_proxy servers when through_proxy is empty
Applications’ own DNS queries routed like any other flow answered by the supervisor’s DNS service from the Dns::destinations resolver of the row above; AAAA answers only when resolve_ipv6
The system hosts file read only through getaddrinfo, by Backend::System; a server backend gets no hosts entries read by the supervisor and answered from first, for both resolvers, when use_hosts

In Single, Backend::System builds Resolver::system(), and any other backend a Resolver::with_upstreams whose ResolverOptions set only socket, the supervisor’s socket policy: no hosts entries, no dialer of its own and no bootstrap. Split builds with_upstreams resolvers from its lists, the through_proxy one dialling through the plane with the pre_proxy one as its bootstrap. With use_hosts, build_dns reads the system hosts file (Hosts::system()) and gives it to both resolvers; a failure to read it fails the build as building dns failed: <error>. A server named by address is a ServerAddr: ServerAddr::new(host, port) makes an IP literal Ip and anything else Name. A CA for a DNS-over-TLS or DNS-over-HTTPS server is carried as bytes (ca_pem), like every other certificate in a spec. etemenanki-app lowers [dns] into Single (a_dns_backend_needs_what_it_names) and a subscribe file’s [dns] into Split (a_subscribe_file_splits_dns). Resolution and the service are on DNS and DNS resolver.

supervisor/src/entity/user.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserSet<U: UserId> {
pub tag: CompactString,
pub users: BTreeMap<U, UserSpec>,
}
/// One user: every credential they may present. Who they are is their key
/// in the UserSet.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserSpec {
pub credentials: Credentials,
pub speed_limit: Option<std::num::NonZeroU64>,
pub device_limit: Option<std::num::NonZeroU32>,
}

Users are their own resource. On a server they change far more often than anything else, and a panel may hold many of them, so they are not part of any inbound: an inbound names a set by tag, and a set can be replaced without reconciling listeners, outbounds or routes. A set is keyed by U, so a change to one user is a change to one entry, and two sets can be compared entry by entry.

speed_limit is the payload bytes per second shared by every flow of the user, in both directions, TCP and UDP. The bucket allows a one-second burst; a transfer is charged in full after it moves, and nothing else of the user’s moves until the debt is repaid. None is unlimited, and zero cannot be written. A speed-limit change keeps the user’s sessions, because a user is admitted by credential and the credential did not change (a_speed_limit_change_keeps_the_users_sessions). The same U in two sets is one user to the supervisor, and the smallest limit any set gives applies; None in one set does not lift a limit another set gives (supervisor.rs → user_limits). Pacing is on Tracking.

device_limit is how many client IPs may have a session of the user at once. Once the user’s bound sessions come from that many IPs, a session from any other IP is refused at its first flow, when it would bind to the user (entity/session.rs → Session::admit): its token is cancelled, and the flow fails with the user's sessions already come from as many IPs as their device limit. A session without a client IP, such as one on a Unix socket, is not counted. The IP counted is the session’s source: the peer, the client a PROXY header names, or on a WebSocket inbound with real_ip the client a trusted proxy names. Lowering the limit closes no session. None admits any number of IPs, zero cannot be written, and the smallest limit any set gives applies, as for speed limits.

user_limits works out both limits while the users are staged; publish_limits hands the speed limits that changed to the tracker and replaces every device limit (Sessions::set_device_limits). Both are published only at commit, of an apply or a user edit, so a refused one leaves the live limits as they were. Device limits are on Users and sessions.

supervisor/src/entity/user.rs
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Credentials {
/// For VLESS and VMess.
pub uuid: Option<Uuid>,
/// For Trojan, AnyTLS and Shadowsocks.
pub password: Option<Secret<String>>,
/// For Shadowsocks 2022: the user's PSK, decoded.
pub ss2022_psk: Option<Secret<Vec<u8>>>,
/// For SOCKS, HTTP and Hysteria 2.
pub account: Option<Account>,
}
impl Credentials {
pub fn get(&self, kind: CredentialKind) -> Option<Credential>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CredentialKind { Uuid, Password, Ss2022Psk, Account }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Credential {
Uuid(Uuid),
Password(Secret<String>),
Ss2022Psk(Secret<Vec<u8>>),
Account(Account),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Account {
pub user: CompactString,
pub pass: Secret<String>,
}

A user holds at most one credential of each kind, and an inbound reads the one kind its protocol takes, so one user can be admitted by inbounds of different protocols. Credentials::get(kind) returns that one credential as a Credential, the form admission and the user tables work with.

Inbound protocol CredentialKind Field read
SOCKS, HTTP Account account
Hysteria 2 with Hysteria2UserAuth::Account Account account
Hysteria 2 with Hysteria2UserAuth::Password Password password
Trojan, AnyTLS, Shadowsocks Password password
VLESS, VMess, sss Uuid uuid
Shadowsocks 2022 Ss2022Psk ss2022_psk
TUN none nothing: a TUN device admits no users

Within one inbound, no two users may present the same credential of the kind it reads: users <a> and <b> present the same credential, or … the same username for accounts. Credentials of other kinds may be shared (a_shared_credential_of_another_kind_is_no_conflict). Admission, principals and what happens to a user’s sessions when their credential changes are on Users and sessions.

supervisor/src/entity/user.rs
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UserName {
Email(CompactString),
Username(CompactString),
}
impl UserName {
pub fn as_str(&self) -> &str;
}
impl fmt::Display for UserName; // the bare name, without the variant
pub trait UserId: Clone + Eq + Ord + Hash + Debug + Send + Sync + 'static {
/// The name nodes surface for this user.
fn name(&self) -> UserName;
}
impl UserId for UserName {
fn name(&self) -> UserName { self.clone() }
}
  • UserName is what a node names a user by: the email a user is configured with, or the username of an account. It is never a secret, so it is what logs and the protocols’ own user labels carry, and what admission errors print. The derived Ord sorts every Email before every Username.
  • UserId is what a front end keys its users by: whatever it already calls them. It is the key of a UserSet and the subject of a UsageDelta. Two properties make a good one. It is not a credential, because its name() is what logs, protocol labels and usage reports carry. And it stays the same when the user’s credentials change, so a password change is an edit of one entry rather than one user removed and another added. The code’s doc puts it as tied to the user’s auth info, either derived from it or deriving it: etemenanki-app keys users by their email or account name and uses UserName itself; katana keys users by the panel’s numeric id, and Uid::name returns UserName::Username of that id in decimal.
  • Inside the supervisor, each user is identified by the supervisor’s own entity::id::UserKey, a u64 newtype that connections carry instead of U. It is unrelated to etemenanki-app’s lower::UserKey. See Users and sessions and Usage.

etemenanki-app keys a Trojan, AnyTLS, Shadowsocks, Shadowsocks 2022, VLESS, VMess or sss user by its email, a SOCKS or HTTP account by its user name, and a Hysteria 2 user by its email when it has one and its user name otherwise (users_are_keyed_by_the_name_the_config_gives_them). Because the key is a name and never a credential, a user whose password changes is the same entry of its set with new credentials, rather than one user removed and another added (a_user_keeps_its_key_when_its_password_changes).

supervisor/src/policy.rs
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum UserRemovalPolicy {
Keep,
#[default]
Close,
CloseAfter(Duration),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum DrainPolicy {
#[default]
Keep,
Close,
CloseAfter(Duration),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Policies {
pub user_removal: UserRemovalPolicy,
pub drain: DrainPolicy,
/// Ban a client IP that fails authentication too often. `None` never
/// bans.
pub ip_ban: Option<IpBanPolicy>,
/// Close a gRPC connection once this many of its streams in a row have
/// failed authentication. `None` never closes one for that.
pub carrier_max_failures: Option<NonZeroU32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IpBanPolicy {
pub max_failures: NonZeroU32,
pub window: Duration,
pub duration: Duration,
}
Policy Applies to Default, and why Override Resolved by
UserRemovalPolicy A user’s live sessions when the user is removed from a set, or their credentials change Close: revoking access is meant to take effect InboundSpec.user_removal supervisor.rs → removal_policy: the inbound’s override, else Policies.user_removal
DrainPolicy The live flows of an outbound version that is removed, or replaced by a new version Keep: a TCP flow cannot migrate to the successor mid-stream, so closing it would only turn a config change into dropped connections OutboundSpec.drain topology/spec_plan/plan.rs → plan (its drain_policy closure): Close when the running spec had a WireGuard outbound with that tag; else the override of the outbound with that tag in the desired spec, or in the old spec when the tag was removed; else Policies.drain
ip_ban (IpBanPolicy) Client IPs that fail authentication None: no IP is ever banned none guard.rs → Guard, which takes the policy at every commit (Guard::set_policy)
carrier_max_failures A gRPC connection whose streams fail authentication None: no connection is closed for that none guard.rs → Report::failed, counted in carrier.rs → Carrier, one per gRPC connection

What each variant has the removal or the drain do:

Variant UserRemovalPolicy DrainPolicy
Keep Closes none of the user’s live sessions Closes none of the old version’s flows; new flows go to the successor
Close Closes them at once Closes them at once (drain → Target::close_flows)
CloseAfter(grace) Closes those still open after grace Closes those still open after grace, from a task on the supervisor’s task tracker that the root token cancels at shutdown
  • A removed outbound drains under its own override from the old spec (a_removed_outbound_drains_under_its_own_policy). A changed outbound drains its old version under the desired spec’s override or default (a_drain_takes_the_supervisor_default_unless_the_outbound_overrides_it).
  • A WireGuard version is the exception: a replaced or removed one is always drained with Close, whatever the policy. Its flows do not keep its device alive, and it could not run beside a successor on the same key and peer (policy.rs → DrainPolicy, a_wireguard_version_is_always_drained_with_close). Only the drained version’s protocol decides, so an outbound turned into WireGuard drains its old version under its own policy (an_outbound_turned_into_wireguard_keeps_its_policy).
  • ip_ban: a failed authentication counts against the IP it came from; an IPv6 client’s whole /64 counts as one IP, and an IPv4-mapped address as its IPv4 address. Once max_failures have counted within window of the first, the IP is banned for duration: a new TCP connection from it is dropped before its transport handshake, and its connections already open run on. A window or a ban that is over starts the count afresh, and a successful authentication clears the IP’s count but not a ban. Turning bans off lifts every ban. On a WebSocket inbound with real_ip, the IP is the client a trusted proxy names, and a WebSocket stream named for a banned one is dropped right after the upgrade.
  • carrier_max_failures: once that many streams of one gRPC connection in a row have failed authentication, the connection is closed; a success resets the count.
  • plan does not compare Spec.policies, so a spec that changes only the defaults builds nothing. An apply resolves its drains and removals with the desired spec’s defaults, and a user edit with the running spec’s, so new defaults govern the apply that brings them and every later apply and user edit. Every commit of an apply hands ip_ban and carrier_max_failures to Guard::set_policy, so those take effect at the commit that brings them.
  • The overrides are fields of InboundSpec and OutboundSpec, so changing one is a change of that inbound or outbound: the inbound’s handler is rebuilt and swapped, and the outbound is rebuilt as a new version.
  • etemenanki-app lowers Policies::default() with no overrides. katana sets ip_ban and carrier_max_failures from [node.controller] and leaves the rest at their defaults: ip_ban from ban_max_failures, ban_window_secs and ban_secs, only while enable_device_limit is on and ban_max_failures is not zero, and carrier_max_failures from the key of that name. Only an embedder that builds its own spec sets the other policies or an override.

How drains and removals run is on Reconcile and Users and sessions, and how streams report to the guard is on Serving.

The supervisor keeps the spec it last applied as RunningState.spec. An apply does not diff two configs; it compares two specs. Because every value is typed, a difference between two specs is a difference in meaning, never in spelling, and the planner decides what to keep without knowing which front end wrote either spec. plan compares resource by resource, not the spec as a whole:

Resource Matched across applies by Compared When unequal
DNS there is one the whole DnsSpec Build(Dns), and every outbound but a blackhole is rebuilt too
Outbound tag the whole OutboundSpec, drain included Build of the next version; the old version gets a Drain step
Balancer tag the whole BalancerSpec, and whether any member was rebuilt Build; health is learned afresh (a_balancer_is_rebuilt_exactly_when_one_of_its_members_is)
Route there is one the whole RouteSpec Build(Route), and nothing else is rebuilt (a_route_change_alone_rebuilds_only_the_route_table)
User set tag the whole UserSet Build(UserSet), which the apply lists in ApplyReport.built. Prepare does not act on that step: it recomputes every inbound’s admissions and rebuilds the user table of each running inbound whose admitted users, principals or credentials differ (supervisor.rs → same_admissions). A set change that leaves them equal, such as a speed_limit or device_limit change or a user without the credential kind the inbound reads, rebuilds no table.
Inbound each of its binds on its own (InboundSpec::binds(): its bind, then its handoff socket) the whole InboundSpec, tag, handoff, proxy_protocol and real_ip included, against the running inbound on the same bind Each bind no running inbound holds gets Bind, and each running bind no desired inbound holds gets StopAccepting. Then Build, with SwapHandler when any of its binds is already bound: a change on the same bind, or to its handoff socket alone, is Build and SwapHandler; a move to a new bind with no handoff socket kept is Bind and Build. Disrupt is added for a TUN inbound that changes on the same bind, for a TUN inbound on a new bind whose tag named a TUN inbound before (the device changed), and for a Hysteria 2 obfs change.
Policies not compared — read when drains and removals are resolved; the guard’s policies take effect at every commit

A running inbound whose tag no desired inbound carries gets CloseSessions, so renaming an inbound on the same bind swaps its handler and closes the sessions of its old tag (an_inbound_renamed_on_the_same_bind_swaps_and_closes_the_old_tags_sessions); see Reconcile. Any rebuilt DNS, outbound, balancer or route, or a removed outbound or balancer, adds one PublishPlane (removing_a_balancer_alone_republishes_the_plane). An unchanged spec plans only Reuse steps and publishes nothing (an_unchanged_spec_reuses_everything_and_publishes_nothing). The full planning rules are on Reconcile.

Equality is structural: every spec type derives PartialEq and Eq, except two whose equality is written by hand. Secret is listed as well: it derives equality, so a changed secret is a changed spec, and hides only its Debug.

Type Impl Equal when Why Pinned by
Secret<T> derived The contents are equal A changed secret must be a changed spec a_user_keeps_its_key_when_its_password_changes, which asserts the user’s entry differs
SuppliedTun by hand Same raw descriptor number and same mtu OwnedFd has no equality, and an open descriptor’s number is unique while it is open none
DomainRegexSet by hand Same source patterns, in the same order RegexSet has no equality; a rule is the rule it was spelled as domain_regex_equality_is_by_pattern

Typing removes differences of spelling before the comparison: a UUID parses to the same Uuid in upper or lower case, a base64 key decodes to the same bytes, a method name becomes one enum value, and etemenanki-app lower-cases route domains (Example.COM becomes DomainSuffix("example.com") in a_server_config_lowers_to_its_spec).

Order matters where the spec says it does:

List Order compared by the plan Why
Spec.inbounds, outbounds, balancers, user_sets no each is found by its bind or tag
UserSet.users no a BTreeMap
RouteSpec.rules yes the first match wins
RouteRuleSpec.matchers yes, though matching does not depend on it Vec equality
BalancerSpec.members yes priority order
DnsSpec::Split server lists yes Vec equality
Ss2022.identity_psks yes the identity chain’s order
WireguardSpec.addresses, DeviceSpec.addresses and routes yes Vec equality
PortHopping.ports yes Vec equality; etemenanki-app lowers the ports in ascending order, each once
RealityServerSpec.server_names and short_ids yes Vec equality
Anytls.padding_scheme yes the scheme’s lines, in order
An sss chain (SssChain) yes a chain’s transforms apply in order
  • Determinism. Lowering the same input twice must give equal specs (lowering_twice_gives_the_same_spec), or every apply rebuilds what did not change. A random value, a timestamp or a HashMap iteration order inside a spec defeats the planner.
  • Consistent fields the builder ignores. The builder overrides ProxyUpstream.server.network with TCP, and Hysteria2OutboundSpec.server.network and WireguardSpec.endpoint.network with UDP. A spec that differs only there is unequal, so the outbound is rebuilt although nothing about it changed. etemenanki-app always writes the network the builder uses (semantic_problems_are_left_to_the_supervisor asserts DialNetwork::Udp for both a WireGuard endpoint and a Hysteria 2 server).
  • Certificate and key bytes. A front end reads certificates, keys and CA bundles itself and puts their bytes in the spec: ServerTlsSpec, VerifyMode::CustomCa and a DNS backend’s ca_pem.
  • Users by name. An inbound names its set by tag, so a user change leaves every InboundSpec equal and touches neither the plane nor the listeners (a_user_set_change_alone_touches_neither_the_plane_nor_the_inbounds).
Invariant Enforced by Pinned by
Two specs compare with ==, resource by resource Derived equality, plus the hand-written impls of SuppliedTun and DomainRegexSet an_unchanged_spec_reuses_everything_and_publishes_nothing, lowering_twice_gives_the_same_spec
A value wrapped in Secret never reaches Debug output Secret’s hand-written Debug a_spec_never_prints_a_credential
Tags are unique within their namespace; outbounds and balancers share one validate → unique_tags and the collision check tags_are_unique_within_each_kind, a_balancer_may_not_share_an_outbounds_tag, an_inbound_may_share_an_outbounds_tag
No spec tag is empty unique_tags no_tag_may_be_empty
Every tag reference resolves, and a balancer member is an outbound validate a_route_rule_must_name_a_known_outbound_or_balancer, the_route_default_must_name_a_known_outbound_or_balancer, a_balancer_member_must_be_a_known_outbound, a_balancer_member_may_not_be_another_balancer, an_inbound_must_name_a_known_user_set
No two binds are the same or share a socket path, an inbound’s own bind or a handoff socket validate two_inbounds_may_not_share_a_bind, the_same_port_over_udp_is_another_bind, two_binds_may_not_share_a_socket_path
Each protocol is served only on its kind of bind validate_inbound each_protocol_is_served_only_on_its_kind_of_bind
TLS material is present exactly when the shape layers TLS, on both sides inbound_transport, outbound_transport an_inbound_carries_tls_material_exactly_when_its_shape_layers_tls, an_outbound_carries_tls_settings_exactly_when_its_shape_layers_tls
Every protocol that takes a transport meets the same transport rules inbound_transport, run for whatever InboundProtocolSpec::transport() returns every_protocol_meets_the_same_transport_rules
REALITY settings are present exactly with the REALITY shape, and hold together reality_settings, outbound_transport reality_field_must_match_its_shape, reality_settings_fail_closed, a_reality_outbound_validates_and_its_field_must_match
REALITY rides under VLESS only, on both sides inbound_transport, validate_outbound reality_is_vless_only, a_reality_outbound_is_vless_only
VLESS Vision rides only over REALITY, on both sides inbound_transport, validate_outbound none: no test refuses it at this revision
AnyTLS runs over TLS only, with a user set on the inbound and a password on the outbound validate_inbound, validate_outbound anytls_needs_tls_a_user_set_and_a_scheme_that_parses, an_anytls_outbound_needs_tls_and_a_password
SOCKS serves UDP ASSOCIATE only over plain TCP validate_inbound socks_relays_udp_over_plain_tcp_only
An outbound’s shape has no gaps; an inbound’s may outbound_transport an_outbound_needs_a_ws_host_and_a_grpc_authority, an_inbound_needs_no_ws_host_or_grpc_authority
A Unix listener carries only plain TCP validate_inbound a_unix_listener_carries_only_the_plain_tcp_shape
Trojan, VLESS, VMess, AnyTLS and sss always admit a user set validate_inbound → needs_users trojan_vless_and_vmess_need_a_user_set, anytls_needs_tls_a_user_set_and_a_scheme_that_parses, validation_refuses_bad_limits (supervisor/tests/sss.rs)
A Hysteria 2 inbound has exactly one of a shared password and a user set validate_inbound hysteria2_takes_a_shared_password_or_a_user_set_but_not_both
Hysteria 2 QUIC settings stay within upstream’s limits, on both sides hy2_quic hysteria2_quic_settings_stay_within_upstreams_limits
Port hopping names real ports and hops no more often than every 5 s port_hopping hysteria2_port_hopping_needs_ports_and_a_sane_interval
Only a TCP bind reads a PROXY protocol header validate_inbound none
Only a WebSocket transport on a TCP bind reads a real-IP header, under a valid header name, from at least one trusted network validate_inbound → real_ip_header a_real_ip_header_needs_a_websocket_a_trusted_proxy_and_a_name
A handoff socket stands only beside a stream protocol’s TCP bind, never as an inbound’s own bind, at an absolute path of at most 107 bytes, with permission bits alone as its mode validate_inbound → handoff_socket, and the pairing for a Handoff bind a_handoff_socket_stands_beside_a_stream_inbounds_tcp_listener, a_handoff_bind_is_no_inbounds_own, a_handoff_socket_is_an_absolute_path_a_unix_socket_can_take, a_handoff_sockets_mode_is_permission_bits_alone
Within an inbound, one credential has one owner validate_admission two_users_may_not_present_the_same_credential, hysteria2_usernames_collide_case_insensitively
Users are keyed by a name, never a credential etemenanki-app’s InlineUsers users_are_keyed_by_the_name_the_config_gives_them, a_user_keeps_its_key_when_its_password_changes
etemenanki-app’s lowering is faithful: it leaves the supervisor’s rules to the supervisor app/src/lower.rs → lower semantic_problems_are_left_to_the_supervisor
A spec that breaks a rule is refused before anything is planned, and changes nothing plan calls validate first an_invalid_spec_is_refused_without_a_plan, a_refused_spec_changes_nothing
An unchanged outbound is carried over with its connection plan emits Reuse an_unchanged_hysteria2_outbound_keeps_its_quic_connection_across_apply
A replaced or removed WireGuard version drains with Close plan’s drain_policy closure a_wireguard_version_is_always_drained_with_close

A spec itself cannot fail: it is data with public fields. The failures around it happen in these places.

Where What fails Text
Lowering, in the front end Parsing, reading files, what a spec cannot express, and the few spec helpers that validate their input DomainRegexSet::new: invalid domain regex "<pattern>": <regex error>, or domain regex set of <n> patterns: <error> when no single pattern is at fault. Strategy::parse: unknown balancer strategy "<s>" (expected "failover" or "round_robin"). normalise_psk: shadowsocks-2022: PSK too short (<n> < <k>). SssChain::parse for an sss chain, and the REALITY decoders for a key, a shortId or a client version. etemenanki-app’s own refusals include config defines no outbounds, <ctx>: udp_idle_timeout is set but udp is not enabled, <ctx>: unknown obfs "<obfs>" (expected "salamander") and <ctx>: unknown congestion "<name>" (expected "cubic", "bbr" or "reno"). The front end reports these as its own errors.
Validation A rule the types cannot carry An ApplyError: duplicate <kind> tag <tag>, <from> references unknown <kind> <tag>, balancer <tag> has no members, balancer <tag>: outbound <member> has no upstream a TCP health probe can reach, or <resource>: <reason> for Invalid
Disruption check, after planning A plan with a Disrupt step, applied without allow_disruptive ApplyError::Disruptive: inbound <tag>: <reason>; this ends its live connections and needs allow_disruptive
Build, during prepare Construction from a valid spec: a PEM that does not parse, geo data that cannot be read, a CA that does not load, a hosts file that cannot be read ApplyError::Build: building <resource> failed: <source>, for example building inbound t failed: no certificate in PEM bundle
Binding, during prepare The listener of a new bind cannot be opened, a handoff socket’s directory, mode or group included ApplyError::Bind: inbound <tag>: binding <bind> failed: <source>, with the bind in the Display form of BindSpec
The actor A supervisor that has shut down, or a user edit naming no set ApplyError::Stopped: the supervisor has shut down; user set <tag>: no such user set

validate checks in a fixed order, and the first failure is the error: user-set tags, outbound tags, balancer tags, a balancer tag that is also an outbound’s, inbound tags; then each outbound; then each balancer (not empty, every member an outbound, every member probeable); then each route rule and the route default; then each inbound in turn: its binds (its own, then its handoff socket) against the binds before them, its user set’s existence, its own fields, and the admission of its set. The rules one by one are on Validation.

The builders also turn a few specs that validation refuses into errors of their own rather than panics: missing tls, missing ws host, missing grpc authority and missing reality from build/outbound.rs → transport_connector, and missing tls, missing reality settings and inbound <tag>: protocol and bind do not match from build/inbound.rs. A spec reaches a builder only after validate, so these are not met through apply, update or check.

Invalid and Build print the resource with entity/id.rs → Resource’s Display:

Resource Printed as
Inbound(tag) inbound <tag>
Outbound(id) outbound <tag>@v<version>
Balancer(tag) balancer <tag>
UserSet(tag) user set <tag>
Route route
Dns dns

DuplicateTag and UnknownReference print the kind with ResourceKind’s Display: inbound, outbound, balancer or user set.

A refused spec is refused whole, and the running state keeps running exactly as it was (a_refused_spec_changes_nothing). That holds for update too: the actor edits a clone, so a closure whose result is refused leaves RunningState.spec unchanged. A refused user edit stores no table and leaves the running spec as it was. etemenanki-app prints these errors after configuration invalid: under --test, and after failed to start: when the first spec is refused. The complete list of rules and texts is on Validation.

A spec spawns nothing and holds no task, so there is nothing to cancel. Dropping a spec frees memory, and closes a supplied TUN descriptor when the spec held its last reference.

Bounds the supervisor enforces on a spec:

Constant or rule Value Defined in Applies to
MIN_TUN_MTU 1280 build/validate.rs TunSource::mtu(), both sources
HY2_UDP_IDLE 2 to 600 s, inclusive build/validate.rs Hysteria2InboundSpec.udp_idle_timeout when set
MIN_SALAMANDER_PSK 4 bytes build/validate.rs ObfsSpec::Salamander.psk, on both sides
Shadowsocks 2022 key length 16 bytes for Blake3Aes128Gcm, 32 for the other two methods protocols/src/ss_2022/crypto.rs → Method::key_len Exact for the server psk and every outbound key; at least that for a user’s ss2022_psk, longer keys folded
Counts at least 1 build/validate.rs max_connections, max_circuits, max_concurrent_streams
Masquerade status 100 to 999, except 233 protocols/src/hysteria/server/masquerade.rs → Masquerade::new MasqueradeSpec.status
Shadowsocks 2022 with users an AES-GCM method protocols/src/ss_2022/users.rs → Validator::from_config, at build InboundProtocolSpec::Ss2022.method when the inbound admits users
Key sizes 32 bytes the types WireGuard private, public and pre-shared keys
reserved 3 bytes the type WireguardSpec.reserved
Speed limit 1 byte/s or more NonZeroU64 UserSpec.speed_limit
Device limit 1 IP or more NonZeroU32 UserSpec.device_limit
Failure counts 1 or more NonZeroU32 IpBanPolicy.max_failures, Policies.carrier_max_failures
QUIC receive windows 16,384 (QuicOptions::MIN_RECEIVE_WINDOW) to 2^62 − 1 (QUIC_VARINT_MAX) bytes protocols/src/hysteria/config.rs, build/validate.rs quic.stream_receive_window and conn_receive_window, on both sides
HY2_IDLE_TIMEOUT 4 to 120 whole seconds build/validate.rs quic.max_idle_timeout, on both sides
HY2_KEEP_ALIVE 2 to 60 whole seconds build/validate.rs quic.keep_alive_period when set, on both sides
Incoming streams at least 1 when set build/validate.rs quic.max_incoming_streams
Hop ports at least one, none of them 0 build/validate.rs PortHopping.ports
HopInterval::MIN_CONFIGURED 5 s shinku-quic/src/hop.rs the shortest interval of PortHopping.interval
MAX_SOCKET_PATH 107 bytes build/validate.rs HandoffSpec.path
Handoff socket mode permission bits only, 0o777 at most build/validate.rs HandoffSpec.mode
MAX_SCHEME_LEN 2,048 bytes, once the lines are joined protocols/src/anytls/padding.rs Anytls.padding_scheme
sss max_datagram at least 1 build/validate.rs → sss_limits both sss settings
MAX_FLUSH_DELAY 10 ms protocols/src/sss/transform/stream.rs both sss settings’ flush_delay
sss header_timeout above zero build/validate.rs SssServerSettings.header_timeout
Key and shortId sizes 32 and 8 bytes the types RealityServerSpec, RealityClientSpec

Defaults the front ends fill in. The spec has none of its own, apart from the Default of Policies, Credentials, Hysteria2UserAuth, AddressFamilyStrategy, BandwidthConfig (no rates), Fallback (Cubic) and HopInterval (30 s):

Constant Value Defined in Field
DEFAULT_MAX_CONNECTIONS 262,144 protocols/src/hysteria/server/config.rs Hysteria2InboundSpec.max_connections
DEFAULT_MAX_CIRCUITS 4,194,304 protocols/src/hysteria/server/config.rs Hysteria2InboundSpec.max_circuits
DEFAULT_MAX_CONCURRENT_STREAMS 102,400 protocols/src/hysteria/config.rs Hysteria2OutboundSpec.max_concurrent_streams
QuicOptions::server() 8 MiB stream window, 20 MiB connection window, 30 s idle timeout, no keep-alive, max_incoming_streams 65,536 protocols/src/hysteria/config.rs Hysteria2InboundSpec.quic
QuicOptions::client() 8 MiB stream window, 20 MiB connection window, 30 s idle timeout, 10 s keep-alive, quinn’s default for incoming streams protocols/src/hysteria/config.rs Hysteria2OutboundSpec.quic
Fallback::Cubic CUBIC shinku_quic::congestion::Fallback congestion, on both sides
HopInterval::DEFAULT 30 s shinku-quic/src/hop.rs PortHopping.interval
tun::DEFAULT_MTU 1500 protocols/src/tun/config.rs DeviceSpec.mtu, SuppliedTun.mtu
DEFAULT_UDP_IDLE_TIMEOUT 60 s protocols/src/tun/config.rs TunSpec.udp_idle_timeout
DEFAULT_MAX_FLOWS 65,536 protocols/src/tun/config.rs TunSpec.max_flows
wireguard::DEFAULT_MTU 1420 protocols/src/wireguard/config.rs WireguardSpec.mtu
DEFAULT_PROBE_INTERVAL 30 s supervisor/src/topology/balancer.rs BalancerSpec.probe_interval
DEFAULT_PROBE_TIMEOUT 5 s supervisor/src/topology/balancer.rs BalancerSpec.probe_timeout
DEFAULT_IDLE 30 s protocols/src/anytls/client/mod.rs AnytlsOutboundSpec.idle_session_check_interval and idle_session_timeout
SssServerSettings::default() identity chains, max_datagram 65,535, header_timeout 10 s (DEFAULT_HEADER_TIMEOUT), Drain, flush_delay 0 protocols/src/sss/server.rs InboundProtocolSpec::Sss.settings

The limits that apply at run time (live connections, handshakes, buffers) are on Serving and the pages it links to.

A new field, variant or protocol touches the spec first. On the spec side:

  1. Add the field or variant with #[derive(Debug, Clone, PartialEq, Eq)]. Type the value: an enum rather than a string, decoded bytes rather than text, bytes rather than a path. Hold a secret value in Secret. A type without Eq needs a hand-written PartialEq that says what “the same” means, as DomainRegexSet and SuppliedTun do.
  2. Put every rule about how the value fits the rest of the spec in build/validate.rs, and add a test to supervisor/tests/unit/validate.rs that breaks exactly that rule in the otherwise valid spec() fixture, plus the edge that is still accepted. A front end checks only what its own format can say and a spec cannot.
  3. Consume the value in its builder: build/inbound.rs, build/outbound.rs, build/dns.rs or build/route.rs. Most matches over the spec’s enums are exhaustive, so the compiler lists the places to extend. One has a catch-all arm that a new variant falls into silently, one exhaustive match decides more than it seems to, and a credential kind spans several files:
    • build/validate.rs → validate_outbound picks the transport to check with _ => None, so a new outbound variant that carries a ProxyUpstream is never checked for TLS settings, a WebSocket host, a gRPC authority, REALITY settings or REALITY’s VLESS-only rule until it is added there. probe_target is exhaustive and needs a decision for it too.
    • On the inbound side the transport is InboundProtocolSpec::transport(), an exhaustive match: a new variant must say whether it carries a transport, and then meets the transport rules and is built over its transport without further code.
    • A new credential kind touches CredentialKind, Credential, the Credentials field and Credentials::get in entity/user.rs, build/users.rs → credential_kind and kind_of, the identity match in build/validate.rs → validate_admission, and the user table in build/inbound.rs → user_table.
  4. If a running listener cannot take the change in place, return a reason from topology/spec_plan/plan.rs → swap_disruption, and pin it in supervisor/tests/unit/plan.rs.
  5. Lower it in every front end: app/src/lower.rs, katana’s src/lower/, and ffi/src/client.rs when it rewrites the spec. Check that lowering the same input twice still gives equal specs.

The rest of a new stream protocol (its user table, its driver, its handshake watchdog) is listed on Serving; the rest of a new outbound on Outbounds.

app/tests/unit/lower.rs pins what etemenanki-app lowers into a spec. It is compiled into the library, so run it with cargo test -p etemenanki-app --lib. Its certificate and key files hold CERT BYTES and KEY BYTES: lowering only reads them, and whether they parse is the supervisor’s to find out.

Test Behaviour it pins
a_server_config_lowers_to_its_spec Binds, including a loopback default for an absent listen; one set per inbound named by the inbound’s tag; TLS files read as bytes; the Shadowsocks 2022 server key sized to 16 bytes and a user’s key only decoded; HTTP without accounts has no set; SOCKS on a Unix socket; the masquerade defaults filled in; the WebSocket host and the SNI taken from tls.server_name; DialNetwork::Tcp for the upstream; lower-cased domains and the matcher order; DnsSpec::Single(Backend::System); Policies::default()
lowering_twice_gives_the_same_spec Lowering is deterministic
a_spec_never_prints_a_credential The Secret-wrapped values of its fixture are absent from Debug output
users_are_keyed_by_the_name_the_config_gives_them Email keys for Trojan, Shadowsocks, VLESS and VMess users; Username keys for accounts; a Hysteria 2 user by its email when it has one
a_user_keeps_its_key_when_its_password_changes Same key, unequal UserSpec
a_user_without_the_name_it_is_keyed_by_is_refused inbound <tag>: users[<i>] has no email; … and accounts[<i>] has no user
two_users_with_one_name_are_refused Both entries are named, and no credential is shown
a_subscribe_file_splits_dns, a_dns_backend_needs_what_it_names DnsSpec::Split from a subscribe file; DnsSpec::Single with a server backend
semantic_problems_are_left_to_the_supervisor The lowering is faithful: two inbounds with one tag, a TUN MTU of 1000, a Hysteria 2 inbound with both a password and users and an out-of-range idle timeout and zero circuits, two Trojan users with one password, a WireGuard family without an address of it, a Hysteria 2 outbound with an empty password and zero streams, and a rule and a balancer naming a missing outbound all reach the spec as written, for the supervisor to refuse
a_unix_listen_lowers_to_a_plain_stream A Unix listener gets StreamShape::Tcp without TLS
a_tun_inbound_lowers_to_its_device TunSource::Create with the device’s name, addresses and routes; TunSpec with the defaults
hysteria2_answers_to_its_aliases hysteria2, hysteria and hy2 all lower to Hysteria2; the outbound gets DEFAULT_MAX_CONCURRENT_STREAMS
a_vmess_inbound_can_turn_the_clock_check_off clock_check = false lowers into InboundProtocolSpec::Vmess; on a VLESS inbound the key is unknown and refused
a_wireguard_endpoint_takes_an_ipv6_literal_bracketed_or_bare, a_malformed_wireguard_endpoint_is_refused_when_the_config_loads, an_upstream_server_takes_a_bracketed_ipv6_literal A bracketed IPv6 literal in a WireGuard endpoint or an upstream server lowers to a Remote::IpAddr; brackets around anything else, or a bracketed endpoint without its port, are refused
hysteria2_takes_upstreams_bandwidth_quic_and_hopping_settings Rates, congestion, the quic block (a max_* window wins, an init_* alone stands in) and the hop ports and interval reach both Hysteria 2 specs; with hopping and no port, server takes the set’s first port
a_hysteria2_server_given_no_rates_keeps_ignoring_client_bandwidth With no rates, ignore_client_bandwidth is true, congestion is Cubic and quic is QuicOptions::server(); an explicit ignore_client_bandwidth wins
hysteria2_tuning_mistakes_are_refused A rate without a unit or with an unknown one, an unknown congestion, an init_* window above its max_*, a server keep_alive_period, a client max_incoming_streams, an unknown quic key, and inconsistent hop intervals or port lists are refused while lowering
syntax_errors_are_refused What only the TOML front end can check is refused while lowering, among them an invalid UUID, an unknown method or stream network, TLS settings on a plain stream, a missing port or certificate file, a UDP idle timeout with UDP off, obfs_password without obfs, an unknown obfs, and a config with no outbounds (config defines no outbounds)
socks_and_shadowsocks_lower_their_stream, socks_and_shadowsocks_on_a_unix_socket_refuse_a_stream SOCKS and Shadowsocks of either kind read [inbound.stream] as Trojan does, and udp is lowered as written for the supervisor to judge; on a Unix socket a stream is refused
an_anytls_inbound_lowers_its_users_tls_and_padding, an_anytls_outbound_lowers_its_pool_settings An AnyTLS inbound’s password users, StreamShape::Tls with its material, and its padding lines; an unknown settings key is refused; the outbound’s pool settings
a_vless_reality_inbound_lowers_to_the_expected_spec, reality_lowering_fails_closed, a_reality_outbound_lowers_and_needs_a_public_key [inbound.stream.reality] lowers to a decoded RealityServerSpec; a bad key, no server names, no or malformed shortIds, a missing dest, port 0 and a malformed client version are refused while lowering, each naming its key; an outbound needs public_key
an_sss_inbound_lowers_to_its_spec, an_sss_inbound_defaults_to_identity_chains_and_draining, an_sss_outbound_lowers_to_its_spec, sss_mistakes_are_refused_with_their_key sss settings in both directions, their defaults, and refusals that name the key

supervisor/tests/unit/validate.rs holds one test per rule, each breaking exactly that rule in an otherwise valid spec. Its fixture spec() is a SOCKS listener on 127.0.0.1:1080, one freedom outbound tagged direct as the route default, and DnsSpec::Single(Backend::System); with_inbound and with_outbound add one resource to it. TLS material is any bytes, because validation never parses PEM. Run it with cargo test -p etemenanki-supervisor --lib. The Invariants table mixes files: its rows from “Tags are unique” to “Within an inbound, one credential has one owner” name tests of this file, and the others name tests of app/tests/unit/lower.rs, supervisor/tests/unit/plan.rs and supervisor/tests/hot_swap.rs. This file also holds:

Test Behaviour it pins
the_base_spec_is_valid The fixture passes
a_route_may_name_a_balancer A balancer tag is a valid route target
a_balancer_needs_a_member, a_balancer_member_needs_an_upstream_a_tcp_probe_can_reach EmptyBalancer; UnprobeableMember for freedom, blackhole, Hysteria 2 and WireGuard members
a_hysteria2_shared_password_may_not_be_empty, a_hysteria2_udp_idle_timeout_is_between_2_and_600_seconds, hysteria2_serves_at_least_one_connection_and_one_circuit The Hysteria 2 inbound’s ranges, including 2 and 600 accepted, 1 and 601 refused, and None accepted
a_salamander_key_is_at_least_4_bytes_on_either_side, a_masquerade_may_not_answer_with_the_success_status 3 bytes refused and 4 accepted on both sides; 233 refused and 404 accepted
a_tun_device_mtu_is_at_least_1280, socks_over_a_unix_socket_needs_a_udp_bind_to_serve_udp 1279 refused, 1280 accepted; SOCKS UDP on a Unix socket only with udp_bind
a_shadowsocks_2022_server_key_is_sized_to_its_method, shadowsocks_2022_client_keys_are_sized_to_their_method, a_shadowsocks_2022_user_key_must_normalise_to_the_method Exact server and client keys; a longer user key folded, a shorter one refused
a_wireguard_address_family_needs_an_address_of_that_family, a_hysteria2_client_needs_a_password_and_a_stream The outbound rules
hysteria2_quic_settings_stay_within_upstreams_limits A 16,383-byte stream window, a zero connection window, a 3 s or 30.5 s idle timeout, a 61 s keep-alive and zero incoming streams refused on both sides; 16,384 bytes, 120 s and 2 s accepted
hysteria2_port_hopping_needs_ports_and_a_sane_interval No ports, port 0 and a 4 s minimum interval refused; 5 s fixed and 10 to 60 s random accepted
a_shared_credential_of_another_kind_is_no_conflict, a_hysteria2_username_may_not_hold_a_colon, a_hysteria2_password_may_not_be_empty Admission rules that depend on the credential kind
every_protocol_meets_the_same_transport_rules SOCKS, HTTP, Trojan, VLESS, VMess, Shadowsocks and Shadowsocks 2022 each accept and refuse the same six transports, on TCP and Unix binds
shadowsocks_runs_over_a_transport, socks_relays_udp_over_plain_tcp_only, shadowsocks_and_socks_over_websocket_read_a_real_ip_header Shadowsocks of either kind over TLS, WebSocket and gRPC; SOCKS with udp refused over TLS, WebSocket and gRPC and accepted over plain TCP; a real-IP header on SOCKS and Shadowsocks over WebSocket
anytls_needs_tls_a_user_set_and_a_scheme_that_parses, an_anytls_outbound_needs_tls_and_a_password AnyTLS over TLS only, with a user set, a padding scheme that parses and is at most 2 KiB, and never on a Unix socket; the outbound over TLS with its TLS settings and a password
a_vless_reality_inbound_validates, reality_settings_fail_closed, reality_field_must_match_its_shape, reality_is_vless_only The REALITY inbound rules: no server name, a wildcard, no shortId, port 0, an empty host and reversed client versions refused; the field against its shape; every other protocol, AnyTLS included, refused over REALITY
a_reality_outbound_validates_and_its_field_must_match, a_reality_outbound_is_vless_only The REALITY client rules, and a Trojan outbound refused over REALITY

Other tests that pin spec semantics:

File Test Behaviour it pins
supervisor/tests/unit/plan.rs an_unchanged_spec_reuses_everything_and_publishes_nothing Equal specs plan only Reuse
supervisor/tests/unit/plan.rs an_invalid_spec_is_refused_without_a_plan plan refuses a spec that breaks a rule before it plans anything
supervisor/tests/unit/plan.rs a_user_set_change_alone_touches_neither_the_plane_nor_the_inbounds Users as their own resource
supervisor/tests/unit/plan.rs a_route_change_alone_rebuilds_only_the_route_table The route is compared as a whole, apart from the outbounds
supervisor/tests/unit/plan.rs a_balancer_is_rebuilt_exactly_when_one_of_its_members_is, removing_a_balancer_alone_republishes_the_plane Balancer equality includes its members’ rebuilds; a removal alone publishes the plane
supervisor/tests/unit/plan.rs an_inbound_renamed_on_the_same_bind_swaps_and_closes_the_old_tags_sessions An inbound is matched by bind; its old tag’s sessions close
supervisor/tests/unit/plan.rs a_tun_settings_change_on_the_same_device_swaps_and_disrupts A TUN change is a Disrupt step
supervisor/tests/unit/plan.rs a_drain_takes_the_supervisor_default_unless_the_outbound_overrides_it, a_removed_outbound_drains_under_its_own_policy How DrainPolicy is resolved
supervisor/tests/unit/plan.rs a_wireguard_version_is_always_drained_with_close, an_outbound_turned_into_wireguard_keeps_its_policy A WireGuard version drains with Close whatever the policy; only the drained version’s protocol decides
supervisor/tests/unit/plan.rs a_vmess_clock_check_change_swaps_the_handler_without_disruption clock_check is taken in place
supervisor/tests/unit/plan.rs a_dns_change_rebuilds_every_outbound_but_a_blackhole Every outbound but a blackhole holds the resolvers
supervisor/tests/unit/plan.rs a_hysteria2_masquerade_change_swaps_without_disrupting, a_hysteria2_obfs_change_swaps_and_disrupts Which Hysteria 2 fields a running listener takes in place
supervisor/tests/unit/plan.rs an_inbound_with_a_handoff_socket_binds_both_listeners, adding_or_removing_a_handoff_socket_keeps_the_tcp_listener, a_changed_handoff_socket_is_bound_anew, a_handoff_socket_passed_to_another_inbound_is_kept Each bind is compared on its own: a handoff socket added or removed keeps the TCP listener and swaps the handler; a moved or regrouped one is bound anew; one passed to another inbound is kept
supervisor/tests/handoff.rs a_reload_adds_moves_and_removes_a_handoff_socket_and_keeps_the_tcp_listener A mode change binds the socket afresh at the same path, with the new mode
supervisor/tests/hot_swap.rs an_unchanged_hysteria2_outbound_keeps_its_quic_connection_across_apply An equal outbound spec keeps its built outbound
supervisor/tests/hot_swap.rs a_speed_limit_change_keeps_the_users_sessions speed_limit is not part of admission
supervisor/tests/hot_swap.rs a_refused_spec_changes_nothing A refused spec leaves what runs untouched
environment/tests/unit/routing.rs domain_regex_equality_is_by_pattern, invalid_domain_regex_is_rejected DomainRegexSet equality and its refusal
ffi/tests/proxy.rs a_server_inbound_is_refused, only_a_local_socks_inbound_is_served refuse_servers: a VLESS inbound is refused, and SOCKS is served only on loopback
ffi/tests/proxy.rs a_tun_device_carries_tcp_and_udp_to_the_outbound supply_tun adds a TUN inbound tagged tun to a config that has none
katana tests/unit/lower/inbound.rs a_stream_node_takes_handed_over_connections_on_a_socket_named_for_its_port, a_node_on_one_address_names_its_handoff_socket_for_it, a_hysteria_node_takes_no_handed_over_connections, a_handoff_setting_the_relay_could_not_use_is_refused katana’s handoff: <handoff_dir>/<port>.sock on every address and <listen_ip>-<port>.sock on one, mode 0o660, the group’s gid; none on a Hysteria 2 node; the refused settings

SuppliedTun’s equality, BindSpec’s Display and Secret’s Debug have no test of their own in the supervisor crate; the last is covered through the app’s a_spec_never_prints_a_credential. Add a test when you change any of them. See Testing for the test layout.