XDP behaviour: - udp_rate_limit6 (LRU, full 128-bit source key) plus throttle_hit6, so udp_policy = "rate-limit" applies to native IPv6 as well. - scripts/xdp_ipv6_smoke.sh: phase 4 patches .rodata before load and verifies policy=2 on a real kernel (3 pass / 3 rate-limited). Loader (XDP could never load before this): - .rodata is one struct rampart_config with frozen offsets enforced by _Static_assert; XdpGlobals is #[repr(C)] and the image is built with offset_of!. Before, a 96-byte image was written into a 76-byte map at offsets clang had reordered, so load() failed with EINVAL. - patch_rodata fails loudly when the map size and image size diverge. - build.rs tracks xdp/core and xdp/hooks, not just universal_filter.c. - build_ringbuf looked for "events_map"; the map is "events_ringbuf". - tests/xdp_object_maps.rs checks map names and .rodata size without root. Docs and style: - Code comments are English, JavaDoc/Rustdoc style, without emoji or banners (new project rule, TODO §0.8). - README rewritten bilingual: Russian first, English below, nav buttons, compact TOC and copy-paste quick start. - docs/README.md: documentation index and maintenance rules; configuration.md documents the whole [xdp] section and IPv4/IPv6 coverage; testing.md covers the real-kernel smoke; stale links removed.
233 lines
7.2 KiB
Rust
233 lines
7.2 KiB
Rust
//! Edge daemon runtime: component assembly, background tasks and listener
|
|
//! startup.
|
|
|
|
use crate::config::Config;
|
|
use crate::engine::challenge::DifficultyAdjuster;
|
|
use crate::engine::listener;
|
|
use crate::engine::subnet::monitor;
|
|
use crate::engine::tunnel::Gateway;
|
|
use crate::filter::blacklist::Blacklist;
|
|
use crate::filter::rate_limit::RateLimiter;
|
|
use crate::metrics;
|
|
use crate::store::clickhouse::{ClickHouseEvent, ClickHouseWriter};
|
|
use crate::traffic::hook::TrafficHook;
|
|
use crate::traffic::intel::detector::{AttackDetector, AttackStatus};
|
|
use crate::traffic::intel::reputation::IpReputation;
|
|
use crate::traffic::profile::alert::{AlertDispatcher, send_webhook};
|
|
use std::sync::Arc;
|
|
use std::sync::Mutex;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
use std::time::Duration;
|
|
use tokio::sync::oneshot;
|
|
use tokio::sync::watch;
|
|
use tracing_subscriber::EnvFilter;
|
|
|
|
use super::services::{build_registry, build_whitelist, start_subnet_tracker, start_xdp};
|
|
use super::shutdown::{RunExit, spawn_shutdown_drain};
|
|
|
|
fn attack_status_value(status: AttackStatus) -> i64 {
|
|
match status {
|
|
AttackStatus::Normal => 0,
|
|
AttackStatus::Suspicious => 1,
|
|
AttackStatus::UnderAttack => 2,
|
|
}
|
|
}
|
|
|
|
/// Entry point of the edge daemon (`rampart`).
|
|
///
|
|
/// Returns [`RunExit`]; the binary decides whether and how the process ends.
|
|
///
|
|
/// # Errors
|
|
/// Startup failures: unreadable config, empty protocol registry, listener error.
|
|
pub async fn run() -> anyhow::Result<RunExit> {
|
|
tracing_subscriber::fmt()
|
|
.with_env_filter(EnvFilter::from_default_env().add_directive("rampart=info".parse()?))
|
|
.init();
|
|
|
|
let config_path = std::env::var("RAMPART_CONFIG").unwrap_or_else(|_| "/etc/rampart/config.toml".to_string());
|
|
let config = Arc::new(Config::from_file(&config_path)?);
|
|
|
|
let registry = Arc::new(build_registry(&config));
|
|
if registry.is_empty() {
|
|
anyhow::bail!(
|
|
"no protocol plugins compiled; available feature flags: protocol-http, \
|
|
store-redis, geoip, xdp, io-uring. Build with --features protocol-http \
|
|
or link an external ProtocolHandler implementation"
|
|
);
|
|
}
|
|
let whitelist = build_whitelist(&config)?;
|
|
|
|
let rate_limiter = Arc::new(RateLimiter::new(
|
|
config.limits.rate_limit_pps,
|
|
config.limits.rate_limit_burst,
|
|
));
|
|
let blacklist = Arc::new(Blacklist::new());
|
|
let reputation = Arc::new(IpReputation::new());
|
|
let detector = Arc::new(Mutex::new(AttackDetector::new()));
|
|
let hook = Arc::new(TrafficHook::new(
|
|
reputation.clone(),
|
|
blacklist.clone(),
|
|
config.detect.autoban.clone(),
|
|
config.ban.ban_duration_secs,
|
|
));
|
|
let alert_dispatcher = AlertDispatcher::new();
|
|
let webhook_url = config.detect.alert.webhook_url.clone();
|
|
let allowed_1s = Arc::new(AtomicU64::new(0));
|
|
|
|
let (shutdown_tx, shutdown_rx) = watch::channel(false);
|
|
let (drain_tx, drain_rx) = oneshot::channel();
|
|
|
|
spawn_shutdown_drain(shutdown_tx.clone(), drain_tx);
|
|
|
|
#[cfg(feature = "store-redis")]
|
|
if let Some(redis_url) = &config.store.redis_url
|
|
&& !redis_url.is_empty()
|
|
{
|
|
let bl = blacklist.clone();
|
|
let sd = shutdown_rx.clone();
|
|
let url = redis_url.clone();
|
|
tokio::spawn(async move {
|
|
let client = match redis::Client::open(url.as_str()) {
|
|
Ok(c) => c,
|
|
Err(e) => {
|
|
tracing::warn!("Invalid redis_url: {e}, blacklist sync disabled");
|
|
return;
|
|
},
|
|
};
|
|
let local_events = bl.subscribe_local_bans();
|
|
let publisher_client = client.clone();
|
|
let publisher_blacklist = bl.clone();
|
|
let publisher_shutdown = sd.clone();
|
|
tokio::spawn(async move {
|
|
crate::store::start_local_ban_publisher(
|
|
publisher_client,
|
|
publisher_blacklist,
|
|
local_events,
|
|
publisher_shutdown,
|
|
)
|
|
.await;
|
|
});
|
|
crate::store::start_blacklist_sync(&client, bl, sd).await;
|
|
});
|
|
}
|
|
|
|
if config.metrics.enabled {
|
|
let metrics_addr = format!("0.0.0.0:{}", config.metrics.port);
|
|
tracing::info!("Metrics server listening on {metrics_addr}");
|
|
tokio::spawn(async move {
|
|
metrics::run_metrics_server(&metrics_addr).await;
|
|
});
|
|
}
|
|
|
|
let clickhouse: Option<Arc<tokio::sync::Mutex<ClickHouseWriter>>> = match &config.store.clickhouse_url {
|
|
Some(url) if !url.is_empty() => {
|
|
let writer = Arc::new(tokio::sync::Mutex::new(ClickHouseWriter::new(url)));
|
|
crate::store::clickhouse::start_flush_task(writer.clone(), shutdown_rx.clone());
|
|
Some(writer)
|
|
},
|
|
_ => None,
|
|
};
|
|
|
|
let xdp_filter = start_xdp(&config, &shutdown_rx, &blacklist)?;
|
|
let subnet_tracker = start_subnet_tracker(&config);
|
|
monitor::spawn(
|
|
&config.detect.prefix,
|
|
subnet_tracker.clone(),
|
|
xdp_filter.clone(),
|
|
shutdown_rx.clone(),
|
|
);
|
|
|
|
let rl = rate_limiter.clone();
|
|
let bl = blacklist.clone();
|
|
let det = detector.clone();
|
|
let intel = hook.clone();
|
|
let a1s = allowed_1s.clone();
|
|
let ch = clickhouse.clone();
|
|
let mut sd = shutdown_rx.clone();
|
|
tokio::spawn(async move {
|
|
let mut sec_tick = tokio::time::interval(Duration::from_secs(1));
|
|
let mut min_tick = tokio::time::interval(Duration::from_secs(60));
|
|
sec_tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
|
min_tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
|
loop {
|
|
tokio::select! {
|
|
biased;
|
|
_ = sd.changed() => {
|
|
if *sd.borrow() {
|
|
return;
|
|
}
|
|
}
|
|
_ = sec_tick.tick() => {
|
|
let pps = a1s.swap(0, Ordering::Relaxed) as f64;
|
|
let cps = intel.take_cps() as f64;
|
|
metrics::INTEL_CPS.set(cps as i64);
|
|
let status = det.lock().expect("detector lock poisoned").analyze(pps, cps);
|
|
metrics::ATTACK_STATUS.set(attack_status_value(status));
|
|
if let Some(alert) = alert_dispatcher.on_status(status) {
|
|
// Deduplication: an alert is emitted only on an attack state transition.
|
|
metrics::INTEL_ALERTS_TOTAL.inc();
|
|
tracing::warn!(pps, cps, "{alert}");
|
|
push_attack_event(&ch, pps).await;
|
|
if let Some(url) = webhook_url.clone() {
|
|
tokio::spawn(send_webhook(url, alert.clone()));
|
|
}
|
|
}
|
|
if status == AttackStatus::UnderAttack {
|
|
let banned = intel.ban_offenders();
|
|
if banned > 0 {
|
|
tracing::info!(banned, "escalation: offenders auto-banned during attack");
|
|
}
|
|
}
|
|
}
|
|
_ = min_tick.tick() => {
|
|
rl.sweep();
|
|
bl.clear_expired();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
tracing::info!("Rampart edge starting on {}:{}", config.bind.address, config.bind.port);
|
|
tracing::info!("Upstreams: {:?}", config.backend.upstreams);
|
|
|
|
let adjuster = Arc::new(Mutex::new(DifficultyAdjuster::default()));
|
|
let gateway = Arc::new(Gateway::new(
|
|
config.clone(),
|
|
rate_limiter,
|
|
blacklist,
|
|
adjuster,
|
|
whitelist,
|
|
reputation,
|
|
xdp_filter,
|
|
clickhouse,
|
|
allowed_1s,
|
|
registry,
|
|
subnet_tracker,
|
|
));
|
|
tokio::select! {
|
|
result = listener::run(config, gateway, hook, shutdown_rx) => {
|
|
result?;
|
|
Ok(RunExit::Ok)
|
|
}
|
|
// The drain timeout fired: hand the signal back so the binary can end the
|
|
// process immediately, replacing the former `process::exit(0)`.
|
|
_ = drain_rx => Ok(RunExit::DrainTimeout),
|
|
}
|
|
}
|
|
|
|
async fn push_attack_event(writer: &Option<Arc<tokio::sync::Mutex<ClickHouseWriter>>>, pps: f64) {
|
|
let Some(writer) = writer else {
|
|
return;
|
|
};
|
|
let event = ClickHouseEvent {
|
|
timestamp: chrono::Utc::now(),
|
|
event_type: "attack".to_string(),
|
|
ip: String::new(),
|
|
data_float: pps,
|
|
data_int: 0,
|
|
data_string: "under_attack".to_string(),
|
|
};
|
|
if let Err(e) = writer.lock().await.push(event).await {
|
|
tracing::debug!("clickhouse push error: {e}");
|
|
}
|
|
}
|