mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
access list: use hex crate for info hash parsing
This commit is contained in:
parent
752873280b
commit
f370dac330
4 changed files with 16 additions and 15 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -76,6 +76,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"hashbrown 0.11.2",
|
||||
"hex",
|
||||
"indexmap",
|
||||
"rand",
|
||||
"serde",
|
||||
|
|
|
|||
1
TODO.md
1
TODO.md
|
|
@ -6,7 +6,6 @@
|
|||
* rename Allow to Require?
|
||||
* serde-rename AccessListTypes to lowercase
|
||||
* serde-rename list_type to type?
|
||||
* add tests for info hash parsing
|
||||
* implement for aquatic_http and aquatic_ws
|
||||
|
||||
* Don't unwrap peer_address
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ name = "aquatic_common"
|
|||
[dependencies]
|
||||
anyhow = "1"
|
||||
hashbrown = "0.11.2"
|
||||
hex = "0.4"
|
||||
indexmap = "1"
|
||||
rand = { version = "0.8", features = ["small_rng"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -36,22 +36,9 @@ pub struct AccessList(HashSet<[u8; 20]>);
|
|||
|
||||
impl AccessList {
|
||||
fn parse_info_hash(line: String) -> anyhow::Result<[u8; 20]> {
|
||||
if line.len() != 20 {
|
||||
return Err(anyhow::anyhow!("Info hash is not 20 bytes long: {}", line));
|
||||
}
|
||||
|
||||
let mut bytes = [0u8; 20];
|
||||
|
||||
for (byte, c) in bytes.iter_mut().zip(line.chars()) {
|
||||
if c as u32 > 255 {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Info hash character is not ASCII: {:#?}",
|
||||
c
|
||||
));
|
||||
}
|
||||
|
||||
*byte = c as u8;
|
||||
}
|
||||
hex::decode_to_slice(line, &mut bytes)?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
|
|
@ -170,3 +157,16 @@ 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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue