diff --git a/resources/mixin.yaml b/resources/mixin.yaml index 4c41941..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 diff --git a/script/clashctl.sh b/script/clashctl.sh index fb2ab52..74bbd01 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -1,15 +1,7 @@ # shellcheck disable=SC2148 # shellcheck disable=SC2155 -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 - } - } - +_set_system_proxy() { local auth=$(sudo "$BIN_YQ" '.authentication[0] // ""' "$CLASH_CONFIG_RUNTIME") [ -n "$auth" ] && auth=$auth@ @@ -28,19 +20,10 @@ function clashon() { export no_proxy=$no_proxy_addr export NO_PROXY=$no_proxy - _okcat '已开启代理环境' + sudo "$BIN_YQ" -i '.system-proxy.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_system_proxy() { unset http_proxy unset https_proxy unset HTTP_PROXY @@ -49,12 +32,75 @@ function clashoff() { unset ALL_PROXY unset no_proxy unset NO_PROXY + + 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 + } + } + _set_system_proxy + _okcat '已开启代理环境' +} + +watch_proxy() { + [ -z "$http_proxy" ] && [[ $- == *i* ]] && { + clashproxy status >&/dev/null && { + _is_root && clashon + } + } +} + +function clashoff() { + sudo systemctl stop "$BIN_KERNEL_NAME" && _okcat '已关闭代理程序' || + _failcat '关闭失败: 执行 "clashstatus" 查看日志' || return 1 + _unset_system_proxy } clashrestart() { { clashoff && clashon; } >&/dev/null } +function clashproxy() { + case "$1" in + on) + systemctl is-active "$BIN_KERNEL_NAME" >&/dev/null || { + _failcat '代理程序未运行,请执行 clashon 开启代理环境' + return 1 + } + _set_system_proxy + _okcat '已开启系统代理' + ;; + off) + _unset_system_proxy + _okcat '已关闭系统代理' + ;; + status) + 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 <&/dev/null _error_quit '不支持的内核版本' } - _okcat "Tun 模式已开启" + + # 开启TUN模式时卸载环境变量,避免冲突 + _unset_proxy_env + _okcat "Tun 模式已开启,已自动卸载环境变量代理" } function clashtun() { @@ -232,7 +281,10 @@ function clashctl() { shift clashstatus "$@" ;; - + proxy) + shift + clashproxy "$@" + ;; tun) shift clashtun "$@" @@ -253,20 +305,22 @@ function clashctl() { cat <