From 10fe014c03cdd5a60e5a5870a6ed2bdc89d3ce27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Fri, 15 Oct 2021 23:03:24 +0200 Subject: [PATCH] aquatic_http access list: use in torrent cleaning, do periodic updates --- aquatic_http/src/lib/lib.rs | 2 +- aquatic_http/src/lib/tasks.rs | 34 +++++++++++++++++++++------ aquatic_http_protocol/src/response.rs | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/aquatic_http/src/lib/lib.rs b/aquatic_http/src/lib/lib.rs index 1c74ae1..25d8389 100644 --- a/aquatic_http/src/lib/lib.rs +++ b/aquatic_http/src/lib/lib.rs @@ -40,7 +40,7 @@ pub fn run(config: Config) -> anyhow::Result<()> { loop { ::std::thread::sleep(Duration::from_secs(config.cleaning.interval)); - tasks::clean_torrents(&state); + tasks::clean_torrents(&config, &state); } } diff --git a/aquatic_http/src/lib/tasks.rs b/aquatic_http/src/lib/tasks.rs index 0ec2275..601ccc0 100644 --- a/aquatic_http/src/lib/tasks.rs +++ b/aquatic_http/src/lib/tasks.rs @@ -1,20 +1,40 @@ -use std::time::Instant; +use std::{ops::DerefMut, time::Instant}; use histogram::Histogram; -use crate::common::*; +use aquatic_common::access_list::{AccessList, AccessListMode}; -pub fn clean_torrents(state: &State) { +use crate::{common::*, config::Config}; + +pub fn clean_torrents(config: &Config, state: &State) { let mut torrent_maps = state.torrent_maps.lock(); + let torrent_maps = torrent_maps.deref_mut(); - clean_torrent_map(&mut torrent_maps.ipv4); - clean_torrent_map(&mut torrent_maps.ipv6); + match config.access_list.mode { + AccessListMode::Require | AccessListMode::Forbid => { + if let Err(err) = torrent_maps.access_list.update_from_path(&config.access_list.path) { + ::log::error!("Couldn't update access list: {:?}", err); + } + }, + AccessListMode::Ignore => { } + } + + clean_torrent_map(config, &torrent_maps.access_list, &mut torrent_maps.ipv4); + clean_torrent_map(config, &torrent_maps.access_list, &mut torrent_maps.ipv6); } -fn clean_torrent_map(torrent_map: &mut TorrentMap) { +fn clean_torrent_map( + config: &Config, + access_list: &AccessList, + torrent_map: &mut TorrentMap, +) { let now = Instant::now(); - torrent_map.retain(|_, torrent_data| { + torrent_map.retain(|info_hash, torrent_data| { + if !access_list.allows(config.access_list.mode, &info_hash.0) { + return false; + } + let num_seeders = &mut torrent_data.num_seeders; let num_leechers = &mut torrent_data.num_leechers; diff --git a/aquatic_http_protocol/src/response.rs b/aquatic_http_protocol/src/response.rs index 5382469..a45c93f 100644 --- a/aquatic_http_protocol/src/response.rs +++ b/aquatic_http_protocol/src/response.rs @@ -137,7 +137,7 @@ pub struct FailureResponse { impl FailureResponse { pub fn new(reason: &str) -> Self { Self { - failure_reason: reason.into() + failure_reason: reason.into(), } }