From 52f1d1c4e5be0936d4becea9900822cdf6b27a58 Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 7 Jul 2026 22:41:58 +0200 Subject: [PATCH] feat: Improve download function --- src/mido.sh | 348 +++++++++++++++++++++++++++------------------------- 1 file changed, 184 insertions(+), 164 deletions(-) diff --git a/src/mido.sh b/src/mido.sh index 4ac0e8d05..413a28f8b 100644 --- a/src/mido.sh +++ b/src/mido.sh @@ -1,42 +1,42 @@ #!/usr/bin/env bash set -Eeuo pipefail -handle_curl_error() { +handleCurlError() { - local error_code="$1" - local server_name="$2" + local code="$1" + local server="$2" - case "$error_code" in + case "$code" in 1) error "Unsupported protocol!" ;; 2) error "Failed to initialize curl!" ;; 3) error "The URL format is malformed!" ;; 5) error "Failed to resolve address of proxy host!" ;; - 6) error "Failed to resolve $server_name servers! Is there an Internet connection?" ;; - 7) error "Failed to contact $server_name servers! Is there an Internet connection or is the server down?" ;; - 8) error "$server_name servers returned a malformed HTTP response!" ;; + 6) error "Failed to resolve $server servers! Is there an Internet connection?" ;; + 7) error "Failed to contact $server servers! Is there an Internet connection or is the server down?" ;; + 8) error "$server servers returned a malformed HTTP response!" ;; 16) error "A problem was detected in the HTTP2 framing layer!" ;; - 22) error "$server_name servers returned a failing HTTP status code!" ;; + 22) error "$server servers returned a failing HTTP status code!" ;; 23) error "Failed at writing Windows media to disk! Out of disk space or permission error?" ;; 26) error "Failed to read Windows media from disk!" ;; 27) error "Ran out of memory during download!" ;; - 28) error "Connection timed out to $server_name server!" ;; - 35) error "SSL connection error from $server_name server!" ;; + 28) error "Connection timed out to $server server!" ;; + 35) error "SSL connection error from $server server!" ;; 36) error "Failed to continue earlier download!" ;; - 52) error "Received no data from the $server_name server!" ;; - 63) error "$server_name servers returned an unexpectedly large response!" ;; + 52) error "Received no data from the $server server!" ;; + 63) error "$server servers returned an unexpectedly large response!" ;; 126) error "Curl command cannot be executed!" ;; 127) error "Curl command not found!" ;; *) - if (( error_code <= 125 )); then + if (( code <= 125 )); then # Must be some other server or network error (possibly with this specific request/file) # This is when accounting for all possible errors in the curl manual assuming a correctly formed # curl command and an HTTP(S) request, using only the curl features we're using, and a sane build. - error "Miscellaneous server or network error, reason: $error_code" + error "Miscellaneous server or network error, reason: $code" else - case "$(kill -l "$error_code" 2>/dev/null || true)" in + case "$(kill -l "$code" 2>/dev/null || true)" in INT) error "Curl was interrupted!" ;; SEGV | ABRT) error "Curl crashed! Please report any core dumps to curl developers." ;; - *) error "Curl terminated due to fatal signal $error_code !" ;; + *) error "Curl terminated due to fatal signal $code !" ;; esac fi ;; @@ -45,7 +45,7 @@ handle_curl_error() { return 1 } -get_agent() { +getAgent() { local browser_version @@ -56,41 +56,42 @@ get_agent() { return 0 } -download_windows() { +downloadWindows() { local id="$1" local lang="$2" local desc="$3" + local rc=0 - local ovw="" rticks="" mdt="" sku_id="" sku_url="" - local iso_url="" iso_json="" language="" org_id="" - local instance_id="" vls_url="" ov_url="" ov_data="" - local session_id="" user_agent="" download_type="" - local windows_version="" iso_download_link="" - local download_page_html="" product_edition_id="" - local language_skuid_json="" + local ovToken="" ovTicks="" ovTime="" + local skuId="" skuUrl="" skuJson="" + local linkUrl="" linkJson="" link="" + local language="" orgId="" + local instance="" vlsUrl="" ovUrl="" ovData="" + local session="" agent="" type="" + local winVer="" page="" productId="" local profile="606624d44113" - user_agent=$(get_agent) + agent=$(getAgent) language=$(getLanguage "$lang" "name") case "${id,,}" in - "win11x64" ) windows_version="11" && download_type="1" ;; - "win11arm64" ) windows_version="11arm64" && download_type="2" ;; + "win11x64" ) winVer="11" && type="1" ;; + "win11arm64" ) winVer="11arm64" && type="2" ;; * ) error "Invalid VERSION specified, value \"$id\" is not recognized!" && return 1 ;; esac - local url="https://www.microsoft.com/en-us/software-download/windows$windows_version" + local url="https://www.microsoft.com/en-us/software-download/windows$winVer" # uuidgen: For MacOS (installed by default) and other systems (e.g. with no /proc) that don't have a kernel interface for generating random UUIDs - if ! session_id=$(cat /proc/sys/kernel/random/uuid 2> /dev/null || uuidgen --random); then + if ! session=$(cat /proc/sys/kernel/random/uuid 2> /dev/null || uuidgen --random); then error "Failed to generate session ID!" return 1 fi - session_id="${session_id//[![:print:]]/}" + session="${session//[![:print:]]/}" - if [ -z "$session_id" ]; then + if [ -z "$session" ]; then error "Failed to generate session ID!" return 1 fi @@ -101,189 +102,189 @@ download_windows() { # Also, keeping a "$WindowsVersions" array like Fido does would be way too much of a maintenance burden # Remove "Accept" header that curl sends by default enabled "$DEBUG" && echo "Parsing download page: ${url}" - download_page_html=$(curl --silent --max-time 30 --user-agent "$user_agent" --header "Accept:" --max-filesize 1M --fail --proto =https --tlsv1.2 --http1.1 -- "$url") || { - handle_curl_error "$?" "Microsoft" + page=$(curl --silent --max-time 30 --user-agent "$agent" --header "Accept:" --max-filesize 1M --fail --proto =https --tlsv1.2 --http1.1 -- "$url") || { + handleCurlError "$?" "Microsoft" return $? } enabled "$DEBUG" && echo -n "Getting Product edition ID: " - product_edition_id=$(echo "$download_page_html" | grep -Eo '