style: switch rustfmt to hard tabs; document tests-separate rule in TODO

- rustfmt.toml: hard_tabs = true; whole tree reformatted (no logic changes)
- TODO §0.7: tests live in tests/, never inline #[cfg(test)] in code files
  (legacy inline tests to be migrated, see Backlog)
- TODO: add backlog item for the inline-test migration
This commit is contained in:
Loki 2026-11-15 22:34:29 +01:00
parent d8393e0f39
commit 18a0ffa53d
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
96 changed files with 6542 additions and 6537 deletions

View file

@ -8,173 +8,173 @@ const LOCAL_BAN_EVENT_CAPACITY: usize = 4096;
#[derive(Clone, Debug)]
pub struct LocalBanEvent {
pub ip: IpAddr,
pub expires_at_unix: i64,
pub reason: String,
pub ip: IpAddr,
pub expires_at_unix: i64,
pub reason: String,
}
struct BanEntry {
expires: Instant,
_reason: String,
expires: Instant,
_reason: String,
}
pub struct Blacklist {
entries: Arc<DashMap<IpAddr, BanEntry>>,
redis_entries: Arc<DashMap<IpAddr, BanEntry>>,
local_ban_events: broadcast::Sender<LocalBanEvent>,
entries: Arc<DashMap<IpAddr, BanEntry>>,
redis_entries: Arc<DashMap<IpAddr, BanEntry>>,
local_ban_events: broadcast::Sender<LocalBanEvent>,
}
impl Default for Blacklist {
fn default() -> Self {
Self::new()
}
fn default() -> Self {
Self::new()
}
}
impl Blacklist {
pub fn new() -> Self {
let (local_ban_events, _) = broadcast::channel(LOCAL_BAN_EVENT_CAPACITY);
Self {
entries: Arc::new(DashMap::new()),
redis_entries: Arc::new(DashMap::new()),
local_ban_events,
}
}
pub fn new() -> Self {
let (local_ban_events, _) = broadcast::channel(LOCAL_BAN_EVENT_CAPACITY);
Self {
entries: Arc::new(DashMap::new()),
redis_entries: Arc::new(DashMap::new()),
local_ban_events,
}
}
pub fn is_blocked(&self, ip: IpAddr) -> bool {
Self::is_blocked_in(&self.entries, ip) || Self::is_blocked_in(&self.redis_entries, ip)
}
pub fn is_blocked(&self, ip: IpAddr) -> bool {
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) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let expires_at_unix = now.saturating_add(duration.as_secs()).min(i64::MAX as u64) as i64;
self.entries.insert(
ip,
BanEntry {
expires: Instant::now() + duration,
_reason: reason.to_string(),
},
);
let _ = self.local_ban_events.send(LocalBanEvent {
ip,
expires_at_unix,
reason: reason.to_string(),
});
}
pub fn add(&self, ip: IpAddr, duration: Duration, reason: &str) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let expires_at_unix = now.saturating_add(duration.as_secs()).min(i64::MAX as u64) as i64;
self.entries.insert(
ip,
BanEntry {
expires: Instant::now() + duration,
_reason: reason.to_string(),
},
);
let _ = self.local_ban_events.send(LocalBanEvent {
ip,
expires_at_unix,
reason: reason.to_string(),
});
}
pub fn subscribe_local_bans(&self) -> broadcast::Receiver<LocalBanEvent> {
self.local_ban_events.subscribe()
}
pub fn subscribe_local_bans(&self) -> broadcast::Receiver<LocalBanEvent> {
self.local_ban_events.subscribe()
}
pub fn local_snapshot(&self) -> Vec<LocalBanEvent> {
self.entries
.iter()
.filter(|entry| entry.expires > Instant::now())
.map(|entry| LocalBanEvent {
ip: *entry.key(),
expires_at_unix: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
.saturating_add(entry.expires.saturating_duration_since(Instant::now()).as_secs())
.min(i64::MAX as u64) as i64,
reason: entry._reason.clone(),
})
.collect()
}
pub fn local_snapshot(&self) -> Vec<LocalBanEvent> {
self.entries
.iter()
.filter(|entry| entry.expires > Instant::now())
.map(|entry| LocalBanEvent {
ip: *entry.key(),
expires_at_unix: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
.saturating_add(entry.expires.saturating_duration_since(Instant::now()).as_secs())
.min(i64::MAX as u64) as i64,
reason: entry._reason.clone(),
})
.collect()
}
pub fn remove(&self, ip: IpAddr) {
self.entries.remove(&ip);
}
pub fn remove(&self, ip: IpAddr) {
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 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);
}
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);
}
}
/// 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.redis_entries.len()
}
pub fn len(&self) -> usize {
self.entries.len() + self.redis_entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty() && self.redis_entries.is_empty()
}
pub fn is_empty(&self) -> bool {
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());
}
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
}
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
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{IpAddr, Ipv4Addr};
use super::*;
use std::net::{IpAddr, Ipv4Addr};
fn ip(octets: [u8; 4]) -> IpAddr {
IpAddr::V4(Ipv4Addr::from(octets))
}
fn ip(octets: [u8; 4]) -> IpAddr {
IpAddr::V4(Ipv4Addr::from(octets))
}
#[test]
fn test_blacklist_block() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_secs(60), "test");
assert!(bl.is_blocked(ip([1, 2, 3, 4])));
}
#[test]
fn test_blacklist_block() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_secs(60), "test");
assert!(bl.is_blocked(ip([1, 2, 3, 4])));
}
#[test]
fn test_blacklist_not_blocked() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_secs(60), "test");
assert!(!bl.is_blocked(ip([5, 6, 7, 8])));
}
#[test]
fn test_blacklist_not_blocked() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_secs(60), "test");
assert!(!bl.is_blocked(ip([5, 6, 7, 8])));
}
#[test]
fn test_blacklist_expired() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_millis(1), "test");
std::thread::sleep(Duration::from_millis(2));
assert!(!bl.is_blocked(ip([1, 2, 3, 4])));
}
#[test]
fn test_blacklist_expired() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_millis(1), "test");
std::thread::sleep(Duration::from_millis(2));
assert!(!bl.is_blocked(ip([1, 2, 3, 4])));
}
#[test]
fn test_blacklist_remove() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_secs(60), "test");
bl.remove(ip([1, 2, 3, 4]));
assert!(!bl.is_blocked(ip([1, 2, 3, 4])));
}
#[test]
fn test_blacklist_remove() {
let bl = Blacklist::new();
bl.add(ip([1, 2, 3, 4]), Duration::from_secs(60), "test");
bl.remove(ip([1, 2, 3, 4]));
assert!(!bl.is_blocked(ip([1, 2, 3, 4])));
}
}

View file

@ -5,55 +5,55 @@ 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,
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),
}
}
#[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),
})
}
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
});
}
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>>>,
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
});
}
}
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,35 +1,35 @@
#[cfg(feature = "geoip")]
pub struct GeoIp {
#[allow(dead_code)]
reader: maxminddb::Reader<Vec<u8>>,
#[allow(dead_code)]
reader: maxminddb::Reader<Vec<u8>>,
}
#[cfg(feature = "geoip")]
impl GeoIp {
pub fn new(db_path: &str) -> anyhow::Result<Self> {
let reader = maxminddb::Reader::open_readfile(db_path)?;
Ok(Self { reader })
}
pub fn new(db_path: &str) -> anyhow::Result<Self> {
let reader = maxminddb::Reader::open_readfile(db_path)?;
Ok(Self { reader })
}
}
pub enum IpCategory {
Residential,
Datacenter,
Mobile,
Vpn,
Tor,
Unknown,
Residential,
Datacenter,
Mobile,
Vpn,
Tor,
Unknown,
}
impl std::fmt::Display for IpCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Residential => write!(f, "residential"),
Self::Datacenter => write!(f, "datacenter"),
Self::Mobile => write!(f, "mobile"),
Self::Vpn => write!(f, "vpn"),
Self::Tor => write!(f, "tor"),
Self::Unknown => write!(f, "unknown"),
}
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Residential => write!(f, "residential"),
Self::Datacenter => write!(f, "datacenter"),
Self::Mobile => write!(f, "mobile"),
Self::Vpn => write!(f, "vpn"),
Self::Tor => write!(f, "tor"),
Self::Unknown => write!(f, "unknown"),
}
}
}

View file

@ -9,144 +9,144 @@ const EVICTION_IDLE: Duration = Duration::from_secs(600);
const SWEEP_INTERVAL: Duration = Duration::from_secs(60);
struct Bucket {
tokens: f64,
last_refill: Instant,
last_access: Instant,
tokens: f64,
last_refill: Instant,
last_access: Instant,
}
pub struct RateLimiter {
buckets: Arc<DashMap<IpAddr, Bucket>>,
max_tokens: f64,
refill_rate: f64,
_refill_interval: Duration,
epoch: Instant,
last_sweep_elapsed: AtomicU64,
eviction_idle: Duration,
buckets: Arc<DashMap<IpAddr, Bucket>>,
max_tokens: f64,
refill_rate: f64,
_refill_interval: Duration,
epoch: Instant,
last_sweep_elapsed: AtomicU64,
eviction_idle: Duration,
}
impl RateLimiter {
pub fn new(rate_per_sec: f64, burst: f64) -> Self {
Self::with_eviction(rate_per_sec, burst, EVICTION_IDLE)
}
pub fn new(rate_per_sec: f64, burst: f64) -> Self {
Self::with_eviction(rate_per_sec, burst, EVICTION_IDLE)
}
fn with_eviction(rate_per_sec: f64, burst: f64, eviction_idle: Duration) -> Self {
Self {
buckets: Arc::new(DashMap::new()),
max_tokens: burst,
refill_rate: rate_per_sec,
_refill_interval: Duration::from_secs(1),
epoch: Instant::now(),
last_sweep_elapsed: AtomicU64::new(0),
eviction_idle,
}
}
fn with_eviction(rate_per_sec: f64, burst: f64, eviction_idle: Duration) -> Self {
Self {
buckets: Arc::new(DashMap::new()),
max_tokens: burst,
refill_rate: rate_per_sec,
_refill_interval: Duration::from_secs(1),
epoch: Instant::now(),
last_sweep_elapsed: AtomicU64::new(0),
eviction_idle,
}
}
pub fn check(&self, ip: IpAddr) -> bool {
let now = Instant::now();
let mut entry = self.buckets.entry(ip).or_insert_with(|| Bucket {
tokens: self.max_tokens,
last_refill: now,
last_access: now,
});
pub fn check(&self, ip: IpAddr) -> bool {
let now = Instant::now();
let mut entry = self.buckets.entry(ip).or_insert_with(|| Bucket {
tokens: self.max_tokens,
last_refill: now,
last_access: now,
});
let elapsed = now.duration_since(entry.last_refill);
let refill = elapsed.as_secs_f64() * self.refill_rate;
entry.tokens = (entry.tokens + refill).min(self.max_tokens);
entry.last_refill = now;
entry.last_access = now;
let elapsed = now.duration_since(entry.last_refill);
let refill = elapsed.as_secs_f64() * self.refill_rate;
entry.tokens = (entry.tokens + refill).min(self.max_tokens);
entry.last_refill = now;
entry.last_access = now;
if entry.tokens >= 1.0 {
entry.tokens -= 1.0;
true
} else {
false
}
}
if entry.tokens >= 1.0 {
entry.tokens -= 1.0;
true
} else {
false
}
}
pub fn len(&self) -> usize {
self.buckets.len()
}
pub fn len(&self) -> usize {
self.buckets.len()
}
pub fn is_empty(&self) -> bool {
self.buckets.is_empty()
}
pub fn is_empty(&self) -> bool {
self.buckets.is_empty()
}
/// Эвиктит простаивающие бакеты. Запускается, когда бакетов больше
/// MAX_BUCKETS либо по расписанию (раз в SWEEP_INTERVAL).
pub fn sweep(&self) {
let now = Instant::now();
let elapsed_secs = now.duration_since(self.epoch).as_secs();
let last = self.last_sweep_elapsed.load(Ordering::Relaxed);
let due = last == 0 || elapsed_secs.saturating_sub(last) >= SWEEP_INTERVAL.as_secs();
let over_cap = self.buckets.len() > MAX_BUCKETS;
if !over_cap && !due {
return;
}
self.buckets
.retain(|_, b| now.duration_since(b.last_access) < self.eviction_idle);
self.last_sweep_elapsed.store(elapsed_secs, Ordering::Relaxed);
}
/// Эвиктит простаивающие бакеты. Запускается, когда бакетов больше
/// MAX_BUCKETS либо по расписанию (раз в SWEEP_INTERVAL).
pub fn sweep(&self) {
let now = Instant::now();
let elapsed_secs = now.duration_since(self.epoch).as_secs();
let last = self.last_sweep_elapsed.load(Ordering::Relaxed);
let due = last == 0 || elapsed_secs.saturating_sub(last) >= SWEEP_INTERVAL.as_secs();
let over_cap = self.buckets.len() > MAX_BUCKETS;
if !over_cap && !due {
return;
}
self.buckets
.retain(|_, b| now.duration_since(b.last_access) < self.eviction_idle);
self.last_sweep_elapsed.store(elapsed_secs, Ordering::Relaxed);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{IpAddr, Ipv4Addr};
use super::*;
use std::net::{IpAddr, Ipv4Addr};
fn test_ip(octet: u8) -> IpAddr {
IpAddr::V4(Ipv4Addr::new(10, 0, 0, octet))
}
fn test_ip(octet: u8) -> IpAddr {
IpAddr::V4(Ipv4Addr::new(10, 0, 0, octet))
}
#[test]
fn test_rate_limit_under() {
let limiter = RateLimiter::new(10.0, 10.0);
assert!(limiter.check(test_ip(1)));
}
#[test]
fn test_rate_limit_under() {
let limiter = RateLimiter::new(10.0, 10.0);
assert!(limiter.check(test_ip(1)));
}
#[test]
fn test_rate_limit_over() {
let limiter = RateLimiter::new(1.0, 1.0);
assert!(limiter.check(test_ip(1)));
assert!(!limiter.check(test_ip(1)));
}
#[test]
fn test_rate_limit_over() {
let limiter = RateLimiter::new(1.0, 1.0);
assert!(limiter.check(test_ip(1)));
assert!(!limiter.check(test_ip(1)));
}
#[test]
fn test_rate_limit_burst() {
let limiter = RateLimiter::new(1.0, 5.0);
for _ in 0..5 {
assert!(limiter.check(test_ip(2)));
}
assert!(!limiter.check(test_ip(2)));
}
#[test]
fn test_rate_limit_burst() {
let limiter = RateLimiter::new(1.0, 5.0);
for _ in 0..5 {
assert!(limiter.check(test_ip(2)));
}
assert!(!limiter.check(test_ip(2)));
}
#[test]
fn test_rate_limit_refill() {
let limiter = RateLimiter::new(100.0, 1.0);
assert!(limiter.check(test_ip(3)));
assert!(!limiter.check(test_ip(3)));
std::thread::sleep(Duration::from_millis(20));
assert!(limiter.check(test_ip(3)));
}
#[test]
fn test_rate_limit_refill() {
let limiter = RateLimiter::new(100.0, 1.0);
assert!(limiter.check(test_ip(3)));
assert!(!limiter.check(test_ip(3)));
std::thread::sleep(Duration::from_millis(20));
assert!(limiter.check(test_ip(3)));
}
#[test]
fn test_sweep_removes_idle_keeps_active() {
let limiter = RateLimiter::with_eviction(1.0, 10.0, Duration::from_millis(20));
limiter.check(test_ip(1));
limiter.check(test_ip(2));
std::thread::sleep(Duration::from_millis(50));
limiter.check(test_ip(2));
limiter.sweep();
assert_eq!(limiter.len(), 1);
assert!(!limiter.buckets.contains_key(&test_ip(1)));
assert!(limiter.buckets.contains_key(&test_ip(2)));
}
#[test]
fn test_sweep_removes_idle_keeps_active() {
let limiter = RateLimiter::with_eviction(1.0, 10.0, Duration::from_millis(20));
limiter.check(test_ip(1));
limiter.check(test_ip(2));
std::thread::sleep(Duration::from_millis(50));
limiter.check(test_ip(2));
limiter.sweep();
assert_eq!(limiter.len(), 1);
assert!(!limiter.buckets.contains_key(&test_ip(1)));
assert!(limiter.buckets.contains_key(&test_ip(2)));
}
#[test]
fn test_sweep_does_not_remove_active() {
let limiter = RateLimiter::with_eviction(1.0, 10.0, Duration::from_millis(50));
limiter.check(test_ip(1));
limiter.sweep();
assert_eq!(limiter.len(), 1);
assert!(limiter.buckets.contains_key(&test_ip(1)));
}
#[test]
fn test_sweep_does_not_remove_active() {
let limiter = RateLimiter::with_eviction(1.0, 10.0, Duration::from_millis(50));
limiter.check(test_ip(1));
limiter.sweep();
assert_eq!(limiter.len(), 1);
assert!(limiter.buckets.contains_key(&test_ip(1)));
}
}