Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

2.7 KB
Raw Blame
use std::collections::BTreeMap;
use std::ffi::OsString;

use sha2::{Digest, Sha256};

/// Captured once per runtime. Debug never exposes credentials in proxy URLs.
#[derive(Clone)]
pub(crate) struct ProxyEnvironment(BTreeMap<OsString, OsString>);

impl ProxyEnvironment {
    pub(crate) fn capture(values: impl IntoIterator<Item = (OsString, OsString)>) -> Self {
        Self(
            values
                .into_iter()
                .filter(|(key, _)| {
                    matches!(
                        key.to_str(),
                        Some(
                            "HTTP_PROXY"
                                | "HTTPS_PROXY"
                                | "NO_PROXY"
                                | "http_proxy"
                                | "https_proxy"
                                | "no_proxy"
                        )
                    )
                })
                .collect(),
        )
    }

    pub(crate) fn iter(&self) -> impl Iterator<Item = (&OsString, &OsString)> {
        self.0.iter()
    }

    pub(crate) fn configure_http_client(
        &self,
        mut builder: reqwest::blocking::ClientBuilder,
    ) -> Result<reqwest::blocking::ClientBuilder, crate::CpaLifecycleError> {
        // Match the HTTPS route used by Go's CPA transport, including explicit no-proxy.
        // Never mutate the daemon's process environment.
        let get = |upper: &str, lower: &str| {
            self.0
                .get(std::ffi::OsStr::new(upper))
                .filter(|v| !v.is_empty())
                .or_else(|| {
                    self.0
                        .get(std::ffi::OsStr::new(lower))
                        .filter(|v| !v.is_empty())
                })
                .and_then(|v| v.to_str())
        };
        builder = builder.no_proxy();
        if let Some(url) = get("HTTPS_PROXY", "https_proxy") {
            let mut proxy = reqwest::Proxy::https(url)
                .map_err(|_| crate::CpaLifecycleError::BorrowedClaudeAuthUnavailable)?;
            proxy =
                proxy.no_proxy(get("NO_PROXY", "no_proxy").and_then(reqwest::NoProxy::from_string));
            builder = builder.proxy(proxy);
        }
        Ok(builder)
    }

    pub(crate) fn digest(&self) -> String {
        let mut digest = Sha256::new();
        for (key, value) in &self.0 {
            for bytes in [key.as_encoded_bytes(), value.as_encoded_bytes()] {
                digest.update((bytes.len() as u64).to_le_bytes());
                digest.update(bytes);
            }
        }
        format!("{:x}", digest.finalize())
    }
}

impl std::fmt::Debug for ProxyEnvironment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("ProxyEnvironment([REDACTED])")
    }
}