Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Option to use a proxy for outgoing upstream search engine requests #573

Merged
merged 13 commits into from
Oct 5, 2024
33 changes: 33 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ reqwest = { version = "0.11.24", default-features = false, features = [
"rustls-tls",
"brotli",
"gzip",
"socks"
] }
tokio = { version = "1.32.0", features = [
"rt-multi-thread",
Expand Down
14 changes: 14 additions & 0 deletions src/config/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved
use std::{collections::HashMap, fs, thread::available_parallelism};

/// A named struct which stores the parsed config file options.
Expand Down Expand Up @@ -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<Proxy>,
m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved
}

impl Config {
Expand Down Expand Up @@ -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
}
};
m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved

m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved
Ok(Config {
port: globals.get::<_, u16>("port")?,
binding_ip: globals.get::<_, String>("binding_ip")?,
Expand Down Expand Up @@ -148,6 +161,7 @@ impl Config {
safe_search,
#[cfg(any(feature = "redis-cache", feature = "memory-cache"))]
cache_expiry_time,
proxy,
m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
Expand Down
50 changes: 28 additions & 22 deletions src/results/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn aggregate(
safe_search: u8,
) -> Result<SearchResults, Box<dyn std::error::Error>> {
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,
Expand All @@ -86,9 +86,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()
m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved
});

let user_agent: &str = random_user_agent();
Expand Down Expand Up @@ -247,6 +251,7 @@ pub async fn filter_with_lists(

Ok(())
}

/// Sorts SearchResults by relevance score.
/// <br> 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)
Expand All @@ -262,6 +267,7 @@ fn sort_search_results(results: &mut [SearchResult]) {
.unwrap_or(Ordering::Less)
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -285,15 +291,15 @@ mod tests {
},
));
map_to_be_filtered.push((
"https://www.rust-lang.org/".to_owned(),
SearchResult {
title: "Rust Programming Language".to_owned(),
url: "https://www.rust-lang.org/".to_owned(),
description: "A systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.".to_owned(),
engine: smallvec!["Google".to_owned(), "DuckDuckGo".to_owned()],
relevance_score:0.0
},)
);
"https://www.rust-lang.org/".to_owned(),
SearchResult {
title: "Rust Programming Language".to_owned(),
url: "https://www.rust-lang.org/".to_owned(),
description: "A systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.".to_owned(),
engine: smallvec!["Google".to_owned(), "DuckDuckGo".to_owned()],
relevance_score: 0.0,
}, )
);

// Create a temporary file with regex patterns
let mut file = NamedTempFile::new()?;
Expand Down Expand Up @@ -336,15 +342,15 @@ mod tests {
},
));
map_to_be_filtered.push((
"https://www.rust-lang.org/".to_owned(),
SearchResult {
title: "Rust Programming Language".to_owned(),
url: "https://www.rust-lang.org/".to_owned(),
description: "A systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.".to_owned(),
engine: smallvec!["Google".to_owned(), "DuckDuckGo".to_owned()],
relevance_score:0.0
},
));
"https://www.rust-lang.org/".to_owned(),
SearchResult {
title: "Rust Programming Language".to_owned(),
url: "https://www.rust-lang.org/".to_owned(),
description: "A systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.".to_owned(),
engine: smallvec!["Google".to_owned(), "DuckDuckGo".to_owned()],
relevance_score: 0.0,
},
));

// Create a temporary file with a regex pattern containing a wildcard
let mut file = NamedTempFile::new()?;
Expand Down
2 changes: 2 additions & 0 deletions websurfx/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
neon-mmd marked this conversation as resolved.
Show resolved Hide resolved
m00nwtchr marked this conversation as resolved.
Show resolved Hide resolved