feat: traffic intel hot path, SYN RST-challenge, XDP environment diagnostics

Traffic Intel (was dead code, now wired):
- TrafficHook in listener accept path: cps/pps windows -> AttackDetector
- auto-ban IPs below reputation threshold under attack ([detect.autoban])
- AlertDispatcher: webhook on attack state transition only (dedup), metrics
  AUTO_BANS_TOTAL / INTEL_* ; [detect.alert].webhook_url

XDP SYN RST-challenge (Oubliette pattern, off by default):
- G_SYN_CHALLENGE_ENABLED=0: kernel replies bad-ACK SYN-ACK via XDP_TX,
  spoofed sources stay silent, live clients answer RST with secret echo ->
  challenge_verified (LRU, sliding TTL); brute-force of marker impossible
- maps challenge_verified/challenge_pending, STAT_CHALLENGE_*, all logic
  in xdp/core/syn_challenge.h (221 lines)

XDP diagnostics (src/xdp/diagnostics.rs):
- EnvironmentReport: kernel version/BTF/driver->AttachMode verdict,
  fail-fast before load on unsupported kernels; wired into  CLI
- SystemProbe trait for kernel-less testing

fix: .gitignore 'bin/' matched src/bin/ — rampart.rs was never committed

cargo build/clippy(-D warnings, --features xdp)/test green: 107 tests
This commit is contained in:
Loki 2026-08-24 10:11:10 +02:00
parent 5d7cdc24e3
commit f53464a45e
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
25 changed files with 1825 additions and 28 deletions

311
src/bin/rampart.rs Normal file
View file

@ -0,0 +1,311 @@
use rampart::config::Config;
use rampart::engine::challenge::DifficultyAdjuster;
use rampart::engine::listener;
use rampart::engine::subnet_monitor;
use rampart::engine::subnet_tracker::SubnetTracker;
use rampart::engine::tunnel::Gateway;
use rampart::filter::blacklist::Blacklist;
use rampart::filter::rate_limit::RateLimiter;
use rampart::metrics;
use rampart::protocol::ProtocolRegistry;
use rampart::store::clickhouse::{ClickHouseEvent, ClickHouseWriter};
use rampart::traffic::alert::{AlertDispatcher, send_webhook};
use rampart::traffic::detector::{AttackDetector, AttackStatus};
use rampart::traffic::hook::TrafficHook;
use rampart::traffic::reputation::IpReputation;
use rampart::xdp::XdpFilter;
use std::collections::HashSet;
use std::net::IpAddr;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use tokio::sync::watch;
use tracing_subscriber::EnvFilter;
fn attack_status_value(status: AttackStatus) -> i64 {
match status {
AttackStatus::Normal => 0,
AttackStatus::Suspicious => 1,
AttackStatus::UnderAttack => 2,
}
}
/// Собирает реестр протоколов из скомпилированных реализаций.
///
/// В ядре реализаций нет: `protocol-http` — зарезервированная пустая feature,
/// показывающая модель plugin-by-feature. Внешние крейты регистрируют свои
/// обработчики в этом месте.
#[allow(unused_mut)]
fn build_registry() -> ProtocolRegistry {
let mut registry = ProtocolRegistry::new();
#[cfg(feature = "protocol-http")]
register_http(&mut registry);
registry
}
#[cfg(feature = "protocol-http")]
fn register_http(_registry: &mut ProtocolRegistry) {}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive("rampart=info".parse()?))
.init();
let registry = Arc::new(build_registry());
if registry.is_empty() {
anyhow::bail!(
"no protocol plugins compiled; build with --features protocol-http \
or link an external ProtocolHandler implementation"
);
}
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 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 sig_tx = shutdown_tx.clone();
tokio::spawn(async move {
wait_for_signal().await;
tracing::info!("shutdown signal received, draining connections...");
let _ = sig_tx.send(true);
tokio::time::sleep(Duration::from_secs(5)).await;
tracing::info!("shutdown timeout reached, exiting");
std::process::exit(0);
});
#[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;
},
};
rampart::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)));
rampart::store::clickhouse::start_flush_task(writer.clone(), shutdown_rx.clone());
Some(writer)
},
_ => None,
};
let xdp_filter = start_xdp(&config, &shutdown_rx)?;
let subnet_tracker = start_subnet_tracker(&config);
subnet_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) {
// Дедупликация: алерт только на переходе состояния атаки.
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,
));
listener::run(config, gateway, hook, shutdown_rx).await
}
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}");
}
}
#[cfg(feature = "xdp")]
fn start_xdp(
config: &Arc<Config>,
shutdown_rx: &watch::Receiver<bool>,
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
use rampart::xdp::XdpMetrics;
if !config.xdp.enabled {
return Ok(None);
}
let filter = XdpFilter::new(&config.xdp.interface);
let shared = Arc::new(Mutex::new(filter));
shared.lock().expect("xdp lock poisoned").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))
}
#[cfg(not(feature = "xdp"))]
fn start_xdp(
_config: &Arc<Config>,
_shutdown_rx: &watch::Receiver<bool>,
) -> anyhow::Result<Option<Arc<Mutex<XdpFilter>>>> {
Ok(None)
}
/// Юзерспейс-агрегатор префиксов: активен, когда detect.prefix включён,
/// а XDP-путь не работает (не собран или выключен в конфиге).
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
}
}
fn build_whitelist(config: &Config) -> anyhow::Result<Arc<HashSet<IpAddr>>> {
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))
}
async fn wait_for_signal() {
let ctrl_c = tokio::signal::ctrl_c();
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install SIGTERM handler");
tokio::select! {
_ = ctrl_c => {}
_ = term.recv() => {}
}
}