Skip to content

Commit 34573eb

Browse files
committed
feat(tool): 为更新订阅新增自动修改 mihomo 的 proxy-providers 配置内容
1 parent 367926a commit 34573eb

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

box/scripts/box.tool

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,60 @@ upgeox_all() {
356356
bin_name=$original_bin_name
357357
}
358358

359+
# 更新 mihomo 配置的 proxy-providers
360+
update_mihomo_providers() {
361+
yq="yq"
362+
if ! command -v yq &>/dev/null; then
363+
if [ ! -e "${box_dir}/bin/yq" ]; then
364+
log Debug "yq 文件未找到, 开始从 GitHub 下载"
365+
${scripts_dir}/box.tool upyq
366+
fi
367+
yq="${box_dir}/bin/yq"
368+
fi
369+
370+
if [ ! -f "${mihomo_config}" ]; then
371+
log Error "配置文件不存在: ${mihomo_config}"
372+
return 1
373+
fi
374+
cp "${mihomo_config}" "${mihomo_config}.bak" 2>/dev/null
375+
local file_count=${#name_provide_mihomo_config[@]}
376+
if [ "$file_count" -eq 0 ]; then
377+
log Warning "没有配置的订阅文件"
378+
return 1
379+
fi
380+
381+
log Debug "开始更新 proxy-providers 配置项..."
382+
local config_dir="$(dirname "${mihomo_config}")"
383+
for i in $(seq 0 $((file_count - 1))); do
384+
local file_name="${name_provide_mihomo_config[$i]}"
385+
local provider_file="${mihomo_provide_path}/${file_name}"
386+
local provider_name="${file_name%.yaml}"
387+
if [ ! -f "${provider_file}" ]; then
388+
log Warning "订阅文件不存在,跳过: ${provider_file}"
389+
continue
390+
fi
391+
local relative_path
392+
if command -v realpath >/dev/null 2>&1; then
393+
relative_path="$(realpath --relative-to="${config_dir}" "${provider_file}")"
394+
else
395+
local provide_dir_name="$(basename "${mihomo_provide_path}")"
396+
relative_path="./${provide_dir_name}/${file_name}"
397+
fi
398+
399+
log Debug "添加 provider: ${provider_name} -> ${relative_path}"
400+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".type = \"file\"" "${mihomo_config}" 2>/dev/null
401+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".path = \"${relative_path}\"" "${mihomo_config}" 2>/dev/null
402+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".\"health-check\".enable = true" "${mihomo_config}" 2>/dev/null
403+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".\"health-check\".url = \"https://cp.cloudflare.com\"" "${mihomo_config}" 2>/dev/null
404+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".\"health-check\".interval = 300" "${mihomo_config}" 2>/dev/null
405+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".\"health-check\".timeout = 1000" "${mihomo_config}" 2>/dev/null
406+
${yq} -i ".\"proxy-providers\".\"${provider_name}\".\"health-check\".tolerance = 100" "${mihomo_config}" 2>/dev/null
407+
done
408+
409+
log Debug "proxy-providers 配置构建完成"
410+
return 0
411+
}
412+
359413
# 检查并更新订阅
360414
upsubs() {
361415
if [ "${update_subscription}" != "true" ]; then
@@ -487,11 +541,20 @@ upsubs() {
487541
done
488542

489543
log Info "成功更新 ${success_count} / ${url_count} 个订阅"
544+
545+
if [ "${renew}" != "true" ] && [ "${success_count}" -gt 0 ]; then
546+
log Info "正在更新 ${name_mihomo_config} 的 proxy-providers 配置..."
547+
if update_mihomo_providers; then
548+
log Info "proxy-providers 配置更新成功"
549+
else
550+
log Warning "proxy-providers 配置更新失败,请手动检查配置文件"
551+
fi
552+
fi
553+
490554
if [ "${update_failed}" = "true" ]; then
491555
log Error "部分订阅链接更新失败"
492556
return 1
493557
else
494-
log Warning "请确保您的 ${name_mihomo_config} 的 'proxy-providers' 部分已正确配置, 以加载这些订阅文件"
495558
log Info "更新订阅于 $(date +"%F %R")"
496559
return 0
497560
fi

box/scripts/ctr.inotify

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,21 @@ clean_old_logs
5858

5959
current_time=$(date +%s)
6060
last_check_time=0
61+
6162
if [ -f "$last_check_file" ]; then
62-
last_check_time=$(cat "$last_check_file")
63+
last_check_time=$(grep -oE '[0-9]+' "$last_check_file" | head -n 1)
64+
last_check_time=${last_check_time:-0}
6365
fi
6466

65-
time_diff=$((current_time - last_check_time))
67+
time_diff=$(( current_time - last_check_time ))
6668
stability_window=3
6769

6870
if [ "$time_diff" -lt "$stability_window" ]; then
6971
log_msg "跳过检查:(${time_diff}秒),处于稳定窗口内"
70-
return
72+
return 0
7173
fi
7274

73-
echo "$current_time" > "$last_check_file"
75+
echo -n "$current_time" > "$last_check_file"
7476

7577
wifi_status=$(is_wifi_connected)
7678
ssid=$(get_current_ssid)
@@ -109,7 +111,7 @@ check_module_service() {
109111
log_msg "SSID:${ssid},IP:${current_ip}"
110112
echo "ssid:${ssid}" > "$last_wifi_state_file"
111113
echo "ip:${current_ip}" >> "$last_wifi_state_file"
112-
114+
113115
if [ "$use_module_on_wifi" = "false" ]; then
114116
log_msg "策略:WiFi 环境禁用服务"
115117
if [ "$current_state" != "disabled" ]; then

0 commit comments

Comments
 (0)