style: switch rustfmt to hard tabs; document tests-separate rule in TODO

- rustfmt.toml: hard_tabs = true.
- TODO §0.7: tests live in tests/, no #[cfg(test)] modules in code files.
This commit is contained in:
Loki 2026-11-15 22:34:29 +01:00
parent d8393e0f39
commit a90f0da226
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
96 changed files with 6542 additions and 6537 deletions

View file

@ -19,116 +19,116 @@ use tokio::sync::watch;
/// HTTP/1.1-обработчик с политикой из секции `[protocol.http]`.
/// Внешние крейты регистрируют свои обработчики в этом же месте.
pub(crate) fn build_registry(config: &Config) -> ProtocolRegistry {
#[cfg(feature = "protocol-minecraft")]
if config.protocol.minecraft.enabled {
let mut registry = ProtocolRegistry::new();
registry.register(Box::new(crate::protocol::minecraft::MinecraftProtocolHandler::new(
&config.protocol.minecraft,
&config.backend.upstreams,
)));
return registry;
}
#[cfg(feature = "protocol-http")]
{
let mut registry = ProtocolRegistry::new();
register_http(&mut registry, config);
registry
}
#[cfg(not(feature = "protocol-http"))]
{
let _ = config;
ProtocolRegistry::new()
}
#[cfg(feature = "protocol-minecraft")]
if config.protocol.minecraft.enabled {
let mut registry = ProtocolRegistry::new();
registry.register(Box::new(crate::protocol::minecraft::MinecraftProtocolHandler::new(
&config.protocol.minecraft,
&config.backend.upstreams,
)));
return registry;
}
#[cfg(feature = "protocol-http")]
{
let mut registry = ProtocolRegistry::new();
register_http(&mut registry, config);
registry
}
#[cfg(not(feature = "protocol-http"))]
{
let _ = config;
ProtocolRegistry::new()
}
}
#[cfg(feature = "protocol-http")]
fn register_http(registry: &mut ProtocolRegistry, config: &Config) {
registry.register(Box::new(crate::protocol::http::HttpProtocolHandler::new(
&config.protocol.http,
&config.backend.upstreams,
)));
registry.register(Box::new(crate::protocol::http::HttpProtocolHandler::new(
&config.protocol.http,
&config.backend.upstreams,
)));
}
#[cfg(feature = "xdp")]
pub(crate) fn start_xdp(
config: &Arc<Config>,
shutdown_rx: &watch::Receiver<bool>,
blacklist: &Arc<Blacklist>,
config: &Arc<Config>,
shutdown_rx: &watch::Receiver<bool>,
blacklist: &Arc<Blacklist>,
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
use std::time::Duration;
use std::time::Duration;
use crate::xdp::XdpMetrics;
use crate::xdp::XdpMetrics;
if !config.xdp.enabled {
return Ok(None);
}
if !config.xdp.enabled {
return Ok(None);
}
let filter = XdpFilter::new(&config.xdp.interface);
let shared = Arc::new(Mutex::new(filter));
{
let mut guard = shared.lock().expect("xdp lock poisoned");
let globals = XdpGlobals::from_config(&config.xdp)?;
guard.set_globals(globals);
// With ingress PROXY protocol, XDP sees only the provider's transport
// address, so its events must not trigger per-player or provider bans.
if !config.proxy_protocol.ingress_enabled {
// load() captures the blacklist and auto-ban policy in its ringbuf
// callback, so set_autoban must happen first.
guard.set_autoban(blacklist.clone(), &config.xdp);
} else if config.xdp.auto_ban_enabled {
tracing::warn!(
"XDP auto-ban is disabled while inbound PROXY protocol is enabled; XDP only sees transport peers"
);
}
guard.load()?;
}
let xdp_metrics = XdpMetrics::register()?;
let filter = XdpFilter::new(&config.xdp.interface);
let shared = Arc::new(Mutex::new(filter));
{
let mut guard = shared.lock().expect("xdp lock poisoned");
let globals = XdpGlobals::from_config(&config.xdp)?;
guard.set_globals(globals);
// With ingress PROXY protocol, XDP sees only the provider's transport
// address, so its events must not trigger per-player or provider bans.
if !config.proxy_protocol.ingress_enabled {
// load() captures the blacklist and auto-ban policy in its ringbuf
// callback, so set_autoban must happen first.
guard.set_autoban(blacklist.clone(), &config.xdp);
} else if config.xdp.auto_ban_enabled {
tracing::warn!(
"XDP auto-ban is disabled while inbound PROXY protocol is enabled; XDP only sees transport peers"
);
}
guard.load()?;
}
let xdp_metrics = XdpMetrics::register()?;
let sd = shutdown_rx.clone();
let shared_thread = shared.clone();
std::thread::spawn(move || {
while !*sd.borrow() {
let guard = match shared_thread.lock() {
Ok(g) => g,
Err(_) => break,
};
guard.drain_events();
if let Ok(stats) = guard.get_stats() {
xdp_metrics.update(&stats);
}
drop(guard);
std::thread::sleep(Duration::from_secs(5));
}
if let Ok(mut guard) = shared_thread.lock() {
guard.unload().ok();
}
});
Ok(Some(shared))
let sd = shutdown_rx.clone();
let shared_thread = shared.clone();
std::thread::spawn(move || {
while !*sd.borrow() {
let guard = match shared_thread.lock() {
Ok(g) => g,
Err(_) => break,
};
guard.drain_events();
if let Ok(stats) = guard.get_stats() {
xdp_metrics.update(&stats);
}
drop(guard);
std::thread::sleep(Duration::from_secs(5));
}
if let Ok(mut guard) = shared_thread.lock() {
guard.unload().ok();
}
});
Ok(Some(shared))
}
#[cfg(not(feature = "xdp"))]
pub(crate) fn start_xdp(
_config: &Arc<Config>,
_shutdown_rx: &watch::Receiver<bool>,
_blacklist: &Arc<Blacklist>,
_config: &Arc<Config>,
_shutdown_rx: &watch::Receiver<bool>,
_blacklist: &Arc<Blacklist>,
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
Ok(None)
Ok(None)
}
/// Юзерспейс-агрегатор префиксов: активен, когда detect.prefix включён,
/// а XDP-путь не работает (не собран или выключен в конфиге).
pub(crate) fn start_subnet_tracker(config: &Config) -> Option<Arc<SubnetTracker>> {
let xdp_active = cfg!(feature = "xdp") && config.xdp.enabled;
if config.detect.prefix.enabled && !xdp_active {
tracing::info!(
window_secs = config.detect.prefix.window_secs,
syn_threshold = config.detect.prefix.syn_threshold,
"subnet tracker enabled (userspace prefix aggregation)"
);
Some(Arc::new(SubnetTracker::new()))
} else {
None
}
let xdp_active = cfg!(feature = "xdp") && config.xdp.enabled;
if config.detect.prefix.enabled && !xdp_active {
tracing::info!(
window_secs = config.detect.prefix.window_secs,
syn_threshold = config.detect.prefix.syn_threshold,
"subnet tracker enabled (userspace prefix aggregation)"
);
Some(Arc::new(SubnetTracker::new()))
} else {
None
}
}
/// Store-фичи проверяются на старте edge-демона: конфиг, запрашивающий redis,
@ -137,71 +137,71 @@ pub(crate) fn start_subnet_tracker(config: &Config) -> Option<Arc<SubnetTracker>
/// Точка вызова — `build_whitelist`: ближайший `?`-хук, принимающий конфиг
/// до бинда listener'а (сам runtime.rs вне правок этой задачи).
fn ensure_store_features(config: &Config) -> anyhow::Result<()> {
#[cfg(not(feature = "store-redis"))]
if config.store.redis_url.as_deref().is_some_and(|url| !url.is_empty()) {
anyhow::bail!("[store].redis_url requires a redis-backed store: built without store-redis feature");
}
#[cfg(feature = "store-redis")]
let _ = config;
Ok(())
#[cfg(not(feature = "store-redis"))]
if config.store.redis_url.as_deref().is_some_and(|url| !url.is_empty()) {
anyhow::bail!("[store].redis_url requires a redis-backed store: built without store-redis feature");
}
#[cfg(feature = "store-redis")]
let _ = config;
Ok(())
}
pub(crate) fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpAddr>>> {
ensure_store_features(config)?;
let mut set = HashSet::with_capacity(config.whitelist.len());
for entry in &config.whitelist {
let ip: IpAddr = entry
.parse()
.map_err(|_| anyhow::anyhow!("invalid whitelist entry: {entry}"))?;
set.insert(ip);
}
Ok(Arc::new(set))
ensure_store_features(config)?;
let mut set = HashSet::with_capacity(config.whitelist.len());
for entry in &config.whitelist {
let ip: IpAddr = entry
.parse()
.map_err(|_| anyhow::anyhow!("invalid whitelist entry: {entry}"))?;
set.insert(ip);
}
Ok(Arc::new(set))
}
#[cfg(test)]
mod tests {
use super::*;
use super::*;
fn config_with_redis() -> Config {
Config {
store: crate::config::StoreConfig {
redis_url: Some("redis://127.0.0.1:6379/0".to_string()),
..Default::default()
},
..Default::default()
}
}
fn config_with_redis() -> Config {
Config {
store: crate::config::StoreConfig {
redis_url: Some("redis://127.0.0.1:6379/0".to_string()),
..Default::default()
},
..Default::default()
}
}
#[cfg(not(feature = "store-redis"))]
#[test]
fn redis_config_without_feature_fails_fast() {
let err = ensure_store_features(&config_with_redis()).expect_err("must fail without store-redis");
assert!(err.to_string().contains("built without store-redis feature"));
}
#[cfg(not(feature = "store-redis"))]
#[test]
fn redis_config_without_feature_fails_fast() {
let err = ensure_store_features(&config_with_redis()).expect_err("must fail without store-redis");
assert!(err.to_string().contains("built without store-redis feature"));
}
#[cfg(not(feature = "store-redis"))]
#[test]
fn empty_store_config_passes_without_feature() {
assert!(ensure_store_features(&Config::default()).is_ok());
}
#[cfg(not(feature = "store-redis"))]
#[test]
fn empty_store_config_passes_without_feature() {
assert!(ensure_store_features(&Config::default()).is_ok());
}
#[cfg(feature = "store-redis")]
#[test]
fn guard_is_noop_with_feature() {
assert!(ensure_store_features(&config_with_redis()).is_ok());
}
#[cfg(feature = "store-redis")]
#[test]
fn guard_is_noop_with_feature() {
assert!(ensure_store_features(&config_with_redis()).is_ok());
}
/// Wiring-тасовка XDP auto-ban: `start_xdp` обязан принимать `Arc<Blacklist>`
/// (т.е. существовать в сигнатуре с blacklist) и при выключенном `[xdp]`
/// не трогать ни ядро, ни blacklist. Порядок `set_autoban` -> `load()`
/// внутри enabled-ветки без ядра непроверяем — см. комментарий на месте вызова.
#[test]
fn start_xdp_accepts_blacklist_and_skips_kernel_when_disabled() {
let (_tx, rx) = watch::channel(false);
let blacklist = Arc::new(Blacklist::new());
let started = start_xdp(&Arc::new(Config::default()), &rx, &blacklist)
.expect("xdp disabled: no kernel work and no error");
assert!(started.is_none());
assert!(blacklist.is_empty());
}
/// Wiring-тасовка XDP auto-ban: `start_xdp` обязан принимать `Arc<Blacklist>`
/// (т.е. существовать в сигнатуре с blacklist) и при выключенном `[xdp]`
/// не трогать ни ядро, ни blacklist. Порядок `set_autoban` -> `load()`
/// внутри enabled-ветки без ядра непроверяем — см. комментарий на месте вызова.
#[test]
fn start_xdp_accepts_blacklist_and_skips_kernel_when_disabled() {
let (_tx, rx) = watch::channel(false);
let blacklist = Arc::new(Blacklist::new());
let started = start_xdp(&Arc::new(Config::default()), &rx, &blacklist)
.expect("xdp disabled: no kernel work and no error");
assert!(started.is_none());
assert!(blacklist.is_empty());
}
}