mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_udp: mio: access list: use SIGUSR1, improve errors
This commit is contained in:
parent
78d29770f3
commit
9b75b50802
2 changed files with 35 additions and 29 deletions
|
|
@ -3,6 +3,7 @@ use std::io::{BufRead, BufReader};
|
|||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Context;
|
||||
use arc_swap::ArcSwap;
|
||||
use hashbrown::HashSet;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
@ -59,7 +60,11 @@ impl AccessList {
|
|||
let mut new_list = Self::default();
|
||||
|
||||
for line in reader.lines() {
|
||||
new_list.insert_from_line(&line?)?;
|
||||
let line = line?;
|
||||
|
||||
new_list
|
||||
.insert_from_line(&line)
|
||||
.with_context(|| format!("Invalid line in access list: {}", line))?;
|
||||
}
|
||||
|
||||
Ok(new_list)
|
||||
|
|
@ -75,31 +80,17 @@ impl AccessList {
|
|||
}
|
||||
|
||||
pub trait AccessListQuery {
|
||||
fn update(&self, config: &AccessListConfig);
|
||||
fn update(&self, config: &AccessListConfig) -> anyhow::Result<()>;
|
||||
fn allows(&self, list_mode: AccessListMode, info_hash_bytes: &[u8; 20]) -> bool;
|
||||
}
|
||||
|
||||
pub type AccessListArcSwap = ArcSwap<AccessList>;
|
||||
|
||||
impl AccessListQuery for AccessListArcSwap {
|
||||
fn update(&self, config: &AccessListConfig) {
|
||||
match config.mode {
|
||||
AccessListMode::White | AccessListMode::Black => {
|
||||
match AccessList::create_from_path(&config.path) {
|
||||
Ok(new) => {
|
||||
self.store(Arc::new(new));
|
||||
}
|
||||
Err(err) => {
|
||||
::log::error!("Updating access list failed: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
AccessListMode::Off => {
|
||||
::log::error!(
|
||||
"AccessListQuery::update_from_path called, but AccessListMode is Off"
|
||||
);
|
||||
}
|
||||
}
|
||||
fn update(&self, config: &AccessListConfig) -> anyhow::Result<()> {
|
||||
self.store(Arc::new(AccessList::create_from_path(&config.path)?));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn allows(&self, mode: AccessListMode, info_hash_bytes: &[u8; 20]) -> bool {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use aquatic_common::privileges::drop_privileges_after_socket_binding;
|
|||
use crossbeam_channel::unbounded;
|
||||
|
||||
use aquatic_common::access_list::AccessListQuery;
|
||||
use signal_hook::consts::SIGHUP;
|
||||
use signal_hook::consts::SIGUSR1;
|
||||
use signal_hook::iterator::Signals;
|
||||
|
||||
use crate::config::Config;
|
||||
|
|
@ -29,10 +29,12 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
|||
});
|
||||
}
|
||||
|
||||
let mut signals = Signals::new(::std::iter::once(SIGHUP))?;
|
||||
|
||||
let state = State::default();
|
||||
|
||||
update_access_list(&config, &state)?;
|
||||
|
||||
let mut signals = Signals::new(::std::iter::once(SIGUSR1))?;
|
||||
|
||||
{
|
||||
let config = config.clone();
|
||||
let state = state.clone();
|
||||
|
|
@ -42,10 +44,8 @@ pub fn run(config: Config) -> ::anyhow::Result<()> {
|
|||
|
||||
for signal in &mut signals {
|
||||
match signal {
|
||||
SIGHUP => {
|
||||
::log::info!("Updating access list");
|
||||
|
||||
state.access_list.update(&config.access_list);
|
||||
SIGUSR1 => {
|
||||
let _ = update_access_list(&config, &state);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
|
@ -61,8 +61,6 @@ pub fn run_inner(config: Config, state: State) -> ::anyhow::Result<()> {
|
|||
});
|
||||
}
|
||||
|
||||
state.access_list.update(&config.access_list);
|
||||
|
||||
let num_bound_sockets = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
let (request_sender, request_receiver) = unbounded();
|
||||
|
|
@ -154,3 +152,20 @@ pub fn run_inner(config: Config, state: State) -> ::anyhow::Result<()> {
|
|||
.clean(&config, state.access_list.load_full().deref());
|
||||
}
|
||||
}
|
||||
|
||||
fn update_access_list(config: &Config, state: &State) -> anyhow::Result<()> {
|
||||
if config.access_list.mode.is_on() {
|
||||
match state.access_list.update(&config.access_list) {
|
||||
Ok(()) => {
|
||||
::log::info!("Access list updated")
|
||||
}
|
||||
Err(err) => {
|
||||
::log::error!("Updating access list failed: {:#}", err);
|
||||
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue