From a05918198b191273529347b7096766202bf7fc7b Mon Sep 17 00:00:00 2001 From: PeterDing Date: Fri, 15 Dec 2023 12:27:37 +0800 Subject: [PATCH] Support proxy --- src/arguments/clap_cli.rs | 7 +++++++ src/arguments/cmd_args.rs | 2 +- src/common/net/mod.rs | 12 +----------- src/common/net/net.rs | 12 ++++++++---- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/arguments/clap_cli.rs b/src/arguments/clap_cli.rs index 44bc2da..ccd6ca0 100644 --- a/src/arguments/clap_cli.rs +++ b/src/arguments/clap_cli.rs @@ -53,6 +53,13 @@ pub struct AgetCli { #[clap(long, help = "The seconds between retries [default: 0]")] pub retry_wait: Option, + #[clap( + long = "proxy", + name = "PROXY", + help = "[protocol://]host[:port] Use this proxy" + )] + pub proxy: Option, + #[clap( long = "type", name = "TYPE", diff --git a/src/arguments/cmd_args.rs b/src/arguments/cmd_args.rs index 366e142..516257b 100644 --- a/src/arguments/cmd_args.rs +++ b/src/arguments/cmd_args.rs @@ -157,7 +157,7 @@ impl Args for CmdArgs { /// socks5h:// /// as SOCKS proxy fn proxy(&self) -> Option<&str> { - None + self.cli.proxy.as_deref() } // Set request timeout diff --git a/src/common/net/mod.rs b/src/common/net/mod.rs index 0821dd5..49cc161 100644 --- a/src/common/net/mod.rs +++ b/src/common/net/mod.rs @@ -1,11 +1,9 @@ pub mod net; -use std::time::Duration; - pub use http::Uri; pub use reqwest::{ header::{HeaderMap, HeaderName}, - Client as HttpClient, Method, Request, Response, + Client as HttpClient, Method, Proxy, Request, Response, }; pub use url::Url; @@ -15,11 +13,3 @@ pub enum ContentLengthValue { DirectLength(u64), NoLength, } - -pub struct ConnectorConfig { - pub timeout: Duration, - pub dns_timeout: Duration, - pub keep_alive: Duration, - pub lifetime: Duration, - pub disable_redirects: bool, -} diff --git a/src/common/net/net.rs b/src/common/net/net.rs index ee32cb2..e2fe4a0 100644 --- a/src/common/net/net.rs +++ b/src/common/net/net.rs @@ -2,7 +2,7 @@ use std::time::Duration; use crate::common::{ errors::{Error, Result}, - net::{ContentLengthValue, HeaderMap, HeaderName, HttpClient, Method, Response, Url}, + net::{ContentLengthValue, HeaderMap, HeaderName, HttpClient, Method, Proxy, Response, Url}, range::RangePair, }; @@ -33,6 +33,7 @@ pub fn build_http_client( timeout: Duration, dns_timeout: Duration, keep_alive: Duration, + proxy: Option<&str>, ) -> Result { let mut default_headers = HeaderMap::new(); headers.iter().for_each(|(k, v)| { @@ -42,12 +43,15 @@ pub fn build_http_client( default_headers.insert("accept", "*/*".parse().unwrap()); } - Ok(HttpClient::builder() + let mut client = HttpClient::builder() .timeout(timeout) .connect_timeout(dns_timeout) .tcp_keepalive(keep_alive) - .default_headers(default_headers) - .build()?) + .default_headers(default_headers); + if let Some(url) = proxy { + client = client.proxy(Proxy::all(url)?); + } + Ok(client.build()?) } /// Check whether the response is success