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 fn is_local_lean_ctx_url(url: &str) -> bool {
21 url.starts_with("http://127.0.0.1:") || url.starts_with("http://localhost:")
22}
23
24pub fn proxy_timeout() -> std::time::Duration {
26 if let Ok(val) = std::env::var("LEAN_CTX_PROXY_TIMEOUT_MS")
27 && let Ok(ms) = val.parse::<u64>()
28 {
29 return std::time::Duration::from_millis(ms);
30 }
31 if let Some(ms) = crate::core::config::Config::load().proxy_timeout_ms {
32 return std::time::Duration::from_millis(ms);
33 }
34 std::time::Duration::from_millis(200)
35}
36
37pub(crate) fn is_proxy_reachable(port: u16) -> bool {
38 use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
39 let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
40 TcpStream::connect_timeout(&addr, proxy_timeout()).is_ok()
41}
42
43pub fn default_port() -> u16 {
44 if let Ok(val) = std::env::var("LEAN_CTX_PROXY_PORT")
45 && let Ok(port) = val.parse::<u16>()
46 {
47 return port;
48 }
49 let cfg = crate::core::config::Config::load();
50 if let Some(port) = cfg.proxy_port {
51 return port;
52 }
53 uid_based_port()
54}
55
56pub(crate) fn uid_based_port() -> u16 {
60 #[cfg(unix)]
61 {
62 let uid = unsafe { libc::getuid() } as u16;
65 let offset = uid.saturating_sub(1000) % 1000;
66 DEFAULT_PROXY_PORT + offset
67 }
68 #[cfg(not(unix))]
69 {
70 DEFAULT_PROXY_PORT
71 }
72}