diff --git a/Cargo.lock b/Cargo.lock index de9a820..b9222bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3266,6 +3266,7 @@ dependencies = [ "sync_wrapper", "tokio 1.40.0", "tokio-rustls", + "tokio-socks", "tokio-util", "tower-service", "url 2.5.2", @@ -4096,6 +4097,18 @@ dependencies = [ "tokio 1.40.0", ] +[[package]] +name = "tokio-socks" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4770b8024672c1101b3f6733eab95b18007dbe0847a8afe341fcf79e06043f" +dependencies = [ + "either", + "futures-util", + "thiserror", + "tokio 1.38.0", +] + [[package]] name = "tokio-sync" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index d4dd298..557d953 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ reqwest = { version = "0.12.5", default-features = false, features = [ "rustls-tls", "brotli", "gzip", - "http2" + "http2", ] } tokio = { version = "1.32.0", features = [ "rt-multi-thread", @@ -174,7 +174,7 @@ opt-level = "z" [features] use-synonyms-search = ["thesaurus/static"] -default = ["memory-cache"] +default = ["memory-cache", "socks"] dhat-heap = ["dep:dhat"] memory-cache = ["dep:moka"] redis-cache = ["dep:redis", "dep:base64"] @@ -183,3 +183,4 @@ encrypt-cache-results = ["dep:chacha20poly1305", "dep:chacha20"] cec-cache-results = ["compress-cache-results", "encrypt-cache-results"] experimental-io-uring = ["actix-web/experimental-io-uring"] use-non-static-synonyms-search = ["thesaurus"] +socks = ["reqwest/socks"] \ No newline at end of file diff --git a/src/config/parser.rs b/src/config/parser.rs index 8bed460..b9556c4 100644 --- a/src/config/parser.rs +++ b/src/config/parser.rs @@ -6,6 +6,7 @@ use crate::handler::{file_path, FileType}; use crate::models::parser_models::{AggregatorConfig, RateLimiter, Style}; use log::LevelFilter; use mlua::Lua; +use reqwest::Proxy; use std::{collections::HashMap, fs, thread::available_parallelism}; /// A named struct which stores the parsed config file options. @@ -48,6 +49,9 @@ pub struct Config { pub tcp_connection_keep_alive: u8, /// It stores the pool idle connection timeout in seconds. pub pool_idle_connection_timeout: u8, + + /// Url of the proxy to use for outgoing requests. + pub proxy: Option, } impl Config { @@ -118,6 +122,15 @@ impl Config { _ => parsed_cet, }; + let proxy_str = globals.get::<_, String>("proxy")?; + let proxy = match Proxy::all(proxy_str) { + Ok(proxy) => Some(proxy), + Err(_) => { + log::error!("Invalid proxy url, defaulting to no proxy."); + None + } + }; + Ok(Config { port: globals.get::<_, u16>("port")?, binding_ip: globals.get::<_, String>("binding_ip")?, @@ -148,6 +161,7 @@ impl Config { safe_search, #[cfg(any(feature = "redis-cache", feature = "memory-cache"))] cache_expiry_time, + proxy, }) } } diff --git a/src/results/aggregator.rs b/src/results/aggregator.rs index 009111b..4538075 100644 --- a/src/results/aggregator.rs +++ b/src/results/aggregator.rs @@ -75,7 +75,7 @@ pub async fn aggregate( safe_search: u8, ) -> Result> { let client = CLIENT.get_or_init(|| { - ClientBuilder::new() + let mut cb = ClientBuilder::new() .timeout(Duration::from_secs(config.request_timeout as u64)) // Add timeout to request to avoid DDOSing the server .pool_idle_timeout(Duration::from_secs( config.pool_idle_connection_timeout as u64, @@ -85,9 +85,13 @@ pub async fn aggregate( .https_only(true) .gzip(true) .brotli(true) - .http2_adaptive_window(config.adaptive_window) - .build() - .unwrap() + .http2_adaptive_window(config.adaptive_window); + + if config.proxy.is_some() { + cb = cb.proxy(config.proxy.clone().unwrap()); + } + + cb.build().unwrap() }); let user_agent: &str = random_user_agent(); @@ -241,6 +245,7 @@ pub async fn filter_with_lists( Ok(()) } + /// Sorts SearchResults by relevance score. ///
sort_unstable is used as its faster,stability is not an issue on our side. /// For reasons why, check out [`this`](https://rust-lang.github.io/rfcs/1884-unstable-sort.html) @@ -256,6 +261,7 @@ fn sort_search_results(results: &mut [SearchResult]) { .unwrap_or(Ordering::Less) }) } + #[cfg(test)] mod tests { use super::*; diff --git a/websurfx/config.lua b/websurfx/config.lua index f346c1f..61857e4 100644 --- a/websurfx/config.lua +++ b/websurfx/config.lua @@ -73,3 +73,5 @@ upstream_search_engines = { Mojeek = false, Bing = false, } -- select the upstream search engines from which the results should be fetched. + +proxy = "" -- Proxy to send outgoing requests through. Set to empty string to disable. \ No newline at end of file