Skip to content

Commit

Permalink
Merge pull request #33 from spencerwooo/swo/proxy-export-fish
Browse files Browse the repository at this point in the history
feat: additional proxy export cmds (based on $SHELL)
  • Loading branch information
spencerwooo authored Jan 27, 2023
2 parents 8d7cb09 + fefbd1c commit f82bc8c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
45 changes: 24 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
42 changes: 42 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit f82bc8c

Please sign in to comment.