From 95b38771f8542e0e4bb04befeb632105352c2e9f Mon Sep 17 00:00:00 2001 From: libm Date: Sat, 12 Jul 2025 17:27:17 +0800 Subject: [PATCH] fix: optimize --- README.md | 33 ++++++++++++--------------------- install.sh | 3 ++- script/clashctl.sh | 12 ++++-------- script/common.sh | 2 +- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 26e0725..6ab0882 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ ### 环境要求 - 用户权限:`root`、`sudo`。(无权限可参考:[#91](https://github.com/nelvko/clash-for-linux-install/issues/91)) -- `shell` 支持:`bash`、`zsh`。 +- `shell` 支持:`bash`、`zsh`、`fish`。 ### 一键安装 @@ -35,23 +35,21 @@ git clone --branch master --depth 1 https://gh-proxy.com/https://github.com/nelv ### 命令一览 -执行 `clash` 列出开箱即用的快捷命令。 +执行 `clashctl` 列出开箱即用的快捷命令。 -> 兼容多种命令风格 +> 同 `clash`、`mihomo`、`mihomoctl` ```bash -$ clash +$ clashctl Usage: clash COMMAND [OPTION] - mihomo COMMAND [OPTION] - clashctl COMMAND [OPTION] - mihomoctl COMMAND [OPTION] - + Commands: on 开启代理 off 关闭代理 ui 面板地址 status 内核状况 + proxy [on|off] 系统代理 tun [on|off] Tun 模式 mixin [-e|-r] Mixin 配置 secret [SECRET] Web 密钥 @@ -61,21 +59,14 @@ Commands: ### 优雅启停 ```bash -$ clashoff -😼 已关闭代理环境 - $ clashon 😼 已开启代理环境 + +$ clashoff +😼 已关闭代理环境 ``` - -
- -原理 - -- 使用 `systemctl` 控制 `clash` 启停,并调整代理环境变量的值(http_proxy 等)。应用程序在发起网络请求时,会通过其指定的代理地址转发流量,不调整会造成:关闭代理但未卸载代理变量导致仍转发请求、开启代理后未设置代理地址导致请求不转发。 -- `clashon` 等命令封装了上述流程。 - -
+- 启停代理内核的同时,设置系统代理。 +- 亦可通过 `clashproxy` 单独控制系统代理。 ### Web 控制台 @@ -148,7 +139,7 @@ $ clashmixin -r 😼 less 查看 运行时 配置 ``` -- 将自定义配置写在 `Mixin` 而不是原配置中,可避免更新订阅后丢失自定义配置。 +- 持久化:将自定义配置写在 `Mixin` 而不是原配置中,可避免更新订阅后丢失自定义配置。 - 运行时配置是订阅配置和 `Mixin` 配置的并集。 - 相同配置项优先级:`Mixin` 配置 > 订阅配置。 diff --git a/install.sh b/install.sh index e02febe..be0ef38 100755 --- a/install.sh +++ b/install.sh @@ -53,4 +53,5 @@ systemctl enable "$BIN_KERNEL_NAME" >&/dev/null || _failcat '💥' "设置自启 clashui _okcat '🎉' 'enjoy 🎉' clash -_quit +# shellcheck disable=SC2016 +_quit '[ -z $http_proxy ] && _okcat "请执行 clashon 开启代理环境"' diff --git a/script/clashctl.sh b/script/clashctl.sh index aabf77b..c9c8432 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -49,15 +49,15 @@ function clashon() { } watch_proxy() { + # 新开交互式shell,且无代理变量时 [ -z "$http_proxy" ] && [[ $- == *i* ]] && { - clashproxy status >&/dev/null && { - _is_root && clashon - } + # 为root时,自动开启代理环境(普通用户会触发sudo验证密码导致卡住) + _is_root && clashon } } function clashoff() { - sudo systemctl stop "$BIN_KERNEL_NAME" && _okcat '已关闭代理程序' || + sudo systemctl stop "$BIN_KERNEL_NAME" && _okcat '已关闭代理环境' || _failcat '关闭失败: 执行 "clashstatus" 查看日志' || return 1 _unset_system_proxy } @@ -315,10 +315,6 @@ Commands: secret [SECRET] Web 密钥 update [auto|log] 更新订阅 -说明: - - clashon: 启动代理程序,并开启系统代理 - - clashproxy: 仅控制系统代理,不影响代理程序 - EOF ;; esac diff --git a/script/common.sh b/script/common.sh index ac10def..b0b9ec4 100644 --- a/script/common.sh +++ b/script/common.sh @@ -193,7 +193,7 @@ function _failcat() { function _quit() { local user=root [ -n "$SUDO_USER" ] && user=$SUDO_USER - exec sudo -u "$user" -- "$_SHELL" -i + exec sudo -u "$user" -- "$_SHELL" -i -c "$1" } function _error_quit() {