|
| 1 | +//! Banning service for UDP tracker. |
| 2 | +//! |
| 3 | +//! It bans clients that send invalid connection id's. |
| 4 | +//! |
| 5 | +//! It uses two levels of filtering: |
| 6 | +//! |
| 7 | +//! 1. First, tt uses a Counting Bloom Filter to keep track of the number of |
| 8 | +//! connection ID errors per ip. That means there can be false positives, but |
| 9 | +//! not false negatives. 1 out of 100000 requests will be a false positive |
| 10 | +//! and the client will be banned and not receive a response. |
| 11 | +//! 2. Since we want to avoid false positives (banning a client that is not |
| 12 | +//! sending invalid connection id's), we use a `HashMap` to keep track of the |
| 13 | +//! exact number of connection ID errors per ip. |
| 14 | +//! |
| 15 | +//! This two level filtering is to avoid false positives. It has the advantage |
| 16 | +//! of being fast by using a Counting Bloom Filter and not having false |
| 17 | +//! negatives at the cost of increasing the memory usage. |
| 18 | +use std::collections::HashMap; |
| 19 | +use std::net::IpAddr; |
| 20 | + |
| 21 | +use bloom::{CountingBloomFilter, ASMS}; |
| 22 | +use tokio::time::Instant; |
| 23 | +use url::Url; |
| 24 | + |
| 25 | +use crate::servers::udp::UDP_TRACKER_LOG_TARGET; |
| 26 | + |
| 27 | +pub struct BanService { |
| 28 | + max_connection_id_errors_per_ip: u32, |
| 29 | + fuzzy_error_counter: CountingBloomFilter, |
| 30 | + accurate_error_counter: HashMap<IpAddr, u32>, |
| 31 | + local_addr: Url, |
| 32 | + last_connection_id_errors_reset: Instant, |
| 33 | +} |
| 34 | + |
| 35 | +impl BanService { |
| 36 | + #[must_use] |
| 37 | + pub fn new(max_connection_id_errors_per_ip: u32, local_addr: Url) -> Self { |
| 38 | + Self { |
| 39 | + max_connection_id_errors_per_ip, |
| 40 | + local_addr, |
| 41 | + fuzzy_error_counter: CountingBloomFilter::with_rate(4, 0.01, 100), |
| 42 | + accurate_error_counter: HashMap::new(), |
| 43 | + last_connection_id_errors_reset: tokio::time::Instant::now(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub fn increase_counter(&mut self, ip: &IpAddr) { |
| 48 | + self.fuzzy_error_counter.insert(&ip.to_string()); |
| 49 | + *self.accurate_error_counter.entry(*ip).or_insert(0) += 1; |
| 50 | + } |
| 51 | + |
| 52 | + #[must_use] |
| 53 | + pub fn get_count(&self, ip: &IpAddr) -> Option<u32> { |
| 54 | + self.accurate_error_counter.get(ip).copied() |
| 55 | + } |
| 56 | + |
| 57 | + #[must_use] |
| 58 | + pub fn get_estimate_count(&self, ip: &IpAddr) -> u32 { |
| 59 | + self.fuzzy_error_counter.estimate_count(&ip.to_string()) |
| 60 | + } |
| 61 | + |
| 62 | + /// Returns true if the given ip address is banned. |
| 63 | + #[must_use] |
| 64 | + pub fn is_banned(&self, ip: &IpAddr) -> bool { |
| 65 | + // First check if the ip is in the bloom filter (fast check) |
| 66 | + if self.fuzzy_error_counter.estimate_count(&ip.to_string()) <= self.max_connection_id_errors_per_ip { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // Check with the exact counter (to avoid false positives) |
| 71 | + match self.get_count(ip) { |
| 72 | + Some(count) => count > self.max_connection_id_errors_per_ip, |
| 73 | + None => false, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Resets the filters and updates the reset timestamp. |
| 78 | + pub fn reset_bans(&mut self) { |
| 79 | + self.fuzzy_error_counter.clear(); |
| 80 | + |
| 81 | + self.accurate_error_counter.clear(); |
| 82 | + |
| 83 | + self.last_connection_id_errors_reset = Instant::now(); |
| 84 | + |
| 85 | + let local_addr = self.local_addr.to_string(); |
| 86 | + tracing::info!(target: UDP_TRACKER_LOG_TARGET, local_addr, "Udp::run_udp_server::loop (connection id errors filter cleared)"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use std::net::IpAddr; |
| 93 | + |
| 94 | + use super::BanService; |
| 95 | + |
| 96 | + /// Sample service with one day ban duration. |
| 97 | + fn ban_service(counter_limit: u32) -> BanService { |
| 98 | + let udp_tracker_url = "udp://127.0.0.1".parse().unwrap(); |
| 99 | + BanService::new(counter_limit, udp_tracker_url) |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn it_should_increase_the_errors_counter_for_a_given_ip() { |
| 104 | + let mut ban_service = ban_service(1); |
| 105 | + |
| 106 | + let ip: IpAddr = "127.0.0.2".parse().unwrap(); |
| 107 | + |
| 108 | + ban_service.increase_counter(&ip); |
| 109 | + |
| 110 | + assert_eq!(ban_service.get_count(&ip), Some(1)); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn it_should_ban_ips_with_counters_exceeding_a_predefined_limit() { |
| 115 | + let mut ban_service = ban_service(1); |
| 116 | + |
| 117 | + let ip: IpAddr = "127.0.0.2".parse().unwrap(); |
| 118 | + |
| 119 | + ban_service.increase_counter(&ip); // Counter = 1 |
| 120 | + ban_service.increase_counter(&ip); // Counter = 2 |
| 121 | + |
| 122 | + println!("Counter: {}", ban_service.get_count(&ip).unwrap()); |
| 123 | + |
| 124 | + assert!(ban_service.is_banned(&ip)); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn it_should_not_ban_ips_whose_counters_do_not_exceed_the_predefined_limit() { |
| 129 | + let mut ban_service = ban_service(1); |
| 130 | + |
| 131 | + let ip: IpAddr = "127.0.0.2".parse().unwrap(); |
| 132 | + |
| 133 | + ban_service.increase_counter(&ip); |
| 134 | + |
| 135 | + assert!(!ban_service.is_banned(&ip)); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn it_should_allow_resetting_all_the_counters() { |
| 140 | + let mut ban_service = ban_service(1); |
| 141 | + |
| 142 | + let ip: IpAddr = "127.0.0.2".parse().unwrap(); |
| 143 | + |
| 144 | + ban_service.increase_counter(&ip); // Counter = 1 |
| 145 | + |
| 146 | + ban_service.reset_bans(); |
| 147 | + |
| 148 | + assert_eq!(ban_service.get_estimate_count(&ip), 0); |
| 149 | + } |
| 150 | +} |
0 commit comments