From 6b48ee127ddee2ad4844345122b749172de696a4 Mon Sep 17 00:00:00 2001 From: aclist <92275929+aclist@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:45:39 +0900 Subject: [PATCH] chore: backports into stable --- CHANGELOG.md | 6 ++++++ dzgui.sh | 37 ++++++++++++++++++----------------- helpers/funcs | 53 +++++++++++++++++++++++++++++++++++++++++---------- install.sh | 47 ++++++++++++++++++++++++++++++++++++++------- 4 files changed, 109 insertions(+), 34 deletions(-) mode change 100644 => 100755 install.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index b7156b8..67c7234 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [5.5.3] 2024-12-13 +### Fixed +- Add remote resource health checks before downloading updates +### Added +- Add fallback repository + ## [5.5.1] 2024-12-03 ### Fixed - Support localized decimal separators when parsing installed mod sizes diff --git a/dzgui.sh b/dzgui.sh index 144f874..a9afd95 100755 --- a/dzgui.sh +++ b/dzgui.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -o pipefail -version=5.5.2 +version=5.5.3 #CONSTANTS aid=221100 @@ -314,8 +314,8 @@ check_unmerged(){ check_version(){ local version_url=$(format_version_url) local upstream=$(curl -Ls "$version_url" | awk -F= '/^version=/ {print $2}') - #2024-12-12: do not clobber local version if unreachable - [[ -z $upstream ]] && return + local res=$(get_response_code "$version_url") + [[ $res -ne 200 ]] && raise_error_and_quit "Remote resource unavailable: '$version_url'" logger INFO "Local branch: '$branch', local version: $version" if [[ $branch == "stable" ]]; then version_url="$stable_url/dzgui.sh" @@ -377,11 +377,7 @@ dl_changelog(){ local md [[ $branch == "stable" ]] && mdbranch="dzgui" [[ $branch == "testing" ]] && mdbranch="testing" - if [[ $remote_host == "gh" ]]; then - md="https://raw.githubusercontent.com/$author/$repo/${mdbranch}/CHANGELOG.md" - else - md="https://codeberg.org/$author/$repo/raw/branch/${mdbranch}/CHANGELOG.md" - fi + local md="$url_prefix/${mdbranch}/$file" curl -Ls "$md" > "$state_path/CHANGELOG.md" } test_display_mode(){ @@ -523,6 +519,7 @@ get_hash(){ md5sum "$1" | awk '{print $1}' } fetch_a2s(){ + # this file is currently monolithic [[ -d $helpers_path/a2s ]] && { logger INFO "A2S helper is current"; return 0; } local sha=c7590ffa9a6d0c6912e17ceeab15b832a1090640 local author="yepoleb" @@ -530,6 +527,8 @@ fetch_a2s(){ local url="https://github.com/$author/$repo/tarball/$sha" local prefix="${author^}-$repo-${sha:0:7}" local file="$prefix.tar.gz" + local res=$(get_response_code "$url") + [[ $res -ne 200 ]] && raise_error_and_quit "Remote resource unavailable: '$file'" curl -Ls "$url" > "$helpers_path/$file" tar xf "$helpers_path/$file" -C "$helpers_path" "$prefix/a2s" --strip=1 rm "$helpers_path/$file" @@ -546,6 +545,8 @@ fetch_dzq(){ local author="yepoleb" local repo="dayzquery" local url="https://raw.githubusercontent.com/$author/$repo/$sha/dayzquery.py" + local res=$(get_response_code "$url") + [[ $res -ne 200 ]] && raise_error_and_quit "Remote resource unavailable: 'dayzquery.py'" curl -Ls "$url" > "$file" logger INFO "Updated DZQ to sha '$sha'" } @@ -577,14 +578,14 @@ fetch_helpers_by_sum(){ [[ -f "$config_file" ]] && source "$config_file" declare -A sums sums=( - ["ui.py"]="be3da1e542d14105f4358dd38901e25a" + ["ui.py"]="dd7aa34df1d374739127cca3033a3f67" ["query_v2.py"]="55d339ba02512ac69de288eb3be41067" ["vdf2json.py"]="2f49f6f5d3af919bebaab2e9c220f397" - ["funcs"]="8aa9eac96c21a3aabbf73c44b643523b" + ["funcs"]="6d985948a3b9b71017c4e0c5f7f55fe0" ["lan"]="c62e84ddd1457b71a85ad21da662b9af" ) local author="aclist" - local repo="dzgui" + local repo="dztui" local realbranch local file local sum @@ -605,16 +606,14 @@ fetch_helpers_by_sum(){ sum="${sums[$i]}" full_path="$helpers_path/$file" - if [[ $remote_host == "gh" ]]; then - url="https://raw.githubusercontent.com/$author/$repo/$realbranch/helpers/$file" - else - url="https://codeberg.org/$author/$repo/raw/branch/$realbranch/helpers/$file" - fi + url="${url_prefix}/$realbranch/helpers/$file" if [[ -f "$full_path" ]] && [[ $(get_hash "$full_path") == $sum ]]; then logger INFO "$file is current" else logger WARN "File '$full_path' checksum != '$sum'" + local res=$(get_response_code "$url") + [[ $res -ne 200 ]] && raise_error_and_quit "Remote resource unavailable: '$url'" curl -Ls "$url" > "$full_path" if [[ ! $? -eq 0 ]]; then raise_error_and_quit "Failed to fetch the file '$file'. Possible timeout?" @@ -635,11 +634,15 @@ fetch_geo_file(){ local km_sum="b038fdb8f655798207bd28de3a004706" local gzip="$helpers_path/ips.csv.gz" if [[ ! -f $geo_file ]] || [[ $(get_hash $geo_file) != $geo_sum ]]; then + local res=$(get_response_code "$geo_file_url") + [[ $res -ne 200 ]] && raise_error_and_quit "Remote resource unavailable: '$geo_file_url'" curl -Ls "$geo_file_url" > "$gzip" #force overwrite gunzip -f "$gzip" fi if [[ ! -f $km_helper ]] || [[ $(get_hash $km_helper) != $km_sum ]]; then + local res=$(get_response_code "$km_helper_url") + [[ $res -ne 200 ]] && raise_error_and_quit "Remote resource unavailable: '$km_helper_url'" curl -Ls "$km_helper_url" > "$km_helper" chmod +x "$km_helper" fi @@ -887,10 +890,10 @@ test_connection(){ res=$(get_response_code "${hr["codeberg.org"]}") [[ $res -ne 200 ]] && raise_error_and_quit "$str (${hr["codeberg.org"]})" fi + logger INFO "Set remote host to '${hr["codeberg.org"]}'" if [[ $remote_host == "cb" ]]; then url_prefix="https://codeberg.org/$author/$repo/raw/branch" releases_url="https://codeberg.org/$author/$repo/releases/download/browser" - # 2024-12-12: interpolate variables again stable_url="$url_prefix/dzgui" testing_url="$url_prefix/testing" km_helper_url="$releases_url/latlon" diff --git a/helpers/funcs b/helpers/funcs index 2f5875d..e8b9322 100755 --- a/helpers/funcs +++ b/helpers/funcs @@ -67,18 +67,12 @@ game_dir="$steam_path/steamapps/common/DayZ" #URLS author="aclist" repo="dztui" -#TODO: this is hardcoded -#2024-12-12 gh_prefix="https://github.com" issues_url="$gh_prefix/$author/$repo/issues" -url_prefix="https://codeberg.org/$author/$repo/raw/branch" +url_prefix="https://raw.githubusercontent.com/$author/$repo" stable_url="$url_prefix/dzgui" testing_url="$url_prefix/testing" -releases_url="https://codeberg.org/$author/$repo/releases/download/browser" -km_helper_url="$releases_url/latlon" -db_file="$releases_url/ips.csv.gz" -sums_url="$stable_url/helpers/sums.md5" -#TODO: move adoc to index +releases_url="$gh_prefix/$author/$repo/releases/download/browser" help_url="https://$author.github.io/dzgui/dzgui" forum_url="$gh_prefix/$author/$repo/discussions" sponsor_url="$gh_prefix/sponsors/$author" @@ -750,7 +744,45 @@ format_version_url(){ esac echo "$version_url" } +get_response_code(){ + local url="$1" + curl -Ls -I -o /dev/null -w "%{http_code}" "$url" +} +test_connection(){ + source "$config_file" + declare -A hr + local res1 + local res2 + local str="No connection could be established to the remote server" + hr=( + ["github.com"]="https://github.com/$author" + ["codeberg.org"]="https://codeberg.org/$author" + ) + res=$(get_response_code "${hr["github.com"]}") + if [[ $res -ne 200 ]]; then + logger WARN "Remote host '${hr["github.com"]}' unreachable', trying fallback" + remote_host=cb + res=$(get_response_code "${hr["codeberg.org"]}") + if [[ $res -ne 200 ]]; then + return 1 + fi + fi + logger INFO "Set remote host to '${hr["codeberg.org"]}'" + if [[ $remote_host == "cb" ]]; then + url_prefix="https://codeberg.org/$author/$repo/raw/branch" + releases_url="https://codeberg.org/$author/$repo/releases/download/browser" + stable_url="$url_prefix/dzgui" + testing_url="$url_prefix/testing" + fi +} download_new_version(){ + test_connection + rc=$? + if [[ $rc -eq 1 ]]; then + printf "Remote resource unavailable. Aborting" + logger WARN "Remote resource unavailable" + return 1 + fi local version_url="$(format_version_url)" mv "$src_path" "$src_path.old" curl -Ls "$version_url" > "$src_path" @@ -775,7 +807,7 @@ dl_changelog(){ local file="CHANGELOG.md" [[ $branch == "stable" ]] && mdbranch="dzgui" [[ $branch == "testing" ]] && mdbranch="testing" - local md="https://codeberg.org/$author/$repo/raw/branch/${mdbranch}/$file" + local md="$url_prefix/${mdbranch}/$file" curl -Ls "$md" > "$state_path/$file" } toggle(){ @@ -790,6 +822,7 @@ toggle(){ fi update_config download_new_version + [[ $? -eq 1 ]] && return 1 return 255 ;; Toggle[[:space:]]mod[[:space:]]install[[:space:]]mode) @@ -853,7 +886,7 @@ remove_from_favs(){ break fi done - if [[ ${#ip_list} -gt 0 ]]; then + if [[ ${#ip_list[@]} -gt 0 ]]; then readarray -t ip_list < <(printf "%s\n" "${ip_list[@]}") fi update_config diff --git a/install.sh b/install.sh old mode 100644 new mode 100755 index 22ec6ab..13f96cc --- a/install.sh +++ b/install.sh @@ -1,8 +1,41 @@ #!/usr/bin/env bash -curl -L "https://codeberg.org/aclist/dzgui/raw/branch/dzgui/dzgui.sh" > dzgui.sh -chmod +x dzgui.sh -xdg_file="$HOME/.local/share/applications/dzgui.desktop" -share="$HOME/.local/share/dzgui" -[[ -f $xdg_file ]] && rm $xdg_file -[[ -d $share ]] && rm -rf "$share" -./dzgui.sh +get_response_code(){ + local url="$1" + curl -Ls -I -o /dev/null -w "%{http_code}" "$url" +} +abort(){ + printf "Remote resource not available. Try again later.\n" + exit 1 +} +fetch(){ + local file="dzgui.sh" + local author="aclist" + local repo="dztui" + local branch="dzgui" + local url + local res + gh_url="https://raw.githubusercontent.com/$author/$repo/$branch/$file" + cb_url="https://codeberg.org/$author/$repo/raw/branch/$branch/$file" + + url="$gh_url" + printf "Checking the remote resource at '%s'\n" "$url" + res=$(get_response_code "$url") + if [[ $res -ne 200 ]]; then + url="$cb_url" + printf "Checking the remote resource at '%s'\n" "$url" + res=$(get_response_code "$url") + if [[ $res -ne 200 ]]; then + abort + fi + fi + + curl -L "$url" > dzgui.sh + chmod +x dzgui.sh + xdg_file="$HOME/.local/share/applications/dzgui.desktop" + share="$HOME/.local/share/dzgui" + [[ -f $xdg_file ]] && rm $xdg_file + [[ -d $share ]] && rm -rf "$share" + ./dzgui.sh +} + +fetch