mirror of
https://codeberg.org/YGGverse/psocks.git
synced 2026-03-31 16:35:28 +00:00
grand refactory to multiple list-based control api
This commit is contained in:
parent
827cb182f2
commit
b93c1e8481
15 changed files with 271 additions and 282 deletions
40
src/rules/list/source.rs
Normal file
40
src/rules/list/source.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use anyhow::{Result, bail};
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
use url::Url;
|
||||
|
||||
pub enum Source {
|
||||
Path(PathBuf),
|
||||
Url(Url),
|
||||
}
|
||||
|
||||
impl Source {
|
||||
pub fn from_str(source: &str) -> Result<Self> {
|
||||
Ok(if source.contains("://") {
|
||||
Self::Url(Url::from_str(source)?)
|
||||
} else {
|
||||
Self::Path(PathBuf::from_str(source)?.canonicalize()?)
|
||||
})
|
||||
}
|
||||
pub async fn get(&self) -> Result<String> {
|
||||
Ok(match self {
|
||||
Source::Path(path) => tokio::fs::read_to_string(path).await?,
|
||||
Source::Url(url) => {
|
||||
let request = url.as_str();
|
||||
let response = reqwest::get(request).await?;
|
||||
let status = response.status();
|
||||
if status.is_success() {
|
||||
response.text().await?
|
||||
} else {
|
||||
bail!("Could not receive remote list `{request}`: `{status}`")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Source {
|
||||
type Err = anyhow::Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Source::from_str(s)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue