mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
implement session cache
This commit is contained in:
parent
e5268e49f1
commit
8de35ff3a6
5 changed files with 123 additions and 31 deletions
53
src/list.rs
53
src/list.rs
|
|
@ -1,37 +1,54 @@
|
|||
mod cache;
|
||||
mod item;
|
||||
|
||||
use anyhow::Result;
|
||||
use cache::Cache;
|
||||
use item::Item;
|
||||
use log::*;
|
||||
use std::collections::HashSet;
|
||||
use tokio::sync::RwLock;
|
||||
use std::{collections::HashSet, path::PathBuf};
|
||||
use tokio::{fs, sync::RwLock};
|
||||
|
||||
pub struct List {
|
||||
/// In-memory registry, based on `--allow-list` + `--cache`
|
||||
index: RwLock<HashSet<Item>>,
|
||||
/// FS cache for JSON/API changes, based on `--cache` value
|
||||
cache: Cache,
|
||||
}
|
||||
|
||||
impl List {
|
||||
pub async fn from_opt(list: &Vec<String>) -> Result<Self> {
|
||||
let mut this = HashSet::new();
|
||||
pub async fn from_opt(list: &Vec<String>, cache: Option<PathBuf>) -> Result<Self> {
|
||||
fn handle(this: &mut HashSet<Item>, line: &str) {
|
||||
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
||||
return;
|
||||
}
|
||||
if !this.insert(Item::from_line(line)) {
|
||||
warn!("Duplicated list record: `{line}`")
|
||||
}
|
||||
}
|
||||
let mut index = 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)?
|
||||
fs::read_to_string(i).await?
|
||||
}
|
||||
.lines()
|
||||
{
|
||||
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if !this.insert(Item::from_line(line)) {
|
||||
warn!("Duplicated whitelist record: `{line}`")
|
||||
}
|
||||
handle(&mut index, line)
|
||||
}
|
||||
}
|
||||
info!("Total whitelist entries parsed: {}", this.len());
|
||||
|
||||
let cache = Cache::from_path(cache).await?;
|
||||
if let Some(data) = cache.read().await? {
|
||||
for line in data.lines() {
|
||||
handle(&mut index, line)
|
||||
}
|
||||
}
|
||||
|
||||
info!("Total list entries parsed: {}", index.len());
|
||||
Ok(Self {
|
||||
index: RwLock::new(this),
|
||||
index: RwLock::new(index),
|
||||
cache,
|
||||
})
|
||||
}
|
||||
pub async fn any(&self, values: &[&str]) -> bool {
|
||||
|
|
@ -46,10 +63,12 @@ impl List {
|
|||
pub async fn entries(&self) -> u64 {
|
||||
self.index.read().await.len() as u64
|
||||
}
|
||||
pub async fn allow(&self, rule: &str) -> bool {
|
||||
self.index.write().await.insert(Item::from_line(rule))
|
||||
pub async fn allow(&self, rule: &str) -> Result<bool> {
|
||||
self.cache.allow(rule).await?;
|
||||
Ok(self.index.write().await.insert(Item::from_line(rule)))
|
||||
}
|
||||
pub async fn block(&self, rule: &str) -> bool {
|
||||
self.index.write().await.remove(&Item::from_line(rule))
|
||||
pub async fn block(&self, rule: &str) -> Result<bool> {
|
||||
self.cache.block(rule).await?;
|
||||
Ok(self.index.write().await.remove(&Item::from_line(rule)))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue