load an entire directory of bans

This commit is contained in:
Brandon Thomas
2023-03-10 01:49:11 -05:00
parent f3dd785331
commit 97bea31b1d
5 changed files with 55 additions and 7 deletions
@@ -0,0 +1,44 @@
use crate::middleware::ip_filter::ip_ban_list::ip_ban_list::IpBanList;
use crate::middleware::ip_filter::ip_ban_list::load_ip_set_from_file::load_ip_set_from_file;
use errors::AnyhowResult;
use std::path::Path;
pub fn load_ip_ban_list_from_directory<P: AsRef<Path>>(path: P) -> AnyhowResult<IpBanList> {
let ip_ban_list = IpBanList::new();
let paths = std::fs::read_dir(path)?;
for entry in paths {
let path = entry?.path();
let path_name = path.to_string_lossy().to_string();
let ip_set = load_ip_set_from_file(path)?;
ip_ban_list.add_set(path_name, ip_set)?;
}
Ok(ip_ban_list)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::middleware::ip_filter::ip_ban_list::load_ip_ban_list_from_directory::load_ip_ban_list_from_directory;
fn test_file(path_from_repo_root: &str) -> PathBuf {
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(format!("../../../{}", path_from_repo_root));
path
}
#[test]
fn test_load_ip_ban_list_from_directory() {
let directory = test_file("test_data/text_files/ip_ban_example/");
let ip_set = load_ip_ban_list_from_directory(directory).unwrap();
// Comments are not included
assert_eq!(ip_set.contains_ip_address("# this is test data").unwrap(), false);
// 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);
}
}
@@ -32,7 +32,7 @@ mod tests {
#[test]
fn test_load_ip_set_from_file() {
let filename = test_file("test_data/text_files/ip_addresses_and_comments.txt");
let filename = test_file("test_data/text_files/ip_ban_example/local_ip_addresses_and_comments.txt");
let ip_set = load_ip_set_from_file(filename).unwrap();
// Comments are not included
@@ -40,8 +40,9 @@ mod tests {
// IP addresses in the file are.
assert_eq!(ip_set.contains_ip_address("127.0.0.1"), true);
assert_eq!(ip_set.contains_ip_address("192.168.1.1"), false);
// Length is expected
assert_eq!(ip_set.len(), 4);
assert_eq!(ip_set.len(), 2);
}
}
@@ -1,3 +1,4 @@
pub mod ip_ban_list;
pub mod ip_set;
pub mod load_ip_ban_list_from_directory;
pub mod load_ip_set_from_file;
@@ -3,8 +3,3 @@
# local addresses
127.0.0.1
192.168.0.1
# remote addresses
4.4.4.4
8.8.8.8
@@ -0,0 +1,7 @@
# this is test data
# remote addresses
4.4.4.4
8.8.4.4
8.8.8.8