From f1589ee3225e934c8c8f06c7542fc017e0888404 Mon Sep 17 00:00:00 2001 From: wnlen <62139570+wnlen@users.noreply.github.com> Date: Wed, 29 Apr 2026 16:49:09 +0800 Subject: [PATCH 1/3] optimize: add fast path for already-enabled clashon --- scripts/core/clashctl.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/core/clashctl.sh b/scripts/core/clashctl.sh index 57df762..159b12c 100644 --- a/scripts/core/clashctl.sh +++ b/scripts/core/clashctl.sh @@ -195,18 +195,27 @@ cmd_on() { local relay_switch local relay_switch_file relay_err_file relay_rc local system_proxy_rc system_proxy_degraded="false" - local already_on="false" trap 'rc=$?; ui_error "开启代理失败:cmd_on 在第 ${LINENO} 行执行失败:${BASH_COMMAND}(返回码:${rc})"; ui_next "clashctl logs service"; exit "$rc"' ERR prepare - ensure_on_path_ready + # fast path:已经完全开启时,直接返回 if status_is_running 2>/dev/null \ && proxy_controller_reachable 2>/dev/null \ && [ "$(system_proxy_status 2>/dev/null || echo off)" = "on" ] \ && system_proxy_matches_runtime 2>/dev/null; then - already_on="true" + print_on_feedback + trap - ERR + return 0 + fi + + ensure_on_path_ready + + if [ "$already_on" = "true" ]; then + print_on_feedback + trap - ERR + return 0 fi if status_is_running 2>/dev/null && ! proxy_controller_reachable 2>/dev/null; then From 42833b3ccbc651029136f319703f9e4774fb4411 Mon Sep 17 00:00:00 2001 From: wnlen <62139570+wnlen@users.noreply.github.com> Date: Wed, 29 Apr 2026 16:51:45 +0800 Subject: [PATCH 2/3] fix: clear system proxy during uninstall --- uninstall.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/uninstall.sh b/uninstall.sh index 2898f7b..a4d4b0a 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -21,29 +21,25 @@ for arg in "$@"; do esac done -# shellcheck source=scripts/core/common.sh source "$PROJECT_DIR/scripts/core/common.sh" -# shellcheck source=scripts/core/runtime.sh source "$PROJECT_DIR/scripts/core/runtime.sh" -# shellcheck source=scripts/core/config.sh source "$PROJECT_DIR/scripts/core/config.sh" -# shellcheck source=scripts/init/systemd.sh +source "$PROJECT_DIR/scripts/core/proxy.sh" source "$PROJECT_DIR/scripts/init/systemd.sh" -# shellcheck source=scripts/init/systemd-user.sh source "$PROJECT_DIR/scripts/init/systemd-user.sh" -# shellcheck source=scripts/init/script.sh source "$PROJECT_DIR/scripts/init/script.sh" init_project_context "$PROJECT_DIR" load_env_if_exists detect_install_scope auto +system_proxy_disable || true +clear_shell_proxy_persist_state || true service_stop || true remove_runtime_entry || true remove_clashctl_entry || true remove_clashctl_completion || true remove_shell_alias_entry || true -clear_shell_proxy_persist_state || true if [ "$PURGE_RUNTIME" = "true" ]; then rm -rf "$RUNTIME_DIR" From 8be724ce337c819017d535b1e9e97ca1adf8d85f Mon Sep 17 00:00:00 2001 From: wnlen <62139570+wnlen@users.noreply.github.com> Date: Wed, 29 Apr 2026 17:06:59 +0800 Subject: [PATCH 3/3] optimize: fast enable proxy when runtime is already running --- scripts/core/alias.sh | 1 - scripts/core/clashctl.sh | 12 +++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/core/alias.sh b/scripts/core/alias.sh index 778f9f8..247925e 100644 --- a/scripts/core/alias.sh +++ b/scripts/core/alias.sh @@ -258,7 +258,6 @@ _clash_alias_auto_restore_proxy() { _clash_alias_persist_enabled || return 0 _clash_alias_export_proxy || return 0 - echo "♻️ 已恢复当前 shell 代理环境(来自持久化状态)" return 0 } diff --git a/scripts/core/clashctl.sh b/scripts/core/clashctl.sh index 159b12c..7e58983 100644 --- a/scripts/core/clashctl.sh +++ b/scripts/core/clashctl.sh @@ -200,7 +200,7 @@ cmd_on() { prepare - # fast path:已经完全开启时,直接返回 + # fast path 1:代理已经完整开启 if status_is_running 2>/dev/null \ && proxy_controller_reachable 2>/dev/null \ && [ "$(system_proxy_status 2>/dev/null || echo off)" = "on" ] \ @@ -210,14 +210,17 @@ cmd_on() { return 0 fi - ensure_on_path_ready - - if [ "$already_on" = "true" ]; then + # fast path 2:runtime 已运行,只是系统代理被关了 + if status_is_running 2>/dev/null \ + && proxy_controller_reachable 2>/dev/null; then + system_proxy_enable || true print_on_feedback trap - ERR return 0 fi + ensure_on_path_ready + if status_is_running 2>/dev/null && ! proxy_controller_reachable 2>/dev/null; then ui_warn "检测到内核已运行但控制器不可访问,正在重启以加载当前配置" service_restart || die_state "控制器启动失败:内核重启未完成" "clashctl logs mihomo" @@ -294,7 +297,6 @@ cmd_off() { local system_proxy_rc prepare - service_stop if system_proxy_disable; then :