feat: add Minecraft proxy and refresh dependencies

This commit is contained in:
Loki 2026-10-16 23:04:41 +02:00
parent a4d0880a0a
commit cfaad47948
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
47 changed files with 2671 additions and 1005 deletions

View file

@ -1,29 +1,13 @@
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;
mod sync;
pub use sync::start_blacklist_sync;
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)?;
@ -31,140 +15,6 @@ impl RedisStore {
}
}
const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
const BACKOFF_MIN: Duration = Duration::from_secs(1);
const BACKOFF_MAX: Duration = Duration::from_secs(30);
/// Capped exponential backoff step: `prev` doubled, clamped to `max`.
fn next_backoff(prev: Duration, max: Duration) -> Duration {
prev.saturating_mul(2).min(max)
}
/// What the outer reconnect loop should do after one pubsub session.
enum Flow {
/// Shutdown requested — exit the task.
Stop,
/// Connection dropped or session failed — reconnect with backoff.
Retry,
}
/// Resolves only when shutdown is truly requested (value `true`) or every
/// sender has been dropped; spurious non-shutdown notifications keep it pending
/// so it never cancels in-flight connect/subscribe work.
async fn wait_for_shutdown(shutdown: &mut watch::Receiver<bool>) {
loop {
if shutdown.changed().await.is_err() || *shutdown.borrow_and_update() {
return;
}
}
}
/// Blacklist pubsub subscriber with real reconnect: on connect/subscribe
/// failure or stream end it retries with capped exponential backoff (reset to
/// `BACKOFF_MIN` after each successfully applied message). Every await is
/// raced against the shutdown watch so the task stays cancellable.
pub async fn start_blacklist_sync(
client: &redis::Client,
blacklist: Arc<Blacklist>,
mut shutdown: watch::Receiver<bool>,
) {
let mut backoff = BACKOFF_MIN;
loop {
match subscribe_once(client, &blacklist, &mut shutdown, &mut backoff).await {
Flow::Stop => {
tracing::info!("shutting down blacklist subscriber");
return;
},
Flow::Retry => {},
}
tokio::select! {
_ = tokio::time::sleep(backoff) => {},
() = wait_for_shutdown(&mut shutdown) => {
tracing::info!("shutting down blacklist subscriber");
return;
},
}
backoff = next_backoff(backoff, BACKOFF_MAX);
}
}
/// One connect+subscribe+consume session. Sets `*backoff` to `BACKOFF_MIN`
/// whenever a message is applied, proving the link is healthy. Each blocking
/// await is raced against shutdown. (Connect+subscribe stay inline: the
/// `PubSub` type produced by `into_pubsub` is awkward to name in a split
/// helper's return type.)
async fn subscribe_once(
client: &redis::Client,
blacklist: &Blacklist,
shutdown: &mut watch::Receiver<bool>,
backoff: &mut Duration,
) -> Flow {
#[allow(deprecated)]
let conn = tokio::select! {
connected = client.get_async_connection() => match connected {
Ok(c) => c,
Err(e) => {
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
};
let mut pubsub = conn.into_pubsub();
tokio::select! {
subscribed = pubsub.subscribe(BLACKLIST_CHANNEL) => match subscribed {
Ok(()) => tracing::info!("subscribed to {BLACKLIST_CHANNEL}"),
Err(e) => {
tracing::error!("failed to subscribe to blacklist events: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
let mut stream = pubsub.on_message();
loop {
tokio::select! {
maybe_msg = stream.next() => match maybe_msg {
Some(msg) => {
if let Err(e) = handle_event(&msg, blacklist) {
tracing::error!("blacklist event error: {e}");
} else {
*backoff = BACKOFF_MIN;
}
},
None => {
tracing::error!("blacklist pubsub stream ended; resubscribing");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
}
}
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?;
@ -189,23 +39,3 @@ impl StateStore for RedisStore {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backoff_doubles_until_cap() {
let mut b = BACKOFF_MIN;
for expected_secs in [2, 4, 8, 16, 30, 30] {
b = next_backoff(b, BACKOFF_MAX);
assert_eq!(b, Duration::from_secs(expected_secs));
}
}
#[test]
fn backoff_never_exceeds_max_even_with_huge_prev() {
let b = next_backoff(Duration::from_secs(u64::MAX / 2), BACKOFF_MAX);
assert!(b <= BACKOFF_MAX);
}
}

134
src/store/redis/sync.rs Normal file
View file

@ -0,0 +1,134 @@
mod event;
mod snapshot;
use crate::filter::blacklist::Blacklist;
use futures::StreamExt;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::watch;
const BLACKLIST_CHANNEL: &str = "rampart:blacklist:events";
const BACKOFF_MIN: Duration = Duration::from_secs(1);
const BACKOFF_MAX: Duration = Duration::from_secs(30);
fn next_backoff(prev: Duration, max: Duration) -> Duration {
prev.saturating_mul(2).min(max)
}
enum Flow {
Stop,
Retry,
}
async fn wait_for_shutdown(shutdown: &mut watch::Receiver<bool>) {
loop {
if shutdown.changed().await.is_err() || *shutdown.borrow_and_update() {
return;
}
}
}
pub async fn start_blacklist_sync(
client: &redis::Client,
blacklist: Arc<Blacklist>,
mut shutdown: watch::Receiver<bool>,
) {
let mut backoff = BACKOFF_MIN;
loop {
match subscribe_once(client, &blacklist, &mut shutdown, &mut backoff).await {
Flow::Stop => {
tracing::info!("shutting down blacklist subscriber");
return;
},
Flow::Retry => {},
}
tokio::select! {
_ = tokio::time::sleep(backoff) => {},
() = wait_for_shutdown(&mut shutdown) => {
tracing::info!("shutting down blacklist subscriber");
return;
},
}
backoff = next_backoff(backoff, BACKOFF_MAX);
}
}
async fn subscribe_once(
client: &redis::Client,
blacklist: &Blacklist,
shutdown: &mut watch::Receiver<bool>,
backoff: &mut Duration,
) -> Flow {
let mut pubsub = tokio::select! {
connected = client.get_async_pubsub() => match connected {
Ok(pubsub) => pubsub,
Err(e) => {
tracing::error!("failed to connect to Redis for blacklist sync: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
};
tokio::select! {
subscribed = pubsub.subscribe(BLACKLIST_CHANNEL) => match subscribed {
Ok(()) => tracing::info!("subscribed to {BLACKLIST_CHANNEL}"),
Err(e) => {
tracing::error!("failed to subscribe to blacklist events: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
tokio::select! {
loaded = snapshot::reconcile(client, blacklist) => match loaded {
Ok(()) => tracing::info!("blacklist snapshot reconciled from Redis"),
Err(e) => {
tracing::error!("failed to reconcile blacklist snapshot: {e}");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
let mut stream = pubsub.on_message();
loop {
tokio::select! {
maybe_msg = stream.next() => match maybe_msg {
Some(msg) => {
if let Err(e) = event::handle(&msg, blacklist) {
tracing::error!("blacklist event error: {e}");
} else {
*backoff = BACKOFF_MIN;
}
},
None => {
tracing::error!("blacklist pubsub stream ended; resubscribing");
return Flow::Retry;
},
},
() = wait_for_shutdown(shutdown) => return Flow::Stop,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backoff_doubles_until_cap() {
let mut b = BACKOFF_MIN;
for expected_secs in [2, 4, 8, 16, 30, 30] {
b = next_backoff(b, BACKOFF_MAX);
assert_eq!(b, Duration::from_secs(expected_secs));
}
}
#[test]
fn backoff_never_exceeds_max_even_with_huge_prev() {
let b = next_backoff(Duration::from_secs(u64::MAX / 2), BACKOFF_MAX);
assert!(b <= BACKOFF_MAX);
}
}

View file

@ -0,0 +1,57 @@
use crate::filter::blacklist::Blacklist;
use redis::Msg;
use serde::Deserialize;
use std::time::Duration;
const MAX_BAN_DURATION_SECS: u64 = 31_536_000;
#[derive(Deserialize)]
struct BlacklistEvent {
ip: String,
action: String,
#[serde(default = "default_duration")]
duration_secs: u64,
#[serde(default)]
expires_at_unix: Option<i64>,
}
fn default_duration() -> u64 {
300
}
pub(super) fn handle(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" => {
let duration = match event.expires_at_unix {
Some(expires_at) => {
if chrono::DateTime::from_timestamp(expires_at, 0).is_none() {
anyhow::bail!("invalid blacklist event expiry timestamp");
}
let remaining = expires_at.saturating_sub(chrono::Utc::now().timestamp());
if remaining <= 0 {
blacklist.remove_redis(ip);
return Ok(());
}
let remaining = u64::try_from(remaining)?;
if remaining > MAX_BAN_DURATION_SECS {
anyhow::bail!("blacklist event expiry exceeds maximum duration");
}
Duration::from_secs(remaining)
},
None => Duration::from_secs(event.duration_secs),
};
blacklist.add_redis(ip, duration, "manager_event");
tracing::info!("blacklist add via Redis: {}", event.ip);
},
"unban" => {
blacklist.remove_redis(ip);
tracing::info!("blacklist remove via Redis: {}", event.ip);
},
action => anyhow::bail!("unknown action: {action}"),
}
Ok(())
}

View file

@ -0,0 +1,77 @@
use crate::filter::blacklist::Blacklist;
use redis::AsyncCommands;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
const BLACKLIST_KEY: &str = "rampart:blacklist";
const BLACKLIST_METADATA_KEY: &str = "rampart:blacklist:metadata";
const LEGACY_BAN_DURATION_SECS: i64 = 300;
const MAX_BAN_DURATION_SECS: i64 = 31_536_000;
#[derive(Deserialize, Serialize)]
struct BlacklistMetadata {
created_at_unix: i64,
expires_at_unix: i64,
reason: String,
}
/// Restore current durable bans after subscribing to the event channel.
pub(super) async fn reconcile(client: &redis::Client, blacklist: &Blacklist) -> anyhow::Result<()> {
let mut conn = client.get_multiplexed_async_connection().await?;
let members: Vec<String> = redis::cmd("SMEMBERS").arg(BLACKLIST_KEY).query_async(&mut conn).await?;
let metadata: HashMap<String, String> = conn.hgetall(BLACKLIST_METADATA_KEY).await?;
let now = chrono::Utc::now().timestamp();
let mut active = Vec::with_capacity(members.len());
for target in members {
let ip: std::net::IpAddr = match target.parse() {
Ok(ip) => ip,
Err(_) => continue,
};
let record = match metadata.get(&target) {
Some(encoded) => match serde_json::from_str::<BlacklistMetadata>(encoded) {
Ok(record) => record,
Err(_) => continue,
},
None => match legacy_record(&mut conn, &target, now).await? {
Some(record) => record,
None => continue,
},
};
if record.expires_at_unix <= now
|| chrono::DateTime::from_timestamp(record.created_at_unix, 0).is_none()
|| chrono::DateTime::from_timestamp(record.expires_at_unix, 0).is_none()
|| record.expires_at_unix.saturating_sub(now) > MAX_BAN_DURATION_SECS
{
continue;
}
let remaining = u64::try_from(record.expires_at_unix - now)?;
active.push((ip, Duration::from_secs(remaining), record.reason));
}
blacklist.replace_redis(active);
Ok(())
}
async fn legacy_record(
conn: &mut redis::aio::MultiplexedConnection,
target: &str,
now: i64,
) -> anyhow::Result<Option<BlacklistMetadata>> {
let legacy = BlacklistMetadata {
created_at_unix: now,
expires_at_unix: now.saturating_add(LEGACY_BAN_DURATION_SECS),
reason: "legacy_unverified".to_string(),
};
let inserted: i64 = redis::cmd("HSETNX")
.arg(BLACKLIST_METADATA_KEY)
.arg(target)
.arg(serde_json::to_string(&legacy)?)
.query_async(conn)
.await?;
if inserted == 1 {
return Ok(Some(legacy));
}
let current: Option<String> = conn.hget(BLACKLIST_METADATA_KEY, target).await?;
Ok(current.and_then(|value| serde_json::from_str(&value).ok()))
}