mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 10:45:30 +00:00
aquatic_ws: almost finish implementing access list support
This commit is contained in:
parent
db596b5038
commit
28cc6c261a
5 changed files with 88 additions and 36 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use aquatic_common::access_list::AccessList;
|
||||||
use crossbeam_channel::{Receiver, Sender};
|
use crossbeam_channel::{Receiver, Sender};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
|
|
@ -12,6 +14,8 @@ pub use aquatic_common::ValidUntil;
|
||||||
|
|
||||||
use aquatic_ws_protocol::*;
|
use aquatic_ws_protocol::*;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
pub const LISTENER_TOKEN: Token = Token(0);
|
pub const LISTENER_TOKEN: Token = Token(0);
|
||||||
pub const CHANNEL_TOKEN: Token = Token(1);
|
pub const CHANNEL_TOKEN: Token = Token(1);
|
||||||
|
|
||||||
|
|
@ -82,6 +86,54 @@ pub type TorrentMap = HashMap<InfoHash, TorrentData>;
|
||||||
pub struct TorrentMaps {
|
pub struct TorrentMaps {
|
||||||
pub ipv4: TorrentMap,
|
pub ipv4: TorrentMap,
|
||||||
pub ipv6: TorrentMap,
|
pub ipv6: TorrentMap,
|
||||||
|
pub access_list: AccessList,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TorrentMaps {
|
||||||
|
pub fn clean(&mut self, config: &Config) {
|
||||||
|
Self::clean_torrent_map(config, &self.access_list, &mut self.ipv4);
|
||||||
|
Self::clean_torrent_map(config, &self.access_list, &mut self.ipv6);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clean_torrent_map(
|
||||||
|
config: &Config,
|
||||||
|
access_list: &AccessList,
|
||||||
|
torrent_map: &mut TorrentMap
|
||||||
|
) {
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
torrent_data.peers.retain(|_, peer| {
|
||||||
|
let keep = peer.valid_until.0 >= now;
|
||||||
|
|
||||||
|
if !keep {
|
||||||
|
match peer.status {
|
||||||
|
PeerStatus::Seeding => {
|
||||||
|
*num_seeders -= 1;
|
||||||
|
}
|
||||||
|
PeerStatus::Leeching => {
|
||||||
|
*num_leechers -= 1;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
keep
|
||||||
|
});
|
||||||
|
|
||||||
|
!torrent_data.peers.is_empty()
|
||||||
|
});
|
||||||
|
|
||||||
|
torrent_map.shrink_to_fit();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use aquatic_common::access_list::AccessListConfig;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use aquatic_cli_helpers::LogLevel;
|
use aquatic_cli_helpers::LogLevel;
|
||||||
|
|
@ -21,6 +22,7 @@ pub struct Config {
|
||||||
pub cleaning: CleaningConfig,
|
pub cleaning: CleaningConfig,
|
||||||
pub statistics: StatisticsConfig,
|
pub statistics: StatisticsConfig,
|
||||||
pub privileges: PrivilegeConfig,
|
pub privileges: PrivilegeConfig,
|
||||||
|
pub access_list: AccessListConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl aquatic_cli_helpers::Config for Config {
|
impl aquatic_cli_helpers::Config for Config {
|
||||||
|
|
@ -105,6 +107,7 @@ impl Default for Config {
|
||||||
cleaning: CleaningConfig::default(),
|
cleaning: CleaningConfig::default(),
|
||||||
statistics: StatisticsConfig::default(),
|
statistics: StatisticsConfig::default(),
|
||||||
privileges: PrivilegeConfig::default(),
|
privileges: PrivilegeConfig::default(),
|
||||||
|
access_list: AccessListConfig::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,19 @@ pub fn handle_announce_requests(
|
||||||
let valid_until = ValidUntil::new(config.cleaning.max_peer_age);
|
let valid_until = ValidUntil::new(config.cleaning.max_peer_age);
|
||||||
|
|
||||||
for (request_sender_meta, request) in requests {
|
for (request_sender_meta, request) in requests {
|
||||||
|
let info_hash_allowed = torrent_maps
|
||||||
|
.access_list
|
||||||
|
.allows(config.access_list.mode, &request.info_hash.0);
|
||||||
|
|
||||||
|
if !info_hash_allowed {
|
||||||
|
// let response = OutMessage::ErrorResponse();
|
||||||
|
|
||||||
|
// out_message_sender.send(request_sender_meta, response);
|
||||||
|
// wake_socket_workers[request_sender_meta.worker_index] = true;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let torrent_data: &mut TorrentData = if request_sender_meta.converted_peer_ip.is_ipv4() {
|
let torrent_data: &mut TorrentData = if request_sender_meta.converted_peer_ip.is_ipv4() {
|
||||||
torrent_maps.ipv4.entry(request.info_hash).or_default()
|
torrent_maps.ipv4.entry(request.info_hash).or_default()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,18 @@ pub const APP_NAME: &str = "aquatic_ws: WebTorrent tracker";
|
||||||
pub fn run(config: Config) -> anyhow::Result<()> {
|
pub fn run(config: Config) -> anyhow::Result<()> {
|
||||||
let state = State::default();
|
let state = State::default();
|
||||||
|
|
||||||
|
tasks::update_access_list(&config, &mut state.torrent_maps.lock());
|
||||||
|
|
||||||
start_workers(config.clone(), state.clone())?;
|
start_workers(config.clone(), state.clone())?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
::std::thread::sleep(Duration::from_secs(config.cleaning.interval));
|
||||||
|
|
||||||
tasks::clean_torrents(&state);
|
let mut torrent_maps = state.torrent_maps.lock();
|
||||||
|
|
||||||
|
tasks::update_access_list(&config, &mut torrent_maps);
|
||||||
|
|
||||||
|
torrent_maps.clean(&config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,23 @@
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use aquatic_common::access_list::AccessListMode;
|
||||||
use histogram::Histogram;
|
use histogram::Histogram;
|
||||||
|
|
||||||
use crate::common::*;
|
use crate::common::*;
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
pub fn clean_torrents(state: &State) {
|
pub fn update_access_list(config: &Config, torrent_maps: &mut TorrentMaps) {
|
||||||
fn clean_torrent_map(torrent_map: &mut TorrentMap) {
|
match config.access_list.mode {
|
||||||
let now = Instant::now();
|
AccessListMode::Require | AccessListMode::Forbid => {
|
||||||
|
if let Err(err) = torrent_maps
|
||||||
torrent_map.retain(|_, torrent_data| {
|
.access_list
|
||||||
let num_seeders = &mut torrent_data.num_seeders;
|
.update_from_path(&config.access_list.path)
|
||||||
let num_leechers = &mut torrent_data.num_leechers;
|
{
|
||||||
|
::log::error!("Couldn't update access list: {:?}", err);
|
||||||
torrent_data.peers.retain(|_, peer| {
|
}
|
||||||
let keep = peer.valid_until.0 >= now;
|
}
|
||||||
|
AccessListMode::Ignore => {}
|
||||||
if !keep {
|
|
||||||
match peer.status {
|
|
||||||
PeerStatus::Seeding => {
|
|
||||||
*num_seeders -= 1;
|
|
||||||
}
|
|
||||||
PeerStatus::Leeching => {
|
|
||||||
*num_leechers -= 1;
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
keep
|
|
||||||
});
|
|
||||||
|
|
||||||
!torrent_data.peers.is_empty()
|
|
||||||
});
|
|
||||||
|
|
||||||
torrent_map.shrink_to_fit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut torrent_maps = state.torrent_maps.lock();
|
|
||||||
|
|
||||||
clean_torrent_map(&mut torrent_maps.ipv4);
|
|
||||||
clean_torrent_map(&mut torrent_maps.ipv6);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_statistics(state: &State) {
|
pub fn print_statistics(state: &State) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue