lsyupdate/lsy-loader/lsyloader/data/lsyloader/lsydeb.sh
2024-12-17 18:07:41 +08:00

260 lines
7.3 KiB
Bash
Executable File
Raw 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
jsonIn=""
#### jsonIn=version接口
# {
# "version": "1.0.5",
# "name": "lsyloader",
# "path": "http://192.168.200.128:5212/versionFile?filename=lsyloader1.0.5.deb",
# "sign": "a0d47ccb8edaa029e2c1262a8351674846e4c055c798ad63c55851fa5dff7059"
# }
cur_dir=$(dirname "$(realpath "$0")")
mkdir -p "/tmp/log"
logFile="/tmp/log/lsyloader.log"
maxLogSize=$((1024*1024*3))
versionLocal="1.0.0"
url="https://deb.jxm.cool/deb/version.ini"
# 只处理
pkgNameArrOnly=("lsyupdate")
# 除了这些都处理,一般是自己, pkgNameArrOnly存在时pkgNameArrExcept无效
pkgNameArrExcept=("lsyloader")
pkgName=""
pkgUrl=""
pkgVersion=""
pkgSign=""
outPath="$cur_dir/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=$jsonIn
nowtime=`date +"%Y-%m-%d %H:%M:%S"`
echo "[$nowtime] hasVersion done,$str_ack" >> $logFile
pkgUrl=$(get_json_value "$str_ack" "path" "")
pkgVersion=$(get_json_value "$str_ack" "version" "-1")
pkgSign=$(get_json_value "$str_ack" "sign" "")
pkgName=$(get_json_value "$str_ack" "name" "")
pkgUrl=$(trim "$pkgUrl" "\"")
pkgSign=$(trim "$pkgSign" "\"")
pkgName=$(trim "$pkgName" "\"")
[ -z "$pkgName" ] && echo "pkgName is empty skip" >> $logFile && return 0
# pkgNameArrOnly
local found=false
for item in "${pkgNameArrOnly[@]}"; do
if [[ "$item" == "$pkgName" ]]; then
found=true
break
fi
done
[ ${#pkgNameArrOnly[@]} -ne 0 ] && [ "$found" == false ] && echo "pkgName not in $pkgNameArrOnly skip" >> $logFile && return 0
# pkgNameArrExcept
for item in "${pkgNameArrExcept[@]}"; do
if [[ "$item" == "$pkgName" ]]; then
echo "pkgName in $pkgNameArrExcept skip" >> $logFile && return 0
fi
done
versionLocal=$(apt show "$pkgName"|grep "Version:"|head -n 1|awk '{print $2}')
[ "$versionLocal" == "" ] && versionLocal="1.0.0"
outPath="$cur_dir/$pkgName.deb"
version=$(trim "$pkgVersion" "\"")
split_str "${version}" "."
len=${#split_str_return_list[@]}
if [ $len -lt 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]}
echo "[$nowtime] pkgVersion=$pkgVersion" >> $logFile
echo "[$nowtime] pkgUrl=$pkgUrl" >> $logFile
echo "[$nowtime] pkgSign=$pkgSign" >> $logFile
echo "[$nowtime] pkgName=$pkgName" >> $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 runInstall() {
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 uninstall $outPath" >> $logFile
bash $cur_dir/uninstall.sh "$pkgName" >> $logFile
echo "[$nowtime] start install $outPath" >> $logFile
apt install -y --allow-downgrades "$outPath" >> $logFile
freshNow
echo "[$nowtime] finish" >> $logFile
}
function main() {
initLog
str_ack=$(
curl --request GET \
--url "$url" \
--header 'content-type: application/json' \
--data ""
)
apps=($(echo "$str_ack" | jq -c '.[]'))
for item in "${apps[@]}"; do
jsonIn=$item
runInstall
sleep 2
done
}
main