Spaces:
Runtime error
Runtime error
File size: 1,273 Bytes
9552aa0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | use std::collections::HashSet;
use std::net::{IpAddr, Ipv6Addr};
pub struct IpBlockList {
blocked_ips: HashSet<IpAddr>,
}
impl IpBlockList {
// Create a new empty block list
pub fn new() -> Self {
Self {
blocked_ips: HashSet::new(),
}
}
// Load the block list from a vector of IP address strings
pub fn load_from_vec(&mut self, ip_list: Vec<&str>) {
for ip_str in ip_list {
match ip_str {
"localhost" => {
self
.blocked_ips
.insert(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).into());
}
_ => {
if let Ok(ip) = ip_str.parse::<IpAddr>() {
self.blocked_ips.insert(ip.to_canonical());
}
}
}
}
}
// Check if an IP address is blocked
pub fn is_blocked(&self, ip: IpAddr) -> bool {
self.blocked_ips.contains(&ip.to_canonical())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ip_block_list() {
let mut block_list = IpBlockList::new();
block_list.load_from_vec(vec!["192.168.1.1", "10.0.0.1"]);
assert!(block_list.is_blocked("192.168.1.1".parse().unwrap()));
assert!(block_list.is_blocked("10.0.0.1".parse().unwrap()));
assert!(!block_list.is_blocked("8.8.8.8".parse().unwrap()));
}
}
|