English
Services
crates/api/src/services.rs.
Services is what modules publish for each other while a configuration is being built. The key is the type: at most one value per TypeId.
rust
impl Services {
fn new() -> Services;
fn of<T>(value: Arc<T>) -> Services;
fn insert<T>(&mut self, value: Arc<T>); // replaces the previous value of the type
fn get<T>(&self) -> Option<Arc<T>>;
fn require<T>(&self) -> Result<Arc<T>, String>; // "T is not available in this build"
}Why by type and not by name: the consumer depends on the provider's crate for the type anyway, and a crate dependency obliges listing the module in requires (requires) — so the provider is in the build and its unit has run earlier. A require error is possible for a service that the provider does not always publish (the cache file — only when it is enabled); such a consumer uses get and does without it.
Where services live
| Who publishes | When | Where it goes |
|---|---|---|
| the kernel | at the beginning of every build() | Arc<DialTable>, Arc<Extensions>, OwnMark, ours_clock::Clock, QuicWindowBudget |
| module units | Unit::build, in module order | the services of this configuration |
| the dispatcher factory | once in Instance::new | the instance's services |
The configuration's services are seen by: Build::services of the following units, Context::services of the inbound/outbound/rule-set factories, DnsContext::services, attach_services of DNS, outbounds and rule-sets. From outside — Instance::service::<T>(): first the services of the running configuration, then those of the instance. The table of what modules publish — Units and services.
Frequently used
| Type | Provider | Who takes it |
|---|---|---|
ours_api::DialTable | the kernel | the ntp, subscriptions units |
ours_config::dial::Dialers (publish/dialers) | dial | outbounds with a dial block (cx.dialer), DNS transports, subscriptions, adblock |
ours_api::Extensions | the kernel | router rule-sets (modules/routing/router/src/rule_set.rs), vless (the transport over QUIC), shadowsocks (v2ray-plugin: the ws transport) |
ours_tls_options::Tls | tls | HTTP downloads: router remote rule-sets, group probes, adblock, subscriptions, the binary (fetch, check --fetch); TLS over streams: http, mixed, anytls, trojan, vless, shadowtls, shadowsocks (v2ray-plugin); dns (DoT, DoH), the hysteria2 masquerade |
ours_quic::tls::Factory | tls (feature quic) | the QUIC protocols (hysteria, hysteria2, tuic), the QUIC transport (the variant quic of the point transport, for vless), dns (DoQ, DoH3) |
ours_api::cache::Cache (publish/of) | cache | groups, DNS, remote rule-sets (the cache may be off) |
ours_dns_types::rules::Compiler | router | dns — compiling DNS rules with route conditions |
ours_api::dns::FakeRanges | dns | the binary's auto_nft |
ours_dns_types::filter::Hook | adblock | dns — the client query filter (attach_services) |
ours_api::dispatch::Vetoes | adblock | the kernel puts it into Routing::veto; dispatch asks it after the rules |
ours_api::admin::Admin | adblock | the Admin API (app/ui/src/api.rs) — /api/v1/<module>/… requests |
ours_mod_ntp::Ntp | ntp | tests; from outside through Instance::service |
ours_tracker::Tracker | dispatch | the Admin API (app/ui/src/api.rs) |