mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 08:25:27 +00:00
implement list(s) reload api
This commit is contained in:
parent
cda06f0811
commit
96b9646c8c
4 changed files with 68 additions and 4 deletions
|
|
@ -29,6 +29,8 @@ RUST_LOG=trace psokcs -c=/path/to/config.toml no-auth
|
|||
* `/api/totals` - blocking summary
|
||||
* `/api/list/enable/<ID>` - enable all parsed rules of given list ID (`[list.ID]` in your config)
|
||||
* `/api/list/disable/<ID>` - disable all parsed rules of given list ID (`[list.ID]` in your config)
|
||||
* `/api/list/reload/<ID>` - 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
|
||||
|
||||
|
|
|
|||
34
src/main.rs
34
src/main.rs
|
|
@ -65,6 +65,31 @@ async fn api_list_disable(
|
|||
))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/list/reload/<alias>")]
|
||||
async fn api_list_reload(
|
||||
alias: Option<&str>,
|
||||
rules: &State<Arc<RwLock<Rules>>>,
|
||||
) -> Result<Json<Vec<String>>, 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<Arc<RwLock<Rules>>>,
|
||||
) -> Result<Json<Vec<String>>, 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
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
26
src/rules.rs
26
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<Option<bool>> {
|
||||
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<Vec<String>> {
|
||||
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))
|
||||
|
|
|
|||
|
|
@ -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<bool> {
|
||||
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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue