@@ -8,7 +8,7 @@ use prefix_trie::joint::set::JointPrefixSet;
88#[ derive( Clone , Default , Debug ) ]
99pub struct IpBlacklist {
1010 ips : Arc < HashSet < IpAddr > > ,
11- networks : Arc < JointPrefixSet < IpNet > > ,
11+ cidrs : Arc < JointPrefixSet < IpNet > > ,
1212 is_loading : bool ,
1313}
1414
@@ -18,37 +18,46 @@ impl IpBlacklist {
1818 return IpBlacklist :: default ( ) ;
1919 } ;
2020 let mut ips = HashSet :: new ( ) ;
21- let mut networks = JointPrefixSet :: new ( ) ;
21+ let mut cidrs = JointPrefixSet :: new ( ) ;
2222 for line in buf. lines ( ) {
2323 let Some ( first) = line. split_whitespace ( ) . next ( ) else {
2424 continue ;
2525 } ;
2626
2727 if let Ok ( ip) = first. parse :: < IpAddr > ( ) {
2828 ips. insert ( ip) ;
29- } else if let Ok ( network ) = first. parse :: < IpNet > ( ) {
30- networks . insert ( network ) ;
29+ } else if let Ok ( cidr ) = first. parse :: < IpNet > ( ) {
30+ cidrs . insert ( cidr ) ;
3131 }
3232 }
3333 IpBlacklist {
3434 ips : Arc :: new ( ips) ,
35- networks : Arc :: new ( networks ) ,
35+ cidrs : Arc :: new ( cidrs ) ,
3636 is_loading : false ,
3737 }
3838 }
3939
4040 pub fn contains ( & self , ip : & IpAddr ) -> bool {
41- self . ips . contains ( ip) || self . networks . get_lpm ( & IpNet :: from ( * ip) ) . is_some ( )
41+ self . ips . contains ( ip) || self . cidrs . get_lpm ( & IpNet :: from ( * ip) ) . is_some ( )
4242 }
4343
4444 pub fn is_invalid ( & self ) -> bool {
45- self . ips . is_empty ( ) && self . networks . is_empty ( ) && !self . is_loading
45+ self . ips . is_empty ( ) && self . cidrs . is_empty ( ) && !self . is_loading
4646 }
4747
4848 pub fn is_loading ( & self ) -> bool {
4949 self . is_loading
5050 }
5151
52+ pub fn imported_items_info ( & self ) -> Option < String > {
53+ match ( self . ips . len ( ) , self . cidrs . len ( ) ) {
54+ ( 0 , 0 ) => None ,
55+ ( ips, 0 ) => Some ( format ! ( "(IPs: {ips})" ) ) ,
56+ ( 0 , cidrs) => Some ( format ! ( "(CIDRs: {cidrs})" ) ) ,
57+ ( ips, cidrs) => Some ( format ! ( "(IPs: {ips}, CIDRs: {cidrs})" ) ) ,
58+ }
59+ }
60+
5261 pub fn start_loading ( & mut self ) {
5362 self . is_loading = true ;
5463 }
@@ -67,7 +76,11 @@ mod tests {
6776 assert ! ( !blacklist. is_invalid( ) ) ;
6877 assert ! ( !blacklist. is_loading( ) ) ;
6978 assert_eq ! ( blacklist. ips. len( ) , 4 ) ;
70- assert_eq ! ( blacklist. networks. len( ) , 0 ) ;
79+ assert_eq ! ( blacklist. cidrs. len( ) , 0 ) ;
80+ assert_eq ! (
81+ blacklist. imported_items_info( ) ,
82+ Some ( "(IPs: 4)" . to_string( ) )
83+ ) ;
7184
7285 assert ! ( blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 8 , 8 , 8 , 8 ) ) ) ) ;
7386 assert ! ( blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 1 , 2 , 3 , 255 ) ) ) ) ;
@@ -88,7 +101,8 @@ mod tests {
88101 assert ! ( blacklist. is_invalid( ) ) ;
89102 assert ! ( !blacklist. is_loading( ) ) ;
90103 assert_eq ! ( blacklist. ips. len( ) , 0 ) ;
91- assert_eq ! ( blacklist. networks. len( ) , 0 ) ;
104+ assert_eq ! ( blacklist. cidrs. len( ) , 0 ) ;
105+ assert_eq ! ( blacklist. imported_items_info( ) , None ) ;
92106
93107 assert ! ( !blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 8 , 8 , 8 , 8 ) ) ) ) ;
94108 assert ! ( !blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 0 , 0 , 0 , 0 ) ) ) ) ;
@@ -105,7 +119,11 @@ mod tests {
105119 assert ! ( !blacklist. is_invalid( ) ) ;
106120 assert ! ( !blacklist. is_loading( ) ) ;
107121 assert_eq ! ( blacklist. ips. len( ) , 2 ) ;
108- assert_eq ! ( blacklist. networks. len( ) , 4 ) ;
122+ assert_eq ! ( blacklist. cidrs. len( ) , 4 ) ;
123+ assert_eq ! (
124+ blacklist. imported_items_info( ) ,
125+ Some ( "(IPs: 2, CIDRs: 4)" . to_string( ) )
126+ ) ;
109127
110128 assert ! ( blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 8 , 8 , 8 , 8 ) ) ) ) ;
111129 assert ! ( blacklist. contains( & "2001:db8::1" . parse:: <IpAddr >( ) . unwrap( ) ) ) ;
@@ -136,7 +154,11 @@ mod tests {
136154 assert ! ( !blacklist. is_invalid( ) ) ;
137155 assert ! ( !blacklist. is_loading( ) ) ;
138156 assert_eq ! ( blacklist. ips. len( ) , 0 ) ;
139- assert_eq ! ( blacklist. networks. len( ) , 1 ) ;
157+ assert_eq ! ( blacklist. cidrs. len( ) , 1 ) ;
158+ assert_eq ! (
159+ blacklist. imported_items_info( ) ,
160+ Some ( "(CIDRs: 1)" . to_string( ) )
161+ ) ;
140162
141163 assert ! ( blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 1 , 2 , 3 , 1 ) ) ) ) ;
142164 assert ! ( !blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 1 , 2 , 4 , 1 ) ) ) ) ;
@@ -150,7 +172,11 @@ mod tests {
150172
151173 assert ! ( !blacklist. is_invalid( ) ) ;
152174 assert_eq ! ( blacklist. ips. len( ) , 0 ) ;
153- assert_eq ! ( blacklist. networks. len( ) , 6 ) ;
175+ assert_eq ! ( blacklist. cidrs. len( ) , 6 ) ;
176+ assert_eq ! (
177+ blacklist. imported_items_info( ) ,
178+ Some ( "(CIDRs: 6)" . to_string( ) )
179+ ) ;
154180
155181 assert ! ( blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 209 , 186 , 20 , 0 ) ) ) ) ;
156182 assert ! ( blacklist. contains( & IpAddr :: V4 ( Ipv4Addr :: new( 209 , 186 , 23 , 255 ) ) ) ) ;
0 commit comments