mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
handle http errors
This commit is contained in:
parent
2a39387a48
commit
ff661ae26b
1 changed files with 19 additions and 5 deletions
24
src/main.rs
24
src/main.rs
|
|
@ -10,7 +10,7 @@ use fast_socks5::{
|
||||||
use list::List;
|
use list::List;
|
||||||
use log::*;
|
use log::*;
|
||||||
use opt::{AuthMode, Opt};
|
use opt::{AuthMode, Opt};
|
||||||
use rocket::{State, serde::json::Json};
|
use rocket::{State, http::Status, serde::json::Json};
|
||||||
use stats::{Snapshot, Total};
|
use stats::{Snapshot, Total};
|
||||||
use std::{future::Future, sync::Arc};
|
use std::{future::Future, sync::Arc};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
@ -22,19 +22,33 @@ async fn index(totals: &State<Arc<Total>>) -> Json<Snapshot> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::get("/allow/<rule>")]
|
#[rocket::get("/allow/<rule>")]
|
||||||
async fn allow(rule: &str, list: &State<Arc<List>>, totals: &State<Arc<Total>>) -> Json<bool> {
|
async fn allow(
|
||||||
|
rule: &str,
|
||||||
|
list: &State<Arc<List>>,
|
||||||
|
totals: &State<Arc<Total>>,
|
||||||
|
) -> Result<Json<bool>, Status> {
|
||||||
let result = list.allow(rule).await;
|
let result = list.allow(rule).await;
|
||||||
totals.set_entries(list.entries().await);
|
totals.set_entries(list.entries().await);
|
||||||
info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})");
|
info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})");
|
||||||
Json(result.is_ok_and(|v| v))
|
Ok(Json(result.map_err(|e| {
|
||||||
|
error!("Allow request handle error for `{rule}`: `{e}`");
|
||||||
|
Status::InternalServerError
|
||||||
|
})?))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::get("/block/<rule>")]
|
#[rocket::get("/block/<rule>")]
|
||||||
async fn block(rule: &str, list: &State<Arc<List>>, totals: &State<Arc<Total>>) -> Json<bool> {
|
async fn block(
|
||||||
|
rule: &str,
|
||||||
|
list: &State<Arc<List>>,
|
||||||
|
totals: &State<Arc<Total>>,
|
||||||
|
) -> Result<Json<bool>, Status> {
|
||||||
let result = list.block(rule).await;
|
let result = list.block(rule).await;
|
||||||
totals.set_entries(list.entries().await);
|
totals.set_entries(list.entries().await);
|
||||||
info!("Add `{rule}` to the in-memory rules (operation status: {result:?})");
|
info!("Add `{rule}` to the in-memory rules (operation status: {result:?})");
|
||||||
Json(result.is_ok_and(|v| v))
|
Ok(Json(result.map_err(|e| {
|
||||||
|
error!("Block request handle error for `{rule}`: `{e}`");
|
||||||
|
Status::InternalServerError
|
||||||
|
})?))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::launch]
|
#[rocket::launch]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue