From eeafed077a6555e5550bae5673308715f0a92db6 Mon Sep 17 00:00:00 2001 From: postscriptum Date: Mon, 23 Mar 2026 09:00:19 +0200 Subject: [PATCH] implement (rules) list api --- README.md | 1 + src/list.rs | 9 +++++++++ src/list/item.rs | 9 +++++++++ src/main.rs | 16 +++++++++++++++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 79c3f96..521e1ea 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ RUST_LOG=trace cargo run -- --allow=http://localhost/allow.txt \ * 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/list` - Return active rules (from server memory) * `/api/cache/clean` - clean `--cache` file (returns deleted rules or `null` if not enabled) ### Allow list example diff --git a/src/list.rs b/src/list.rs index d614018..c6e0797 100644 --- a/src/list.rs +++ b/src/list.rs @@ -95,4 +95,13 @@ impl List { pub async fn cache_clean(&self) -> Result>> { self.cache.clean().await } + /// Return active rules (from server memory) + pub async fn list(&self) -> Vec { + self.index + .read() + .await + .iter() + .map(|item| item.to_string()) + .collect() + } } diff --git a/src/list/item.rs b/src/list/item.rs index 9e7093b..138e443 100644 --- a/src/list/item.rs +++ b/src/list/item.rs @@ -17,3 +17,12 @@ impl Item { } } } + +impl std::fmt::Display for Item { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Ending(s) => write!(f, ".{}", s), + Self::Exact(s) => write!(f, "{}", s), + } + } +} diff --git a/src/main.rs b/src/main.rs index 7428932..feb733c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,13 @@ async fn api_block( })?)) } +#[rocket::get("/api/list")] +async fn api_list(list: &State>) -> Result>, Status> { + let result = list.list().await; + info!("Get list rules (total: {})", result.len()); + Ok(Json(result)) +} + #[rocket::get("/api/cache/clean")] async fn api_cache_clean(list: &State>) -> Result>>, Status> { let result = list.cache_clean().await; @@ -101,7 +108,14 @@ async fn rocket() -> _ { .manage(Instant::now()) .mount( "/", - rocket::routes![index, api_totals, api_allow, api_block, api_cache_clean], + rocket::routes![ + index, + api_totals, + api_allow, + api_block, + api_list, + api_cache_clean + ], ) }