Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto detect proxy from environments #649

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum RedirectAuthHeaders {
/// Accumulates options towards building an [Agent].
pub struct AgentBuilder {
config: AgentConfig,
try_proxy_from_env: bool,
max_idle_connections: usize,
max_idle_connections_per_host: usize,
/// Cookies saved between requests.
Expand Down Expand Up @@ -263,6 +264,7 @@ impl AgentBuilder {
user_agent: format!("ureq/{}", env!("CARGO_PKG_VERSION")),
tls_config: TlsConfig(crate::default_tls_config()),
},
try_proxy_from_env: false,
max_idle_connections: DEFAULT_MAX_IDLE_CONNECTIONS,
max_idle_connections_per_host: DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST,
resolver: StdResolver.into(),
Expand All @@ -277,7 +279,12 @@ impl AgentBuilder {
// AgentBuilder to be used multiple times, except CookieStore does
// not implement clone, so we have to give ownership to the newly
// built Agent.
pub fn build(self) -> Agent {
pub fn build(mut self) -> Agent {
if self.config.proxy.is_none() && self.try_proxy_from_env {
if let Some(proxy) = Proxy::try_from_system() {
self.config.proxy = Some(proxy);
}
}
dojiong marked this conversation as resolved.
Show resolved Hide resolved
Agent {
config: Arc::new(self.config),
state: Arc::new(AgentState {
Expand Down Expand Up @@ -306,11 +313,24 @@ impl AgentBuilder {
/// # Ok(())
/// # }
/// ```
///
/// Adding a proxy will disable `try_proxy_from_env`.
pub fn proxy(mut self, proxy: Proxy) -> Self {
self.config.proxy = Some(proxy);
self
}

/// Attempt to detect proxy settings from the environment, i.e. HTTP_PROXY
///
/// The default is `false`, i.e. not detecting proxy from env since this is
/// a potential security risk.
///
/// If the `proxy` is set on the builder, this setting has no effect.
pub fn try_proxy_from_env(mut self, do_try: bool) -> Self {
self.try_proxy_from_env = do_try;
self
}

/// Enforce the client to only perform HTTPS requests.
/// This setting also makes the client refuse HTTPS to HTTP redirects.
/// Default is false
Expand Down
24 changes: 24 additions & 0 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ impl Proxy {
self.user.is_some() && self.password.is_some()
}

pub(crate) fn try_from_system() -> Option<Self> {
macro_rules! try_env {
($($env:literal),+) => {
$(
if let Ok(env) = std::env::var($env) {
if let Ok(proxy) = Self::new(env) {
return Some(proxy);
}
}
)+
};
}

try_env!(
"ALL_PROXY",
"all_proxy",
"HTTPS_PROXY",
"https_proxy",
"HTTP_PROXY",
"http_proxy"
);
None
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. Nice!

/// Create a proxy from a format string.
/// # Arguments:
/// * `proxy` - a str of format `<protocol>://<user>:<password>@<host>:port` . All parts except host are optional.
Expand Down