Skip to content

Commit

Permalink
Support proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterDing committed Dec 15, 2023
1 parent 62e9984 commit a059181
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/arguments/clap_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub struct AgetCli {
#[clap(long, help = "The seconds between retries [default: 0]")]
pub retry_wait: Option<u64>,

#[clap(
long = "proxy",
name = "PROXY",
help = "[protocol://]host[:port] Use this proxy"
)]
pub proxy: Option<String>,

#[clap(
long = "type",
name = "TYPE",
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/cmd_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions src/common/net/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
}
12 changes: 8 additions & 4 deletions src/common/net/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -33,6 +33,7 @@ pub fn build_http_client(
timeout: Duration,
dns_timeout: Duration,
keep_alive: Duration,
proxy: Option<&str>,
) -> Result<HttpClient> {
let mut default_headers = HeaderMap::new();
headers.iter().for_each(|(k, v)| {
Expand All @@ -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
Expand Down

0 comments on commit a059181

Please sign in to comment.