Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
config::{cache_dir, config_dir, CONFIG_FILE_NAME, THEME_FILE_NAME},
};
use wiki_api::languages::Language;
use wiki_api::proxy::init_proxy;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -35,6 +36,10 @@ struct Cli {
#[arg(long = "theme-config-path")]
print_theme_config_path: bool,

/// Set the proxy
#[arg(long = "proxy")]
proxy: Option<String>,

#[cfg(debug_assertions)]
#[arg(value_name = "PATH", long = "page")]
load_debug_page: Option<std::path::PathBuf>,
Expand All @@ -43,6 +48,7 @@ struct Cli {
pub struct CliResults {
pub actions: Option<ActionPacket>,
pub log_level: Option<tracing::level_filters::LevelFilter>,
pub warn_list: Vec<String>,
}

pub fn match_cli() -> CliResults {
Expand All @@ -52,6 +58,7 @@ pub fn match_cli() -> CliResults {
let mut results = CliResults {
actions: None,
log_level: None,
warn_list: Vec::new(),
};

let mut packet = ActionPacket::default();
Expand Down Expand Up @@ -103,6 +110,14 @@ pub fn match_cli() -> CliResults {
should_quit = true;
}

if let Some(proxy) = cli.proxy {
if let Err(err) = init_proxy(&proxy) {
results.warn_list.push(err);
let action = Action::PopupMessage("Information".to_string(), "Something went wrong when trying to initial proxy \nCheck the logs for further information".to_string());
packet.add_action(action);
}
};

#[cfg(debug_assertions)]
if let Some(ref debug_page) = cli.load_debug_page {
if let Some(page) = wiki_api::page::Page::from_path(debug_page) {
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ async fn main() -> Result<()> {
initialize_logging(results.log_level)?;
initialize_panic_handler()?;

// Log match cli message
for err in results.warn_list {
warn!(err)
}

let (action_tx, mut action_rx) = mpsc::unbounded_channel();

let app_component = Arc::new(Mutex::new(AppComponent::default()));
Expand Down
2 changes: 1 addition & 1 deletion wiki-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bitflags = { version = "2.6.0", features = ["serde"] }
ego-tree = "0.6.2"
html5ever = "0.26.0"
markup5ever_rcdom = "0.2.0"
reqwest = "0.11.20"
reqwest = { version = "0.11.20", features = ["socks"] }
scraper = "0.17.1"
serde = "1.0.188"
serde_json = "1.0.105"
Expand Down
1 change: 1 addition & 0 deletions wiki-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod document;
pub mod languages;
pub mod page;
pub mod parser;
pub mod proxy;
pub mod search;

// TODO: Make Endpoint a real struct
Expand Down
11 changes: 10 additions & 1 deletion wiki-api/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tracing::{debug, warn};
use url::Url;

use super::languages::Language;
use super::proxy::get_proxy;

pub mod link_data {
use crate::{languages::Language, search::Namespace, Endpoint};
Expand Down Expand Up @@ -398,7 +399,15 @@ impl<I, P, U, L> PageBuilder<I, P, U, L> {
impl<I, P> PageBuilder<I, P, WithEndpoint, WithLanguage> {
async fn fetch_with_params(self, mut params: Vec<(&str, String)>) -> Result<Page> {
async fn action_parse(params: Vec<(&str, String)>, endpoint: Url) -> Result<Response> {
Client::new()
// Create builder with proxy
let client_builder = match get_proxy() {
Some(proxy) => Client::builder().proxy(proxy.clone()),
None => Client::builder(),
};

client_builder
.build()
.expect("clinet build error")
.get(endpoint)
.query(&[
("action", "parse"),
Expand Down
34 changes: 34 additions & 0 deletions wiki-api/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use reqwest::Proxy;
use std::sync::OnceLock;

static PROXY: OnceLock<Proxy> = OnceLock::new();

fn validate_proxy(addr: &str) -> Result<(), String> {
let _addr_clean = if let Some(addr) = addr.strip_prefix("socks5://") {
addr
} else if let Some(addr) = addr.strip_prefix("http://") {
addr
} else if let Some(addr) = addr.strip_prefix("https://") {
addr
} else {
return Err(format!("Invalid proxy protocol: {}", addr));
};

// TODO: check host and port
Ok(())
}

// Init proxy only once
pub fn init_proxy(str: &str) -> Result<(), String> {
validate_proxy(str)?;
let proxy = match Proxy::all(str) {
Ok(o) => o,
Err(e) => return Err(e.to_string()),
};
PROXY.set(proxy).expect("init error");
Ok(())
}

pub fn get_proxy() -> Option<&'static Proxy> {
PROXY.get()
}
11 changes: 10 additions & 1 deletion wiki-api/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::fmt::Write;
use crate::Endpoint;

use crate::languages::Language;
use crate::proxy::get_proxy;

/// A finished search containing the found results and additional optional information regarding
/// the search
Expand Down Expand Up @@ -699,7 +700,15 @@ impl SearchBuilder<WithQuery, WithEndpoint, WithLanguage> {
/// - The returned result could not interpreted as a `Search`
pub async fn search(self) -> Result<Search> {
async fn action_query(params: Vec<(&str, String)>, endpoint: Endpoint) -> Result<Response> {
Client::new()
// Create builder with proxy
let client_builder = match get_proxy() {
Some(proxy) => Client::builder().proxy(proxy.clone()),
None => Client::builder(),
};

client_builder
.build()
.expect("clinet build error")
.get(endpoint)
.query(&[
("action", "query"),
Expand Down