From 6232eefcd40b67948bf4da5a565cbac7f2e90def Mon Sep 17 00:00:00 2001 From: zhao Date: Wed, 25 Dec 2024 14:41:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20bin/ZeroWrt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhao --- bin/ZeroWrt | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/bin/ZeroWrt b/bin/ZeroWrt index 332af1f..f990f97 100644 --- a/bin/ZeroWrt +++ b/bin/ZeroWrt @@ -34,7 +34,7 @@ print_header() { color_output "\e[32m博客:https://www.kejizero.online\e[0m" color_output "----------------------------------------" echo "" -} +} # 显示菜单 show_menu() { @@ -46,10 +46,11 @@ show_menu() { color_output "\e[36m┃\e[0m 4. 恢复出厂设置 \e[36m┃\e[0m" color_output "\e[36m┃\e[0m 5. 一键换源 \e[36m┃\e[0m" color_output "\e[36m┃\e[0m 6. 安装 ShellClash \e[36m┃\e[0m" + color_output "\e[36m┃\e[0m 7. 检测更新 \e[36m┃\e[0m" color_output "\e[36m┃\e[0m 0. 退出 \e[36m┃\e[0m" color_output "\e[36m┃ ┃\e[0m" color_output "\e[36m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\e[0m" - read -p "$(color_output "\e[33m请输入您的选择 [0-6]: \e[0m")" choice + read -p "$(color_output "\e[33m请输入您的选择 [0-7]: \e[0m")" choice case "$choice" in 1) change_ip ;; 2) change_password ;; @@ -57,6 +58,7 @@ show_menu() { 4) reset_config ;; 5) change_source ;; 6) install_shellclash ;; + 7) check_update ;; 0) exit 0 ;; *) echo "无效选项,请重新输入"; show_menu ;; esac @@ -175,6 +177,82 @@ install_shellclash() { show_menu } +# 7. 检测更新 +check_update() { + color_output "\e[34m[检测更新]\e[0m" + color_output "正在检查更新..." + + # 检查 wget 是否安装 + if ! command -v wget >/dev/null 2>&1; then + color_output "\e[33m正在安装 wget...\e[0m" + opkg update && opkg install wget + fi + + # 创建临时目录 + local temp_dir="/tmp/zerowrt_update" + mkdir -p "$temp_dir" + + # 下载远程版本文件 + if ! wget -q "https://git.kejizero.online/zhao/files/raw/branch/main/bin/version.txt" -O "$temp_dir/version.txt"; then + color_output "\e[31m无法连接到更新服务器。\e[0m" + rm -rf "$temp_dir" + read -p "按 Enter 键返回菜单..." + show_menu + return + fi + + # 获取本地版本和远程版本 + local_version=$(cat /root/version.txt 2>/dev/null || echo "0") + remote_version=$(cat "$temp_dir/version.txt" 2>/dev/null || echo "0") + + # 比较版本 + if [ "$local_version" != "$remote_version" ]; then + color_output "\e[33m发现新版本:$remote_version\e[0m" + color_output "\e[33m当前版本:$local_version\e[0m" + read -p "是否更新到最新版本?(y/n): " confirm + if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then + color_output "开始下载更新..." + + # 下载新版本到临时文件 + if wget -q "https://git.kejizero.online/zhao/files/raw/branch/main/bin/ZeroWrt" -O "$temp_dir/ZeroWrt.new"; then + # 检查文件是否为有效的 shell 脚本 + if head -n1 "$temp_dir/ZeroWrt.new" | grep -q "^#!/bin/bash"; then + # 备份当前版本 + cp /bin/ZeroWrt /bin/ZeroWrt.bak 2>/dev/null + + # 安装新版本 + mv "$temp_dir/ZeroWrt.new" /bin/ZeroWrt + chmod +x /bin/ZeroWrt + + # 更新版本文件 + echo "$remote_version" > /root/version.txt + + color_output "\e[32m更新成功!\e[0m" + color_output "\e[32m已备份原版本为 /bin/ZeroWrt.bak\e[0m" + color_output "\e[32m请重新运行脚本以应用更新。\e[0m" + + # 清理并退出 + rm -rf "$temp_dir" + exit 0 + else + color_output "\e[31m下载的文件格式不正确。\e[0m" + fi + else + color_output "\e[31m更新下载失败。\e[0m" + fi + else + color_output "\e[32m已取消更新。\e[0m" + fi + else + color_output "\e[32m当前已是最新版本:$local_version\e[0m" + fi + + # 清理临时文件 + rm -rf "$temp_dir" + read -p "按 Enter 键返回菜单..." + show_menu +} + # 启动菜单 print_header -show_menu +show_menu \ No newline at end of file