feat: Refactor download code

This commit is contained in:
Kroese 2024-05-16 21:47:33 +02:00 committed by GitHub
parent 3aee40f160
commit 87e56fe0e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -77,12 +77,24 @@ download_windows() {
local sku_id=""
local iso_download_link_html=""
local iso_download_link=""
local windows_version=""
echo "Downloading Windows ${RELEASE} (${I18N})"
# Download newer consumer Windows versions from behind gated Microsoft API
case "${id,,}" in
"win11${PLATFORM,,}" )
windows_version="11"
;;
"win10${PLATFORM,,}" )
windows_version="10"
;;
"win81${PLATFORM,,}" )
windows_version="8"
;;
* )
echo "Unknown version: $id"
return 1
esac
# Either 8, 10, or 11
local windows_version="$1"
echo "Downloading $id"
local url="https://www.microsoft.com/en-us/software-download/windows$windows_version"
case "$windows_version" in
@ -145,24 +157,14 @@ download_windows() {
# --referer: Required by Microsoft servers to allow request
iso_download_link_html="$(curl --silent --request POST --user-agent "$user_agent" --data "" --referer "$url" --header "Accept:" --max-filesize 100K --fail --proto =https --tlsv1.2 --http1.1 -- "https://www.microsoft.com/en-US/api/controls/contentinclude/html?pageId=6e2a1789-ef16-4f27-a296-74ef7ef5d96b&host=www.microsoft.com&segments=software-download,$url_segment_parameter&query=&action=GetProductDownloadLinksBySku&sessionId=$session_id&skuId=$sku_id&language=English&sdVersion=2")"
local failed=0
if ! [ "$iso_download_link_html" ]; then
# This should only happen if there's been some change to how this API works
echo " - Microsoft servers gave us an empty response to our request for an automated download."
failed=1
return 1
fi
if echo "$iso_download_link_html" | grep -q "We are unable to complete your request at this time."; then
echo " - WARNING! Microsoft blocked the automated download request based on your IP address."
failed=1
fi
if [ ${failed} -eq 1 ]; then
echo " Manually download the Windows ${windows_version} ISO using a web browser from: ${url}"
echo " Save the downloaded ISO to: $(realpath "${VM_PATH}")"
echo " Update the config file to reference the downloaded ISO: ./${VM_PATH}.conf"
echo " Continuing with the VM creation process..."
return 1
fi
@ -173,33 +175,57 @@ download_windows() {
if ! [ "$iso_download_link" ]; then
# This should only happen if there's been some change to the download endpoint web address
echo " - Microsoft servers gave us no download link to our request for an automated download. Please manually download this ISO in a web browser: $url"
echo " - Microsoft servers gave us no download link to our request for an automated download!"
return 1
fi
echo " - URL: ${iso_download_link%%\?*}"
# Download ISO
FILE_NAME="$(echo "$iso_download_link" | cut -d'?' -f1 | cut -d'/' -f5)"
web_get "${iso_download_link}" "${VM_PATH}" "${FILE_NAME}"
MIDO_URL="$iso_download_link"
return 0
}
download_windows_server() {
local iso_download_page_html=""
local id="$1"
local windows_version=""
local enterprise_type=""
# Download enterprise evaluation Windows versions
local windows_version="$1"
local enterprise_type="$2"
local PRETTY_RELEASE=""
case "${RELEASE}" in
"10-ltsc") PRETTY_RELEASE="10 LTSC";;
"2012-r2") PRETTY_RELEASE="2012 R2";;
*) PRETTY_RELEASE="${RELEASE}";;
case "${id,,}" in
"win11${PLATFORM,,}-enterprise-eval" )
windows_version="windows-11-enterprise"
enterprise_type="enterprise"
;;
"win10${PLATFORM,,}-enterprise-eval" )
windows_version="windows-10-enterprise"
enterprise_type="enterprise"
;;
"win10${PLATFORM,,}-enterprise-ltsc-eval" )
windows_version="windows-10-enterprise"
enterprise_type="ltsc"
;;
"win2022-eval" )
windows_version="windows-server-2022"
enterprise_type="server"
;;
"win2019-eval" )
windows_version="windows-server-2019"
enterprise_type="server"
;;
"win2016-eval" )
windows_version="windows-server-2016"
enterprise_type="server"
;;
"win2012r2-eval" )
windows_version="windows-server-2012-r2"
enterprise_type="server"
;;
* )
echo "Unknown version: $id"
return 1
esac
echo "Downloading $(pretty_name "${OS}") ${PRETTY_RELEASE} (${I18N})"
local iso_download_page_html=""
echo "Downloading $id"
local url="https://www.microsoft.com/en-us/evalcenter/download-$windows_version"
@ -248,44 +274,39 @@ download_windows_server() {
# Limit untrusted size for input validation
iso_download_link="$(echo "$iso_download_link" | head -c 1024)"
echo " - URL: $iso_download_link"
# Download ISO
FILE_NAME="${iso_download_link##*/}"
web_get "${iso_download_link}" "${VM_PATH}" "${FILE_NAME}"
OS="windows-server"
MIDO_URL="$iso_download_link"
return 0
}
getWindows() {
local id="$1"
MIDO_URL=""
case "${id,,}" in
"win11${PLATFORM,,}" )
"win81${PLATFORM,,}" | "win10${PLATFORM,,}" | "win11${PLATFORM,,}" )
download_windows "$id" && return 0
;;
"win11${PLATFORM,,}-enterprise-eval" )
return 1
;;
"win10${PLATFORM,,}" )
download_windows "$id" && return 0
;;
"win10${PLATFORM,,}-enterprise-eval" )
return 1
;;
"win10${PLATFORM,,}-enterprise-ltsc-eval" )
# download_windows_server windows-10-enterprise ltsc
return 1
;;
"win81${PLATFORM,,}" )
download_windows "$id" && return 0
;;
"win81${PLATFORM,,}-enterprise-eval" )
return 1
;;
"win2022-eval" | "win2019-eval" | "win2016-eval" | "win2012r2-eval" | "win2008r2" )
download_windows_server "$id" && return 0
;;
"win10${PLATFORM,,}-enterprise-eval" | "win10${PLATFORM,,}-enterprise-ltsc-eval" )
download_windows_server "$id" && return 0
;;
"win2022-eval" | "win2019-eval" | "win2016-eval" | "win2012r2-eval" )
download_windows_server "$id" && return 0
;;
"win81${PLATFORM,,}-enterprise-eval" )
MIDO_URL="https://download.microsoft.com/download/B/9/9/B999286E-0A47-406D-8B3D-5B5AD7373A4A/9600.17050.WINBLUE_REFRESH.140317-1640_X64FRE_ENTERPRISE_EVAL_EN-US-IR3_CENA_X64FREE_EN-US_DV9.ISO"
return 0
;;
"win2008r2" )
MIDO_URL="https://download.microsoft.com/download/4/1/D/41DEA7E0-B30D-4012-A1E3-F24DC03BA1BB/7601.17514.101119-1850_x64fre_server_eval_en-us-GRMSXEVAL_EN_DVD.iso"
return 0
;;
* )
echo "Unknown version: $id"
esac
return 1