7 Commits

Author SHA1 Message Date
Hermes
304d4d5b57 fix: 替换here-string嵌套语法为标准管道,兼容Android mksh 2026-04-28 22:38:13 -04:00
Hermes
fcd72cc1a0 [optimized] 代码优化:清理死代码、修复竞态条件与空参数安全问题
- 删除 backup() 中的调试 echo 残留
- 删除被注释的死代码行
- 修复 backup_wifi rm -rf 空参数安全问题
- 重写 start.sh 逻辑,优化配置生成流程
- 重构 kill_Serve 使用 mkdir 原子锁替代 PID 文件
2026-04-28 22:22:23 -04:00
Yawasau
dfef750e5c bugfix 2026-04-23 20:00:55 +08:00
Yawasau
2fe35e5a35 bugfix 2026-03-31 08:08:28 +08:00
Yawasau
e64fa5d6b9 bugfix 2026-03-16 21:15:27 +08:00
Yawasau
32b023373f bugfix 2025-12-31 15:01:48 +08:00
Yawasau
656d85a19f bugfix 2025-11-09 17:32:10 +08:00
7 changed files with 1612 additions and 318 deletions

View File

@@ -1,9 +1,16 @@
#!/system/bin/sh
if [ -f "${0%/*}/tools/tools.sh" ]; then
MODDIR="${0%/*}"
conf_path="${0%/*}/backup_settings.conf"
[ ! -f "${0%/*}/backup_settings.conf" ] && . "${0%/*}/tools/tools.sh"
else
echo "${0%/*}/tools/tools.sh遺失"
if [ ! -f "${0%/*}/tools/tools.sh" ]; then
echo "${0%/*}/tools/tools.sh遺失"
exit 1
fi
. "${0%/*}/tools/tools.sh" | tee "${0%/*}/log.txt"
MODDIR="${0%/*}"
conf_path="${0%/*}/backup_settings.conf"
# 若配置文件不存在,啟動腳本自動生成默認配置後退出
if [ ! -f "$conf_path" ]; then
. "${0%/*}/tools/tools.sh"
exit 0
fi
. "${0%/*}/tools/tools.sh" | tee "${0%/*}/log_$(date +%Y-%m-%d_%H-%M).txt"

File diff suppressed because it is too large Load Diff

BIN
tools/jq

Binary file not shown.

File diff suppressed because one or more lines are too long

BIN
tools/tar

Binary file not shown.

View File

@@ -9,7 +9,7 @@ MODDIR="$MODDIR"
MODDIR_NAME="${MODDIR##*/}"
tools_path="$MODDIR/tools"
script="${0##*/}"
backup_version="202507071944"
backup_version="202508162209"
[[ $SHELL = *mt* ]] && echo "請勿使用MT管理器拓展包環境執行,請更換系統環境" && exit 2
update_backup_settings_conf() {
echo "#0關閉音量鍵選擇 (如選項未設置,則強制使用音量鍵選擇)
@@ -18,8 +18,8 @@ update_backup_settings_conf() {
Lo="${Lo:-0}"
#後台執行腳本
0不能關閉當前終端有壓縮速率
1終端有可能完全無顯示但是log會持續刷新可直接完全關閉終端
#0不能關閉當前終端有壓縮速率
#1終端有可能完全無顯示但是log會持續刷新可直接完全關閉終端
background_execution="${background_execution:-0}"
#腳本語言設置 留空則自動識別系統語言環境並翻譯
@@ -148,8 +148,8 @@ update_Restore_settings_conf() {
Lo="${Lo:-0}"
#後台執行腳本
0不能關閉當前終端有壓縮速率
1終端有可能完全無顯示但是log會持續刷新可直接完全關閉終端
#0不能關閉當前終端有壓縮速率
#1終端有可能完全無顯示但是log會持續刷新可直接完全關閉終端
background_execution="${background_execution:-0}"
#恢復開始後偽裝亮屏
@@ -223,7 +223,7 @@ esac
echoRgb() {
#轉換echo顏色提高可讀性
if [[ $2 = 0 ]]; then
echo -e "\e[38;5;197m -$1\e[0m"
echo -e "\e[38;5;197m -!$1\e[0m"
elif [[ $2 = 1 ]]; then
echo -e "\e[38;5;121m -$1\e[0m"
elif [[ $2 = 2 ]]; then
@@ -312,9 +312,20 @@ fi
[[ ! -f $filepath/zstd ]] && echoRgb "$filepath缺少zstd" && exit 2
export PATH="$filepath:$PATH"
export TZ=Asia/Taipei
export CLASSPATH="$tools_path/classes.dex"
ln -fs "$tools_path/classes.dex" "$filepath/classes.dex"
export CLASSPATH="$filepath/classes.dex"
quit=0
while read -r file expected_hash; do
cat <<EOF | while read -r file expected_hash; do
zstd 9ef4b54148699c9874cfd45aaf38e5cc950e5d168afdcf2edf58a2463f5561ed
tar 882639ac310a7eb4052c68c21cea02633307700f9cc8c7c469c2dd18d734a112
classes.dex 63934f7d15de40f4b188672e36fe22a01b55abb235becee2c2738f29aaf8299b
bc b15d730591f6fb52af59284b87d939c5bea204f944405a3518224d8df788dc15
busybox 4d60ab3f5a59ebb2ca863f2f514e6924401b581e9b64f602665c008177626651
find 7fa812e58aafa29679cf8b50fc617ecf9fec2cfb2e06ea491e0a2d6bf79b903b
jq 6bc62f25981328edd3cfcfe6fe51b073f2d7e7710d7ef7fcdac28d4e384fc3d4
keycheck 50645ee0e0d2a7d64fb4a1286446df7a4445f3d11aefd49eeeb88515b314c363
cmd 08da8ac23b6e99788fd3ce6c19c7b5a083b2ad48be35963a48d01d6ee7f3bb6d
EOF
if [[ -f $tools_path/$file ]]; then
computed_hash="$(sha256sum "$tools_path/$file" | awk '{print $1}')"
if [[ $computed_hash = $expected_hash ]]; then
@@ -329,17 +340,7 @@ while read -r file expected_hash; do
quit=1
break
fi
done <<< "$(cat <<EOF
zstd ab32aecb389c3ba5c1f7ab05d5eb6a861bad80261fd14ef9a8f4c283ac48c22c
tar 3c605b1e9eb8283555225dcad4a3bf1777ae39c5f19a2c8b8943140fd7555814
classes.dex 63934f7d15de40f4b188672e36fe22a01b55abb235becee2c2738f29aaf8299b
bc b15d730591f6fb52af59284b87d939c5bea204f944405a3518224d8df788dc15
busybox 4d60ab3f5a59ebb2ca863f2f514e6924401b581e9b64f602665c008177626651
find 7fa812e58aafa29679cf8b50fc617ecf9fec2cfb2e06ea491e0a2d6bf79b903b
jq 4dd2d8a0661df0b22f1bb9a1f9830f06b6f3b8f7d91211a1ef5d7c4f06a8b4a5
keycheck 50645ee0e0d2a7d64fb4a1286446df7a4445f3d11aefd49eeeb88515b314c363
cmd 08da8ac23b6e99788fd3ce6c19c7b5a083b2ad48be35963a48d01d6ee7f3bb6d
EOF)"
done
if [[ $background_execution = 1 || $setDisplayPowerMode = 1 ]]; then
alias notification="app_process /system/bin com.xayah.dex.NotificationUtil notify -t 'SpeedBackup' "$@""
else
@@ -411,6 +412,7 @@ get_version() {
done
}
isBoolean() {
unset nsx
nsx="$1"
if [[ $1 = 1 ]]; then
nsx=true
@@ -434,20 +436,30 @@ echo_log() {
fi
}
kill_Serve() {
LOCK_FILE="/tmp/my_backup.lock"
if [[ -f $LOCK_FILE ]]; then
OLD_PID="$(cat "$LOCK_FILE")"
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "發現先前的備份程序 (PID=$OLD_PID),將其終止"
kill -KILL "$OLD_PID"
echo "結束自身,避免重複執行"
exit 1
local LOCK_DIR="/data/local/tmp/.backup_lock"
local MY_PID="$$"
# 使用 mkdir 作為原子鎖操作,避免 TOCTOU 競態條件
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
if [[ -f $LOCK_DIR/pid ]]; then
OLD_PID="$(cat "$LOCK_DIR/pid")"
if kill -0 "$OLD_PID" 2>/dev/null; then
echo "發現先前的備份程序 (PID=$OLD_PID),將其終止"
kill -KILL "$OLD_PID"
echo "結束自身,避免重複執行"
exit 1
else
echo "發現 lock 但程序已不存在,視為殘留 lock"
rm -rf "$LOCK_DIR"
mkdir "$LOCK_DIR" 2>/dev/null || exit 1
fi
else
echo "發現 lock 檔但程序已不存在,視為殘留 lock"
rm -rf "$LOCK_DIR"
mkdir "$LOCK_DIR" 2>/dev/null || exit 1
fi
fi
echo $$ > "$LOCK_FILE"
trap "rm -f '$LOCK_FILE'" EXIT
echo "$MY_PID" > "$LOCK_DIR/pid"
trap "rm -rf '$LOCK_DIR'" EXIT
}
echo $$
kill_Serve
@@ -640,11 +652,13 @@ alias Set_Ops="app_process /system/bin com.xayah.dex.HiddenApiUtil setOpsMode $U
alias setDisplay="app_process /system/bin com.xayah.dex.HiddenApiUtil setDisplayPowerMode $@"
find_tools_path="$(find "$path_hierarchy"/* -maxdepth 1 -name "tools" -type d ! -path "$path_hierarchy/tools")"
backup_wifi() {
[[ ! -d $1 ]] && mkdir -p "$1"
if [[ -d $1 ]]; then
local wifi_dir="$1"
[[ -z $wifi_dir ]] && echoRgb "backup_wifi: 目錄參數為空" "0" && return 1
[[ ! -d $wifi_dir ]] && mkdir -p "$wifi_dir"
if [[ -d $wifi_dir ]]; then
echoRgb "備份wifi密碼"
rm -rf "$1"/*
app_process /system/bin com.xayah.dex.NetworkUtil saveNetworks>"$1/wifi.json"
rm -rf "${wifi_dir:?}"/*
app_process /system/bin com.xayah.dex.NetworkUtil saveNetworks>"$wifi_dir/wifi.json"
echo_log "wifi備份"
fi
}
@@ -712,7 +726,7 @@ if [ -f \"$MODDIR_Path/tools/tools.sh\" ]; then
else
echo \"$MODDIR_Path/tools/tools.sh遺失\"
fi
. \"$MODDIR_Path/tools/tools.sh\" | tee \"\${0%/*}/log.txt\""> "$2"
. \"$MODDIR_Path/tools/tools.sh\" | tee \"\${0%/*}/log_\$(date +%Y-%m-%d_%H-%M).txt\""> "$2"
}
update_script() {
[[ $zipFile = "" ]] && zipFile="$(find "$MODDIR" -maxdepth 1 -name "*.zip" -type f 2>/dev/null)"
@@ -804,15 +818,15 @@ else
[[ $(unzip -l "$zipFile" 2>/dev/null | awk '{print $4}' | egrep -wo "^backup_settings.conf$") != "" ]] && update_script
fi
if [[ $(getprop ro.build.version.sdk) -lt 30 ]]; then
alias INSTALL="pm install --user $user -r -t &>/dev/null"
alias create="pm install-create --user $user -t 2>/dev/null"
alias INSTALL="pm install --user $user -r -t >/dev/null"
alias create="pm install-create --user $user -tl"
else
if [[ $(getprop ro.build.version.sdk) -gt 33 ]]; then
alias INSTALL="pm install -r --bypass-low-target-sdk-block -i com.android.vending --user $user -t &>/dev/null"
alias create="pm install-create -i com.android.vending --bypass-low-target-sdk-block --user $user -t 2>/dev/null"
alias INSTALL="pm install -r --bypass-low-target-sdk-block -i com.android.vending --user $user -t >/dev/null"
alias create="pm install-create -i com.android.vending --bypass-low-target-sdk-block --user $user -t"
else
alias INSTALL="pm install -r -i com.android.vending --user $user -t &>/dev/null"
alias create="pm install-create -i com.android.vending --user $user -t 2>/dev/null"
alias INSTALL="pm install -r -i com.android.vending --user $user -t >/dev/null"
alias create="pm install-create -i com.android.vending --user $user -t"
fi
fi
#settings get system system_locales
@@ -877,7 +891,7 @@ case $Lo in
*) echoRgb "$conf_path Lo=$Lo填寫錯誤正確值0 1 2" "0" && exit 2 ;;
esac
if [[ $update = true ]]; then
json="$(down "$Language" 2>/dev/null)"
json="$(down "$Language")"
else
echoRgb "自動更新被關閉" "0"
fi
@@ -895,7 +909,7 @@ if [[ $json != "" ]]; then
if [[ $(expr "$(echo "$backup_version" | tr -d "a-zA-Z")" \> "$(echo "$download" | tr -d "a-zA-Z")") -eq 0 ]]; then
echoRgb "發現新版本:$tag"
if [[ $update = true ]]; then
echoRgb "$(ts "更新日誌:\n$(down "$Language" | jq -r '.body' 2>/dev/null)")"
echoRgb "$(ts "更新日誌:\n$(down "$Language" | jq -r '.body')")"
case $Lo in
0|1)
echoRgb "是否更新腳本?\n -音量上更新,音量下不更新" "2"
@@ -1028,11 +1042,11 @@ kill_app() {
process_Information="$(Process_Information "$name2")"
if [[ $name2 != bin.mt.plus && $name2 != com.termux && $name2 != bin.mt.plus.canary ]]; then
if [[ $process_Information != "" ]]; then
am force-stop --user "$user" "$name2" &>/dev/null
echo "$process_Information" | xargs -r kill -9
pkill -9 -f "$name2$|$name2[:/_]"
killall -9 "$name2" &>/dev/null
am force-stop --user "$user" "$name2" &>/dev/null
am kill "$name2" &>/dev/null
#killall -9 "$name2" &>/dev/null
#am kill "$name2" &>/dev/null
echoRgb "殺死$name1進程"
fi
fi
@@ -1430,7 +1444,7 @@ installapk() {
;;
*)
echoRgb "恢復split apk" "2"
b="$(create 2>/dev/null | egrep -o '[0-9]+')"
b="$(create | egrep -o '[0-9]+')"
if [[ -f $TMPDIR/nmsl.apk ]]; then
INSTALL "$TMPDIR/nmsl.apk"
echo_log "nmsl.apk安裝"
@@ -1439,7 +1453,7 @@ installapk() {
pm install-write "$b" "${apk##*/}" "$apk" </dev/null >/dev/null
echo_log "${apk##*/}安裝"
done
pm install-commit "$b" &>/dev/null
pm install-commit "$b" >/dev/null
echo_log "split Apk安裝"
;;
esac
@@ -1458,6 +1472,10 @@ disable_verify() {
settings put global upload_apk_enable 0 2>/dev/null
echoRgb "PLAY安全驗證為開啟狀態已被腳本關閉防止apk安裝失敗" "3"
fi
# 額外安全性攔截
settings put global harmful_app_warning_on 0 2>/dev/null
# 關閉應用的受限模式 (針對 Android 13/14 側載應用)
settings put secure enhanced_confirmation_states 0 2>/dev/null
# 設定檔案路徑
FILE="/data/data/com.android.vending/shared_prefs/finsky.xml"
if [[ -f $FILE ]]; then
@@ -1623,6 +1641,7 @@ get_name(){
Enter_options "確認列表無誤後輸入1刪除輸入0退出腳本編輯列表" "刪除" "退出腳本" && isBoolean "$parameter" "Delete_App" && Delete_App="$nsx" ;;
esac
if [[ $Delete_App = true ]]; then
echoRgb "警告 即將刪除未安裝應用資料夾,請再三確認後在執行" "0"
i=1
r="$(echo "$delete_app" | awk 'NF != 0 { count++ } END { print count }')"
while [[ $i -le $r ]]; do
@@ -1960,7 +1979,6 @@ backup() {
fi
[[ $backup_media = false ]] && echoRgb "當前$MODDIR_NAME/backup_settings.conf的\n -backup_media=0將不備份自定義資料夾" "0"
txt2="$Backup/appList.txt"
txt2="${txt2/'/storage/emulated/'/'/data/media/'}"
txt_path2="$txt2"
[[ ! -f $txt2 ]] && echo "#不需要恢復還原的應用請在開頭使用#注釋 比如:#酷安 com.coolapk.market">"$txt2"
txt2="$(cat "$txt2")"
@@ -1992,7 +2010,11 @@ backup() {
keyboard="$(settings get secure default_input_method 2>/dev/null)"
Set_screen_pause_seconds on
[[ $txt != "" ]] && [[ $(echo "$txt" | cut -d' ' -f2 | grep -w "^${keyboard%/*}$") != ${keyboard%/*} ]] && unset keyboard
ssaid_info="$(get_ssaid "$(echo "$txt" | awk '{printf "%s ", $2}')")"
if [[ -f ${0%/*}/app_details.json ]]; then
ssaid_info="$(get_ssaid "$(jq -r '.[] | select(.PackageName != null).PackageName' "${0%/*}/app_details.json")")"
else
ssaid_info="$(get_ssaid "$(echo "$txt" | awk '{printf "%s ", $2}')")"
fi
starttime1="$(date -u "+%s")"
TIME="$starttime1"
notification "101" "開始備份"
@@ -2252,7 +2274,7 @@ Restore() {
[[ $setDisplayPowerMode = "" ]] && {
Enter_options "應用恢復時關閉螢幕\n -輸入1關閉0不關閉" "關閉" "不關閉" && isBoolean "$parameter" "setDisplayPowerMode" && setDisplayPowerMode="$nsx"
} || {
isBoolean "$recovery_mode" "recovery_mode" && recovery_mode="$nsx"
isBoolean "$setDisplayPowerMode" "setDisplayPowerMode" && setDisplayPowerMode="$nsx"
}
Get_user="$(echo "$MODDIR" | rev | cut -d '/' -f1 | cut -d '_' -f1 | rev | egrep -o '[0-9]+')"
[[ $Get_user != $user ]] && {
@@ -2518,6 +2540,69 @@ Restore3() {
endtime 1 "恢復結束"
notification "108" "Media恢復完成 $(endtime 1 "Media恢復")"
}
Restore4() {
if [[ $ssaid_mode_1 = true ]]; then
while read; do
if [[ $(jq -r '.[] | select(.Ssaid != null).Ssaid' "$REPLY") != "" ]]; then
ChineseName="$(jq -r 'to_entries[] | select(.key != null).key' "$REPLY" | head -n 1)"
PackageName="$(jq -r '.[] | select(.PackageName != null).PackageName' "$REPLY")"
if [[ $ssaid_name = "" ]]; then
ssaid_name="$ChineseName $PackageName"
else
ssaid_name="$ssaid_name\n$ChineseName $PackageName"
fi
fi
done<<<"$(find "$MODDIR" -maxdepth 2 -name "app_details.json" -type f 2>/dev/null | sort)"
[[ $ssaid_name != "" ]] && txt="$ssaid_name"
i=1
[[ $(echo "$txt") != "" ]] && txt="$(echo "$txt" | sed -e '/^$/d')"
r="$(echo "$txt" | awk 'NF != 0 { count++ } END { print count }')"
while [[ $i -le $r ]]; do
name1="$(echo "$txt" | sed -n "${i}p" | cut -d' ' -f1)"
name2="$(echo "$txt" | sed -n "${i}p" | cut -d' ' -f2)"
Backup_folder="$MODDIR/$name1"
if [[ -f "$Backup_folder/app_details.json" ]]; then
app_details="$Backup_folder/app_details.json"
apk_version="$(jq -r '.[] | select(.apk_version != null).apk_version' "$app_details")"
else
echoRgb "$Backup_folder/app_details.json不存在" "0"
fi
[[ $name2 = "" ]] && echoRgb "應用包名獲取失敗" "0" && exit 1
if [[ $(pm list packages --user "$user" | awk -v pkg="$name2" -F':' '$2 == pkg {print $2}') != "" ]]; then
[[ $name2 != *mt* ]] && {
kill_app
Ssaid="$(jq -r '.[] | select(.Ssaid != null).Ssaid' "$app_details")"
if [[ $Ssaid != "" ]]; then
SSAID_Package="$(echo "$name1 $name2 $Ssaid")"
SSAID_Package2="$(echo "$SSAID_Package\n$SSAID_Package2")"
unset Ssaid
fi
}
fi
if [[ $i = $r ]]; then
[[ $SSAID_Package2 != "" ]] && {
echoRgb "開始恢復saaid" "0"
set_ssaid "$(echo "$SSAID_Package2" | awk '{printf "%s %s ", $2, $3}')"
ssaid_info="$(get_ssaid "$(echo "$SSAID_Package2" | awk '{printf "%s ", $2}')")"
echo "$SSAID_Package2" | while read; do
Ssaid="$(echo "$REPLY" | cut -d' ' -f3)"
name1="$(echo "$REPLY" | cut -d' ' -f1)"
name2="$(echo "$REPLY" | cut -d' ' -f2)"
if [[ $(awk -v pkg="$name2" '$1 == pkg {print $2}'<<<"$ssaid_info") = $Ssaid ]]; then
echoRgb "$name1 SSAID恢復成功" "1"
else
echoRgb "$name1 SSAID恢復失敗" "0"
fi
unset Ssaid
done
echoRgb "SSAID恢復後必須重新開機套用,否則應用閃退,如果沒有應用恢復ssaid則無須重啟" "0"
notification "107" "SSAID恢復後必須重新開機套用,否則應用閃退,如果沒有應用恢復ssaid則無須重啟"
}
fi
let i++
done
fi
}
Getlist() {
case $MODDIR in
/storage/emulated/0/Android/* | /data/media/0/Android/* | /sdcard/Android/*) echoRgb "請勿在$MODDIR內生成列表" "0" && exit 2 ;;
@@ -2551,7 +2636,7 @@ Getlist() {
rgb_a=118
starttime1="$(date -u "+%s")"
echoRgb "提示! 腳本默認會屏蔽預裝應用 如需備份請添加預裝應用白名單" "0"
Apk_info="$(appinfo "system|user|xposed" "label|pkgName|flag" | egrep -v 'ice.message|com.topjohnwu.magisk')"
Apk_info="$(appinfo "system|user|xposed" "label|pkgName|flag" | egrep -v 'ice.message|com.topjohnwu.magisk' | tr '/:' '_')"
xposed_name="$(echo "$Apk_info" | awk '$3 == "xposed" {print $2}')"
TARGET_PACKAGES="$(echo "$system" | paste -sd'|' - | sed 's/^|//')"
Pre_installed_apps="$(echo "$Apk_info" | awk '$3 == "system" {print $1, $2}' | egrep -w "$TARGET_PACKAGES")"
@@ -2813,21 +2898,23 @@ else
)
elif [[ -f $MODDIR/restore_settings.conf ]]; then
steps=(
"重新生成應用列表"
"恢復備份"
"僅恢復包含ssaid應用"
"僅恢復包含ssaid應用(含數據)"
"僅恢復包含ssaid應用(不含數據)"
"恢復自定義資料夾"
"恢復wifi"
"重新生成應用列表"
"壓縮檔完整性檢查"
"轉換文件夾名稱"
"殺死運行中腳本"
)
commands=(
"dumpname"
"Restore"
"ssaid_mode=true && Restore"
"ssaid_mode_1=true && Restore4"
"Restore3"
"recover_wifi \"$MODDIR/wifi\""
"dumpname"
"check_file"
"convert"
"echoRgb '等待腳本停止中,請稍後.....' && echoRgb '腳本終止'; exit"

Binary file not shown.