From f523881dc62e4ee435149709c9961fa00479666b Mon Sep 17 00:00:00 2001 From: gejiaxiang <353358601@qq.com> Date: Wed, 25 Jun 2025 09:16:05 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=86=E7=A6=BB=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=92=8C=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E8=A3=85=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/mixin.yaml | 4 ++ script/clashctl.sh | 145 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 120 insertions(+), 29 deletions(-) diff --git a/resources/mixin.yaml b/resources/mixin.yaml index 4c41941..8cd5655 100644 --- a/resources/mixin.yaml +++ b/resources/mixin.yaml @@ -28,6 +28,10 @@ tun: # - docker0 # - podman0 +# 环境变量代理配置 +proxy-env: + enable: false + # DNS 配置 dns: enable: true diff --git a/script/clashctl.sh b/script/clashctl.sh index fb2ab52..4080375 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -1,15 +1,9 @@ # shellcheck disable=SC2148 # shellcheck disable=SC2155 -function clashon() { +# 设置代理环境变量 +_set_proxy_env() { _get_proxy_port - systemctl is-active "$BIN_KERNEL_NAME" >&/dev/null || { - sudo systemctl start "$BIN_KERNEL_NAME" >/dev/null || { - _failcat '启动失败: 执行 clashstatus 查看日志' - return 1 - } - } - local auth=$(sudo "$BIN_YQ" '.authentication[0] // ""' "$CLASH_CONFIG_RUNTIME") [ -n "$auth" ] && auth=$auth@ @@ -28,19 +22,12 @@ function clashon() { export no_proxy=$no_proxy_addr export NO_PROXY=$no_proxy - _okcat '已开启代理环境' + # 持久化:记录环境变量代理状态到YAML配置 + sudo "$BIN_YQ" -i '.proxy-env.enable = true' "$CLASH_CONFIG_MIXIN" } -watch_proxy() { - [ -z "$http_proxy" ] && [[ $- == *i* ]] && { - _is_root || _failcat '未检测到代理变量,可执行 clashon 开启代理环境' && clashon - } -} - -function clashoff() { - sudo systemctl stop "$BIN_KERNEL_NAME" && _okcat '已关闭代理环境' || - _failcat '关闭失败: 执行 "clashstatus" 查看日志' || return 1 - +# 卸载代理环境变量 +_unset_proxy_env() { unset http_proxy unset https_proxy unset HTTP_PROXY @@ -49,12 +36,100 @@ function clashoff() { unset ALL_PROXY unset no_proxy unset NO_PROXY + + # 持久化:清除环境变量代理状态 + sudo "$BIN_YQ" -i '.proxy-env.enable = false' "$CLASH_CONFIG_MIXIN" +} + +function clashon() { + systemctl is-active "$BIN_KERNEL_NAME" >&/dev/null || { + sudo systemctl start "$BIN_KERNEL_NAME" >/dev/null || { + _failcat '启动失败: 执行 clashstatus 查看日志' + return 1 + } + } + + # 检查TUN模式状态,如果开启则不设置环境变量 + local tun_status=$(sudo "$BIN_YQ" '.tun.enable' "$CLASH_CONFIG_MIXIN") + if [ "$tun_status" = 'true' ]; then + _okcat '代理程序已启动,TUN模式已开启(环境变量代理已禁用)' + else + _set_proxy_env + _okcat '已开启代理环境' + fi +} + + + +watch_proxy() { + [ -z "$http_proxy" ] && [[ $- == *i* ]] && { + local proxy_env_status=$(sudo "$BIN_YQ" '.proxy-env.enable' "$CLASH_CONFIG_MIXIN" 2>/dev/null) + if [ "$proxy_env_status" = 'true' ]; then + _set_proxy_env >/dev/null 2>&1 + fi + } +} + +function clashoff() { + sudo systemctl stop "$BIN_KERNEL_NAME" && _okcat '已关闭代理程序' || + _failcat '关闭失败: 执行 "clashstatus" 查看日志' || return 1 + + # 同时清除环境变量 + _unset_proxy_env } clashrestart() { { clashoff && clashon; } >&/dev/null } +# 独立的环境变量代理控制 +function clashproxy() { + case "$1" in + on) + # 检查代理程序是否运行 + systemctl is-active "$BIN_KERNEL_NAME" >&/dev/null || { + _failcat '代理程序未运行,请先执行 clashon' + return 1 + } + + # 检查TUN模式状态 + local tun_status=$(sudo "$BIN_YQ" '.tun.enable' "$CLASH_CONFIG_MIXIN") + if [ "$tun_status" = 'true' ]; then + _failcat 'TUN模式已开启,无法同时使用环境变量代理' + return 1 + fi + + _set_proxy_env # 自动持久化状态 + _okcat '已开启环境变量代理(新shell会话将自动应用)' + ;; + off) + _unset_proxy_env # 自动持久化状态 + _okcat '已关闭环境变量代理' + ;; + status) + local proxy_env_status=$(sudo "$BIN_YQ" '.proxy-env.enable' "$CLASH_CONFIG_MIXIN" 2>/dev/null) + if [ -n "$http_proxy" ]; then + _okcat "环境变量代理:已开启 ($http_proxy)" + else + if [ "$proxy_env_status" = 'true' ]; then + echo "环境变量代理:已配置开启,但当前shell未生效" + echo "💡 执行 'clash proxy on' 在当前shell中生效" + else + _failcat "环境变量代理:已关闭" + fi + fi + ;; + *) + cat <&/dev/null _error_quit '不支持的内核版本' } - _okcat "Tun 模式已开启" + + # 开启TUN模式时卸载环境变量,避免冲突 + _unset_proxy_env + _okcat "Tun 模式已开启,已自动卸载环境变量代理" } function clashtun() { @@ -232,7 +310,10 @@ function clashctl() { shift clashstatus "$@" ;; - + proxy) + shift + clashproxy "$@" + ;; tun) shift clashtun "$@" @@ -259,14 +340,20 @@ Usage: mihomoctl COMMAND [OPTION】 Commands: - on 开启代理 - off 关闭代理 - ui 面板地址 - status 内核状况 - tun [on|off] Tun 模式 - mixin [-e|-r] Mixin 配置 - secret [SECRET] Web 密钥 - update [auto|log] 更新订阅 + on 开启代理程序 + off 关闭代理程序 + proxy [on|off|status] 环境变量代理控制 + ui 面板地址 + status 内核状况 + tun [on|off] Tun 模式 + mixin [-e|-r] Mixin 配置 + secret [SECRET] Web 密钥 + update [auto|log] 更新订阅 + +说明: + - on/off: 控制代理程序启停 + - proxy: 独立控制环境变量代理(系统代理) + - tun: TUN模式与环境变量代理互斥 EOF ;; From 4fb5fea1ebfaca0a7b0ae05f0ac45d29963db1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AD=E5=BE=8B=E3=81=93?= Date: Tue, 1 Jul 2025 18:25:55 +0800 Subject: [PATCH 2/3] Update clashctl.sh --- script/clashctl.sh | 99 ++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 66 deletions(-) diff --git a/script/clashctl.sh b/script/clashctl.sh index 4080375..74bbd01 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -1,9 +1,7 @@ # shellcheck disable=SC2148 # shellcheck disable=SC2155 -# 设置代理环境变量 -_set_proxy_env() { - _get_proxy_port +_set_system_proxy() { local auth=$(sudo "$BIN_YQ" '.authentication[0] // ""' "$CLASH_CONFIG_RUNTIME") [ -n "$auth" ] && auth=$auth@ @@ -22,12 +20,10 @@ _set_proxy_env() { export no_proxy=$no_proxy_addr export NO_PROXY=$no_proxy - # 持久化:记录环境变量代理状态到YAML配置 - sudo "$BIN_YQ" -i '.proxy-env.enable = true' "$CLASH_CONFIG_MIXIN" + sudo "$BIN_YQ" -i '.system-proxy.enable = true' "$CLASH_CONFIG_MIXIN" } -# 卸载代理环境变量 -_unset_proxy_env() { +_unset_system_proxy() { unset http_proxy unset https_proxy unset HTTP_PROXY @@ -37,94 +33,69 @@ _unset_proxy_env() { unset no_proxy unset NO_PROXY - # 持久化:清除环境变量代理状态 - sudo "$BIN_YQ" -i '.proxy-env.enable = false' "$CLASH_CONFIG_MIXIN" + sudo "$BIN_YQ" -i '.system-proxy.enable = false' "$CLASH_CONFIG_MIXIN" } function clashon() { + _get_proxy_port systemctl is-active "$BIN_KERNEL_NAME" >&/dev/null || { sudo systemctl start "$BIN_KERNEL_NAME" >/dev/null || { _failcat '启动失败: 执行 clashstatus 查看日志' return 1 } } - - # 检查TUN模式状态,如果开启则不设置环境变量 - local tun_status=$(sudo "$BIN_YQ" '.tun.enable' "$CLASH_CONFIG_MIXIN") - if [ "$tun_status" = 'true' ]; then - _okcat '代理程序已启动,TUN模式已开启(环境变量代理已禁用)' - else - _set_proxy_env - _okcat '已开启代理环境' - fi + _set_system_proxy + _okcat '已开启代理环境' } - - watch_proxy() { [ -z "$http_proxy" ] && [[ $- == *i* ]] && { - local proxy_env_status=$(sudo "$BIN_YQ" '.proxy-env.enable' "$CLASH_CONFIG_MIXIN" 2>/dev/null) - if [ "$proxy_env_status" = 'true' ]; then - _set_proxy_env >/dev/null 2>&1 - fi + clashproxy status >&/dev/null && { + _is_root && clashon + } } } function clashoff() { sudo systemctl stop "$BIN_KERNEL_NAME" && _okcat '已关闭代理程序' || _failcat '关闭失败: 执行 "clashstatus" 查看日志' || return 1 - - # 同时清除环境变量 - _unset_proxy_env + _unset_system_proxy } clashrestart() { { clashoff && clashon; } >&/dev/null } -# 独立的环境变量代理控制 function clashproxy() { case "$1" in on) - # 检查代理程序是否运行 systemctl is-active "$BIN_KERNEL_NAME" >&/dev/null || { - _failcat '代理程序未运行,请先执行 clashon' + _failcat '代理程序未运行,请执行 clashon 开启代理环境' return 1 } - - # 检查TUN模式状态 - local tun_status=$(sudo "$BIN_YQ" '.tun.enable' "$CLASH_CONFIG_MIXIN") - if [ "$tun_status" = 'true' ]; then - _failcat 'TUN模式已开启,无法同时使用环境变量代理' - return 1 - fi - - _set_proxy_env # 自动持久化状态 - _okcat '已开启环境变量代理(新shell会话将自动应用)' + _set_system_proxy + _okcat '已开启系统代理' ;; off) - _unset_proxy_env # 自动持久化状态 - _okcat '已关闭环境变量代理' + _unset_system_proxy + _okcat '已关闭系统代理' ;; status) - local proxy_env_status=$(sudo "$BIN_YQ" '.proxy-env.enable' "$CLASH_CONFIG_MIXIN" 2>/dev/null) - if [ -n "$http_proxy" ]; then - _okcat "环境变量代理:已开启 ($http_proxy)" - else - if [ "$proxy_env_status" = 'true' ]; then - echo "环境变量代理:已配置开启,但当前shell未生效" - echo "💡 执行 'clash proxy on' 在当前shell中生效" - else - _failcat "环境变量代理:已关闭" - fi - fi + local system_proxy_status=$(sudo "$BIN_YQ" '.system-proxy.enable' "$CLASH_CONFIG_MIXIN" 2>/dev/null) + [ "$system_proxy_status" = "false" ] && { + _failcat "系统代理:关闭" + return 1 + } + _okcat "系统代理:开启 +http_proxy: $http_proxy +socks_proxy:$all_proxy" ;; *) cat < Date: Tue, 1 Jul 2025 18:27:06 +0800 Subject: [PATCH 3/3] Update mixin.yaml --- resources/mixin.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/mixin.yaml b/resources/mixin.yaml index 8cd5655..5d86dbb 100644 --- a/resources/mixin.yaml +++ b/resources/mixin.yaml @@ -1,3 +1,7 @@ +# 系统代理配置 +system-proxy: + enable: true + # Web 控制台配置 external-controller: "0.0.0.0:9090" external-ui: public @@ -28,10 +32,6 @@ tun: # - docker0 # - podman0 -# 环境变量代理配置 -proxy-env: - enable: false - # DNS 配置 dns: enable: true