Add support for using proxies.

This commit is contained in:
Kavin 2023-08-21 22:56:18 +01:00
parent ab32f6a140
commit 63953d283e
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F
3 changed files with 39 additions and 6 deletions

17
Cargo.lock generated
View file

@ -946,9 +946,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "h2"
version = "0.3.20"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97ec8491ebaf99c8eaa73058b045fe58073cd6be7f596ac993ced0b0a0c01049"
checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833"
dependencies = [
"bytes",
"fnv",
@ -1914,6 +1914,7 @@ dependencies = [
"serde_urlencoded",
"tokio",
"tokio-rustls",
"tokio-socks",
"tokio-util",
"tower-service",
"url",
@ -2402,6 +2403,18 @@ dependencies = [
"tokio",
]
[[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",
]
[[package]]
name = "tokio-util"
version = "0.7.8"

View file

@ -15,7 +15,7 @@ qstring = "0.7.2"
ravif = { version = "0.11.2", optional = true }
rgb = { version = "0.8.36", optional = true }
regex = "1.9.3"
reqwest = { version = "0.11.19", features = ["rustls-tls", "stream", "brotli", "gzip"], default-features = false }
reqwest = { version = "0.11.19", features = ["rustls-tls", "stream", "brotli", "gzip", "socks"], default-features = false }
tokio = { version = "1.32.0", features = ["full"] }
[features]

View file

@ -1,8 +1,8 @@
use std::env;
use std::error::Error;
use actix_web::{App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, web};
use actix_web::http::Method;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer};
use mimalloc::MiMalloc;
use once_cell::sync::Lazy;
use qstring::QString;
@ -41,6 +41,26 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
let builder = Client::builder()
.user_agent("Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0");
let proxy = if let Ok(proxy) = env::var("PROXY") {
Some(reqwest::Proxy::all(proxy).unwrap())
} else {
None
};
if proxy.is_some() {
// proxy basic auth
let builder = if let Ok(proxy_auth_user) = env::var("PROXY_USER") {
let proxy_auth_pass = env::var("PROXY_PASS").unwrap_or_default();
builder.proxy(
proxy
.unwrap()
.basic_auth(&proxy_auth_user, &proxy_auth_pass),
)
} else {
builder.proxy(proxy.unwrap())
};
}
if env::var("IPV4_ONLY").is_ok() {
builder
.local_address(Some("0.0.0.0".parse().unwrap()))