lsyupdate/lsy-update/lsy_update-bak.sh
2024-12-17 18:07:41 +08:00

217 lines
5.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#### version接口
# {
# "version": "1.2.3",
# "path": "http://192.168.200.128:5212/versionFile?filename=app1.2.3",
# "sign": "2d28d9af69d48c964abafaff97f6e9958ecf40a9ca6d62a7ae69e00b279c22f2"
# }
mkdir -p "/data"
logFile="/data/lsyudpate.log"
maxLogSize=$((1024*1024*3))
url="http://192.168.200.128:5212/version"
versionLocal="1.0.1"
pkgUrl=""
pkgVersion=""
pkgSign=""
outPath="out.deb"
function initLog() {
[ ! -e "$logFile" ] && return
local size=`ls -al "$logFile" |awk '{print $5}'`
echo $size
[ $size -gt $maxLogSize ] && rm -rf "$logFile"
}
function freshNow() {
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
}
function get_json_value() {
awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
foundKeyCount = 0
while (length(json) > 0) {
# pos = index(json, "\""key"\""); ## 这行更快一些但是如果有value是字符串且刚好与要查找的key相同会被误认为是key而导致值获取错误
pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}
++foundKeyCount;
start = 0; stop = 0; layer = 0;
for (i = pos + length(key) + 1; i <= length(json); ++i) {
lastChar = substr(json, i - 1, 1)
currChar = substr(json, i, 1)
if (start <= 0) {
if (lastChar == ":") {
start = currChar == " " ? i + 1: i;
if (currChar == "{" || currChar == "[") {
layer = 1;
}
}
} else {
if (currChar == "{" || currChar == "[") {
++layer;
}
if (currChar == "}" || currChar == "]") {
--layer;
}
if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
stop = currChar == "," ? i : i + 1 + layer;
break;
}
}
}
if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
if (foundKeyCount == 0) {print defaultValue;} exit 0;
} else {
print substr(json, start, stop - start);
}
json = substr(json, stop + 1, length(json) - stop)
}
}'
}
# split_str "${tmp_data}" "|"
split_str_return_list=()
function split_str() {
local str_data=$1
local str_pattern=$2
str=$str_data
delimiter=$str_pattern
array=()
while [ "$str" ]; do
substring="${str%%"$delimiter"*}"
[ -z "$substring" ] && str="${str#"$delimiter"}" && continue
array+=( "$substring" )
str="${str:${#substring}}"
[ "$str" == "$delimiter" ] && break
done
#declare -p array
split_str_return_list=()
for var in "${array[@]}"; do
#echo "[v]=${var}"
split_str_return_list+=( "${var}" )
done
}
function trimL() {
local str=$1
local toTrim=$2
local dst=${str#$toTrim}
echo $dst
}
function trimR() {
local str=$1
local toTrim=$2
local dst=${str%$toTrim}
echo $dst
}
function trim() {
local str=$1
local toTrim=$2
str=${str#$toTrim}
local dst=${str%$toTrim}
echo $dst
}
function hasVersion() {
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
echo "[$nowtime] hasVersion start" >> $logFile
local str_ack=$(
curl --request GET \
--url "$url" \
--header 'content-type: application/json' \
--data ""
)
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
echo "[$nowtime] hasVersion done,$str_ack" >> $logFile
local version=$(get_json_value "$str_ack" "version" "-1")
version=$(trim "$version" "\"")
split_str "${version}" "."
len=${#split_str_return_list[@]}
if [ ! $len -eq 3 ];then
return 0
fi
local v1=${split_str_return_list[0]}
local v2=${split_str_return_list[1]}
local v3=${split_str_return_list[2]}
split_str "${versionLocal}" "."
local lv1=${split_str_return_list[0]}
local lv2=${split_str_return_list[1]}
local lv3=${split_str_return_list[2]}
pkgUrl=$(get_json_value "$str_ack" "path" "")
pkgVersion=$version
pkgSign=$(get_json_value "$str_ack" "sign" "")
pkgUrl=$(trim "$pkgUrl" "\"")
pkgSign=$(trim "$pkgSign" "\"")
echo "[$nowtime] pkgVersion=$pkgVersion" >> $logFile
echo "[$nowtime] pkgUrl=$pkgUrl" >> $logFile
echo "[$nowtime] pkgSign=$pkgSign" >> $logFile
[ -z "$pkgUrl" ] && echo "[$nowtime] pkgurl empty skip" >> $logFile && return 0
[ -z "$pkgSign" ] && echo "[$nowtime] pkgSign empty skip" >> $logFile && return 0
echo "[$nowtime] v=$v1.$v2.$v3" >> $logFile
echo "[$nowtime] lv=$lv1.$lv2.$lv3" >> $logFile
[ $v1 -gt $lv1 ] && return 1
[ $v2 -gt $lv2 ] && return 1
[ $v3 -gt $lv3 ] && return 1
return 0
}
function main() {
initLog
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
echo "[$nowtime] start..." >> $logFile
hasVersion
local bUpdate=$?
if [ $bUpdate -eq 0 ]; then
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
echo "[$nowtime] no need to update" >> $logFile
return
fi
echo "[$nowtime] need update" >> $logFile
freshNow
rm -rf "$outPath"
str_ack=`wget -O "$outPath" $pkgUrl`
[ ! -e "$outPath" ] && echo "[$nowtime] $outPath not exist" >> $logFile && return
freshNow
local fileSign=`sha256sum "$outPath"|awk '{print $1}'`
echo "[$nowtime] get sign,fileSign=$fileSign,$outPath" >> $logFile
[ "$fileSign" != "$pkgSign" ] && echo "[$nowtime] $fileSign!=$pkgSign,$outPath skip" >> $logFile && return
chmod +x "$outPath"
echo "[$nowtime] start run $outPath" >> $logFile
bash -c "./$outPath" >> $logFile
rm -rf "$outPath"
freshNow
echo "[$nowtime] finish" >> $logFile
}
main