lean_ctx/proxy_setup/
util.rs1pub(crate) const PROXY_ENV_START: &str = "# >>> lean-ctx proxy env >>>";
4pub(crate) const PROXY_ENV_END: &str = "# <<< lean-ctx proxy env <<<";
5pub(crate) const DEFAULT_PROXY_PORT: u16 = 4444;
6
7pub(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)";
12
13pub(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";
15
16pub(crate) const COMMANDCODE_OMITTED_NOTE: &str =
18 "Command Code omitted (no ~/.commandcode auth — run `cmd login` or set COMMAND_CODE_API_KEY)";
19
20pub(crate) const OPENAI_OMITTED_NOTE: &str = "OPENAI_BASE_URL omitted: Codex is signed in with a ChatGPT subscription, whose token only authenticates against chatgpt.com (set OPENAI_API_KEY to route OpenAI through the proxy)";
27
28pub fn is_local_lean_ctx_url(url: &str) -> bool {
29 url.starts_with("http://127.0.0.1:") || url.starts_with("http://localhost:")
30}
31
32pub fn proxy_timeout() -> std::time::Duration {
34 if let Ok(val) = std::env::var("LEAN_CTX_PROXY_TIMEOUT_MS")
35 && let Ok(ms) = val.parse::<u64>()
36 {
37 return std::time::Duration::from_millis(ms);
38 }
39 if let Some(ms) = crate::core::config::Config::load().proxy_timeout_ms {
40 return std::time::Duration::from_millis(ms);
41 }
42 std::time::Duration::from_millis(200)
43}
44
45pub(crate) fn is_proxy_reachable(port: u16) -> bool {
46 use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
47 let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
48 TcpStream::connect_timeout(&addr, proxy_timeout()).is_ok()
49}
50
51pub fn default_port() -> u16 {
52 if let Ok(val) = std::env::var("LEAN_CTX_PROXY_PORT")
53 && let Ok(port) = val.parse::<u16>()
54 {
55 return port;
56 }
57 let cfg = crate::core::config::Config::load();
58 if let Some(port) = cfg.proxy_port {
59 return port;
60 }
61 uid_based_port()
62}
63
64pub(crate) fn uid_based_port() -> u16 {
68 #[cfg(unix)]
69 {
70 let uid = unsafe { libc::getuid() } as u16;
73 let offset = uid.saturating_sub(1000) % 1000;
74 DEFAULT_PROXY_PORT + offset
75 }
76 #[cfg(not(unix))]
77 {
78 DEFAULT_PROXY_PORT
79 }
80}