Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(terminal): pass the global proxy to clients launched in a terminal

Jason7c3d3f81833a
diff --git a/src-tauri/src/commands/terminal_launch.rs b/src-tauri/src/commands/terminal_launch.rs
index 38a32154a..3017228f9 100644
--- a/src-tauri/src/commands/terminal_launch.rs
+++ b/src-tauri/src/commands/terminal_launch.rs
@@ -238,6 +238,12 @@ pub async fn open_provider_terminal(
claude_terminal_launch(&env_vars, &providerId, &provider.name, &Default::default())?
}
};
+ add_global_proxy_env(
+ &mut launch,
+ &app_type,
+ card_env_keys(provider),
+ crate::proxy::http_client::get_current_proxy_url().as_deref(),
+ );
if skipPermissions == Some(true) {
launch.skip_permissions();
}
@@ -703,6 +709,58 @@ fn agy_terminal_launch(provider: &crate::provider::Provider) -> Result<ProviderL
})
}
+/// 把设置里的全局代理交给终端里启动的这一个客户端(#10119)。
+///
+/// 全局代理只作用于 CC Switch 进程内的请求;终端里的 CLI 只认 `HTTPS_PROXY` 这类变量、
+/// 不读系统代理,用户 shell 里没 export 时会直连出去。变量只给这一个进程,盖过用户 rc
+/// 里的同名值。
+/// - 卡片自己配了代理(任一代理变量,不分大小写;Claude 的在临时 settings 的 `env` 里)
+/// 就整组不加,免得大小写两套变量指向不同代理;卡片设了 NO_PROXY 也以卡片为准。
+/// - agy(Go)也传 SOCKS5,不补 NO_PROXY:Go 本来就不代理 localhost 和回环地址。
+/// - 其余客户端只传 http(s):Node 写的 CLI 不支持 SOCKS,传了反而整个连不上;
+/// NO_PROXY 设成本机地址,连本地路由的请求不绕进代理。
+fn add_global_proxy_env<'a>(
+ launch: &mut ProviderLaunch,
+ app_type: &AppType,
+ card_env: impl IntoIterator<Item = &'a str>,
+ proxy_url: Option<&str>,
+) {
+ let Some(url) = proxy_url else {
+ return;
+ };
+ let card_keys: Vec<&str> = launch
+ .env
+ .iter()
+ .map(|(name, _)| *name)
+ .chain(card_env)
+ .collect();
+ let card_has = |key: &str| card_keys.iter().any(|name| name.eq_ignore_ascii_case(key));
+ if ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"]
+ .iter()
+ .any(|key| card_has(key))
+ {
+ return;
+ }
+ let go_client = *app_type == AppType::Agy;
+ let vars =
+ crate::services::claude_account::login::global_proxy_env(url, go_client, |_| go_client);
+ for (key, value) in vars {
+ if !card_has(key) {
+ launch.env.push((key, Some(value)));
+ }
+ }
+}
+
+/// 卡片配置里 `env` 的变量名(Claude 等应用的环境变量写在这里,不在 `ProviderLaunch.env`)。
+fn card_env_keys(provider: &crate::provider::Provider) -> impl Iterator<Item = &str> {
+ provider
+ .settings_config
+ .get("env")
+ .and_then(|env| env.as_object())
+ .into_iter()
+ .flat_map(|env| env.keys().map(String::as_str))
+}
+
/// 恢复 Antigravity 第三方 Key 模式的会话:和「打开终端」同样带私有目录和 Key,再接上
/// `--conversation`。卡上已经没有 Key 时私有目录对不上,直接报错,不退回官方登录。
pub(crate) fn open_agy_resume_terminal(
@@ -711,6 +769,12 @@ pub(crate) fn open_agy_resume_terminal(
cwd: Option<String>,
) -> Result<(), String> {
let mut launch = agy_terminal_launch(provider)?;
+ add_global_proxy_env(
+ &mut launch,
+ &AppType::Agy,
+ card_env_keys(provider),
+ crate::proxy::http_client::get_current_proxy_url().as_deref(),
+ );
// 只有 Key 卡有私有目录(启动前打印的那行路径);官方卡的参数可能只有 `--model`。
if launch.detail.is_none() {
return Err(format!(
@@ -2688,6 +2752,93 @@ mod tests {
);
}
+ #[test]
+ fn global_proxy_reaches_terminal_clients() {
+ let proxy_keys = ["HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"];
+ let launch = |env: Vec<(&'static str, Option<String>)>| ProviderLaunch {
+ title: "",
+ detail: None,
+ program: "claude",
+ args: Vec::new(),
+ batch_command: String::new(),
+ env,
+ batch_vars: Vec::new(),
+ cleanup: Vec::new(),
+ };
+ let with_proxy = |app: AppType, url: Option<&str>| {
+ let mut launch = launch(Vec::new());
+ add_global_proxy_env(&mut launch, &app, std::iter::empty(), url);
+ launch.env
+ };
+
+ // agy (Go): SOCKS5 too, socks5h becomes socks5, NO_PROXY left to the user (#10119)
+ assert_eq!(
+ with_proxy(AppType::Agy, Some("socks5h://127.0.0.1:1080")),
+ proxy_keys
+ .map(|key| (key, Some("socks5://127.0.0.1:1080".to_string())))
+ .to_vec()
+ );
+
+ // Other clients: http(s) only, and the local router stays off the proxy
+ let mut expected: Vec<_> = proxy_keys
+ .map(|key| (key, Some("http://127.0.0.1:7890".to_string())))
+ .to_vec();
+ expected.extend(
+ ["NO_PROXY", "no_proxy"].map(|key| (key, Some("localhost,127.0.0.1,::1".to_string()))),
+ );
+ for app in [
+ AppType::Claude,
+ AppType::Codex,
+ AppType::Gemini,
+ AppType::Copilot,
+ ] {
+ assert_eq!(with_proxy(app, Some("http://127.0.0.1:7890")), expected);
+ }
+ assert!(with_proxy(AppType::Claude, Some("socks5://127.0.0.1:1080")).is_empty());
+
+ // No proxy configured, or an unusable one: nothing added
+ assert!(with_proxy(AppType::Agy, None).is_empty());
+ assert!(with_proxy(AppType::Codex, Some("not a url")).is_empty());
+
+ // A card that configures any proxy keeps it whole: no mixed-case pair pointing elsewhere
+ let mut card = launch(vec![("HTTPS_PROXY", Some("http://card:1".to_string()))]);
+ add_global_proxy_env(
+ &mut card,
+ &AppType::Codex,
+ std::iter::empty(),
+ Some("http://127.0.0.1:7890"),
+ );
+ assert_eq!(
+ card.env,
+ vec![("HTTPS_PROXY", Some("http://card:1".to_string()))]
+ );
+
+ // Claude keeps its card env in the temporary settings file, not in `launch.env`
+ let mut claude = launch(Vec::new());
+ add_global_proxy_env(
+ &mut claude,
+ &AppType::Claude,
+ ["ANTHROPIC_BASE_URL", "https_proxy"],
+ Some("http://127.0.0.1:7890"),
+ );
+ assert!(claude.env.is_empty());
+
+ // A card NO_PROXY (either case) wins; the proxy itself still applies
+ let mut no_proxy = launch(Vec::new());
+ add_global_proxy_env(
+ &mut no_proxy,
+ &AppType::Claude,
+ ["no_proxy"],
+ Some("http://127.0.0.1:7890"),
+ );
+ assert_eq!(
+ no_proxy.env,
+ proxy_keys
+ .map(|key| (key, Some("http://127.0.0.1:7890".to_string())))
+ .to_vec()
+ );
+ }
+
#[test]
fn copilot_byok_and_login_terminal_launches() {
// BYOK card: clears COPILOT_PROVIDER_* / COPILOT_MODEL, ignores bound account, passes reasoning effort