From 96b9646c8c74540ea67b7e0aff53502478f51ee3 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 28 Mar 2026 06:40:01 +0200 Subject: [PATCH] implement list(s) reload api --- README.md | 2 ++ src/main.rs | 34 +++++++++++++++++++++++++++++++++- src/rules.rs | 26 +++++++++++++++++++++++++- src/rules/list.rs | 10 ++++++++-- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3ebaf8a..828aa1e 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ RUST_LOG=trace psokcs -c=/path/to/config.toml no-auth * `/api/totals` - blocking summary * `/api/list/enable/` - enable all parsed rules of given list ID (`[list.ID]` in your config) * `/api/list/disable/` - disable all parsed rules of given list ID (`[list.ID]` in your config) + * `/api/list/reload/` - reload list rules from its `source` by list ID (`[list.ID]` in your config) + * `/api/list/reload` - reload all lists rules from their `source` ### Allow list example diff --git a/src/main.rs b/src/main.rs index bf47419..aaa0e27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,31 @@ async fn api_list_disable( )) } +#[rocket::get("/api/list/reload/")] +async fn api_list_reload( + alias: Option<&str>, + rules: &State>>, +) -> Result>, Status> { + Ok(Json(rules.write().await.reload(alias).await.map_err( + |err| { + error!("List reload error: `{err}`"); + Status::InternalServerError + }, + )?)) +} + +#[rocket::get("/api/list/reload")] +async fn api_list_reload_all( + rules: &State>>, +) -> Result>, Status> { + Ok(Json(rules.write().await.reload(None).await.map_err( + |err| { + error!("Lists reload error: `{err}`"); + Status::InternalServerError + }, + )?)) +} + #[rocket::launch] async fn rocket() -> _ { env_logger::init(); @@ -106,7 +131,14 @@ async fn rocket() -> _ { .manage(Instant::now()) .mount( "/", - rocket::routes![index, api_totals, api_list_enable, api_list_disable], + rocket::routes![ + index, + api_totals, + api_list_enable, + api_list_disable, + api_list_reload, + api_list_reload_all + ], ) } diff --git a/src/rules.rs b/src/rules.rs index dc67470..56304c9 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -16,13 +16,37 @@ impl Rules { .insert(list_alias.into(), List::init(src, status).await?) .is_none()) } - /// Change rule set status by list ID + /// Change rule set status by alias pub async fn set_enabled(&mut self, list_alias: &str, status: bool) -> Result> { Ok(match self.0.get_mut(list_alias) { Some(list) => Some(list.set_enabled(status).await?), None => None, }) } + /// Reload rule set by alias from it source + /// * `None` to reload all + pub async fn reload(&mut self, list_alias: Option<&str>) -> Result> { + let mut affected = Vec::with_capacity(if list_alias.is_some() { + 1 + } else { + self.0.len() + }); + match list_alias { + Some(alias) => { + if let Some(list) = self.0.get_mut(alias) { + list.reload().await?; + affected.push(alias.into()); + } + } + None => { + for (alias, list) in self.0.iter_mut() { + list.reload().await?; + affected.push(alias.into()); + } + } + } + Ok(affected) + } /// Check if rule is exist in the index pub fn any(&self, value: &str) -> bool { self.0.values().any(|list| list.contains(value)) diff --git a/src/rules/list.rs b/src/rules/list.rs index dcf5d10..7209ff2 100644 --- a/src/rules/list.rs +++ b/src/rules/list.rs @@ -24,7 +24,7 @@ impl List { is_enabled, }) } - /// Change rule set status by list ID + /// Change rule set status pub async fn set_enabled(&mut self, is_enabled: bool) -> Result { let was_enabled = self.is_enabled; self.is_enabled = is_enabled; @@ -35,7 +35,13 @@ impl List { } Ok(was_enabled) } - /// Check if rule is exist in the items index + /// Reload rule set from it source + pub async fn reload(&mut self) -> Result<()> { + self.rules.clear(); + load(&mut self.rules, &self.source).await?; + Ok(()) + } + /// Check if rule is exist in the index pub fn contains(&self, value: &str) -> bool { self.is_enabled && self.rules.iter().any(|rule| rule.contains(value)) }