mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 08:25:27 +00:00
implement cache/clean api
This commit is contained in:
parent
0603fdf4a6
commit
461f838cc0
4 changed files with 35 additions and 3 deletions
|
|
@ -26,8 +26,9 @@ RUST_LOG=trace cargo run -- --allow=http://localhost/allow.txt \
|
|||
```
|
||||
* set `socks5://127.0.0.1:1080` proxy in your application
|
||||
* open http://127.0.0.1:8010 in browser for stats:
|
||||
* `/api/allow/domain.com` - add rule to the current session (and `--cache` if defined)
|
||||
* `/api/block/domain.com` - delete rule from the current session (and `--cache` if defined)
|
||||
* `/api/allow/{domain.com}` - add rule to the current session (and `--cache` if defined)
|
||||
* `/api/block/{domain.com}` - delete rule from the current session (and `--cache` if defined)
|
||||
* `/api/cache/clean` - clean `--cache` file (returns deleted rules or `null` if not enabled)
|
||||
|
||||
### Allow list example
|
||||
|
||||
|
|
|
|||
|
|
@ -90,4 +90,9 @@ impl List {
|
|||
self.cache.block(rule).await?;
|
||||
Ok(self.index.write().await.remove(&Item::from_line(rule)))
|
||||
}
|
||||
/// Privately clean `--cache` file collected, return deleted rules
|
||||
/// * we can implement `self.index` update at this step @TODO
|
||||
pub async fn cache_clean(&self) -> Result<Option<Vec<String>>> {
|
||||
self.cache.clean().await
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,22 @@ impl Cache {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Clean `--cache` file collected, return deleted rules
|
||||
pub async fn clean(&self) -> Result<Option<Vec<String>>> {
|
||||
match self.0 {
|
||||
Some(ref p) => {
|
||||
init_file(p).await?;
|
||||
let mut rules = Vec::new();
|
||||
let lines = fs::read_to_string(p).await?;
|
||||
for line in lines.lines() {
|
||||
rules.push(line.into());
|
||||
}
|
||||
fs::write(p, "").await?;
|
||||
Ok(Some(rules))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Make sure that cache file is always exist (e.g. user may remove it when the daemon is running)
|
||||
|
|
|
|||
12
src/main.rs
12
src/main.rs
|
|
@ -56,6 +56,16 @@ async fn api_block(
|
|||
})?))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/cache/clean")]
|
||||
async fn api_cache_clean(list: &State<Arc<List>>) -> Result<Json<Option<Vec<String>>>, Status> {
|
||||
let result = list.cache_clean().await;
|
||||
info!("Clean cache file: {result:?}");
|
||||
Ok(Json(result.map_err(|e| {
|
||||
error!("Could not handle cache clean request: `{e}`");
|
||||
Status::InternalServerError
|
||||
})?))
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
async fn rocket() -> _ {
|
||||
env_logger::init();
|
||||
|
|
@ -91,7 +101,7 @@ async fn rocket() -> _ {
|
|||
.manage(Instant::now())
|
||||
.mount(
|
||||
"/",
|
||||
rocket::routes![index, api_totals, api_allow, api_block],
|
||||
rocket::routes![index, api_totals, api_allow, api_block, api_cache_clean],
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue