diff --git a/aquatic_common/src/access_list.rs b/aquatic_common/src/access_list.rs new file mode 100644 index 0000000..377f8eb --- /dev/null +++ b/aquatic_common/src/access_list.rs @@ -0,0 +1,77 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +use hashbrown::HashSet; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +pub enum AccessListType { + Allow, + Deny, + Ignore, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AccessListConfig { + pub path: PathBuf, + pub list_type: AccessListType, +} + +impl Default for AccessListConfig { + fn default() -> Self { + Self { + path: "".into(), + list_type: AccessListType::Ignore, + } + } +} + +#[derive(Default)] +pub struct AccessList(HashSet<[u8; 20]>); + +impl AccessList { + fn parse_info_hash(line: String) -> anyhow::Result<[u8; 20]> { + let mut bytes = [0u8; 20]; + + hex::decode_to_slice(line, &mut bytes)?; + + Ok(bytes) + } + + pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> { + let file = File::open(path)?; + let reader = BufReader::new(file); + + let mut new_list = HashSet::new(); + + for line in reader.lines() { + new_list.insert(Self::parse_info_hash(line?)?); + } + + self.0 = new_list; + + Ok(()) + } + + pub fn allows(&self, list_type: AccessListType, info_hash_bytes: &[u8; 20]) -> bool { + match list_type { + AccessListType::Allow => self.0.contains(info_hash_bytes), + AccessListType::Deny => !self.0.contains(info_hash_bytes), + AccessListType::Ignore => true, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_info_hash(){ + assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeee".to_owned()).is_ok()); + assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeeef".to_owned()).is_err()); + assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeee".to_owned()).is_err()); + assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeeö".to_owned()).is_err()); + } +} \ No newline at end of file diff --git a/aquatic_common/src/lib.rs b/aquatic_common/src/lib.rs index f5805cc..3e90bbc 100644 --- a/aquatic_common/src/lib.rs +++ b/aquatic_common/src/lib.rs @@ -1,71 +1,10 @@ -use std::fs::File; -use std::io::{BufRead, BufReader}; use std::net::IpAddr; -use std::path::PathBuf; use std::time::{Duration, Instant}; -use hashbrown::HashSet; use indexmap::IndexMap; use rand::Rng; -use serde::{Deserialize, Serialize}; -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] -pub enum AccessListType { - Allow, - Deny, - Ignore, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct AccessListConfig { - pub path: PathBuf, - pub list_type: AccessListType, -} - -impl Default for AccessListConfig { - fn default() -> Self { - Self { - path: "".into(), - list_type: AccessListType::Ignore, - } - } -} - -#[derive(Default)] -pub struct AccessList(HashSet<[u8; 20]>); - -impl AccessList { - fn parse_info_hash(line: String) -> anyhow::Result<[u8; 20]> { - let mut bytes = [0u8; 20]; - - hex::decode_to_slice(line, &mut bytes)?; - - Ok(bytes) - } - - pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> { - let file = File::open(path)?; - let reader = BufReader::new(file); - - let mut new_list = HashSet::new(); - - for line in reader.lines() { - new_list.insert(Self::parse_info_hash(line?)?); - } - - self.0 = new_list; - - Ok(()) - } - - pub fn allows(&self, list_type: AccessListType, info_hash_bytes: &[u8; 20]) -> bool { - match list_type { - AccessListType::Allow => self.0.contains(info_hash_bytes), - AccessListType::Deny => !self.0.contains(info_hash_bytes), - AccessListType::Ignore => true, - } - } -} +pub mod access_list; /// Peer or connection valid until this instant /// @@ -157,16 +96,3 @@ pub fn convert_ipv4_mapped_ipv6(ip_address: IpAddr) -> IpAddr { ip_address } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_access_list_parse_info_hash(){ - assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeee".to_owned()).is_ok()); - assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeeef".to_owned()).is_err()); - assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeee".to_owned()).is_err()); - assert!(AccessList::parse_info_hash("aaaabbbbccccddddeeeeaaaabbbbccccddddeeeö".to_owned()).is_err()); - } -} \ No newline at end of file diff --git a/aquatic_udp/src/lib/common.rs b/aquatic_udp/src/lib/common.rs index 0fc0ebe..99a19fc 100644 --- a/aquatic_udp/src/lib/common.rs +++ b/aquatic_udp/src/lib/common.rs @@ -3,12 +3,12 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; use std::sync::{atomic::AtomicUsize, Arc}; use std::time::Instant; -use aquatic_common::AccessListType; +use aquatic_common::access_list::AccessListType; use hashbrown::HashMap; use indexmap::IndexMap; use parking_lot::Mutex; -pub use aquatic_common::{AccessList, ValidUntil}; +pub use aquatic_common::{access_list::AccessList, ValidUntil}; pub use aquatic_udp_protocol::*; pub const MAX_PACKET_SIZE: usize = 4096; diff --git a/aquatic_udp/src/lib/config.rs b/aquatic_udp/src/lib/config.rs index 3db9156..5ce9685 100644 --- a/aquatic_udp/src/lib/config.rs +++ b/aquatic_udp/src/lib/config.rs @@ -1,6 +1,6 @@ use std::net::SocketAddr; -use aquatic_common::AccessListConfig; +use aquatic_common::access_list::AccessListConfig; use serde::{Deserialize, Serialize}; use aquatic_cli_helpers::LogLevel; diff --git a/aquatic_udp/src/lib/handlers.rs b/aquatic_udp/src/lib/handlers.rs index a5b21d7..3a75668 100644 --- a/aquatic_udp/src/lib/handlers.rs +++ b/aquatic_udp/src/lib/handlers.rs @@ -9,7 +9,7 @@ use rand::{ Rng, SeedableRng, }; -use aquatic_common::{convert_ipv4_mapped_ipv6, extract_response_peers, AccessListType}; +use aquatic_common::{convert_ipv4_mapped_ipv6, extract_response_peers, access_list::AccessListType}; use aquatic_udp_protocol::*; use crate::common::*; diff --git a/aquatic_udp/src/lib/lib.rs b/aquatic_udp/src/lib/lib.rs index f7bc466..e406d9a 100644 --- a/aquatic_udp/src/lib/lib.rs +++ b/aquatic_udp/src/lib/lib.rs @@ -6,7 +6,7 @@ use std::thread::Builder; use std::time::Duration; use anyhow::Context; -use aquatic_common::AccessListType; +use aquatic_common::access_list::AccessListType; use crossbeam_channel::unbounded; use privdrop::PrivDrop; diff --git a/aquatic_udp/src/lib/tasks.rs b/aquatic_udp/src/lib/tasks.rs index 628a5ab..e8e1aa1 100644 --- a/aquatic_udp/src/lib/tasks.rs +++ b/aquatic_udp/src/lib/tasks.rs @@ -3,7 +3,7 @@ use std::time::Instant; use histogram::Histogram; -use aquatic_common::AccessListType; +use aquatic_common::access_list::AccessListType; use crate::common::*; use crate::config::Config;