access list: log when update fails; run cargo fmt

This commit is contained in:
Joakim Frostegård 2021-10-15 02:35:13 +02:00
parent b5a2b81bd7
commit 6cfa220097
4 changed files with 30 additions and 28 deletions

View file

@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
pub enum AccessListType {
Allow,
Deny,
Ignore
Ignore,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
@ -47,7 +47,11 @@ impl AccessList {
if count == 20 {
Ok(bytes)
} else {
Err(anyhow::anyhow!("Info hash length only {} bytes: {}", count, line))
Err(anyhow::anyhow!(
"Info hash length only {} bytes: {}",
count,
line
))
}
}
@ -61,21 +65,14 @@ impl AccessList {
self.0.insert(Self::parse_line_to_info_hash(line?)?);
}
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
}
AccessListType::Allow => self.0.contains(info_hash_bytes),
AccessListType::Deny => !self.0.contains(info_hash_bytes),
AccessListType::Ignore => true,
}
}
}

View file

@ -9,7 +9,7 @@ use rand::{
Rng, SeedableRng,
};
use aquatic_common::{AccessListType, convert_ipv4_mapped_ipv6, extract_response_peers};
use aquatic_common::{convert_ipv4_mapped_ipv6, extract_response_peers, AccessListType};
use aquatic_udp_protocol::*;
use crate::common::*;
@ -126,7 +126,7 @@ pub fn run_request_worker(
// Check announce requests for allowed info hashes
match config.access_list.list_type {
access_list_type@(AccessListType::Allow | AccessListType::Deny) => {
access_list_type @ (AccessListType::Allow | AccessListType::Deny) => {
let access_list: MutexGuard<AccessList> = state.access_list.lock();
announce_requests.retain(|(request, src)| {
@ -143,8 +143,8 @@ pub fn run_request_worker(
true
});
},
AccessListType::Ignore => {},
}
AccessListType::Ignore => {}
};
// Handle announce and scrape requests

View file

@ -19,30 +19,38 @@ pub fn clean_connections_and_torrents(config: &Config, state: &State) {
}
match config.access_list.list_type {
access_list_type@(AccessListType::Allow | AccessListType::Deny) => {
access_list_type @ (AccessListType::Allow | AccessListType::Deny) => {
let mut access_list = state.access_list.lock();
access_list.update_from_path(&config.access_list.path);
if let Err(err) = access_list.update_from_path(&config.access_list.path) {
::log::error!("Update access list from path: {:?}", err);
}
let mut torrents = state.torrents.lock();
torrents.ipv4.retain(|info_hash, torrent| {
access_list.allows(access_list_type, &info_hash.0) && clean_torrent_and_peers(now, torrent)
access_list.allows(access_list_type, &info_hash.0)
&& clean_torrent_and_peers(now, torrent)
});
torrents.ipv4.shrink_to_fit();
torrents.ipv6.retain(|info_hash, torrent| {
access_list.allows(access_list_type, &info_hash.0) && clean_torrent_and_peers(now, torrent)
access_list.allows(access_list_type, &info_hash.0)
&& clean_torrent_and_peers(now, torrent)
});
torrents.ipv6.shrink_to_fit();
},
}
AccessListType::Ignore => {
let mut torrents = state.torrents.lock();
torrents.ipv4.retain(|_, torrent| clean_torrent_and_peers(now, torrent));
torrents
.ipv4
.retain(|_, torrent| clean_torrent_and_peers(now, torrent));
torrents.ipv4.shrink_to_fit();
torrents.ipv6.retain(|_, torrent| clean_torrent_and_peers(now, torrent));
torrents
.ipv6
.retain(|_, torrent| clean_torrent_and_peers(now, torrent));
torrents.ipv6.shrink_to_fit();
}
}

View file

@ -145,7 +145,7 @@ impl HandshakeMachine {
);
(Some(Either::Right(Self::TlsStream(stream))), false)
},
}
Err(native_tls::HandshakeError::WouldBlock(handshake)) => {
(Some(Either::Right(Self::TlsMidHandshake(handshake))), true)
}
@ -170,10 +170,7 @@ impl HandshakeMachine {
peer_addr
);
let established_ws = EstablishedWs {
ws,
peer_addr,
};
let established_ws = EstablishedWs { ws, peer_addr };
(Some(Either::Left(established_ws)), false)
}