pub(crate) const PROXY_ENV_START: &str = "# >>> lean-ctx proxy env >>>";
pub(crate) const PROXY_ENV_END: &str = "# <<< lean-ctx proxy env <<<";
pub(crate) const DEFAULT_PROXY_PORT: u16 = 4444;
pub(crate) const ANTHROPIC_OMITTED_NOTE: &str = "ANTHROPIC_BASE_URL omitted: Claude Pro/Max subscription authenticates against api.anthropic.com directly (set ANTHROPIC_API_KEY to route Claude through the proxy)";
pub(crate) const GROK_OMITTED_NOTE: &str = "Grok proxy env omitted: run `grok login` (subscription) or set XAI_API_KEY to route Grok through lean-ctx";
pub(crate) const COMMANDCODE_OMITTED_NOTE: &str =
"Command Code omitted (no ~/.commandcode auth — run `cmd login` or set COMMAND_CODE_API_KEY)";
pub fn is_local_lean_ctx_url(url: &str) -> bool {
url.starts_with("http://127.0.0.1:") || url.starts_with("http://localhost:")
}
pub fn proxy_timeout() -> std::time::Duration {
if let Ok(val) = std::env::var("LEAN_CTX_PROXY_TIMEOUT_MS")
&& let Ok(ms) = val.parse::<u64>()
{
return std::time::Duration::from_millis(ms);
}
if let Some(ms) = crate::core::config::Config::load().proxy_timeout_ms {
return std::time::Duration::from_millis(ms);
}
std::time::Duration::from_millis(200)
}
pub(crate) fn is_proxy_reachable(port: u16) -> bool {
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
TcpStream::connect_timeout(&addr, proxy_timeout()).is_ok()
}
pub fn default_port() -> u16 {
if let Ok(val) = std::env::var("LEAN_CTX_PROXY_PORT")
&& let Ok(port) = val.parse::<u16>()
{
return port;
}
let cfg = crate::core::config::Config::load();
if let Some(port) = cfg.proxy_port {
return port;
}
uid_based_port()
}
pub(crate) fn uid_based_port() -> u16 {
#[cfg(unix)]
{
let uid = unsafe { libc::getuid() } as u16;
let offset = uid.saturating_sub(1000) % 1000;
DEFAULT_PROXY_PORT + offset
}
#[cfg(not(unix))]
{
DEFAULT_PROXY_PORT
}
}