Don't load IP ban sets that are empty
This commit is contained in:
parent
86382bf219
commit
554766bd2a
|
|
@ -47,7 +47,7 @@ impl IpBanList {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn total_len(&self) -> AnyhowResult<usize> {
|
||||
pub fn total_ip_address_count(&self) -> AnyhowResult<usize> {
|
||||
match self.ip_sets.read() {
|
||||
Err(_) => Err(anyhow!("Can't read lock")),
|
||||
Ok(sets) => {
|
||||
|
|
@ -57,6 +57,15 @@ impl IpBanList {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_count(&self) -> AnyhowResult<usize> {
|
||||
match self.ip_sets.read() {
|
||||
Err(_) => Err(anyhow!("Can't read lock")),
|
||||
Ok(sets) => {
|
||||
Ok(sets.len())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -38,4 +38,8 @@ impl IpSet {
|
|||
pub fn len(&self) -> usize {
|
||||
self.ip_set.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.ip_set.is_empty()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ pub fn load_ip_ban_list_from_directory<P: AsRef<Path>>(path: P) -> AnyhowResult<
|
|||
let path = entry?.path();
|
||||
let path_name = path.to_string_lossy().to_string();
|
||||
let ip_set = load_ip_set_from_file(path)?;
|
||||
if ip_set.is_empty() {
|
||||
continue;
|
||||
}
|
||||
ip_ban_list.add_set(path_name, ip_set)?;
|
||||
}
|
||||
|
||||
|
|
@ -40,5 +43,17 @@ mod tests {
|
|||
// IP addresses in both files are
|
||||
assert_eq!(ip_set.contains_ip_address("127.0.0.1").unwrap(), true);
|
||||
assert_eq!(ip_set.contains_ip_address("192.168.0.1").unwrap(), true);
|
||||
|
||||
// All five IPs were loaded
|
||||
assert_eq!(ip_set.total_ip_address_count().unwrap(), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_files_are_not_loaded() {
|
||||
let directory = test_file("test_data/text_files/ip_ban_example/");
|
||||
let ip_set = load_ip_ban_list_from_directory(directory).unwrap();
|
||||
|
||||
// NB: There are two files with IP addresses and a single empty file, which should not be loaded
|
||||
assert_eq!(ip_set.set_count().unwrap(), 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -475,7 +475,7 @@ fn load_static_container_ip_bans() -> IpBanList {
|
|||
let ip_ban_list = load_ip_ban_list_from_directory(ip_ban_directory)
|
||||
.unwrap_or(IpBanList::new());
|
||||
|
||||
info!("Static IP bans loaded: {}", ip_ban_list.total_len().unwrap_or(0));
|
||||
info!("Static IP bans loaded: {}", ip_ban_list.total_ip_address_count().unwrap_or(0));
|
||||
ip_ban_list
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ pub async fn poll_ip_bans(
|
|||
|
||||
match ip_ban_list.add_set(DYNAMIC_BAN_LIST_NAME.to_string(), ip_set) {
|
||||
Ok(_) => {
|
||||
let total_count = ip_ban_list.total_len().unwrap_or(0);
|
||||
let total_count = ip_ban_list.total_ip_address_count().unwrap_or(0);
|
||||
info!("Internal IP ban list updated! Total bans: {} ({} from database)",
|
||||
total_count, database_count);
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue