]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Refactored download
authorFrank Brehm <frank@brehm-online.com>
Mon, 19 Apr 2021 11:58:19 +0000 (13:58 +0200)
committerFrank Brehm <frank@brehm-online.com>
Mon, 19 Apr 2021 11:58:19 +0000 (13:58 +0200)
bin/update-minecraft-server-jar

index 30282abad8c0128fe41177379e02370676ef731d..692ad2f16c16c317078a78e1b3a6285f779213a0 100755 (executable)
@@ -29,9 +29,10 @@ MC_GROUP="users"
 
 VERSION_MANIFEST='https://launchermeta.mojang.com/mc/game/version_manifest.json'
 
-CURRENT_VERSION=
+CURRENT_VERSION="<unknown>"
 UPSTREAM_VERSION=
 DOWNLOAD_URL=
+SHA1_SUM=
 TARGET_JAR=
 
 DESCRIPTION=$( cat <<-EOF
@@ -104,7 +105,7 @@ check_preferences() {
 
     check_for_root
 
-    local -a tools=('curl' 'jq')
+    local -a tools=('curl' 'jq' 'sha1sum')
     local tool=
     local folder=
 
@@ -181,13 +182,35 @@ get_upstream_info() {
     fi
     debug "Current snapshot id: '${CYAN}${snapshot}${NORMAL}'."
 
-    DOWNLOAD_URL=$( echo "${upstream_manifest}" | jq -r --arg VERSION_TARGET "${snapshot}" '.versions | .[] | select(.id==$VERSION_TARGET) | .url' - )
-    if [[ -z "${DOWNLOAD_URL=}" ]] ; then
-        error "Could not evaluate download URL of snapshot id '${CYAN}${snapshot}${NORMAL}' from manifest."
+    local release_manifest_url=$( echo "${upstream_manifest}" | jq -r --arg VERSION_TARGET "${snapshot}" '.versions | .[] | select(.id==$VERSION_TARGET) | .url' - )
+    if [[ -z "${release_manifest_url}" ]] ; then
+        error "Could not evaluate release manifest URL of snapshot id '${CYAN}${snapshot}${NORMAL}' from manifest."
         exit 7
     fi
+    debug "Release manifest URL: '${CYAN}${release_manifest_url}${NORMAL}'."
+
+    local release_manifest=$( curl -s "${release_manifest_url}" )
+    if [[ -z "${release_manifest}" ]] ; then
+        error "Did not get a valid release manifest from '${CYAN}${release_manifest_url}${NORMAL}'."
+        exit 8
+    fi
+
+    DOWNLOAD_URL=$( echo "${release_manifest}" | jq -r .downloads.server.url )
+    if [[ -z "${DOWNLOAD_URL}" ]] ; then
+        error "Could not evaluate download URL from release manifest."
+        exit 9
+    fi
     debug "Download URL: '${CYAN}${DOWNLOAD_URL}${NORMAL}'."
+
+    SHA1_SUM=$( echo "${release_manifest}" | jq -r .downloads.server.sha1 )
+    if [[ -z "${SHA1_SUM}" ]] ; then
+        error "Could not evaluate SHA1 sum of jar file from release manifest."
+        exit 9
+    fi
+    debug "SHA1 sum of jar file: '${CYAN}${SHA1_SUM}${NORMAL}'."
+
     TARGET_JAR="minecraft_server.${UPSTREAM_VERSION}.jar"
+    debug "Target jar file: '${CYAN}${TARGET_JAR}${NORMAL}'."
 
 }
 
@@ -212,6 +235,23 @@ do_download() {
 
 }
 
+#------------------------------------------------------------------------------
+check_sha1() {
+
+    info "Checking SHA1 sum integrety of '${CYAN}${TARGET_JAR}${NORMAL}' ..."
+
+    local cur_sha1sum=$( sha1sum "${TARGET_JAR}" | awk '{print $1}' )
+    debug "SHA1 sum of current file: '${cur_sha1sum}'."
+
+    if [[ "${cur_sha1sum}" != "${SHA1_SUM}" ]] ; then
+        error "SHA1 sums of local jar file and upstream are not matching."
+        return 10
+    fi
+    debug "SHA1 sums of local jar file and upstream are matching."
+    return 0
+
+}
+
 #------------------------------------------------------------------------------
 main() {
 
@@ -221,11 +261,15 @@ main() {
     check_preferences
     detecting_cur_version
     get_upstream_info
+    if [[ -f "${TARGET_JAR}" ]] ; then
+        check_sha1
+    fi
     if [[ "${UPSTREAM_VERSION}" == "${CURRENT_VERSION}" ]] ; then
         info "Version '${GREEN}${UPSTREAM_VERSION}${NORMAL}' not changed, no update."
         exit 0
     fi
     do_download
+    check_sha1
 
 }