0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-25 23:48:21 -05:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Łukasz Mariański
9fa0c0a790
Merge 745197152b into 9984ba0d12 2024-09-08 03:26:49 +00:00
neon_arch
745197152b
Merge branch 'rolling' into proxy 2024-09-04 21:56:04 +05:30
m00nwtchr
ed661174ba
Fix formatting 2024-05-25 15:41:32 +02:00
m00nwtchr
f6cf1ce5f5
Enable socks feature in reqwest 2024-05-25 15:34:03 +02:00
m00nwtchr
07e1f663df
Add option to use a proxy for outgoing (search engine) requests. 2024-05-25 15:30:08 +02:00
5 changed files with 44 additions and 8 deletions

13
Cargo.lock generated
View File

@ -3178,6 +3178,7 @@ dependencies = [
"sync_wrapper", "sync_wrapper",
"tokio 1.38.0", "tokio 1.38.0",
"tokio-rustls", "tokio-rustls",
"tokio-socks",
"tokio-util", "tokio-util",
"tower-service", "tower-service",
"url 2.5.0", "url 2.5.0",
@ -4032,6 +4033,18 @@ dependencies = [
"tokio 1.38.0", "tokio 1.38.0",
] ]
[[package]]
name = "tokio-socks"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51165dfa029d2a65969413a6cc96f354b86b464498702f174a4efa13608fd8c0"
dependencies = [
"either",
"futures-util",
"thiserror",
"tokio 1.36.0",
]
[[package]] [[package]]
name = "tokio-sync" name = "tokio-sync"
version = "0.1.8" version = "0.1.8"

View File

@ -17,7 +17,8 @@ reqwest = { version = "0.12.5", default-features = false, features = [
"rustls-tls", "rustls-tls",
"brotli", "brotli",
"gzip", "gzip",
"http2" "socks",
"http2",
] } ] }
tokio = { version = "1.32.0", features = [ tokio = { version = "1.32.0", features = [
"rt-multi-thread", "rt-multi-thread",

View File

@ -6,6 +6,7 @@ use crate::handler::{file_path, FileType};
use crate::models::parser_models::{AggregatorConfig, RateLimiter, Style}; use crate::models::parser_models::{AggregatorConfig, RateLimiter, Style};
use log::LevelFilter; use log::LevelFilter;
use mlua::Lua; use mlua::Lua;
use reqwest::Proxy;
use std::{collections::HashMap, fs, thread::available_parallelism}; use std::{collections::HashMap, fs, thread::available_parallelism};
/// A named struct which stores the parsed config file options. /// A named struct which stores the parsed config file options.
@ -48,6 +49,9 @@ pub struct Config {
pub tcp_connection_keep_alive: u8, pub tcp_connection_keep_alive: u8,
/// It stores the pool idle connection timeout in seconds. /// It stores the pool idle connection timeout in seconds.
pub pool_idle_connection_timeout: u8, pub pool_idle_connection_timeout: u8,
/// Url of the proxy to use for outgoing requests.
pub proxy: Option<Proxy>,
} }
impl Config { impl Config {
@ -118,6 +122,15 @@ impl Config {
_ => parsed_cet, _ => 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 { Ok(Config {
port: globals.get::<_, u16>("port")?, port: globals.get::<_, u16>("port")?,
binding_ip: globals.get::<_, String>("binding_ip")?, binding_ip: globals.get::<_, String>("binding_ip")?,
@ -148,6 +161,7 @@ impl Config {
safe_search, safe_search,
#[cfg(any(feature = "redis-cache", feature = "memory-cache"))] #[cfg(any(feature = "redis-cache", feature = "memory-cache"))]
cache_expiry_time, cache_expiry_time,
proxy,
}) })
} }
} }

View File

@ -76,7 +76,7 @@ pub async fn aggregate(
safe_search: u8, safe_search: u8,
) -> Result<SearchResults, Box<dyn std::error::Error>> { ) -> Result<SearchResults, Box<dyn std::error::Error>> {
let client = CLIENT.get_or_init(|| { 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 .timeout(Duration::from_secs(config.request_timeout as u64)) // Add timeout to request to avoid DDOSing the server
.pool_idle_timeout(Duration::from_secs( .pool_idle_timeout(Duration::from_secs(
config.pool_idle_connection_timeout as u64, config.pool_idle_connection_timeout as u64,
@ -86,9 +86,13 @@ pub async fn aggregate(
.https_only(true) .https_only(true)
.gzip(true) .gzip(true)
.brotli(true) .brotli(true)
.http2_adaptive_window(config.adaptive_window) .http2_adaptive_window(config.adaptive_window);
.build()
.unwrap() if config.proxy.is_some() {
cb = cb.proxy(config.proxy.clone().unwrap());
}
cb.build().unwrap()
}); });
let user_agent: &str = random_user_agent(); let user_agent: &str = random_user_agent();
@ -247,6 +251,7 @@ pub async fn filter_with_lists(
Ok(()) Ok(())
} }
/// Sorts SearchResults by relevance score. /// Sorts SearchResults by relevance score.
/// <br> sort_unstable is used as its faster,stability is not an issue on our side. /// <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) /// For reasons why, check out [`this`](https://rust-lang.github.io/rfcs/1884-unstable-sort.html)
@ -262,6 +267,7 @@ fn sort_search_results(results: &mut [SearchResult]) {
.unwrap_or(Ordering::Less) .unwrap_or(Ordering::Less)
}) })
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -291,8 +297,8 @@ mod tests {
url: "https://www.rust-lang.org/".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(), 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()], engine: smallvec!["Google".to_owned(), "DuckDuckGo".to_owned()],
relevance_score:0.0 relevance_score: 0.0,
},) }, )
); );
// Create a temporary file with regex patterns // Create a temporary file with regex patterns
@ -342,7 +348,7 @@ mod tests {
url: "https://www.rust-lang.org/".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(), 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()], engine: smallvec!["Google".to_owned(), "DuckDuckGo".to_owned()],
relevance_score:0.0 relevance_score: 0.0,
}, },
)); ));

View File

@ -73,3 +73,5 @@ upstream_search_engines = {
Mojeek = false, Mojeek = false,
Bing = false, Bing = false,
} -- select the upstream search engines from which the results should be fetched. } -- 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.