V6.5.0 限制 API 访问次数为 1次/分钟; 重写部分代码

This commit is contained in:
Hyy2001X 2021-07-21 15:37:29 +08:00
parent e20dc404d3
commit 94de0af9af
1 changed files with 333 additions and 342 deletions

View File

@ -3,6 +3,8 @@
# AutoUpdate for Openwrt
# Depends on: bash wget-ssl/wget/uclient-fetch curl x86:gzip openssl
Version=V6.5.0
TITLE() {
clear && echo "Openwrt-AutoUpdate Script by Hyy2001 ${Version} [${DL}]"
}
@ -12,41 +14,42 @@ SHELL_HELP() {
echo -e "\n当前指令: ${Run_Command}"
cat <<EOF
使用方法: $0 [-n] [-f] [-u] [-F] [-P] [path=<Path>]
$0 [-x] [path=<Path>] [url=<URL>]
使用方法: $0 [-n] [-f] [-u] [-F] [-P] [path=<PATH>]
$0 [-x] [path=<PATH>] [url=<URL>]
* 可附加参数
更新固件:
-n 不保留配置更新固件 *
-f 跳过版本号、SHA256验证,并强制刷写固件 (危险) *
-u 适用于定时更新 LUCI 的参数 *
-F 强制刷写固件 *
-P 优先开启镜像加速下载固件 *
--skip 跳过云端固件 SHA256 校验 (危险) *
path=<Path> 保存固件到提供的绝对路径 <Path> *
-f 跳过版本号、SHA256 校验,并强制刷写固件 (危险) *
-F, --force-write 强制刷写固件 *
-P, --proxy 优先开启镜像加速下载固件 *
--skip 跳过固件 SHA256 校验 (危险) *
path=<PATH> 保存固件到提供的绝对路径 <PATH> *
更新脚本:
-x 更新 AutoUpdate.sh 脚本
-x path=<Path> 更新 AutoUpdate.sh 脚本 (保存脚本到提供的绝对路径 <Path>) *
-x path=<PATH> 更新 AutoUpdate.sh 脚本 (保存脚本到提供的绝对路径 <PATH>) *
-x url=<URL> 更新 AutoUpdate.sh 脚本 (使用提供的地址 <URL> 更新脚本) *
其他参数:
-C <Github URL> 更改 Github 地址为提供的 <Github URL>
-B <UEFI | Legacy> 指定 x86 设备下载 <UEFI | Legacy> 引导的固件 (危险)
-V < | cloud> 打印 <当前 | 云端> AutoUpdate.sh 版本号
-E < | cloud | *> 打印 <当前 | 云端 | 指定版本> 版本的固件更新日志
-H,--help 打印 AutoUpdate 帮助信息
-L,--list 打印当前系统信息
-Q < | cloud> 打印 <当前 | 云端> 固件版本
--bak <Path> <Name> 备份当前系统配置文件到指定的 <Path> 路径及名称 <Name>
-B, --boot-mode <TYPE> 指定 x86 设备下载 <TYPE> 引导的固件 (e.g. UEFI Legacy)
-C, --change <Github URL> 更改 Github 地址为提供的 <Github URL>
-H, --help 打印 AutoUpdate 帮助信息
-L, --log < | del> <打印 | 删除> AutoUpdate 历史运行日志
--log path=<PATH> 更改 AutoUpdate 运行日志路径为提供的绝对路径 <PATH>
--backup path=<PATH> 备份当前系统配置文件到提供的绝对路径 <PATH>
--check-depends 检查 AutoUpdate 依赖软件包
--clean 清理 AutoUpdate 缓存
--check 检查 AutoUpdate 依赖软件包
--env-list < | 1 | 2> 打印 AutoUpdate 环境变量 <全部 | 变量名称 | 值>
--fw-log < | cloud | *> 打印 <当前 | 云端 | 指定版本> 版本的固件更新日志
--fw-version < | cloud> 打印 <当前 | 云端> 固件版本
--fw-version cloud -a 打印所有云端固件版本
--list 打印当前系统信息
--random <Number> 打印一个 0-30 位的随机数字字母组合
--var <VARIABLE> 打印用户指定的变量 <VARIABLE>
--verbose 打印更详细的下载信息
--var <Variable> 打印用户指定的变量 <Variable>
--var-rm <Variable> 删除用户指定的变量 <Variable>
--env < | 1 | 2> 打印 AutoUpdate 环境变量 <全部 | 变量名称 | 值>
--log < | del> <打印 | 删除> AutoUpdate 历史运行日志
--log path=<Path> 更改 AutoUpdate 运行日志路径为提供的路径 <Path>
--random <Number> 打印一个 <0-31> 位的随机数字与字母组合
--version < | cloud> 打印 <当前 | 云端> AutoUpdate.sh 版本
EOF
EXIT
@ -64,42 +67,28 @@ SHOW_VARIABLE() {
Release URL: ${Github_Release_URL}
Release API: ${Github_API}
OpenWrt 源码 URL: https://github.com/${OP_Maintainer}/${OP_REPO_NAME}:${OP_BRANCH}
固件匹配框架: $(GET_VARIABLE Egrep_Firmware ${Default_Variable})
固件匹配框架: $(GET_VARIABLE REGEX_Firmware ${Default_Variable})
固件格式: ${Firmware_Type}
固件保存路径: ${AutoUpdate_Path}
运行日志路径: ${AutoUpdate_Log_Path}/AutoUpdate.log
固件保存路径: ${Run_Path}
运行日志路径: ${Log_Path}/AutoUpdate.log
Downloader: ${Downloader}
EOF
[[ ${TARGET_BOARD} == x86 ]] && {
echo "x86 固件引导模式: ${x86_Boot}"
echo "固件引导模式: ${x86_Boot}"
}
echo
EXIT 0
}
GET_PID() {
local Result
local PID
while [[ $1 ]];do
Result=$(busybox ps | grep "$1" | grep -v "grep" | awk '{print $1}' | awk 'NR==1')
[[ -n ${Result} ]] && echo ${Result}
PID=$(busybox ps | grep "$1" | grep -v "grep" | awk '{print $1}' | awk 'NR==1')
[[ -n ${PID} ]] && echo "${PID}"
shift
done
}
KILL_PID() {
local Result
Result=$(kill -9 $1)
}
UCI_GET() {
local Result="$(uci get $1.@$2[0].$3 2>/dev/null)"
[[ -n ${Result} ]] && echo "${Result}"
}
UCI_SET() {
uci set $1.@$2[0].$3=$4 2>/dev/null
uci commit $1
}
LIST_ENV() {
local X
cat /etc/AutoBuild/*_Variable | grep -v '#' | while read X;do
@ -144,19 +133,19 @@ ECHO() {
}
LOGGER() {
[[ ! -d ${AutoUpdate_Log_Path} ]] && mkdir -p ${AutoUpdate_Log_Path}
[[ ! -f ${AutoUpdate_Log_Path}/AutoUpdate.log ]] && touch ${AutoUpdate_Log_Path}/AutoUpdate.log
echo "[$(date "+%Y-%m-%d-%H:%M:%S")] [$(GET_PID AutoUpdate.sh)] $*" >> ${AutoUpdate_Log_Path}/AutoUpdate.log
[[ ! -d ${Log_Path} ]] && mkdir -p ${Log_Path}
[[ ! -f ${Log_Path}/AutoUpdate.log ]] && touch ${Log_Path}/AutoUpdate.log
echo "[$(date "+%Y-%m-%d-%H:%M:%S")] [$(GET_PID AutoUpdate.sh)] $*" >> ${Log_Path}/AutoUpdate.log
}
CHECK_PKG() {
which $1 > /dev/null 2>&1
[[ $? == 0 ]] && echo true || echo false
[[ $? == 0 ]] && echo "true" || echo "false"
}
RANDOM() {
local Result=$(openssl rand -base64 $1 | md5sum | cut -c 1-$1)
[[ -n ${Result} ]] && echo ${Result}
[[ -n ${Result} ]] && echo "${Result}"
LOGGER "[RANDOM] $1-bit random-number : ${Result}"
}
@ -167,7 +156,7 @@ GET_SHA256SUM() {
}
LOGGER "[GET_SHA256SUM] Target File: $1"
local Result=$(sha256sum $1 | cut -c1-$2)
[[ -n ${Result} ]] && echo ${Result}
[[ -n ${Result} ]] && echo "${Result}"
LOGGER "[GET_SHA256SUM] Calculated result: ${Result}"
}
@ -175,7 +164,7 @@ GET_VARIABLE() {
[[ $# != 2 ]] && SHELL_HELP
[[ ! -f $2 ]] && ECHO "未检测到定义文件: [$2] !" && EXIT 1
local Result="$(grep "$1=" $2 | grep -v "#" | awk 'NR==1' | sed -r "s/$1=(.*)/\1/")"
[[ -n ${Result} ]] && echo ${Result}
[[ -n ${Result} ]] && echo "${Result}"
LOGGER "[GET_VARIABLE] Get Variable: ${Result}"
}
@ -241,20 +230,17 @@ EDIT_VARIABLE() {
CHANGE_GITHUB() {
[[ ! $1 =~ https://github.com/ ]] && {
ECHO r "ERROR Github URL: $1"
ECHO r "错误的 Github 地址,示例: https://github.com/Hyy2001X/AutoBuild-Actions"
ECHO r "Github 地址输入有误,示例: https://github.com/Hyy2001X/AutoBuild-Actions"
EXIT 1
}
UCI_Github_URL=$(UCI_GET autoupdate common github)
UCI_Github_URL=$(uci get autoupdate.@common[0].github 2>/dev/null)
[[ -n ${UCI_Github_URL} && ! ${UCI_Github_URL} == $1 ]] && {
UCI_SET autoupdate common github $1
ECHO y "UCI 设置已更新!"
uci set autoupdate.@common[0].github=$1 2>/dev/null
LOGGER "[CHANGE_GITHUB] UCI setting to $1"
}
[[ ! ${Github} == $1 ]] && {
EDIT_VARIABLE edit ${Custom_Variable} Github $1
ECHO y "Github 地址已修改为: $1"
} || {
ECHO y "当前输入的地址与原地址相同,无需修改!"
}
EXIT 0
}
@ -264,13 +250,12 @@ CHANGE_BOOT() {
case "$1" in
UEFI | Legacy)
EDIT_VARIABLE edit ${Custom_Variable} x86_Boot $1
echo "ON" > /force_dump
ECHO r "警告: 更换引导方式后更新固件后可能导致设备无法正常启动!"
ECHO y "固件引导格式已指定为: [$1],AutoUpdate 将在下一次更新时执行强制刷写固件!"
ECHO y "固件引导格式已指定为: [$1]"
EXIT 0
;;
*)
ECHO r "错误的参数: [$1],当前支持的选项: [UEFI/Legacy] !"
ECHO r "错误的参数: [$1],支持的启动方式: [UEFI/Legacy]"
EXIT 1
;;
esac
@ -278,8 +263,8 @@ CHANGE_BOOT() {
UPDATE_SCRIPT() {
[[ $# != 2 ]] && SHELL_HELP
ECHO b "脚本下载地址: $2"
ECHO b "脚本保存路径: $1"
ECHO g "下载地址: $2"
ECHO g "保存路径: $1"
[[ -f $1 ]] && {
ECHO r "AutoUpdate 脚本保存路径有误,请重新输入!"
EXIT 1
@ -309,13 +294,13 @@ CHECK_DEPENDS() {
while [[ $1 ]];do
if [[ $1 =~ : ]];then
[[ $(echo $1 | cut -d ":" -f1) == ${TARGET_BOARD} ]] && {
PKG="$(echo $1 | cut -d ":" -f2)"
[[ $(echo ${PKG} | wc -c) -gt 8 ]] && Tab=" " || Tab=" "
PKG="$(echo "$1" | cut -d ":" -f2)"
[[ $(echo "${PKG}" | wc -c) -gt 8 ]] && Tab=" " || Tab=" "
echo -e "${PKG}${Tab}$(CHECK_PKG ${PKG})"
LOGGER "[CHECK_DEPENDS] Checking [${PKG}] ... $(CHECK_PKG ${PKG})"
}
else
[[ $(echo $1 | wc -c) -gt 8 ]] && Tab=" " || Tab=" "
[[ $(echo "$1" | wc -c) -gt 8 ]] && Tab=" " || Tab=" "
echo -e "$1${Tab}$(CHECK_PKG $1)"
LOGGER "[CHECK_DEPENDS] Checking [$1] ... $(CHECK_PKG $1)"
fi
@ -337,67 +322,91 @@ FW_VERSION_CHECK() {
}
GET_FW_LOG() {
local Update_Log
local Result
case "$1" in
[Ll]ocal)
FW_Version="${CURRENT_Version}"
;;
[Cc]loud)
[[ -z ${CLOUD_Firmware_Version} ]] && GET_CLOUD_VERSION
FW_Version="${CLOUD_Firmware_Version}"
FW_Version="$(GET_CLOUD_VERSION)"
;;
-v)
shift
FW_Version="$1"
;;
esac
${Downloader} ${AutoUpdate_Path}/Update_Logs.json ${Release_URL}/Update_Logs.json
[[ $? == 0 ]] && {
Update_Log=$(jsonfilter -e '@["'"""${TARGET_PROFILE}"""'"]["'"""${FW_Version}"""'"]' < ${AutoUpdate_Path}/Update_Logs.json)
rm -f ${AutoUpdate_Path}/Update_Logs.json
}
[[ -n ${Update_Log} ]] && {
if [[ -z $(find ${Run_Path} -type f -mmin -1 -name Update_Logs.json) || ! -s ${Run_Path}/Update_Logs.json ]];then
rm -f ${Run_Path}/Update_Logs.json
${Downloader} ${Run_Path}/Update_Logs.json ${Release_URL}/Update_Logs.json
[[ $? == 0 || -s ${Run_Path}/Update_Logs.json ]] && {
touch -a ${Run_Path}/Update_Logs.json
} || rm -f ${Run_Path}/Update_Logs.json
fi
[[ -f ${Run_Path}/Update_Logs.json ]] && {
Result=$(jsonfilter -e '@["'"""${TARGET_PROFILE}"""'"]["'"""${FW_Version}"""'"]' < ${Run_Path}/Update_Logs.json)
[[ -n ${Result} ]] && {
echo -e "\n${Grey}${FW_Version} for ${TARGET_PROFILE} 更新日志:"
echo -e "\n${Green}${Update_Log}${White}"
echo -e "\n${Green}${Result}${White}"
}
}
}
GET_CLOUD_INFO() {
[[ -f ${AutoUpdate_Path}/Github_Tags ]] && rm -f ${AutoUpdate_Path}/Github_Tags
${Downloader} ${AutoUpdate_Path}/Github_Tags ${Github_API}
[[ $? != 0 || ! -s ${AutoUpdate_Path}/Github_Tags ]] && {
echo "false"
} || echo "true"
if [[ -z $(find ${Run_Path} -type f -mmin -1 -name Github_Tags) || ! -s ${Run_Path}/Github_Tags ]];then
[[ -f ${Run_Path}/Github_Tags ]] && rm -f ${Run_Path}/Github_Tags
${Downloader} ${Run_Path}/Github_Tags ${Github_API}
[[ $? != 0 || ! -s ${Run_Path}/Github_Tags ]] && echo "false" || {
touch -a ${Run_Path}/Github_Tags
echo "true"
}
else
LOGGER "[GET_CLOUD_INFO] Skip downloading [Github_Tags] ..."
echo "true"
fi
}
GET_CLOUD_FW() {
local X Y
[[ $(GET_CLOUD_INFO) == false ]] && {
ECHO r "检查更新失败,请检查网络后重试!"
EXIT 1
}
eval X=$(GET_VARIABLE REGEX_Firmware ${Default_Variable})
case $1 in
-a)
Y=$(egrep -o "${X}" ${Run_Path}/Github_Tags | sort | uniq)
;;
*)
Y=$(egrep -o "${X}" ${Run_Path}/Github_Tags | awk 'END {print}')
;;
esac
[[ -n ${Y} ]] && echo "${Y}"
}
GET_CLOUD_VERSION() {
local X
[[ $(GET_CLOUD_INFO) == false ]] && {
ECHO r "检查更新失败,请稍后重试!"
EXIT 1
}
eval X=$(GET_VARIABLE Egrep_Firmware ${Default_Variable})
FW_Name=$(egrep -o "${X}" ${AutoUpdate_Path}/Github_Tags | awk 'END {print}')
[[ -z ${FW_Name} ]] && ECHO "云端固件名称获取失败!" && EXIT 1
CLOUD_Firmware_Version=$(echo "${FW_Name}" | egrep -o "R[0-9].*202[1-2][0-9]+")
[[ -z ${CLOUD_Firmware_Version} ]] && ECHO r "云端固件版本获取失败!" && EXIT 1
[[ ! $(FW_VERSION_CHECK ${CLOUD_Firmware_Version}) == true ]] && ECHO r "云端固件版本号合法性检查失败!"
local Z
Z=$(GET_CLOUD_FW $1 | egrep -o "R[0-9].*202[1-2][0-9]+")
[[ -n ${Z} ]] && echo "$Z"
}
CHECK_UPDATES() {
ECHO "正在获取版本更新 ..."
GET_CLOUD_VERSION
[[ ${CLOUD_Firmware_Version} == ${CURRENT_Version} ]] && {
local A
ECHO "正在检查版本更新 ..."
A=$(GET_CLOUD_VERSION)
[[ ! $(FW_VERSION_CHECK $A ) == true ]] && {
ECHO r "固件版本合法性校验失败!"
EXIT 1
}
[[ ${A} == ${CURRENT_Version} ]] && {
CURRENT_Type="${Yellow} [已是最新]${White}"
Upgrade_Stopped=1
} || {
[[ $(echo ${CLOUD_Firmware_Version} | cut -d "-" -f2) -gt $(echo ${CURRENT_Version} | cut -d "-" -f2) ]] && CURRENT_Type="${Green} [可更新]${White}"
[[ $(echo ${CLOUD_Firmware_Version} | cut -d "-" -f2) -lt $(echo ${CURRENT_Version} | cut -d "-" -f2) ]] && {
CLOUD_Type="${Red} [旧版本]${White}"
[[ $(echo ${A} | cut -d "-" -f2) -gt $(echo ${CURRENT_Version} | cut -d "-" -f2) ]] && CURRENT_Type="${Green} [可更新]${White}"
[[ $(echo ${A} | cut -d "-" -f2) -lt $(echo ${CURRENT_Version} | cut -d "-" -f2) ]] && {
CHECKED_Type="${Red} [旧版本]${White}"
Upgrade_Stopped=2
}
}
# SHA5BIT=$(echo ${FW_Name} | egrep -o "[a-zA-Z0-9]+.${Firmware_Type}" | sed -r "s/(.*).${Firmware_Type}/\1/")
}
PREPARE_UPGRADES() {
@ -411,18 +420,18 @@ PREPARE_UPGRADES() {
Test_Mode=1
Special_Commands="${Special_Commands} [测试模式]"
;;
-P)
-P | --proxy)
Proxy_Mode=1
Special_Commands="${Special_Commands} [镜像加速]"
;;
-F)
-F | --force-write)
[[ -n ${Force_Mode} ]] && SHELL_HELP
Only_Force_Write=1
Special_Commands="${Special_Commands} [强制刷写]"
Upgrade_Option="${Upgrade_Option} -F"
;;
--skip)
Skip_SHA256=1
Skip_Verify=1
Special_Commands="${Special_Commands} [跳过 SHA256 验证]"
;;
-f)
@ -440,9 +449,9 @@ PREPARE_UPGRADES() {
Special_Commands="${Special_Commands} [定时更新]"
;;
path=/*)
[[ -z $(echo $1 | cut -d "=" -f2) ]] && ECHO r "固件保存路径不能为空!" && EXIT 1
AutoUpdate_Path=$(echo $1 | cut -d "=" -f2)
ECHO g "使用自定义固件保存路径: ${AutoUpdate_Path}"
[[ -z $(echo "$1" | cut -d "=" -f2) ]] && ECHO r "固件保存路径不能为空!" && EXIT 1
Run_Path=$(echo "$1" | cut -d "=" -f2)
ECHO g "使用自定义固件保存路径: ${Run_Path}"
;;
--verbose)
Special_Commands="${Special_Commands} [详细信息]"
@ -455,23 +464,24 @@ PREPARE_UPGRADES() {
LOGGER "Upgrade Options: ${Upgrade_Option}"
[[ -n "${Special_Commands}" ]] && ECHO g "特殊指令:${Special_Commands} / ${Upgrade_Option}"
ECHO g "执行: ${MSG}${Special_MSG}"
REMOVE_CACHE quiet
if [[ $(CHECK_PKG curl) == true && ${Proxy_Mode} != 1 ]];then
Google_Check=$(curl -I -s --connect-timeout 3 google.com -w %{http_code} | tail -n1)
LOGGER "Google_Check: ${Google_Check}"
[[ ${Google_Check} != 301 ]] && {
ECHO r "Google 连接失败,优先使用镜像加速!"
ECHO r "网络连接不佳,优先使用镜像加速!"
Proxy_Mode=1
}
fi
CHECK_UPDATES
[[ -z ${CLOUD_Firmware_Version} ]] && {
ECHO r "云端固件信息获取失败!"
CLOUD_FW_Version=$(GET_CLOUD_VERSION)
CLOUD_FW_Name=$(GET_CLOUD_FW)
[[ -z ${CLOUD_FW_Version} || -z ${CLOUD_FW_Name} ]] && {
ECHO r "检查更新失败,请检查网络后重试!"
EXIT 1
}
CHECK_UPDATES
[[ ${Proxy_Mode} == 1 ]] && {
FW_URL="${Release_FastGit_URL}"
} || FW_URL="${Release_URL}"
CLOUD_FW_URL="${Release_Goproxy_URL}"
} || CLOUD_FW_URL="${Release_URL}"
cat <<EOF
固件作者: ${FW_Author%/*}
@ -479,13 +489,12 @@ PREPARE_UPGRADES() {
$([[ ${TARGET_BOARD} == x86 ]] && echo "固件格式: ${Firmware_Type} / ${x86_Boot}" || echo "固件格式: ${Firmware_Type}")
$(echo -e "当前固件版本: ${CURRENT_Version}${CURRENT_Type}")
$(echo -e "云端固件版本: ${CLOUD_Firmware_Version}${CLOUD_Type}")
$(echo -e "云端固件版本: ${CLOUD_FW_Version}${CHECKED_Type}")
云端固件名称: ${FW_Name}
固件下载地址: ${FW_URL}
云端固件名称: ${CLOUD_FW_Name}
固件下载地址: ${CLOUD_FW_URL}
EOF
GET_FW_LOG -v ${CLOUD_Firmware_Version}
rm -f ${AutoUpdate_Path}/Github_Tags
GET_FW_LOG -v ${CLOUD_FW_Version}
case "${Upgrade_Stopped}" in
1 | 2)
[[ ${AutoUpdate_Mode} == 1 ]] && ECHO y "已是最新版本,无需更新!" && EXIT 0
@ -501,49 +510,49 @@ EOF
while [[ ${Retry_Times} -ge 0 ]];do
if [[ ! ${PROXY_Mode} == 1 && ${Retry_Times} == 4 ]];then
ECHO g "尝试使用 [FastGit] 镜像加速下载固件!"
FW_URL="${Release_FastGit_URL}"
CLOUD_FW_URL="${Release_FastGit_URL}"
fi
[[ ${Retry_Times} == 3 ]] && {
ECHO g "尝试使用 [Github Proxy] 镜像加速下载固件!"
FW_URL="${Release_Goproxy_URL}"
CLOUD_FW_URL="${Release_Goproxy_URL}"
}
[[ ${Retry_Times} == 2 ]] && FW_URL="${Github_Release_URL}"
[[ ${Retry_Times} == 2 ]] && CLOUD_FW_URL="${Github_Release_URL}"
if [[ ${Retry_Times} == 0 ]];then
ECHO r "固件下载失败,请检查网络后重试!"
EXIT 1
else
${Downloader} ${AutoUpdate_Path}/${FW_Name} "${FW_URL}/${FW_Name}"
[[ $? == 0 && -s ${AutoUpdate_Path}/${FW_Name} ]] && ECHO y "固件下载成功!" && break
${Downloader} ${Run_Path}/${CLOUD_FW_Name} "${CLOUD_FW_URL}/${CLOUD_FW_Name}"
[[ $? == 0 && -s ${Run_Path}/${CLOUD_FW_Name} ]] && ECHO y "固件下载成功!" && break
fi
Retry_Times=$((${Retry_Times} - 1))
ECHO r "固件下载失败,剩余尝试次数: ${Retry_Times}"
done
if [[ ! ${Skip_SHA256} == 1 || ! Force_Mode == 1 ]];then
CURRENT_SHA256=$(GET_SHA256SUM ${AutoUpdate_Path}/${FW_Name} 5)
CLOUD_SHA256=$(echo ${FW_Name} | egrep -o "[0-9a-z]+.${Firmware_Type}" | sed -r "s/(.*).${Firmware_Type}/\1/")
[[ ${CURRENT_SHA256} != ${CLOUD_SHA256} ]] && {
ECHO r "本地固件 SHA256 与云端比对校验失败 [${CURRENT_SHA256}],请检查网络后重试!"
if [[ ! ${Skip_Verify} == 1 || ! Force_Mode == 1 ]];then
LOCAL_SHA256=$(GET_SHA256SUM ${Run_Path}/${CLOUD_FW_Name} 5)
CLOUD_SHA256=$(echo "${CLOUD_FW_Name}" | egrep -o "[0-9a-z]+.${Firmware_Type}" | sed -r "s/(.*).${Firmware_Type}/\1/")
[[ ${LOCAL_SHA256} != ${CLOUD_SHA256} ]] && {
ECHO r "本地固件 SHA256 与云端比对校验失败 [${LOCAL_SHA256}],请检查网络后重试!"
EXIT 1
} || LOGGER "固件 SHA256 比对通过!"
fi
case "${Firmware_Type}" in
img.gz)
ECHO "正在解压固件,请耐心等待 ..."
gzip -d -q -f -c ${AutoUpdate_Path}/${FW_Name} > ${AutoUpdate_Path}/$(echo ${FW_Name} | sed -r 's/(.*).gz/\1/')
gzip -d -q -f -c ${Run_Path}/${CLOUD_FW_Name} > ${Run_Path}/$(echo "${CLOUD_FW_Name}" | sed -r 's/(.*).gz/\1/')
[[ $? != 0 ]] && {
ECHO r "固件解压失败,请检查网络稳定性或更换固件保存路径!"
EXIT 1
} || {
FW_Name="$(echo ${FW_Name} | sed -r 's/(.*).gz/\1/')"
ECHO "固件解压成功,固件已解压到: ${AutoUpdate_Path}/${FW_Name}"
CLOUD_FW_Name="$(echo "${CLOUD_FW_Name}" | sed -r 's/(.*).gz/\1/')"
ECHO "固件解压成功,固件已解压到: ${Run_Path}/${CLOUD_FW_Name}"
}
;;
esac
[[ ${Test_Mode} != 1 ]] && {
chmod 777 ${AutoUpdate_Path}/${FW_Name}
DO_UPGRADE ${Upgrade_Option} ${AutoUpdate_Path}/${FW_Name}
chmod 777 ${Run_Path}/${CLOUD_FW_Name}
DO_UPGRADE ${Upgrade_Option} ${Run_Path}/${CLOUD_FW_Name}
} || {
ECHO x "[测试模式] ${Upgrade_Option} ${AutoUpdate_Path}/${FW_Name}"
ECHO x "[测试模式] ${Upgrade_Option} ${Run_Path}/${CLOUD_FW_Name}"
ECHO x "[测试模式] 运行完毕!"
EXIT 0
}
@ -555,31 +564,22 @@ DO_UPGRADE() {
ECHO g "正在更新固件,请耐心等待 ..."
$*
[[ $? -ne 0 ]] && {
ECHO r "固件刷写失败,请尝试手动下载更新固件或附加 -F 参数强制刷写!"
ECHO r "固件刷写失败,请尝试手动下载固件或附加 -F 参数强制刷写!"
EXIT 1
} || EXIT 0
}
REMOVE_CACHE() {
rm -rf ${AutoUpdate_Path}/AutoBuild-${TARGET_PROFILE}-* \
${AutoUpdate_Path}/Github_Tags \
${AutoUpdate_Path}/Update_Logs.json
case "$1" in
quiet)
rm -rf ${Run_Path}/*
LOGGER "AutoUpdate 缓存清理完成!"
;;
*)
ECHO y "AutoUpdate 缓存清理完成!"
EXIT 0
;;
esac
}
AutoUpdate_LOG() {
[[ -z $1 ]] && {
[[ -s ${AutoUpdate_Log_Path}/AutoUpdate.log ]] && {
[[ -s ${Log_Path}/AutoUpdate.log ]] && {
TITLE && echo
cat ${AutoUpdate_Log_Path}/AutoUpdate.log
cat ${Log_Path}/AutoUpdate.log
}
} || {
while [[ $1 ]];do
@ -592,8 +592,8 @@ AutoUpdate_LOG() {
;;
esac
if [[ $1 =~ path= ]];then
LOG_PATH="$(echo $1 | cut -d "=" -f2)"
[[ ${LOG_PATH} == ${AutoUpdate_Log_Path} ]] && {
LOG_PATH="$(echo "$1" | cut -d "=" -f2)"
[[ ${LOG_PATH} == ${Log_Path} ]] && {
ECHO y "AutoUpdate 日志保存路径相同,无需修改!"
EXIT 0
}
@ -602,16 +602,16 @@ AutoUpdate_LOG() {
ECHO r "AutoUpdate 日志保存路径有误,请重新输入!"
EXIT 1
}
EDIT_VARIABLE rm ${Custom_Variable} AutoUpdate_Log_Path
EDIT_VARIABLE edit ${Custom_Variable} AutoUpdate_Log_Path ${LOG_PATH}
EDIT_VARIABLE rm ${Custom_Variable} Log_Path
EDIT_VARIABLE edit ${Custom_Variable} Log_Path ${LOG_PATH}
[[ ! -d ${LOG_PATH} ]] && mkdir -p ${LOG_PATH}
[[ -f ${AutoUpdate_Log_Path}/AutoUpdate.log ]] && mv ${AutoUpdate_Log_Path}/AutoUpdate.log ${LOG_PATH}
AutoUpdate_Log_Path=${LOG_PATH}
[[ -f ${Log_Path}/AutoUpdate.log ]] && mv ${Log_Path}/AutoUpdate.log ${LOG_PATH}
Log_Path=${LOG_PATH}
ECHO y "AutoUpdate 日志保存路径已修改为: ${LOG_PATH}"
EXIT 0
fi
[[ $1 == rm || $1 == del ]] && {
[[ -f ${AutoUpdate_Log_Path}/AutoUpdate.log ]] && rm ${AutoUpdate_Log_Path}/AutoUpdate.log
[[ -f ${Log_Path}/AutoUpdate.log ]] && rm ${Log_Path}/AutoUpdate.log
}
EXIT 0
done
@ -619,57 +619,67 @@ AutoUpdate_LOG() {
}
AutoUpdate_Main() {
local Result
[[ ! -f ${Default_Variable} ]] && {
LOGGER "Unable to access default variable file ..."
ECHO r "脚本运行环境检测失败,请手动更新固件!"
EXIT 1
}
[[ ! -f ${Custom_Variable} ]] && touch ${Custom_Variable}
LOAD_VARIABLE ${Default_Variable} ${Custom_Variable}
[[ ! -d ${AutoUpdate_Path} ]] && mkdir -p ${AutoUpdate_Path}
[[ ! -d ${Run_Path} ]] && {
mkdir -p ${Run_Path}
[[ ! $? == 0 ]] && {
ECHO r "脚本运行目录创建失败!"
EXIT 1
}
}
if [[ $(CHECK_PKG wget-ssl) == true ]];then
Downloader="wget-ssl --quiet --no-check-certificate -T 5 --no-dns-cache -x -O"
Downloader="wget-ssl --quiet --no-check-certificate --tries 1 -T 5 --no-dns-cache -x -4 -O"
DL="wget-ssl"
elif [[ $(CHECK_PKG curl) == true ]];then
Downloader="curl -L -k -m 5 --silent -o"
Downloader="curl --insecure -L -k --connect-timeout 5 --retry 1 --silent -o"
DL="curl"
else
Downloader="uclient-fetch --quiet --no-check-certificate -T 5 -O"
Downloader="uclient-fetch --quiet --no-check-certificate -T 5 -4 -O"
DL="uclient-fetch"
fi
[[ -z $* ]] && PREPARE_UPGRADES $*
[[ $1 =~ path=/ && ! $* =~ -x && ! $* =~ -U ]] && PREPARE_UPGRADES $*
[[ $1 =~ path=/ && ! $* =~ -x ]] && PREPARE_UPGRADES $*
[[ $1 =~ --skip ]] && PREPARE_UPGRADES $*
[[ $* =~ -T || $* =~ --verbose ]] && {
Downloader="$(echo ${Downloader/ --quiet / })"
Downloader="$(echo ${Downloader/ --silent / })"
Downloader="${Downloader/ --quiet / }"
Downloader="${Downloader/ --silent / }"
}
while [[ $1 ]];do
case "$1" in
-n | -f | -u | -T | -P | -F | --verbose)
-n | -f | -u | -T | -P | --proxy | -F | --force-write | --verbose)
LOGGER "Downloader: ${DL} / ${Downloader}"
PREPARE_UPGRADES $*
;;
--bak)
--backup)
local FILE="backup-$(uname -n)-$(date +%Y-%m-%d)-$(RANDOM 5).tar.gz"
shift
[[ $# -lt 1 || $# -gt 2 ]] && ECHO r "格式错误,示例: [bash $0 --bak /mnt/sda1 Openwrt_Backups.tar.gz]" && EXIT 1
[[ $# == 2 ]] && {
[[ ! -d $1 ]] && mkdir -p $1
FILE="$1/$2"
[[ -f ${FILE} ]] && FILE="${FILE}-$(RANDOM 5)"
} || {
[[ ! -d $1 ]] && mkdir -p $1
FILE="$1/$(uname -n)-Backups-$(date +%Y-%m-%d)-$(RANDOM 5)"
[[ $# -gt 1 ]] && SHELL_HELP
[[ -z $* ]] && {
FILE=$(pwd)/${FILE}
}
[[ $1 =~ path=/ ]] && {
[[ ! -d $1 ]] && {
mkdir -p $1
[[ ! $? == 0 ]] && ECHO r "文件夹创建失败,请更换保存路径!" && EXIT 1
}
FILE="$(echo "$1" | cut -d "=" -f2)/${FILE}"
}
[[ ! ${FILE} =~ tar.gz ]] && FILE="${FILE}.tar.gz"
ECHO "Saving config files to [${FILE}] ..."
sysupgrade -b "${FILE}" >/dev/null 2>&1
[[ $? == 0 ]] && {
ECHO y "系统文件备份成功!"
ECHO y "保存位置: ${FILE}"
ECHO y "备份文件创建成功!"
EXIT 0
} || {
ECHO r "备份文件创建失败,请尝试更换保存路径!"
ECHO r "备份文件创建失败,请更换保存路径!"
EXIT 1
}
;;
@ -677,11 +687,11 @@ AutoUpdate_Main() {
shift && [[ -n $* ]] && SHELL_HELP
REMOVE_CACHE
;;
--check)
--check-depends)
shift && [[ -n $* ]] && SHELL_HELP
CHECK_DEPENDS bash x86:gzip x86:wget-ssl uclient-fetch curl wget openssl which
CHECK_DEPENDS bash x86:gzip uclient-fetch curl wget openssl
;;
--env)
--env-list)
shift
[[ -z $* ]] && LIST_ENV 0 && EXIT 0
case "$1" in
@ -694,73 +704,19 @@ AutoUpdate_Main() {
esac
EXIT 0
;;
--log)
--fw-version)
shift
AutoUpdate_LOG $*
;;
--random)
shift
[[ $# != 1 || ! $1 =~ [0-9] || $1 == 0 || $1 -gt 30 ]] && SHELL_HELP || {
RANDOM $1
EXIT 0
}
;;
--sleep)
shift
sleep $1
EXIT 0
;;
--var)
shift
[[ $# != 1 ]] && SHELL_HELP
SHOW_VARIABLE=$(GET_VARIABLE "$1" ${Custom_Variable})
[[ -z ${SHOW_VARIABLE} ]] && SHOW_VARIABLE=$(GET_VARIABLE "$1" ${Default_Variable})
echo "${SHOW_VARIABLE}"
[[ $? == 0 ]] && EXIT 0 || EXIT 1
;;
--var-rm)
shift
[[ $# != 1 ]] && SHELL_HELP
EDIT_VARIABLE rm ${Custom_Variable} $1
[[ $? == 0 ]] && EXIT 0 || EXIT 1
;;
-x)
shift
while [[ $1 ]];do
case "$1" in
url=* | path=/*)
:
[Cc]loud)
GET_CLOUD_VERSION $2
;;
*)
SHELL_HELP
echo "${CURRENT_Version}"
;;
esac
if [[ $1 =~ url= ]];then
[[ $1 =~ url= ]] && {
[[ -z $(echo $1 | cut -d "=" -f2) ]] && ECHO r "脚本地址不能为空!" && EXIT 1
AutoUpdate_Script_URL="$(echo $1 | cut -d "=" -f2)"
}
fi
[[ $1 =~ path= ]] && {
[[ -z $(echo $1 | cut -d "=" -f2) ]] && ECHO r "保存路径不能为空!" && EXIT 1
SH_SAVE_PATH="$(echo $1 | cut -d "=" -f2)"
}
shift
done
LOGGER "Downloader: ${Downloader}"
[[ -z ${SH_SAVE_PATH} ]] && SH_SAVE_PATH=/bin
UPDATE_SCRIPT ${SH_SAVE_PATH} ${AutoUpdate_Script_URL}
EXIT 0
;;
-B)
shift
[[ ${TARGET_BOARD} != x86 ]] && EXIT 1
CHANGE_BOOT $1
;;
-C)
shift
CHANGE_GITHUB $1
;;
-E)
--fw-log)
shift
[[ -z $* ]] && GET_FW_LOG local
case "$1" in
@ -769,40 +725,41 @@ AutoUpdate_Main() {
;;
*)
[[ -z $* ]] && EXIT 0
[[ $(FW_VERSION_CHECK $1) == true ]] && GET_FW_LOG -v $1 || {
[[ ! $(FW_VERSION_CHECK $1) == true ]] && {
ECHO r "固件版本号合法性检查失败!"
EXIT 1
} || {
GET_FW_LOG -v $1
}
;;
esac
EXIT
EXIT 0
;;
-H | --[Hh]elp)
SHELL_HELP
;;
-L | --[Ll]ist)
--list)
shift && [[ -n $* ]] && SHELL_HELP
SHOW_VARIABLE
;;
-Q)
--random)
shift
[[ -z $* ]] && {
echo ${CURRENT_Version}
[[ $# != 1 || ! $1 =~ [0-9] || $1 == 0 || $1 -gt 30 ]] && SHELL_HELP || {
RANDOM $1
EXIT 0
}
case "$1" in
[Cc]loud)
GET_CLOUD_VERSION
[[ -z ${CLOUD_Firmware_Version} ]] && echo "未知" || echo "${CLOUD_Firmware_Version}"
;;
esac
EXIT 0
--var)
local Result
shift
[[ $# != 1 ]] && SHELL_HELP
Result=$(GET_VARIABLE "$1" ${Custom_Variable})
[[ -z ${Result} ]] && Result=$(GET_VARIABLE "$1" ${Default_Variable})
[[ -n ${Result} ]] && echo "${Result}"
;;
-V)
--version)
local Result
shift
case "$1" in
[Cc]loud)
Result="$(${Downloader} - ${AutoUpdate_Script_URL} | egrep -o "V[0-9].+")"
Result="$(${Downloader} - ${Script_URL} | egrep -o "V[0-9].+")"
;;
*)
Result=${Version}
@ -813,18 +770,52 @@ AutoUpdate_Main() {
EXIT 0
}
;;
-x)
shift
while [[ $1 ]];do
case "$1" in
url=https://*)
[[ -z $(echo "$1" | cut -d "=" -f2) ]] && ECHO r "脚本地址不能为空!" && EXIT 1
Script_URL="$(echo "$1" | cut -d "=" -f2)"
;;
path=/*)
[[ -z $(echo "$1" | cut -d "=" -f2) ]] && ECHO r "保存路径不能为空!" && EXIT 1
Script_Path="$(echo "$1" | cut -d "=" -f2)"
;;
*)
SHELL_HELP
;;
esac
shift
done
[[ -z ${Script_Path} ]] && Script_Path=/bin
UPDATE_SCRIPT ${Script_Path} ${Script_URL}
;;
-B | --boot-mode)
shift
[[ ${TARGET_BOARD} != x86 ]] && EXIT 1
CHANGE_BOOT $1
;;
-C | --change)
shift
CHANGE_GITHUB $1
;;
-H | --help)
SHELL_HELP
;;
-L | --log)
shift
AutoUpdate_LOG $*
;;
*)
SHELL_HELP
;;
esac
}
Version=V6.4.6
AutoUpdate_Path=/tmp/AutoUpdate
AutoUpdate_Log_Path=/tmp
AutoUpdate_Script_URL=https://ghproxy.com/https://raw.githubusercontent.com/Hyy2001X/AutoBuild-Actions/master/Scripts/AutoUpdate.sh
Run_Path=/tmp/AutoUpdate
Log_Path=/tmp
Script_URL=https://ghproxy.com/https://raw.githubusercontent.com/Hyy2001X/AutoBuild-Actions/master/Scripts/AutoUpdate.sh
Default_Variable=/etc/AutoBuild/Default_Variable
Custom_Variable=/etc/AutoBuild/Custom_Variable
[[ -n $* ]] && Run_Command="$0 $*" || Run_Command="$0"