1
1
use std:: net:: IpAddr ;
2
- use std:: time:: Duration ;
3
2
4
3
use bloom:: { CountingBloomFilter , ASMS } ;
5
4
use tokio:: time:: Instant ;
6
5
use url:: Url ;
7
6
8
7
use crate :: servers:: udp:: UDP_TRACKER_LOG_TARGET ;
9
8
10
- /// The maximum number of connection id errors per ip. Clients will be banned if
11
- /// they exceed this limit.
12
- pub const MAX_CONNECTION_ID_ERRORS_PER_IP : u32 = 10 ;
13
- pub const RESET_CONNECTION_ID_ERRORS_COUNTER_FREQUENCY_IN_SECS : u64 = 3600 ;
14
-
15
9
pub struct BanService {
16
10
max_connection_id_errors_per_ip : u32 ,
17
- ban_duration : Duration ,
18
11
connection_id_errors_per_ip : CountingBloomFilter ,
19
12
local_addr : Url ,
20
13
last_connection_id_errors_reset : Instant ,
21
14
}
22
15
23
16
impl BanService {
24
17
#[ must_use]
25
- pub fn new ( max_connection_id_errors_per_ip : u32 , duration_in_seconds : u64 , local_addr : Url ) -> Self {
18
+ pub fn new ( max_connection_id_errors_per_ip : u32 , local_addr : Url ) -> Self {
26
19
Self {
27
20
max_connection_id_errors_per_ip,
28
- ban_duration : Duration :: from_secs ( duration_in_seconds) ,
29
21
local_addr,
30
22
connection_id_errors_per_ip : CountingBloomFilter :: with_rate ( 4 , 0.01 , 100 ) ,
31
23
last_connection_id_errors_reset : tokio:: time:: Instant :: now ( ) ,
@@ -48,14 +40,8 @@ impl BanService {
48
40
connection_id_errors_from_ip > self . max_connection_id_errors_per_ip
49
41
}
50
42
51
- pub fn run_bans_cleaner ( & mut self ) {
52
- if self . last_connection_id_errors_reset . elapsed ( ) >= self . ban_duration {
53
- self . reset_filter ( ) ;
54
- }
55
- }
56
-
57
43
/// Resets the filter and updates the reset timestamp.
58
- pub fn reset_filter ( & mut self ) {
44
+ pub fn reset_bans ( & mut self ) {
59
45
self . connection_id_errors_per_ip . clear ( ) ;
60
46
61
47
self . last_connection_id_errors_reset = Instant :: now ( ) ;
@@ -68,23 +54,18 @@ impl BanService {
68
54
#[ cfg( test) ]
69
55
mod tests {
70
56
use std:: net:: IpAddr ;
71
- use std:: time:: Duration ;
72
-
73
- use tokio:: time:: sleep;
74
57
75
58
use super :: BanService ;
76
59
77
60
/// Sample service with one day ban duration.
78
- fn service_with_one_day_ban ( counter_limit : u32 ) -> BanService {
79
- let one_day_in_seconds = 86400 ;
61
+ fn ban_service ( counter_limit : u32 ) -> BanService {
80
62
let udp_tracker_url = "udp://127.0.0.1" . parse ( ) . unwrap ( ) ;
81
-
82
- BanService :: new ( counter_limit, one_day_in_seconds, udp_tracker_url)
63
+ BanService :: new ( counter_limit, udp_tracker_url)
83
64
}
84
65
85
66
#[ test]
86
67
fn it_should_increase_the_ip_counter ( ) {
87
- let mut ban_service = service_with_one_day_ban ( 1 ) ;
68
+ let mut ban_service = ban_service ( 1 ) ;
88
69
89
70
let ip: IpAddr = "127.0.0.2" . parse ( ) . unwrap ( ) ;
90
71
@@ -95,7 +76,7 @@ mod tests {
95
76
96
77
#[ test]
97
78
fn it_should_ban_ips_with_counters_exceeding_a_predefined_limit ( ) {
98
- let mut ban_service = service_with_one_day_ban ( 1 ) ;
79
+ let mut ban_service = ban_service ( 1 ) ;
99
80
100
81
let ip: IpAddr = "127.0.0.2" . parse ( ) . unwrap ( ) ;
101
82
@@ -107,7 +88,7 @@ mod tests {
107
88
108
89
#[ test]
109
90
fn it_should_not_ban_ips_whose_counters_do_not_exceed_the_predefined_limit ( ) {
110
- let mut ban_service = service_with_one_day_ban ( 1 ) ;
91
+ let mut ban_service = ban_service ( 1 ) ;
111
92
112
93
let ip: IpAddr = "127.0.0.2" . parse ( ) . unwrap ( ) ;
113
94
@@ -118,31 +99,13 @@ mod tests {
118
99
119
100
#[ test]
120
101
fn it_should_allow_resetting_all_the_counters ( ) {
121
- let mut ban_service = service_with_one_day_ban ( 1 ) ;
122
-
123
- let ip: IpAddr = "127.0.0.2" . parse ( ) . unwrap ( ) ;
124
-
125
- ban_service. increase_counter ( & ip) ; // Counter = 1
126
-
127
- ban_service. reset_filter ( ) ;
128
-
129
- assert_eq ! ( ban_service. get_counter( & ip) , 0 ) ;
130
- }
131
-
132
- #[ tokio:: test]
133
- async fn it_should_allow_run_a_bans_cleaner_to_reset_the_counters_periodically ( ) {
134
- let udp_tracker_url = "udp://127.0.0.1" . parse ( ) . unwrap ( ) ;
135
- let ban_duration_in_secs = 1 ;
136
-
137
- let mut ban_service = BanService :: new ( 1 , ban_duration_in_secs, udp_tracker_url) ;
102
+ let mut ban_service = ban_service ( 1 ) ;
138
103
139
104
let ip: IpAddr = "127.0.0.2" . parse ( ) . unwrap ( ) ;
140
105
141
106
ban_service. increase_counter ( & ip) ; // Counter = 1
142
107
143
- sleep ( Duration :: from_secs ( 2 ) ) . await ;
144
-
145
- ban_service. run_bans_cleaner ( ) ;
108
+ ban_service. reset_bans ( ) ;
146
109
147
110
assert_eq ! ( ban_service. get_counter( & ip) , 0 ) ;
148
111
}
0 commit comments