mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
implement (rules) list api
This commit is contained in:
parent
461f838cc0
commit
eeafed077a
4 changed files with 34 additions and 1 deletions
|
|
@ -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:
|
* 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/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/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)
|
* `/api/cache/clean` - clean `--cache` file (returns deleted rules or `null` if not enabled)
|
||||||
|
|
||||||
### Allow list example
|
### Allow list example
|
||||||
|
|
|
||||||
|
|
@ -95,4 +95,13 @@ impl List {
|
||||||
pub async fn cache_clean(&self) -> Result<Option<Vec<String>>> {
|
pub async fn cache_clean(&self) -> Result<Option<Vec<String>>> {
|
||||||
self.cache.clean().await
|
self.cache.clean().await
|
||||||
}
|
}
|
||||||
|
/// Return active rules (from server memory)
|
||||||
|
pub async fn list(&self) -> Vec<String> {
|
||||||
|
self.index
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.iter()
|
||||||
|
.map(|item| item.to_string())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
16
src/main.rs
16
src/main.rs
|
|
@ -56,6 +56,13 @@ async fn api_block(
|
||||||
})?))
|
})?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rocket::get("/api/list")]
|
||||||
|
async fn api_list(list: &State<Arc<List>>) -> Result<Json<Vec<String>>, Status> {
|
||||||
|
let result = list.list().await;
|
||||||
|
info!("Get list rules (total: {})", result.len());
|
||||||
|
Ok(Json(result))
|
||||||
|
}
|
||||||
|
|
||||||
#[rocket::get("/api/cache/clean")]
|
#[rocket::get("/api/cache/clean")]
|
||||||
async fn api_cache_clean(list: &State<Arc<List>>) -> Result<Json<Option<Vec<String>>>, Status> {
|
async fn api_cache_clean(list: &State<Arc<List>>) -> Result<Json<Option<Vec<String>>>, Status> {
|
||||||
let result = list.cache_clean().await;
|
let result = list.cache_clean().await;
|
||||||
|
|
@ -101,7 +108,14 @@ async fn rocket() -> _ {
|
||||||
.manage(Instant::now())
|
.manage(Instant::now())
|
||||||
.mount(
|
.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
|
||||||
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue