Skip to content

Commit fa13774

Browse files
committed
refactor: manage controller secret via env instead of mixin
1 parent a9e5842 commit fa13774

3 files changed

Lines changed: 113 additions & 35 deletions

File tree

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ bash install.sh
5252

5353
```bash
5454
〽️ 常用命令
55-
clashon 🚀 开启代理
56-
clashoff ⛔ 关闭代理
57-
clashctl select 💫 选择节点
55+
clashon 🚀 开启代理
56+
clashoff ⛔ 关闭代理
57+
clashctl select 💫 选择节点
5858
🕹️ 控制台
59-
clashui 🕹️ 查看 Web 控制台
60-
clashsecret 🔑 查看或设置 Web 密钥
59+
clashui 🕹️ 查看 Web 控制台
6160
📦 订阅
62-
clashctl add ➕ 添加订阅
63-
clashctl add local ➕ 从 runtime/subscriptions 导入本地订阅
64-
clashctl use 💱 切换订阅
65-
clashctl ls 📡 查看订阅列表
61+
clashctl add ➕ 添加订阅
62+
clashctl add local ➕ 从 runtime/subscriptions 导入本地订阅
63+
clashctl use 💱 切换订阅
64+
clashctl ls 📡 查看订阅列表
65+
🔐 密钥管理
66+
clashctl secret show 🔑 查看密钥
67+
clashctl secret set 🔐 设置密钥
6668
📌 高级
6769
clashctl tun 🧪 Tun 模式管理
6870
clashctl boot 🚦 开机代理接管管理

scripts/core/clashctl.sh

Lines changed: 101 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Usage:
3434
3535
🕹️ Control:
3636
clashui 🕹️ 查看 Web 控制台
37+
secret 🔑 管理 Web 密钥(show / set)
3738
clashsecret 🔑 查看或设置 Web 密钥
3839
3940
🩺 Diagnose:
@@ -3897,6 +3898,9 @@ cmd_mixin_show() {
38973898
print_mixin_template_example
38983899
else
38993900
cat "$file"
3901+
if mixin_config_has_secret_override "$file"; then
3902+
ui_warn "检测到 override.secret:该字段已忽略,请改用 clashctl secret set"
3903+
fi
39003904
fi
39013905
ui_blank
39023906
ui_next "clashctl mixin edit"
@@ -3932,6 +3936,21 @@ mixin_config_is_empty() {
39323936
return 1
39333937
}
39343938

3939+
mixin_config_has_secret_override() {
3940+
local file="$1"
3941+
local exists
3942+
3943+
[ -s "$file" ] || return 1
3944+
[ -x "$(yq_bin)" ] || return 1
3945+
3946+
exists="$("$(yq_bin)" eval '
3947+
(.override // {}) as $override |
3948+
(($override | type) == "!!map" and ($override | has("secret")))
3949+
' "$file" 2>/dev/null | head -n 1 || true)"
3950+
3951+
[ "$exists" = "true" ]
3952+
}
3953+
39353954
print_mixin_template_example() {
39363955
cat <<'EOF'
39373956
当前 mixin 还没有实际补丁。可按这个结构填写:
@@ -3955,6 +3974,7 @@ append:
39553974
39563975
说明:
39573976
override 会覆盖同名字段
3977+
override.secret 会被忽略,控制器密钥只从 .env 的 CLASH_CONTROLLER_SECRET 读取
39583978
prepend 会把数组内容放到原始订阅前面
39593979
append 会把数组内容放到原始订阅后面
39603980
EOF
@@ -4504,51 +4524,107 @@ doctor_evidence_lines() {
45044524

45054525
set_controller_secret() {
45064526
local secret="$1"
4527+
4528+
is_valid_controller_secret "$secret" || die "密钥不能为空"
4529+
4530+
write_env_value "CLASH_CONTROLLER_SECRET" "$secret"
4531+
export CLASH_CONTROLLER_SECRET="$secret"
4532+
}
4533+
4534+
sync_runtime_controller_secret_from_env() {
45074535
local file="$RUNTIME_DIR/config.yaml"
4536+
local secret
45084537

4509-
[ -n "${secret:-}" ] || die "密钥不能为空"
4510-
[ -s "$file" ] || die "运行时配置不存在:$file"
4538+
[ -s "$file" ] || return 0
4539+
[ -x "$(yq_bin)" ] || return 1
45114540

4541+
secret="$(ensure_controller_secret)"
45124542
SECRET_VALUE="$secret" "$(yq_bin)" eval -i '
45134543
.secret = strenv(SECRET_VALUE)
45144544
' "$file"
4545+
}
45154546

4516-
write_env_value "CLASH_CONTROLLER_SECRET" "$secret"
4547+
show_controller_secret_from_env() {
4548+
local current_secret
4549+
4550+
current_secret="$(read_env_value "CLASH_CONTROLLER_SECRET" 2>/dev/null || true)"
4551+
if ! is_valid_controller_secret "$current_secret"; then
4552+
current_secret="${CLASH_CONTROLLER_SECRET:-}"
4553+
fi
4554+
4555+
echo
4556+
if is_valid_controller_secret "$current_secret"; then
4557+
ui_kv "🔑" "当前密钥" "$current_secret"
4558+
else
4559+
ui_kv "🚨" "当前密钥" "未设置"
4560+
fi
4561+
4562+
ui_kv "🔧" "密钥来源" "$PROJECT_DIR/.env"
4563+
ui_blank
4564+
}
4565+
4566+
print_controller_secret_apply_feedback() {
4567+
local synced="${1:-true}"
4568+
4569+
echo
4570+
4571+
if [ "$synced" = "true" ]; then
4572+
if status_is_running; then
4573+
service_restart
4574+
ui_kv "🐱" "状态" "密钥更新成功,已重启生效"
4575+
else
4576+
ui_kv "🐱" "状态" "将在下次启动时生效"
4577+
fi
4578+
else
4579+
ui_warn "运行时配置暂未同步:缺少 yq 或写入失败,请稍后执行 clashctl config regen"
4580+
ui_kv "🐱" "状态" "密钥已写入 .env,运行时配置同步后生效"
4581+
fi
4582+
4583+
ui_kv "🔧" "密钥来源" "$PROJECT_DIR/.env"
4584+
ui_blank
45174585
}
45184586

45194587
cmd_secret() {
4520-
local current_secret new_secret
4588+
local new_secret synced
45214589

45224590
prepare
4523-
runtime_config_exists || die_state "运行时配置不存在" "clashctl add <订阅链接> 或 clashctl config regen"
45244591

45254592
case "${1:-}" in
4526-
"")
4527-
current_secret="$(controller_secret 2>/dev/null || true)"
4528-
4529-
echo
4530-
if [ -n "${current_secret:-}" ] && [ "$current_secret" != "null" ]; then
4531-
ui_kv "🔑" "当前密钥" "$current_secret"
4532-
else
4533-
ui_kv "🚨" "当前密钥" "未设置"
4534-
fi
4593+
""|show)
4594+
[ "$#" -le 1 ] || die_usage "secret show 参数不合法" "clashctl secret show"
4595+
show_controller_secret_from_env
4596+
;;
4597+
set)
4598+
shift || true
4599+
[ "$#" -le 1 ] || die_usage "secret set 参数不合法" "clashctl secret set [密钥]"
4600+
new_secret="${1:-}"
4601+
[ -n "${new_secret:-}" ] || new_secret="$(generate_secure_secret)"
4602+
set_controller_secret "$new_secret"
45354603

4536-
ui_blank
4604+
synced="true"
4605+
sync_runtime_controller_secret_from_env || synced="false"
4606+
print_controller_secret_apply_feedback "$synced"
4607+
;;
4608+
help|-h|--help)
4609+
echo
4610+
echo "🔑 控制器密钥"
4611+
echo
4612+
echo "用法:"
4613+
echo " clashctl secret show"
4614+
echo " clashctl secret set [密钥]"
4615+
echo
4616+
echo "兼容:"
4617+
echo " clashctl secret [密钥]"
4618+
echo " clashsecret [密钥]"
4619+
echo
45374620
;;
45384621
*)
45394622
new_secret="$1"
45404623
set_controller_secret "$new_secret"
45414624

4542-
echo
4543-
4544-
if status_is_running; then
4545-
service_restart
4546-
ui_kv "🐱" "状态" "密钥更新成功,已重启生效"
4547-
else
4548-
ui_kv "🐱" "状态" "将在下次启动时生效"
4549-
fi
4550-
4551-
ui_blank
4625+
synced="true"
4626+
sync_runtime_controller_secret_from_env || synced="false"
4627+
print_controller_secret_apply_feedback "$synced"
45524628
;;
45534629
esac
45544630
}

scripts/core/config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ apply_mixin_override() {
19911991

19921992
RUNTIME_FILE="$runtime_file" MIXIN_FILE="$mixin_file_path" "$(yq_bin)" eval-all -i '
19931993
select(fileIndex == 0)
1994-
* ((select(fileIndex == 1).override // {}))
1994+
* (((select(fileIndex == 1).override // {}) | del(.secret)))
19951995
' "$runtime_file" "$mixin_file_path"
19961996
}
19971997

0 commit comments

Comments
 (0)