use crate::log_info;
use crate::proxy::{normalize_proxy_url, ProxyScheme};
use std::env;
use url::Url;
pub fn get_env_proxy(url: &str) -> Option<String> {
let parsed = Url::parse(url).ok()?;
if let Ok(no_proxy) = env::var("no_proxy").or_else(|_| env::var("NO_PROXY")) {
if is_url_in_no_proxy(&parsed, &no_proxy) {
log_info!("URL {} is in NO_PROXY list", url);
return Some("DIRECT".to_string());
}
}
let (vars, default_scheme): (&[&str], ProxyScheme) = match parsed.scheme() {
"http" => (&["http_proxy", "HTTP_PROXY"], ProxyScheme::Http),
"https" => (&["https_proxy", "HTTPS_PROXY"], ProxyScheme::Http),
"ftp" => (&["ftp_proxy", "FTP_PROXY"], ProxyScheme::Http),
_ => (&[][..], ProxyScheme::Http),
};
for name in vars.iter().chain(["all_proxy", "ALL_PROXY"].iter()) {
if should_ignore_uppercase_http_proxy(name) {
continue;
}
let Ok(value) = env::var(name) else {
continue;
};
if value.trim().is_empty() {
continue;
}
if let Some(proxy) = normalize_proxy_url(&value, default_scheme) {
log_info!("Proxy {} found in {}", proxy, name);
return Some(proxy);
}
}
None
}
fn should_ignore_uppercase_http_proxy(name: &str) -> bool {
name == "HTTP_PROXY" && env::var("REQUEST_METHOD").is_ok()
}
fn is_url_in_no_proxy(url: &Url, no_proxy: &str) -> bool {
let Some(host) = url.host_str() else {
return false;
};
let host = host.trim_matches('.').to_ascii_lowercase();
let port = url.port_or_known_default();
for raw_entry in no_proxy.split([',', ' ']) {
let entry = raw_entry.trim().trim_matches('.').to_ascii_lowercase();
if entry.is_empty() {
continue;
}
if entry == "*" {
return true;
}
let (entry_host, entry_port) = split_no_proxy_entry(&entry);
if let Some(required_port) = entry_port {
if Some(required_port) != port {
continue;
}
}
if entry_host.starts_with("*.") {
let suffix = &entry_host[2..];
if host == suffix || host.ends_with(&format!(".{}", suffix)) {
return true;
}
continue;
}
if entry_host.starts_with('.') {
let suffix = entry_host.trim_start_matches('.');
if host == suffix || host.ends_with(&format!(".{}", suffix)) {
return true;
}
continue;
}
if host == entry_host || host.ends_with(&format!(".{}", entry_host)) {
return true;
}
}
false
}
fn split_no_proxy_entry(entry: &str) -> (&str, Option<u16>) {
if entry.starts_with('[') {
if let Some((host, port)) = entry.rsplit_once("]:") {
return (host.trim_start_matches('['), port.parse().ok());
}
return (entry.trim_matches(['[', ']']), None);
}
if let Some((host, port)) = entry.rsplit_once(':') {
if !host.contains(':') {
return (host, port.parse().ok());
}
}
(entry, None)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_proxy_matches_suffix_wildcard_and_port() {
let url = Url::parse("https://api.example.com:8443/path").unwrap();
assert!(is_url_in_no_proxy(&url, "*.example.com"));
assert!(is_url_in_no_proxy(&url, "example.com:8443"));
assert!(!is_url_in_no_proxy(&url, "example.com:443"));
}
#[test]
fn no_proxy_star_matches_everything() {
let url = Url::parse("http://example.com").unwrap();
assert!(is_url_in_no_proxy(&url, "*"));
}
}