From 49d295937676d8e5fb0d56335f93563816b34173 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Fri, 27 Jan 2023 13:53:09 +0800 Subject: [PATCH 1/2] feat: additional proxy export cmds (based on $SHELL) --- src/main.rs | 45 ++++++++++++++++++++++++--------------------- src/utils.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index e047b2a..cb310ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,8 @@ use utils::create_clash_service; use utils::delete_file; use utils::download_file; use utils::extract_gzip; +use utils::proxy_export_cmd; +use utils::proxy_unset_cmd; #[tokio::main] async fn main() { @@ -177,15 +179,14 @@ async fn main() { } Some(Commands::Proxy { proxy }) => match proxy { Some(ProxyCommands::Export) => { - let proxy_cmd = format!( - "export https_proxy=http://{hostname}:{http_port} \ - http_proxy=http://{hostname}:{http_port} \ - all_proxy=socks5://{hostname}:{socks_port}", - hostname = "127.0.0.1", - http_port = config.clash_config.port, - socks_port = config.clash_config.socks_port - ); - println!("{} Run ->\n {}", prefix.blue(), &proxy_cmd.bold()); + println!( + "{}", + proxy_export_cmd( + "127.0.0.1", + &config.clash_config.port, + &config.clash_config.socks_port + ) + ) } Some(ProxyCommands::ExportLan) => { if !config.clash_config.allow_lan.unwrap_or(false) { @@ -197,20 +198,22 @@ async fn main() { return; } - let host = local_ip().unwrap(); - let proxy_cmd = format!( - "export https_proxy=http://{hostname}:{http_port} \ - http_proxy=http://{hostname}:{http_port} \ - all_proxy=socks5://{hostname}:{socks_port}", - hostname = host, - http_port = config.clash_config.port, - socks_port = config.clash_config.socks_port - ); - println!("{} Run ->\n {}", prefix.blue(), &proxy_cmd.bold()); + let hostname = local_ip(); + if let Ok(hostname) = hostname { + println!( + "{}", + proxy_export_cmd( + &hostname.to_string(), + &config.clash_config.port, + &config.clash_config.socks_port + ) + ) + } else { + println!("{} Failed to get local IP address", prefix.red()); + } } Some(ProxyCommands::Unset) => { - let proxy_cmd = "unset https_proxy http_proxy all_proxy"; - println!("{} Run ->\n {}", prefix.blue(), &proxy_cmd.bold()); + println!("{}", proxy_unset_cmd()) } _ => { println!("{} No proxy command, --help for ussage", prefix.red()); diff --git a/src/utils.rs b/src/utils.rs index b2158aa..c61e633 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,6 +5,7 @@ use std::io; use std::io::Write; use std::path::Path; +use clap_complete::shells::Shell; use colored::Colorize; use flate2::read::GzDecoder; use futures_util::StreamExt; @@ -153,3 +154,44 @@ WantedBy=default.target", clash_service_path.underline().yellow() ); } + +pub fn proxy_export_cmd(hostname: &str, http_port: &u16, socks_port: &u16) -> String { + // Check current shell + let shell = Shell::from_env().unwrap_or(Shell::Bash); + match shell { + Shell::Fish => { + // For fish, use `set -gx $ENV_VAR value` to set environment variables + return format!( + "set -gx https_proxy http://{hostname}:{http_port} \ + set -gx http_proxy http://{hostname}:{http_port} \ + set -gx all_proxy socks5://{hostname}:{socks_port}", + hostname = hostname, + http_port = http_port, + socks_port = socks_port + ); + } + _ => { + // For all other shells (bash/zsh), use `export $ENV_VAR=value` + return format!( + "export https_proxy=http://{hostname}:{http_port} \ + http_proxy=http://{hostname}:{http_port} \ + all_proxy=socks5://{hostname}:{socks_port}" + ); + } + } +} + +pub fn proxy_unset_cmd() -> String { + // Check current shell + let shell = Shell::from_env().unwrap_or(Shell::Bash); + match shell { + Shell::Fish => { + // For fish, use `set -e $ENV_VAR` to unset environment variables + return "set -e https_proxy http_proxy all_proxy".to_owned(); + } + _ => { + // For all other shells (bash/zsh), use `unset $ENV_VAR` + return "unset https_proxy http_proxy all_proxy".to_owned(); + } + } +} From fefbd1c0da4d586e96a58ac9d7e635511526bc87 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Fri, 27 Jan 2023 13:58:49 +0800 Subject: [PATCH 2/2] build: bump clashrup version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2d9058b..b4ca9b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "clashrup" description = "Simple CLI to manage your systemd clash.service and config subscriptions on Linux." -version = "0.2.5" +version = "0.2.6" edition = "2021" readme = "README.md" license = "MIT"