Skip to content

Instantly share code, notes, and snippets.

@noplanman
Created November 21, 2016 22:44
Show Gist options
  • Save noplanman/407c7237e1acddd636380cad6ed9a60f to your computer and use it in GitHub Desktop.
Save noplanman/407c7237e1acddd636380cad6ed9a60f to your computer and use it in GitHub Desktop.
Download all VPN config files for Mullvad VPN
#!/usr/bin/env bash
# Set the Mullvad account number.
ACCOUNT_NUMBER="1234"
# For the home network, make sure the external IP is set here
# to prevent local traffic from going through the VPN.
HOME_IP=""
# Load the config download page to get CSRF tokens.
OUTPUT=$(curl "https://mullvad.net/download/config/" --compressed -s -i)
CSRF_TOKEN=$(echo ${OUTPUT} | grep -o 'csrftoken=\w*' | cut -d '=' -f 2)
CSRF_MIDDLEWARE_TOKEN=$(echo ${OUTPUT} | grep -o 'name=.csrfmiddlewaretoken. value=.\w*.' | head -1 | cut -d ' ' -f 2 | cut -d '=' -f 2 | sed "s/'//g")
COOKIE="csrftoken=${CSRF_TOKEN}"
# Get the country options from the output.
COUNTRIES="$(echo ${OUTPUT} | \
sed -n -E 's/.*<select id="id_country"(.*)<\/select>.*/\1/p' | \
sed -e 's/<option/\\n<option/g' \
)"
echo -e "${COUNTRIES}" | sed -n -E 's/.*<option value="([^"]+)">(.*)<\/option>.*/\1 \2/p' | \
while read -r COUNTRY_CODE COUNTRY_NAME; do
mkdir -p "zip" "ovpn" "tblk"
ZIP_FILE="zip/Mullvad - ${COUNTRY_NAME}.zip"
OVPN_FILE="ovpn/Mullvad - ${COUNTRY_NAME}.ovpn"
TBLK_FILE="tblk/Mullvad - ${COUNTRY_NAME}.tblk"
TBLK_FILE_HOME="tblk/Mullvad - ${COUNTRY_NAME} (home).tblk"
printf "Getting ${COUNTRY_NAME} (${COUNTRY_CODE})..."
if [ ! -f "${ZIP_FILE}" ]; then
curl "https://mullvad.net/download/config/" \
--compressed \
-s \
-o "${ZIP_FILE}" \
-H "Host: mullvad.net" \
-H "Referer: https://mullvad.net/download/config/" \
-H "Cookie: ${COOKIE}" \
-d "csrfmiddlewaretoken=${CSRF_MIDDLEWARE_TOKEN}&type=zip&account_number=${ACCOUNT_NUMBER}&port=1300&country=${COUNTRY_CODE}"
fi
if [ ! -f "${OVPN_FILE}" ]; then
curl "https://mullvad.net/download/config/" \
--compressed \
-s \
-o "${OVPN_FILE}" \
-H "Host: mullvad.net" \
-H "Referer: https://mullvad.net/download/config/" \
-H "Cookie: ${COOKIE}" \
-d "csrfmiddlewaretoken=${CSRF_MIDDLEWARE_TOKEN}&type=ovpn&account_number=${ACCOUNT_NUMBER}&port=1300&country=${COUNTRY_CODE}"
fi
printf "ok"
printf " - unzipping..."
unzip -qq -o "${ZIP_FILE}" -d "${COUNTRY_CODE}"
printf "ok"
printf " - move stuff around..."
rm -rf "${TBLK_FILE}"
mv -f "${COUNTRY_CODE}/$(ls ${COUNTRY_CODE})/OSX/Mullvad.tblk" "${TBLK_FILE}"
rm -rf "${COUNTRY_CODE}"
printf "ok"
if [ ! -z "${HOME_IP}" ]; then
printf " - create home version..."
rm -rf "${TBLK_FILE_HOME}"
cp -R "${TBLK_FILE}" "${TBLK_FILE_HOME}"
echo "route ${HOME_IP} 255.255.255.255 net_gateway" >> "${TBLK_FILE_HOME}/mullvad_osx.conf"
printf "ok"
fi
echo " ...done!"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment