mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
grand refactory to multiple list-based control api
This commit is contained in:
parent
827cb182f2
commit
b93c1e8481
15 changed files with 271 additions and 282 deletions
44
src/rules/list.rs
Normal file
44
src/rules/list.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
mod rule;
|
||||
mod source;
|
||||
|
||||
use anyhow::Result;
|
||||
use log::warn;
|
||||
use rule::Rule;
|
||||
use source::Source;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub struct List {
|
||||
pub is_enabled: bool,
|
||||
//pub source: Source,
|
||||
pub rules: HashSet<Rule>,
|
||||
}
|
||||
|
||||
impl List {
|
||||
pub async fn init(src: &str, is_enabled: bool) -> Result<Self> {
|
||||
let mut rules = HashSet::new();
|
||||
let source = Source::from_str(src)?;
|
||||
if is_enabled {
|
||||
for line in source.get().await?.lines() {
|
||||
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
||||
continue; // skip comments
|
||||
}
|
||||
if !rules.insert(Rule::from_line(line)) {
|
||||
warn!("List `{src}` contains duplicated entry: `{line}`")
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
rules,
|
||||
//source,
|
||||
is_enabled,
|
||||
})
|
||||
}
|
||||
/// Change rule set status by list ID
|
||||
pub fn set_status(&mut self, is_enabled: bool) {
|
||||
self.is_enabled = is_enabled;
|
||||
}
|
||||
/// Check if rule is exist in the items index
|
||||
pub fn contains(&self, value: &str) -> bool {
|
||||
self.is_enabled && self.rules.iter().any(|rule| rule.contains(value))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue