Use SOCKS5 configuration for HTTP requests

This commit is contained in:
link2xt
2023-02-08 23:26:39 +00:00
parent 151b34ea79
commit fa198c3b5e
7 changed files with 80 additions and 12 deletions

View File

@@ -4,10 +4,21 @@ use std::time::Duration;
use anyhow::Result;
use crate::socks::Socks5Config;
const HTTP_TIMEOUT: Duration = Duration::from_secs(30);
pub(crate) fn get_client() -> Result<reqwest::Client> {
Ok(reqwest::ClientBuilder::new()
.timeout(HTTP_TIMEOUT)
.build()?)
pub(crate) fn get_client(socks5_config: Option<Socks5Config>) -> Result<reqwest::Client> {
let builder = reqwest::ClientBuilder::new().timeout(HTTP_TIMEOUT);
let builder = if let Some(socks5_config) = socks5_config {
let proxy = reqwest::Proxy::all(socks5_config.to_url())?;
builder.proxy(proxy)
} else {
// Disable usage of "system" proxy configured via environment variables.
// It is enabled by default in `reqwest`, see
// <https://docs.rs/reqwest/0.11.14/reqwest/struct.ClientBuilder.html#method.no_proxy>
// for documentation.
builder.no_proxy()
};
Ok(builder.build()?)
}