mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
rename some members; add documentation
This commit is contained in:
parent
eeafed077a
commit
8586a5b036
3 changed files with 12 additions and 5 deletions
|
|
@ -16,6 +16,7 @@ pub struct List {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl List {
|
impl List {
|
||||||
|
/// Build new List object
|
||||||
pub async fn from_opt(list: &Vec<String>, cache: Option<PathBuf>) -> Result<Self> {
|
pub async fn from_opt(list: &Vec<String>, cache: Option<PathBuf>) -> Result<Self> {
|
||||||
fn handle(this: &mut HashSet<Item>, line: &str) -> Option<bool> {
|
fn handle(this: &mut HashSet<Item>, line: &str) -> Option<bool> {
|
||||||
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
||||||
|
|
@ -73,19 +74,25 @@ impl List {
|
||||||
cache,
|
cache,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
/// Check if rule is exist in the (allow) index
|
||||||
pub async fn any(&self, value: &str) -> bool {
|
pub async fn any(&self, value: &str) -> bool {
|
||||||
self.index.read().await.iter().any(|item| match item {
|
self.index.read().await.iter().any(|item| match item {
|
||||||
Item::Exact(v) => v == value,
|
Item::Exact(v) => v == value,
|
||||||
Item::Ending(v) => value.ends_with(v),
|
Item::Ending(v) => value.ends_with(v),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub async fn entries(&self) -> u64 {
|
/// Get total rules from the current session
|
||||||
|
pub async fn rules(&self) -> u64 {
|
||||||
self.index.read().await.len() as u64
|
self.index.read().await.len() as u64
|
||||||
}
|
}
|
||||||
|
/// Allow given `rule`
|
||||||
|
/// * return `false` if the `rule` is exist
|
||||||
pub async fn allow(&self, rule: &str) -> Result<bool> {
|
pub async fn allow(&self, rule: &str) -> Result<bool> {
|
||||||
self.cache.allow(rule).await?;
|
self.cache.allow(rule).await?;
|
||||||
Ok(self.index.write().await.insert(Item::from_line(rule)))
|
Ok(self.index.write().await.insert(Item::from_line(rule)))
|
||||||
}
|
}
|
||||||
|
/// Block given `rule`
|
||||||
|
/// * return `false` if the `rule` is not exist
|
||||||
pub async fn block(&self, rule: &str) -> Result<bool> {
|
pub async fn block(&self, rule: &str) -> Result<bool> {
|
||||||
self.cache.block(rule).await?;
|
self.cache.block(rule).await?;
|
||||||
Ok(self.index.write().await.remove(&Item::from_line(rule)))
|
Ok(self.index.write().await.remove(&Item::from_line(rule)))
|
||||||
|
|
|
||||||
|
|
@ -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 = list.allow(rule).await;
|
let result = list.allow(rule).await;
|
||||||
totals.set_entries(list.entries().await);
|
totals.set_entries(list.rules().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 = list.block(rule).await;
|
let result = list.block(rule).await;
|
||||||
totals.set_entries(list.entries().await);
|
totals.set_entries(list.rules().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}`");
|
||||||
|
|
@ -85,7 +85,7 @@ async fn rocket() -> _ {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let totals = Arc::new(Total::with_entries(list.entries().await));
|
let totals = Arc::new(Total::with_rules(list.rules().await));
|
||||||
|
|
||||||
tokio::spawn({
|
tokio::spawn({
|
||||||
let socks_list = list.clone();
|
let socks_list = list.clone();
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ pub struct Total {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Total {
|
impl Total {
|
||||||
pub fn with_entries(entries: u64) -> Self {
|
pub fn with_rules(entries: u64) -> Self {
|
||||||
Self {
|
Self {
|
||||||
entries: entries.into(),
|
entries: entries.into(),
|
||||||
..Self::default()
|
..Self::default()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue