feat(xdp): auto-ban from ringbuf events; fix feature gating, exit path, pow flake
- xdp: real 24-byte xdp_event parsing (fixes LE byte-swap of src IP), opt-in [xdp] auto_ban (RATE_LIMIT always, CONN_DROP at fails>=threshold; EVENT_BAN/POLICY_DROP excluded by design), rampart_xdp_autobans_total; wired in app before load(); 10 tests in tests/xdp_events.rs - build: --no-default-features compiles — redis paths cfg-gated behind store-redis, manager fail-fasts without it; CI gates the config now - app: RunExit enum replaces process::exit in lib; single exit site in main - tests: seed PoW roundtrip token (was ~1/16 flaky; 50 pre-fix fails -> 0) - docs: TODO statuses refreshed (round 2)
This commit is contained in:
parent
bf6d34c882
commit
f85ea6c380
23 changed files with 714 additions and 50 deletions
|
|
@ -1,6 +1,9 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
use crate::manager::AppState;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use axum::{Json, extract::State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "store-redis")]
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
@ -29,6 +32,7 @@ pub struct AddBlacklistRequest {
|
|||
pub duration_secs: Option<u64>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub async fn list_blacklist(State(state): State<Arc<AppState>>) -> Json<BlacklistResponse> {
|
||||
let mut conn = match state.redis_client.get_multiplexed_async_connection().await {
|
||||
Ok(c) => c,
|
||||
|
|
@ -69,6 +73,7 @@ pub async fn list_blacklist(State(state): State<Arc<AppState>>) -> Json<Blacklis
|
|||
Json(BlacklistResponse { items, total })
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub async fn add_blacklist(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(req): Json<AddBlacklistRequest>,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
use crate::manager::AppState;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use axum::{Json, extract::State};
|
||||
#[cfg(feature = "store-redis")]
|
||||
use redis::AsyncCommands;
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "store-redis")]
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
@ -18,6 +22,7 @@ pub struct NodesResponse {
|
|||
pub nodes: Vec<NodeInfo>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub async fn list_nodes(State(state): State<Arc<AppState>>) -> Json<NodesResponse> {
|
||||
let mut conn = match state.redis_client.get_multiplexed_async_connection().await {
|
||||
Ok(c) => c,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
use crate::manager::AppState;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use axum::{Json, extract::State};
|
||||
#[cfg(feature = "store-redis")]
|
||||
use redis::AsyncCommands;
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "store-redis")]
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
|
@ -19,6 +23,7 @@ pub struct ServersResponse {
|
|||
pub servers: Vec<ServerEntry>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub async fn list_servers(State(state): State<Arc<AppState>>) -> Json<ServersResponse> {
|
||||
let mut conn = match state.redis_client.get_multiplexed_async_connection().await {
|
||||
Ok(c) => c,
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ use std::time::Instant;
|
|||
|
||||
/// Общее состояние management-API.
|
||||
pub struct AppState {
|
||||
/// Redis-клиент управления состоянием; присутствует только при собранной
|
||||
/// фиче `store-redis` (manager требует её на старте — см. bin/rampart-manager).
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub redis_client: redis::Client,
|
||||
pub jwt_secret: String,
|
||||
pub jwt_audience: String,
|
||||
|
|
|
|||
|
|
@ -1,17 +1,27 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
use crate::manager::AppState;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use futures::StreamExt;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use redis::AsyncCommands;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use redis::ScanOptions;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "store-redis")]
|
||||
use tokio::time::{Duration, interval};
|
||||
|
||||
/// SCAN pattern for node records (blocking KEYS is forbidden under load).
|
||||
#[cfg(feature = "store-redis")]
|
||||
const NODES_KEY_PATTERN: &str = "rampart:nodes:*";
|
||||
/// SCAN COUNT hint per round.
|
||||
#[cfg(feature = "store-redis")]
|
||||
const SCAN_BATCH: usize = 100;
|
||||
/// Node is offline when the last heartbeat is older than this.
|
||||
#[cfg(any(feature = "store-redis", test))]
|
||||
const NODE_TTL_SECS: i64 = 60;
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub async fn start_heartbeat_check(state: Arc<AppState>) {
|
||||
let mut ticker = interval(Duration::from_secs(30));
|
||||
loop {
|
||||
|
|
@ -22,6 +32,7 @@ pub async fn start_heartbeat_check(state: Arc<AppState>) {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
async fn check_nodes(state: &AppState) -> anyhow::Result<()> {
|
||||
let mut conn = state.redis_client.get_multiplexed_async_connection().await?;
|
||||
let keys = collect_node_keys(&mut conn).await?;
|
||||
|
|
@ -40,6 +51,7 @@ async fn check_nodes(state: &AppState) -> anyhow::Result<()> {
|
|||
}
|
||||
|
||||
/// Incrementally iterates the keyspace via SCAN (cursor-based, non-blocking).
|
||||
#[cfg(feature = "store-redis")]
|
||||
async fn collect_node_keys(conn: &mut redis::aio::MultiplexedConnection) -> anyhow::Result<Vec<String>> {
|
||||
let opts = ScanOptions::default()
|
||||
.with_pattern(NODES_KEY_PATTERN)
|
||||
|
|
@ -56,6 +68,7 @@ async fn collect_node_keys(conn: &mut redis::aio::MultiplexedConnection) -> anyh
|
|||
/// the RFC3339 `last_heartbeat` is older than `NODE_TTL_SECS` (a missing or
|
||||
/// unparseable timestamp counts as expired, matching the previous behavior);
|
||||
/// `None` when the node is fresh, unparsable, or not a JSON object.
|
||||
#[cfg(any(feature = "store-redis", test))]
|
||||
fn offline_update(raw: &str, now: i64) -> Option<String> {
|
||||
let mut node = serde_json::from_str::<serde_json::Value>(raw).ok()?;
|
||||
let hb = node["last_heartbeat"]
|
||||
|
|
@ -115,6 +128,7 @@ mod tests {
|
|||
assert!(offline_update("not json", 2_000_000).is_none());
|
||||
}
|
||||
|
||||
#[cfg(feature = "store-redis")]
|
||||
#[test]
|
||||
fn node_key_pattern_targets_rampart_nodes() {
|
||||
assert_eq!(NODES_KEY_PATTERN, "rampart:nodes:*");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue