mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 08:25:27 +00:00
implement list reload method; reload list on enable request
This commit is contained in:
parent
a27d7a4ab8
commit
d52b580844
4 changed files with 55 additions and 18 deletions
23
src/main.rs
23
src/main.rs
|
|
@ -34,7 +34,16 @@ async fn api_list_enable(
|
||||||
rules: &State<Arc<RwLock<Rules>>>,
|
rules: &State<Arc<RwLock<Rules>>>,
|
||||||
) -> Result<Json<Option<bool>>, Status> {
|
) -> Result<Json<Option<bool>>, Status> {
|
||||||
Ok(Json(
|
Ok(Json(
|
||||||
rules.write().await.set_enabled(alias, true).map(|old| !old),
|
rules
|
||||||
|
.write()
|
||||||
|
.await
|
||||||
|
.set_enabled(alias, true)
|
||||||
|
.await
|
||||||
|
.map_err(|err| {
|
||||||
|
error!("List status change error: `{err}`");
|
||||||
|
Status::InternalServerError
|
||||||
|
})?
|
||||||
|
.map(|old| !old),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +52,17 @@ async fn api_list_disable(
|
||||||
alias: &str,
|
alias: &str,
|
||||||
rules: &State<Arc<RwLock<Rules>>>,
|
rules: &State<Arc<RwLock<Rules>>>,
|
||||||
) -> Result<Json<Option<bool>>, Status> {
|
) -> Result<Json<Option<bool>>, Status> {
|
||||||
Ok(Json(rules.write().await.set_enabled(alias, false)))
|
Ok(Json(
|
||||||
|
rules
|
||||||
|
.write()
|
||||||
|
.await
|
||||||
|
.set_enabled(alias, false)
|
||||||
|
.await
|
||||||
|
.map_err(|err| {
|
||||||
|
error!("List status change error: `{err}`");
|
||||||
|
Status::InternalServerError
|
||||||
|
})?,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::launch]
|
#[rocket::launch]
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,11 @@ impl Rules {
|
||||||
.is_none())
|
.is_none())
|
||||||
}
|
}
|
||||||
/// Change rule set status by list ID
|
/// Change rule set status by list ID
|
||||||
pub fn set_enabled(&mut self, list_alias: &str, status: bool) -> Option<bool> {
|
pub async fn set_enabled(&mut self, list_alias: &str, status: bool) -> Result<Option<bool>> {
|
||||||
self.0
|
Ok(match self.0.get_mut(list_alias) {
|
||||||
.get_mut(list_alias)
|
Some(list) => Some(list.set_enabled(status).await?),
|
||||||
.map(|list| list.set_enabled(status))
|
None => None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
/// Check if rule is exist in the index
|
/// Check if rule is exist in the index
|
||||||
pub fn any(&self, value: &str) -> bool {
|
pub fn any(&self, value: &str) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ use std::collections::HashSet;
|
||||||
|
|
||||||
pub struct List {
|
pub struct List {
|
||||||
pub is_enabled: bool,
|
pub is_enabled: bool,
|
||||||
//pub source: Source,
|
|
||||||
pub rules: HashSet<Rule>,
|
pub rules: HashSet<Rule>,
|
||||||
|
pub source: Source,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl List {
|
impl List {
|
||||||
|
|
@ -18,29 +18,37 @@ impl List {
|
||||||
let mut rules = HashSet::new();
|
let mut rules = HashSet::new();
|
||||||
let source = Source::from_str(src)?;
|
let source = Source::from_str(src)?;
|
||||||
if is_enabled {
|
if is_enabled {
|
||||||
for line in source.get().await?.lines() {
|
reload(&mut rules, &source).await?;
|
||||||
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
|
||||||
continue; // skip comments
|
|
||||||
}
|
|
||||||
if !rules.insert(Rule::from_line(line)) {
|
|
||||||
warn!("List `{src}` contains duplicated entry: `{line}`")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
rules,
|
rules,
|
||||||
//source,
|
source,
|
||||||
is_enabled,
|
is_enabled,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
/// Change rule set status by list ID
|
/// Change rule set status by list ID
|
||||||
pub fn set_enabled(&mut self, is_enabled: bool) -> bool {
|
pub async fn set_enabled(&mut self, is_enabled: bool) -> Result<bool> {
|
||||||
let was_enabled = self.is_enabled;
|
let was_enabled = self.is_enabled;
|
||||||
self.is_enabled = is_enabled;
|
self.is_enabled = is_enabled;
|
||||||
was_enabled
|
if is_enabled {
|
||||||
|
reload(&mut self.rules, &self.source).await?;
|
||||||
|
}
|
||||||
|
Ok(was_enabled)
|
||||||
}
|
}
|
||||||
/// Check if rule is exist in the items index
|
/// Check if rule is exist in the items index
|
||||||
pub fn contains(&self, value: &str) -> bool {
|
pub fn contains(&self, value: &str) -> bool {
|
||||||
self.is_enabled && self.rules.iter().any(|rule| rule.contains(value))
|
self.is_enabled && self.rules.iter().any(|rule| rule.contains(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async fn reload(rules: &mut HashSet<Rule>, source: &Source) -> Result<()> {
|
||||||
|
rules.clear();
|
||||||
|
for line in source.get().await?.lines() {
|
||||||
|
if line.starts_with("/") || line.starts_with("#") || line.is_empty() {
|
||||||
|
continue; // skip comments
|
||||||
|
}
|
||||||
|
if !rules.insert(Rule::from_line(line)) {
|
||||||
|
warn!("List `{source}` contains duplicated entry: `{line}`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,3 +38,12 @@ impl FromStr for Source {
|
||||||
Source::from_str(s)
|
Source::from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Source {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Path(path) => write!(f, "{}", path.to_string_lossy()),
|
||||||
|
Self::Url(url) => write!(f, "{url}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue