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

@ -10,6 +10,7 @@ struct BanEntry {
pub struct Blacklist {
entries: Arc<DashMap<IpAddr, BanEntry>>,
redis_entries: Arc<DashMap<IpAddr, BanEntry>>,
}
impl Default for Blacklist {
@ -22,18 +23,12 @@ impl Blacklist {
pub fn new() -> Self {
Self {
entries: Arc::new(DashMap::new()),
redis_entries: Arc::new(DashMap::new()),
}
}
pub fn is_blocked(&self, ip: IpAddr) -> bool {
if let Some(entry) = self.entries.get(&ip) {
if entry.expires > Instant::now() {
return true;
}
drop(entry);
self.entries.remove(&ip);
}
false
Self::is_blocked_in(&self.entries, ip) || Self::is_blocked_in(&self.redis_entries, ip)
}
pub fn add(&self, ip: IpAddr, duration: Duration, reason: &str) {
@ -50,16 +45,53 @@ impl Blacklist {
self.entries.remove(&ip);
}
pub fn add_redis(&self, ip: IpAddr, duration: Duration, reason: &str) {
self.redis_entries.insert(
ip,
BanEntry {
expires: Instant::now() + duration,
_reason: reason.to_string(),
},
);
}
pub fn remove_redis(&self, ip: IpAddr) {
self.redis_entries.remove(&ip);
}
/// Replace Redis-managed bans while preserving local detector bans.
pub fn replace_redis<I>(&self, entries: I)
where
I: IntoIterator<Item = (IpAddr, Duration, String)>,
{
self.redis_entries.clear();
for (ip, duration, reason) in entries {
self.add_redis(ip, duration, &reason);
}
}
pub fn len(&self) -> usize {
self.entries.len()
self.entries.len() + self.redis_entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
self.entries.is_empty() && self.redis_entries.is_empty()
}
pub fn clear_expired(&self) {
self.entries.retain(|_, entry| entry.expires > Instant::now());
self.redis_entries.retain(|_, entry| entry.expires > Instant::now());
}
fn is_blocked_in(entries: &DashMap<IpAddr, BanEntry>, ip: IpAddr) -> bool {
if let Some(entry) = entries.get(&ip) {
if entry.expires > Instant::now() {
return true;
}
drop(entry);
entries.remove(&ip);
}
false
}
}

View file

@ -0,0 +1,59 @@
use dashmap::DashMap;
use std::net::IpAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
/// Caps simultaneous in-flight connections per source IP.
pub struct ConnectionLimiter {
counts: Arc<DashMap<IpAddr, Arc<AtomicUsize>>>,
limit: usize,
}
impl ConnectionLimiter {
#[must_use]
pub fn new(limit: usize) -> Self {
Self {
counts: Arc::new(DashMap::new()),
limit: limit.max(1),
}
}
pub fn try_acquire(&self, ip: IpAddr) -> Option<ConnectionPermit> {
let entry = self.counts.entry(ip).or_insert_with(|| Arc::new(AtomicUsize::new(0)));
let count = Arc::clone(entry.value());
let current = count.fetch_add(1, Ordering::AcqRel);
drop(entry);
if current >= self.limit {
count.fetch_sub(1, Ordering::AcqRel);
self.remove_if_idle(ip, &count);
return None;
}
Some(ConnectionPermit {
ip,
count,
counts: Arc::clone(&self.counts),
})
}
fn remove_if_idle(&self, ip: IpAddr, count: &Arc<AtomicUsize>) {
self.counts.remove_if(&ip, |_, current| {
Arc::ptr_eq(current, count) && current.load(Ordering::Acquire) == 0
});
}
}
pub struct ConnectionPermit {
ip: IpAddr,
count: Arc<AtomicUsize>,
counts: Arc<DashMap<IpAddr, Arc<AtomicUsize>>>,
}
impl Drop for ConnectionPermit {
fn drop(&mut self) {
if self.count.fetch_sub(1, Ordering::AcqRel) == 1 {
self.counts.remove_if(&self.ip, |_, current| {
Arc::ptr_eq(current, &self.count) && current.load(Ordering::Acquire) == 0
});
}
}
}

View file

@ -1,5 +1,6 @@
//! Фильтры входящего трафика.
pub mod blacklist;
pub mod connection_limit;
pub mod geo;
pub mod rate_limit;