mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-04-01 00:45:27 +00:00
draft multi-list rules api
This commit is contained in:
parent
752ea23f80
commit
18509e6d1a
4 changed files with 133 additions and 76 deletions
60
src/main.rs
60
src/main.rs
|
|
@ -33,12 +33,9 @@ async fn api_allow(
|
|||
totals: &State<Arc<Total>>,
|
||||
) -> Result<Json<bool>, Status> {
|
||||
let result = rules.allow(rule).await;
|
||||
totals.set_entries(rules.total().await);
|
||||
totals.set_entries(rules.total(true).await); // @TODO separate active/inactive totals
|
||||
info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})");
|
||||
Ok(Json(result.map_err(|e| {
|
||||
error!("Allow request handle error for `{rule}`: `{e}`");
|
||||
Status::InternalServerError
|
||||
})?))
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/block/<rule>")]
|
||||
|
|
@ -46,14 +43,11 @@ async fn api_block(
|
|||
rule: &str,
|
||||
rules: &State<Arc<Rules>>,
|
||||
totals: &State<Arc<Total>>,
|
||||
) -> Result<Json<bool>, Status> {
|
||||
) -> Result<Json<()>, Status> {
|
||||
let result = rules.block(rule).await;
|
||||
totals.set_entries(rules.total().await);
|
||||
totals.set_entries(rules.total(true).await); // @TODO separate active/inactive totals
|
||||
info!("Add `{rule}` to the in-memory rules (operation status: {result:?})");
|
||||
Ok(Json(result.map_err(|e| {
|
||||
error!("Block request handle error for `{rule}`: `{e}`");
|
||||
Status::InternalServerError
|
||||
})?))
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/rules")]
|
||||
|
|
@ -65,7 +59,7 @@ async fn api_rules(rules: &State<Arc<Rules>>) -> Result<Json<Vec<String>>, Statu
|
|||
|
||||
#[rocket::get("/api/lists")]
|
||||
async fn api_lists(rules: &State<Arc<Rules>>) -> Result<Json<Vec<rules::ListEntry>>, Status> {
|
||||
let lists = rules.lists();
|
||||
let lists = rules.lists().await;
|
||||
debug!("Get lists index (total: {})", lists.len());
|
||||
Ok(Json(lists))
|
||||
}
|
||||
|
|
@ -74,15 +68,39 @@ async fn api_lists(rules: &State<Arc<Rules>>) -> Result<Json<Vec<rules::ListEntr
|
|||
async fn api_list(
|
||||
id: usize,
|
||||
rules: &State<Arc<Rules>>,
|
||||
) -> Result<Json<Option<&rules::List>>, Status> {
|
||||
let list = rules.list(&id);
|
||||
) -> Result<Json<Option<rules::List>>, Status> {
|
||||
let list = rules.list(&id).await;
|
||||
debug!(
|
||||
"Get list #{id} rules (total: {:?})",
|
||||
list.map(|l| l.items.len())
|
||||
list.as_ref().map(|l| l.items.len())
|
||||
);
|
||||
Ok(Json(list))
|
||||
}
|
||||
|
||||
#[rocket::get("/api/list/enable/<id>")]
|
||||
async fn api_list_enable(
|
||||
id: usize,
|
||||
rules: &State<Arc<Rules>>,
|
||||
totals: &State<Arc<Total>>,
|
||||
) -> Result<Json<Option<()>>, Status> {
|
||||
let affected = rules.enable(&id, true).await;
|
||||
totals.set_entries(rules.total(true).await); // @TODO separate active/inactive totals
|
||||
info!("Enabled {affected:?} rules from the active rule set");
|
||||
Ok(Json(affected)) // @TODO handle empty result
|
||||
}
|
||||
|
||||
#[rocket::get("/api/list/disable/<id>")]
|
||||
async fn api_list_disable(
|
||||
id: usize,
|
||||
rules: &State<Arc<Rules>>,
|
||||
totals: &State<Arc<Total>>,
|
||||
) -> Result<Json<Option<()>>, Status> {
|
||||
let affected = rules.enable(&id, false).await;
|
||||
totals.set_entries(rules.total(true).await); // @TODO separate active/inactive totals
|
||||
info!("Disabled {affected:?} rules from the active rule set");
|
||||
Ok(Json(affected)) // @TODO handle empty result
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
async fn rocket() -> _ {
|
||||
env_logger::init();
|
||||
|
|
@ -91,7 +109,7 @@ async fn rocket() -> _ {
|
|||
|
||||
let rules = Arc::new(Rules::from_opt(&opt.allow_list).await.unwrap());
|
||||
|
||||
let totals = Arc::new(Total::with_rules(rules.total().await));
|
||||
let totals = Arc::new(Total::with_rules(rules.total(true).await)); // @TODO separate active/inactive totals
|
||||
|
||||
tokio::spawn({
|
||||
let socks_rules = rules.clone();
|
||||
|
|
@ -115,7 +133,15 @@ async fn rocket() -> _ {
|
|||
.mount(
|
||||
"/",
|
||||
rocket::routes![
|
||||
index, api_totals, api_allow, api_block, api_rules, api_lists, api_list
|
||||
index,
|
||||
api_totals,
|
||||
api_allow,
|
||||
api_block,
|
||||
api_rules,
|
||||
api_lists,
|
||||
api_list,
|
||||
api_list_enable,
|
||||
api_list_disable
|
||||
],
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue