proxyparser 0.2.0

A cross-platform system proxy detector with strong support for complex enterprise PAC scripts
Documentation
// src/lib.rs
pub mod env_proxy;
pub mod log;
pub mod pac;
pub mod proxy;
pub mod system_proxy;
use std::error::Error;

/// Detects the appropriate proxy for the given URL, following standard priority order:
///
/// 1. Environment variables (HTTP_PROXY, etc.)
/// 2. System proxy settings (with full PAC script support on supported platforms)
/// 3. DIRECT (no proxy)
///
/// Supports macOS, Windows, and Linux system settings, including PAC files.
///
/// # Examples
///
/// ```
/// use proxyparser::find_proxy_for_url;
///
/// let proxy = find_proxy_for_url("https://httpbin.org/ip")?;
/// println!("{}", proxy); // DIRECT or normalized proxy URL(s)
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn find_proxy_for_url(url: &str) -> Result<String, Box<dyn Error>> {
    // 1. 环境变量最高优先级
    if let Some(proxy) = env_proxy::get_env_proxy(url) {
        log_info!("Using environment proxy: {}", proxy);
        return Ok(proxy);
    }

    // 2. 系统代理(macOS / Windows / Linux)
    if let Some(system_result) = system_proxy::get_system_proxy_config(url) {
        match system_result {
            system_proxy::SystemProxyConfig::PacUrl(pac_url) => {
                log_info!("Detected PAC configuration: {}", pac_url);
                match pac::evaluate_pac_for_url(&pac_url, url) {
                    Ok(proxy) => {
                        log_info!("PAC resolved proxy: {}", proxy);
                        return Ok(proxy);
                    }
                    Err(e) => {
                        log_warn!("PAC evaluation failed: {}. Falling back to DIRECT", e);
                    }
                }
            }
            system_proxy::SystemProxyConfig::Manual(proxy) => {
                log_info!("Using system manual proxy: {}", proxy);
                let parsed =
                    proxy::parse_proxy_result(&proxy, &proxy::ProxyParseOptions::default());
                return Ok(parsed.to_proxy_string());
            }
        }
    }

    log_info!("No proxy found, using DIRECT");
    Ok("DIRECT".to_string())
}