feat!: universal redesign — drop Minecraft stack, single-crate architecture
- remove Java plugins (velocity/paper), dashboard, all MC-specific code
(handshake, death_code, varint, hostname-HMAC); available in history pre-v0.2
- merge crates/* into one package with src/bin/{rampart,rampart-manager,rampart-cli}
- ProtocolHandler trait + registry (no implementations yet), universal PoW kept
- XDP: universal L3/L4 filter (xdp/core/) + pluggable hook API (xdp/hooks/),
fix IPv6 saddr bug; clang build verified
- docs: bilingual knowledge base (docs/kb/: attacks x4, defense-levels,
practice x3), rewrite README/architecture for universal concept
- TODO.md v4.0: <=300-line module limit, competitor benchmark section (ref/)
- deploy/CI/docs cleanup: no MC references, new binary names
cargo build/clippy(-D warnings)/test green (55 tests)
This commit is contained in:
parent
7d15dfd45e
commit
08e26df73f
179 changed files with 5044 additions and 11519 deletions
89
src/store/clickhouse.rs
Normal file
89
src/store/clickhouse.rs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::watch;
|
||||
|
||||
const BATCH_SIZE: usize = 1000;
|
||||
const FLUSH_INTERVAL: Duration = Duration::from_secs(1);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ClickHouseEvent {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub event_type: String,
|
||||
pub ip: String,
|
||||
pub data_float: f64,
|
||||
pub data_int: i64,
|
||||
pub data_string: String,
|
||||
}
|
||||
|
||||
pub struct ClickHouseWriter {
|
||||
url: String,
|
||||
client: reqwest::Client,
|
||||
buffer: Vec<ClickHouseEvent>,
|
||||
}
|
||||
|
||||
impl ClickHouseWriter {
|
||||
pub fn new(url: &str) -> Self {
|
||||
Self {
|
||||
url: url.to_string(),
|
||||
client: reqwest::Client::new(),
|
||||
buffer: Vec::with_capacity(BATCH_SIZE),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn push(&mut self, event: ClickHouseEvent) -> anyhow::Result<()> {
|
||||
self.buffer.push(event);
|
||||
if self.buffer.len() >= BATCH_SIZE {
|
||||
self.flush().await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn flush(&mut self) -> anyhow::Result<()> {
|
||||
if self.buffer.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let events = std::mem::take(&mut self.buffer);
|
||||
let json = serde_json::to_string(&events)?;
|
||||
let response = self
|
||||
.client
|
||||
.post(&self.url)
|
||||
.query(&[("query", "INSERT INTO rampart_events FORMAT JSONEachRow")])
|
||||
.header("Content-Type", "application/json")
|
||||
.body(json)
|
||||
.send()
|
||||
.await?;
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
let text = response.text().await?;
|
||||
anyhow::bail!("clickhouse insert failed ({}): {}", status, text);
|
||||
}
|
||||
tracing::debug!("flushed {} events to clickhouse", events.len());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_flush_task(writer: Arc<Mutex<ClickHouseWriter>>, mut shutdown: watch::Receiver<bool>) {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(FLUSH_INTERVAL) => {
|
||||
if let Err(e) = writer.lock().await.flush().await {
|
||||
tracing::error!("clickhouse flush error: {e}");
|
||||
}
|
||||
}
|
||||
_ = shutdown.changed() => {
|
||||
if *shutdown.borrow() {
|
||||
tracing::info!("flushing clickhouse on shutdown");
|
||||
if let Err(e) = writer.lock().await.flush().await {
|
||||
tracing::error!("clickhouse final flush error: {e}");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
26
src/store/mod.rs
Normal file
26
src/store/mod.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#[cfg(feature = "store-redis")]
|
||||
pub mod redis;
|
||||
#[cfg(feature = "store-redis")]
|
||||
pub use redis::start_blacklist_sync;
|
||||
|
||||
pub mod clickhouse;
|
||||
|
||||
/// Общий контракт внешнего хранилища состояния.
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait StateStore: Send + Sync {
|
||||
/// # Errors
|
||||
/// Возвращает ошибку при недоступности хранилища.
|
||||
async fn get(&self, key: &str) -> anyhow::Result<Option<String>>;
|
||||
|
||||
/// # Errors
|
||||
/// Возвращает ошибку при недоступности хранилища.
|
||||
async fn set(&self, key: &str, value: &str) -> anyhow::Result<()>;
|
||||
|
||||
/// # Errors
|
||||
/// Возвращает ошибку при недоступности хранилища.
|
||||
async fn del(&self, key: &str) -> anyhow::Result<()>;
|
||||
|
||||
/// # Errors
|
||||
/// Возвращает ошибку при недоступности хранилища.
|
||||
async fn publish(&self, channel: &str, message: &str) -> anyhow::Result<()>;
|
||||
}
|
||||
128
src/store/redis.rs
Normal file
128
src/store/redis.rs
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::store::StateStore;
|
||||
use futures::StreamExt;
|
||||
use redis::AsyncCommands;
|
||||
use redis::Msg;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
|
||||
pub struct RedisStore {
|
||||
client: redis::Client,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct BlacklistEvent {
|
||||
ip: String,
|
||||
action: String,
|
||||
#[serde(default = "default_duration")]
|
||||
duration_secs: u64,
|
||||
}
|
||||
|
||||
fn default_duration() -> u64 {
|
||||
300
|
||||
}
|
||||
|
||||
impl RedisStore {
|
||||
pub fn new(url: &str) -> anyhow::Result<Self> {
|
||||
let client = redis::Client::open(url)?;
|
||||
Ok(Self { client })
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_blacklist_sync(
|
||||
client: &redis::Client,
|
||||
blacklist: Arc<Blacklist>,
|
||||
mut shutdown: watch::Receiver<bool>,
|
||||
) {
|
||||
#[allow(deprecated)]
|
||||
let conn = match client.get_async_connection().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
let mut pubsub = conn.into_pubsub();
|
||||
if let Err(e) = pubsub.subscribe("rampart:blacklist:events").await {
|
||||
tracing::error!("failed to subscribe to blacklist events: {e}");
|
||||
return;
|
||||
}
|
||||
tracing::info!("subscribed to rampart:blacklist:events");
|
||||
|
||||
loop {
|
||||
let mut stream = pubsub.on_message();
|
||||
let msg_fut = stream.next();
|
||||
tokio::pin!(msg_fut);
|
||||
|
||||
tokio::select! {
|
||||
_ = shutdown.changed() => {
|
||||
if *shutdown.borrow() {
|
||||
tracing::info!("shutting down blacklist subscriber");
|
||||
return;
|
||||
}
|
||||
}
|
||||
result = &mut msg_fut => {
|
||||
match result {
|
||||
Some(msg) => {
|
||||
if let Err(e) = handle_event(&msg, &blacklist) {
|
||||
tracing::error!("blacklist event error: {e}");
|
||||
}
|
||||
}
|
||||
None => {
|
||||
tracing::error!("pubsub stream ended");
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_event(msg: &Msg, blacklist: &Blacklist) -> anyhow::Result<()> {
|
||||
let payload: String = msg.get_payload()?;
|
||||
let event: BlacklistEvent = serde_json::from_str(&payload)?;
|
||||
|
||||
let ip: std::net::IpAddr = event.ip.parse()?;
|
||||
|
||||
match event.action.as_str() {
|
||||
"ban" => {
|
||||
blacklist.add(ip, Duration::from_secs(event.duration_secs), "redis");
|
||||
tracing::info!("blacklist add via Redis: {}", event.ip);
|
||||
},
|
||||
"unban" => {
|
||||
blacklist.remove(ip);
|
||||
tracing::info!("blacklist remove via Redis: {}", event.ip);
|
||||
},
|
||||
a => anyhow::bail!("unknown action: {a}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl StateStore for RedisStore {
|
||||
async fn get(&self, key: &str) -> anyhow::Result<Option<String>> {
|
||||
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
||||
Ok(conn.get(key).await?)
|
||||
}
|
||||
|
||||
async fn set(&self, key: &str, value: &str) -> anyhow::Result<()> {
|
||||
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
||||
let _: () = conn.set(key, value).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn del(&self, key: &str) -> anyhow::Result<()> {
|
||||
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
||||
let _: () = conn.del(key).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn publish(&self, channel: &str, message: &str) -> anyhow::Result<()> {
|
||||
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
||||
let _: () = conn.publish(channel, message).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue