Script for reliably and quickly downloading Xcode on a poor connection

It's a small thing, but I wanted to post a script I wrote to reliably download Xcode during this year spent travelling - where unreliable and slow connections have been the norm.

The problems I was experiencing were the frequent dropped connections, where Safari would inevitably fail to download the huge Xcode zip, or if not then a slow connection which would just take forever.

Downloading these in the browser was just not cutting it - Safari couldn't resume broken downloads, and also allowed only a single download connection which didn't really utilise the fluctuating bandwidth availability very well.

Remembering the days of dial-up and the download managers which were popular then to download files resumably, split into several pieces, I tried a few different Mac clients (Folx, iGetter etc), but unfortunately they weren't compatible with developer.apple.com's CDN, who's authentication depends on a certain browser cookie being present.

What would be great is if Apple ran a bittorrent server for distributing these huge files ☁️🤔. Maybe one day.

Time to roll our sleeves up, then. I discovered I could use the command-line tool aria2c (also available from homebrew) to download reliably from Apple's CDN using multiple connections, with the tool automatically retrying and resuming the download if the connection died.

All that's necessary is to deduce the ADCDownloadAuth cookie which Apple's CDN expects to be given. Initially I used Charles Proxy to sniff this from the Safari connection (which I'd then just cancel), to see cookies were sent. But it's easier than that - we can just use Safari's Cookie browser to pull it out:

Safari's Cookie Inspector (Develop -> Show Page Resources -> Storage)

Safari's Cookie Inspector (Develop -> Show Page Resources -> Storage)

Here's the script I came up with which asks for the URL to be downloaded and the ADCDownloadAuth cookie, and then kicks off aria2c to download your file:

#!/usr/bin/env ruby

print "What is the URL of your Apple Downloads resource?\nURL:"
url = gets.strip

print "What is the ADCDownloadAuth cookie token:\nADCDownloadAuth: "
token = gets.strip

command = "aria2c --header \"Host: adcdownload.apple.com\" --header \"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\" --header \"Upgrade-Insecure-Requests: 1\" --header \"Cookie: ADCDownloadAuth=#{token}\" --header \"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B72 Safari/602.1\" --header \"Accept-Language: en-us\" -x 16 -s 16 #{url} -d ~/Downloads"

exec(command)

Gist: https://gist.github.com/iandundas/fabe07455e5216442a421922361f698c