access lists: filter requests in socket workers instead

This commit is contained in:
Joakim Frostegård 2021-10-16 17:26:40 +02:00
parent 33966bed57
commit 7ccd5fcbf7
18 changed files with 221 additions and 163 deletions

View file

@ -12,6 +12,7 @@ name = "aquatic_common"
[dependencies]
anyhow = "1"
arc-swap = "1"
hashbrown = "0.11.2"
hex = "0.4"
indexmap = "1"

View file

@ -1,7 +1,9 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::sync::Arc;
use arc_swap::ArcSwap;
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};
@ -34,8 +36,13 @@ impl Default for AccessListConfig {
}
}
#[derive(Default)]
pub struct AccessList(HashSet<[u8; 20]>);
pub struct AccessList(ArcSwap<HashSet<[u8; 20]>>);
impl Default for AccessList {
fn default() -> Self {
Self(ArcSwap::from(Arc::new(HashSet::default())))
}
}
impl AccessList {
fn parse_info_hash(line: String) -> anyhow::Result<[u8; 20]> {
@ -46,7 +53,7 @@ impl AccessList {
Ok(bytes)
}
pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> {
pub fn update_from_path(&self, path: &PathBuf) -> anyhow::Result<()> {
let file = File::open(path)?;
let reader = BufReader::new(file);
@ -56,15 +63,15 @@ impl AccessList {
new_list.insert(Self::parse_info_hash(line?)?);
}
self.0 = new_list;
self.0.store(Arc::new(new_list));
Ok(())
}
pub fn allows(&self, list_type: AccessListMode, info_hash_bytes: &[u8; 20]) -> bool {
match list_type {
AccessListMode::Require => self.0.contains(info_hash_bytes),
AccessListMode::Forbid => !self.0.contains(info_hash_bytes),
AccessListMode::Require => self.0.load().contains(info_hash_bytes),
AccessListMode::Forbid => !self.0.load().contains(info_hash_bytes),
AccessListMode::Ignore => true,
}
}