mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 08:25:27 +00:00
rename some methods
This commit is contained in:
parent
f5345e963d
commit
0b08744f8f
2 changed files with 11 additions and 11 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -33,7 +33,7 @@ async fn api_allow(
|
||||||
totals: &State<Arc<Total>>,
|
totals: &State<Arc<Total>>,
|
||||||
) -> Result<Json<bool>, Status> {
|
) -> Result<Json<bool>, Status> {
|
||||||
let result = rules.allow(rule).await;
|
let result = rules.allow(rule).await;
|
||||||
totals.set_entries(rules.rules().await);
|
totals.set_entries(rules.total().await);
|
||||||
info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})");
|
info!("Delete `{rule}` from the in-memory rules (operation status: {result:?})");
|
||||||
Ok(Json(result.map_err(|e| {
|
Ok(Json(result.map_err(|e| {
|
||||||
error!("Allow request handle error for `{rule}`: `{e}`");
|
error!("Allow request handle error for `{rule}`: `{e}`");
|
||||||
|
|
@ -48,7 +48,7 @@ async fn api_block(
|
||||||
totals: &State<Arc<Total>>,
|
totals: &State<Arc<Total>>,
|
||||||
) -> Result<Json<bool>, Status> {
|
) -> Result<Json<bool>, Status> {
|
||||||
let result = rules.block(rule).await;
|
let result = rules.block(rule).await;
|
||||||
totals.set_entries(rules.rules().await);
|
totals.set_entries(rules.total().await);
|
||||||
info!("Add `{rule}` to the in-memory rules (operation status: {result:?})");
|
info!("Add `{rule}` to the in-memory rules (operation status: {result:?})");
|
||||||
Ok(Json(result.map_err(|e| {
|
Ok(Json(result.map_err(|e| {
|
||||||
error!("Block request handle error for `{rule}`: `{e}`");
|
error!("Block request handle error for `{rule}`: `{e}`");
|
||||||
|
|
@ -58,9 +58,9 @@ async fn api_block(
|
||||||
|
|
||||||
#[rocket::get("/api/rules")]
|
#[rocket::get("/api/rules")]
|
||||||
async fn api_rules(rules: &State<Arc<Rules>>) -> Result<Json<Vec<String>>, Status> {
|
async fn api_rules(rules: &State<Arc<Rules>>) -> Result<Json<Vec<String>>, Status> {
|
||||||
let result = rules.list().await;
|
let active = rules.active().await;
|
||||||
info!("Get rules (total: {})", result.len());
|
info!("Get rules (total: {})", active.len());
|
||||||
Ok(Json(result))
|
Ok(Json(active))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::launch]
|
#[rocket::launch]
|
||||||
|
|
@ -71,7 +71,7 @@ async fn rocket() -> _ {
|
||||||
|
|
||||||
let rules = Arc::new(Rules::from_opt(&opt.allow_list).await.unwrap());
|
let rules = Arc::new(Rules::from_opt(&opt.allow_list).await.unwrap());
|
||||||
|
|
||||||
let totals = Arc::new(Total::with_rules(rules.rules().await));
|
let totals = Arc::new(Total::with_rules(rules.total().await));
|
||||||
|
|
||||||
tokio::spawn({
|
tokio::spawn({
|
||||||
let socks_rules = rules.clone();
|
let socks_rules = rules.clone();
|
||||||
|
|
|
||||||
10
src/rules.rs
10
src/rules.rs
|
|
@ -58,7 +58,7 @@ impl Rules {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Get total rules from the current session
|
/// Get total rules from the current session
|
||||||
pub async fn rules(&self) -> u64 {
|
pub async fn total(&self) -> u64 {
|
||||||
self.0.read().await.len() as u64
|
self.0.read().await.len() as u64
|
||||||
}
|
}
|
||||||
/// Allow given `rule`
|
/// Allow given `rule`
|
||||||
|
|
@ -72,15 +72,15 @@ impl Rules {
|
||||||
Ok(self.0.write().await.remove(&Item::from_line(rule)))
|
Ok(self.0.write().await.remove(&Item::from_line(rule)))
|
||||||
}
|
}
|
||||||
/// Return active rules (from server memory)
|
/// Return active rules (from server memory)
|
||||||
pub async fn list(&self) -> Vec<String> {
|
pub async fn active(&self) -> Vec<String> {
|
||||||
let mut list: Vec<String> = self
|
let mut rules: Vec<String> = self
|
||||||
.0
|
.0
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.iter()
|
.iter()
|
||||||
.map(|item| item.to_string())
|
.map(|item| item.to_string())
|
||||||
.collect();
|
.collect();
|
||||||
list.sort(); // HashSet does not keep the order
|
rules.sort(); // HashSet does not keep the order
|
||||||
list
|
rules
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue