mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 08:25:27 +00:00
reorganize List from enum to struct; separate features to children mods
This commit is contained in:
parent
0b6b0ebd0e
commit
e5268e49f1
2 changed files with 37 additions and 41 deletions
59
src/list.rs
59
src/list.rs
|
|
@ -1,28 +1,13 @@
|
|||
mod item;
|
||||
|
||||
use anyhow::Result;
|
||||
use item::Item;
|
||||
use log::*;
|
||||
use std::collections::HashSet;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
pub enum Item {
|
||||
Ending(String),
|
||||
Exact(String),
|
||||
}
|
||||
|
||||
impl Item {
|
||||
pub fn from_line(rule: &str) -> Self {
|
||||
if let Some(item) = rule.strip_prefix(".") {
|
||||
debug!("Add `{rule}` to the whitelist");
|
||||
Self::Ending(item.to_string())
|
||||
} else {
|
||||
debug!("Add `{rule}` exact match to the whitelist");
|
||||
Self::Exact(rule.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum List {
|
||||
Allow(RwLock<HashSet<Item>>),
|
||||
pub struct List {
|
||||
index: RwLock<HashSet<Item>>,
|
||||
}
|
||||
|
||||
impl List {
|
||||
|
|
@ -45,34 +30,26 @@ impl List {
|
|||
}
|
||||
}
|
||||
info!("Total whitelist entries parsed: {}", this.len());
|
||||
Ok(Self::Allow(RwLock::new(this)))
|
||||
Ok(Self {
|
||||
index: RwLock::new(this),
|
||||
})
|
||||
}
|
||||
pub async fn any(&self, values: &[&str]) -> bool {
|
||||
match self {
|
||||
Self::Allow(list) => {
|
||||
let guard = list.read().await;
|
||||
values.iter().any(|&value| {
|
||||
guard.iter().any(|item| match item {
|
||||
Item::Exact(v) => v == value,
|
||||
Item::Ending(v) => value.ends_with(v),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
let guard = self.index.read().await;
|
||||
values.iter().any(|&value| {
|
||||
guard.iter().any(|item| match item {
|
||||
Item::Exact(v) => v == value,
|
||||
Item::Ending(v) => value.ends_with(v),
|
||||
})
|
||||
})
|
||||
}
|
||||
pub async fn entries(&self) -> u64 {
|
||||
match self {
|
||||
Self::Allow(list) => list.read().await.len() as u64,
|
||||
}
|
||||
self.index.read().await.len() as u64
|
||||
}
|
||||
pub async fn allow(&self, rule: &str) -> bool {
|
||||
match self {
|
||||
List::Allow(list) => list.write().await.insert(Item::from_line(rule)),
|
||||
}
|
||||
self.index.write().await.insert(Item::from_line(rule))
|
||||
}
|
||||
pub async fn block(&self, rule: &str) -> bool {
|
||||
match self {
|
||||
List::Allow(list) => list.write().await.remove(&Item::from_line(rule)),
|
||||
}
|
||||
self.index.write().await.remove(&Item::from_line(rule))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
19
src/list/item.rs
Normal file
19
src/list/item.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
use log::debug;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
pub enum Item {
|
||||
Ending(String),
|
||||
Exact(String),
|
||||
}
|
||||
|
||||
impl Item {
|
||||
pub fn from_line(rule: &str) -> Self {
|
||||
if let Some(item) = rule.strip_prefix(".") {
|
||||
debug!("Add `{rule}` to the whitelist");
|
||||
Self::Ending(item.to_string())
|
||||
} else {
|
||||
debug!("Add `{rule}` exact match to the whitelist");
|
||||
Self::Exact(rule.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue