use crate::constants::env::system;
use std::sync::{Mutex, OnceLock};
static PROXY_CACHE: OnceLock<Mutex<ProxyConfig>> = OnceLock::new();
#[derive(Debug, Clone, Default)]
pub struct ProxyConfig {
pub http_proxy: Option<String>,
pub https_proxy: Option<String>,
pub no_proxy: Option<String>,
}
pub fn get_proxy_config() -> ProxyConfig {
PROXY_CACHE
.get_or_init(|| Mutex::new(load_proxy_config()))
.lock()
.unwrap()
.clone()
}
fn load_proxy_config() -> ProxyConfig {
let http_proxy = std::env::var(system::HTTP_PROXY)
.or_else(|_| std::env::var(system::HTTP_PROXY_LOWER))
.ok();
let https_proxy = std::env::var(system::HTTPS_PROXY)
.or_else(|_| std::env::var(system::HTTPS_PROXY_LOWER))
.ok();
let no_proxy = std::env::var(system::NO_PROXY)
.or_else(|_| std::env::var(system::NO_PROXY_LOWER))
.ok();
ProxyConfig {
http_proxy,
https_proxy,
no_proxy,
}
}
pub fn clear_proxy_cache() {
}
pub fn get_http_proxy() -> Option<String> {
get_proxy_config().http_proxy.clone()
}
pub fn get_https_proxy() -> Option<String> {
get_proxy_config().https_proxy.clone()
}
pub fn get_no_proxy() -> Option<String> {
get_proxy_config().no_proxy.clone()
}
pub fn should_bypass_proxy(host: &str) -> bool {
let no_proxy = match get_no_proxy() {
Some(np) => np,
None => return false,
};
for pattern in no_proxy.split(',') {
let pattern = pattern.trim();
if pattern.is_empty() {
continue;
}
if pattern.starts_with('.') {
let domain = &pattern[1..];
if host.ends_with(domain) || host == domain {
return true;
}
} else if host == pattern {
return true;
}
}
false
}
pub fn configure_global_agents() {
clear_proxy_cache();
}