From 63953d283e2b2f655bd59acbcde3a1e4f9d31233 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 21 Aug 2023 22:56:18 +0100 Subject: [PATCH] Add support for using proxies. --- Cargo.lock | 17 +++++++++++++++-- Cargo.toml | 2 +- src/main.rs | 26 +++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 726cd87..e51437d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 8761896..3838032 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/main.rs b/src/main.rs index aa74be1..14877c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -27,8 +27,8 @@ async fn main() -> std::io::Result<()> { let bind = env::var("BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string()); server.bind(bind)? } - .run() - .await + .run() + .await } static RE_DOMAIN: Lazy = @@ -41,6 +41,26 @@ static CLIENT: Lazy = 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()))