Skip to content

Commit f82bc8c

Browse files
authored
Merge pull request #33 from spencerwooo/swo/proxy-export-fish
feat: additional proxy export cmds (based on $SHELL)
2 parents 8d7cb09 + fefbd1c commit f82bc8c

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "clashrup"
33
description = "Simple CLI to manage your systemd clash.service and config subscriptions on Linux."
4-
version = "0.2.5"
4+
version = "0.2.6"
55
edition = "2021"
66
readme = "README.md"
77
license = "MIT"

src/main.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use utils::create_clash_service;
3131
use utils::delete_file;
3232
use utils::download_file;
3333
use utils::extract_gzip;
34+
use utils::proxy_export_cmd;
35+
use utils::proxy_unset_cmd;
3436

3537
#[tokio::main]
3638
async fn main() {
@@ -177,15 +179,14 @@ async fn main() {
177179
}
178180
Some(Commands::Proxy { proxy }) => match proxy {
179181
Some(ProxyCommands::Export) => {
180-
let proxy_cmd = format!(
181-
"export https_proxy=http://{hostname}:{http_port} \
182-
http_proxy=http://{hostname}:{http_port} \
183-
all_proxy=socks5://{hostname}:{socks_port}",
184-
hostname = "127.0.0.1",
185-
http_port = config.clash_config.port,
186-
socks_port = config.clash_config.socks_port
187-
);
188-
println!("{} Run ->\n {}", prefix.blue(), &proxy_cmd.bold());
182+
println!(
183+
"{}",
184+
proxy_export_cmd(
185+
"127.0.0.1",
186+
&config.clash_config.port,
187+
&config.clash_config.socks_port
188+
)
189+
)
189190
}
190191
Some(ProxyCommands::ExportLan) => {
191192
if !config.clash_config.allow_lan.unwrap_or(false) {
@@ -197,20 +198,22 @@ async fn main() {
197198
return;
198199
}
199200

200-
let host = local_ip().unwrap();
201-
let proxy_cmd = format!(
202-
"export https_proxy=http://{hostname}:{http_port} \
203-
http_proxy=http://{hostname}:{http_port} \
204-
all_proxy=socks5://{hostname}:{socks_port}",
205-
hostname = host,
206-
http_port = config.clash_config.port,
207-
socks_port = config.clash_config.socks_port
208-
);
209-
println!("{} Run ->\n {}", prefix.blue(), &proxy_cmd.bold());
201+
let hostname = local_ip();
202+
if let Ok(hostname) = hostname {
203+
println!(
204+
"{}",
205+
proxy_export_cmd(
206+
&hostname.to_string(),
207+
&config.clash_config.port,
208+
&config.clash_config.socks_port
209+
)
210+
)
211+
} else {
212+
println!("{} Failed to get local IP address", prefix.red());
213+
}
210214
}
211215
Some(ProxyCommands::Unset) => {
212-
let proxy_cmd = "unset https_proxy http_proxy all_proxy";
213-
println!("{} Run ->\n {}", prefix.blue(), &proxy_cmd.bold());
216+
println!("{}", proxy_unset_cmd())
214217
}
215218
_ => {
216219
println!("{} No proxy command, --help for ussage", prefix.red());

src/utils.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io;
55
use std::io::Write;
66
use std::path::Path;
77

8+
use clap_complete::shells::Shell;
89
use colored::Colorize;
910
use flate2::read::GzDecoder;
1011
use futures_util::StreamExt;
@@ -153,3 +154,44 @@ WantedBy=default.target",
153154
clash_service_path.underline().yellow()
154155
);
155156
}
157+
158+
pub fn proxy_export_cmd(hostname: &str, http_port: &u16, socks_port: &u16) -> String {
159+
// Check current shell
160+
let shell = Shell::from_env().unwrap_or(Shell::Bash);
161+
match shell {
162+
Shell::Fish => {
163+
// For fish, use `set -gx $ENV_VAR value` to set environment variables
164+
return format!(
165+
"set -gx https_proxy http://{hostname}:{http_port} \
166+
set -gx http_proxy http://{hostname}:{http_port} \
167+
set -gx all_proxy socks5://{hostname}:{socks_port}",
168+
hostname = hostname,
169+
http_port = http_port,
170+
socks_port = socks_port
171+
);
172+
}
173+
_ => {
174+
// For all other shells (bash/zsh), use `export $ENV_VAR=value`
175+
return format!(
176+
"export https_proxy=http://{hostname}:{http_port} \
177+
http_proxy=http://{hostname}:{http_port} \
178+
all_proxy=socks5://{hostname}:{socks_port}"
179+
);
180+
}
181+
}
182+
}
183+
184+
pub fn proxy_unset_cmd() -> String {
185+
// Check current shell
186+
let shell = Shell::from_env().unwrap_or(Shell::Bash);
187+
match shell {
188+
Shell::Fish => {
189+
// For fish, use `set -e $ENV_VAR` to unset environment variables
190+
return "set -e https_proxy http_proxy all_proxy".to_owned();
191+
}
192+
_ => {
193+
// For all other shells (bash/zsh), use `unset $ENV_VAR`
194+
return "unset https_proxy http_proxy all_proxy".to_owned();
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)