mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-04-01 17:05:27 +00:00
initial commit
This commit is contained in:
parent
6dfda87e7b
commit
3b23d14e25
7 changed files with 2490 additions and 1 deletions
51
src/list.rs
Normal file
51
src/list.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use anyhow::Result;
|
||||
use log::*;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
pub enum Item {
|
||||
Ending(String),
|
||||
Exact(String),
|
||||
}
|
||||
|
||||
pub enum List {
|
||||
Allow(HashSet<Item>),
|
||||
}
|
||||
|
||||
impl List {
|
||||
pub async fn from_opt(list: &Vec<String>) -> Result<Self> {
|
||||
let mut this = HashSet::new();
|
||||
for i in list {
|
||||
for line in if i.contains("://") {
|
||||
reqwest::get(i).await?.text().await?
|
||||
} else {
|
||||
std::fs::read_to_string(i)?
|
||||
}
|
||||
.lines()
|
||||
{
|
||||
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if !this.insert(if let Some(item) = line.strip_prefix(".") {
|
||||
debug!("Add `{line}` to the whitelist");
|
||||
Item::Ending(item.to_string())
|
||||
} else {
|
||||
debug!("Add `{line}` exact match to the whitelist");
|
||||
Item::Exact(line.to_string())
|
||||
}) {
|
||||
warn!("Duplicated whitelist record: `{line}`")
|
||||
}
|
||||
}
|
||||
}
|
||||
info!("Total whitelist entries parsed: {}", this.len());
|
||||
Ok(Self::Allow(this))
|
||||
}
|
||||
pub fn has(&self, value: &str) -> bool {
|
||||
match self {
|
||||
Self::Allow(list) => list.iter().any(|item| match item {
|
||||
Item::Exact(s) => s == value,
|
||||
Item::Ending(s) => value.ends_with(s),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue