Skip to content

Commit

Permalink
simplex-servers-update: safeguard files and add cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shumvgolove committed Jan 31, 2025
1 parent 79118fe commit e718d93
Showing 1 changed file with 93 additions and 6 deletions.
99 changes: 93 additions & 6 deletions scripts/main/simplex-servers-update
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ Proceed? (${GRN}yes${NC}/${RED}no${NC}): "
# Set CHOOSe globally and only once
choose="${CHOOSE:-}"

# Our general exit handler
cleanup() {
err=$?
rm -rf "$path_tmp_bin"
trap '' EXIT INT TERM
exit $err
}
sig_cleanup() {
trap '' EXIT # some shells will call EXIT after the INT handler
false # sets $?
cleanup
}
trap cleanup EXIT
trap sig_cleanup INT QUIT TERM

# Currently, XFTP default to v0.1.0, so it doesn't make sense to check its version

os_test() {
Expand Down Expand Up @@ -102,7 +117,12 @@ set_version() {
case "$ver" in
latest)
bin="https://github.com/simplex-chat/simplexmq/releases/latest/download"
remote_version="$(curl --proto '=https' --tlsv1.2 -sSf -L https://api.github.com/repos/simplex-chat/simplexmq/releases/latest | grep -i "tag_name" | awk -F \" '{print $4}')"
remote_version="$(curl --proto '=https' --tlsv1.2 -sSf -L https://api.github.com/repos/simplex-chat/simplexmq/releases/latest 2>/dev/null | grep -i "tag_name" | awk -F \" '{print $4}')"

if [ -z "$remote_version" ]; then
printf "Something went wrong: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi
;;
*)
bin="https://github.com/simplex-chat/simplexmq/releases/download/${ver}"
Expand All @@ -126,11 +146,59 @@ erase_lines() {
done
}

check_sanity() {
path="$1"
criteria="$2"

case "$criteria" in
string:*)
pattern="$(printf '%s' "$criteria" | awk '{print $2}')"

if grep -q "$pattern" "$path"; then
sane=0
else
sane=1
fi
;;
file:*)
pattern="$(printf '%s' "$criteria" | awk '{print $2}')"

if file "$path" | grep -q "$pattern"; then
sane=0
else
sane=1
fi
;;
*) printf 'Unknown criteria.\n'; sane=1 ;;
esac

unset path string

return "$sane"
}

update_scripts() {
curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_update" -o "$path_tmp_bin_update" && chmod +x "$path_tmp_bin_update"

if ! check_sanity "$path_tmp_bin_update" 'string: /usr/bin/env'; then
printf "Something went wrong when downloading Update script: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_uninstall" -o "$path_tmp_bin_uninstall" && chmod +x "$path_tmp_bin_uninstall"

if ! check_sanity "$path_tmp_bin_uninstall" 'string: /usr/bin/env'; then
printf "Something went wrong when downloading Uninstall script: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_stopscript" -o "$path_tmp_bin_stopscript" && chmod +x "$path_tmp_bin_stopscript"

if ! check_sanity "$path_tmp_bin_stopscript" 'string: /usr/bin/env'; then
printf "Something went wrong when downloading Update script: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

if diff -q "$path_bin_uninstall" "$path_tmp_bin_uninstall" > /dev/null 2>&1; then
printf -- "- ${YLW}Uninstall script is up-to-date${NC}.\n"
rm "$path_tmp_bin_uninstall"
Expand Down Expand Up @@ -169,6 +237,11 @@ update_systemd() {

curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_systemd" -o "$path_tmp_systemd"

if ! check_sanity "$path_tmp_systemd" 'string: [Service]'; then
printf "Something went wrong when downloading Systemd service: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

if diff -q "$path_systemd" "$path_tmp_systemd" > /dev/null 2>&1; then
printf -- "- ${YLW}%s service is up-to-date${NC}.\n" "$service"
rm "$path_tmp_systemd"
Expand Down Expand Up @@ -227,15 +300,33 @@ update_bins() {
printf "${GRN}Done!${NC}\n"

printf -- "- Updating %s to %s..." "$service" "$remote_version"
curl --proto '=https' --tlsv1.2 -sSf -L "$bin" -o "$path_bin" && chmod +x "$path_bin"

curl --proto '=https' --tlsv1.2 -sSf -L "$bin" -o "$path_bin.old" && chmod +x "$path_bin.old"
if ! check_sanity "$path_tmp_systemd" 'file: ELF'; then
printf "Something went wrong when downloading %s binary: either you don't have connection to Github or you're rate-limited.\n" "$service"
rm "$path_bin.old"
exit 1
else
mv "$path_bin.old" "$path_bin"
fi

printf "${GRN}Done!${NC}\n"

printf -- "- Starting %s service..." "$service"
systemctl start "$service"
printf "${GRN}Done!${NC}\n"
else
printf -- "- Updating %s to %s..." "$service" "$remote_version"

curl --proto '=https' --tlsv1.2 -sSf -L "$bin" -o "$path_bin" && chmod +x "$path_bin"
if ! check_sanity "$path_tmp_systemd" 'file: ELF'; then
printf "Something went wrong when downloading %s binary: either you don't have connection to Github or you're rate-limited.\n" "$service"
rm "$path_bin.old"
exit 1
else
mv "$path_bin.old" "$path_bin"
fi

printf "${GRN}Done!${NC}\n"
fi
else
Expand Down Expand Up @@ -333,8 +424,6 @@ main() {
for i in $apps; do
update_bins "$i"
done

rm -rf "$path_tmp_bin"
;;

2)
Expand All @@ -351,8 +440,6 @@ main() {
for i in $apps; do
update_bins "$i"
done

rm -rf "$path_tmp_bin"
;;

3)
Expand Down

0 comments on commit e718d93

Please sign in to comment.