Skip to content

Instantly share code, notes, and snippets.

@1player
Last active December 8, 2022 21:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1player/e9cadfef833d5eb5a23c30223f560147 to your computer and use it in GitHub Desktop.
Save 1player/e9cadfef833d5eb5a23c30223f560147 to your computer and use it in GitHub Desktop.
Script to set up Mullvad VPN from Wireguard config files (+ Tailscale VPN support)
#!/bin/bash
#
# Requires: wg-quick from wireguard-tools package.
#
# How to configure: go on your Mullvad account page and download the
# Wireguard configuration for all countries.
# Unpack the archive at $HOME/.config/mullvad/
#
# Then run `mullvad.sh up <2-char country code>`
# and `mullvad.sh down` to tear it down.
set -euo pipefail
CONFIGS_DIR=$HOME/.config/mullvad
RUNTIME_CONFIG=$XDG_RUNTIME_DIR/mullvad.conf
usage() {
echo "usage:"
echo " $(basename "$0") up <2-char country code>"
echo " $(basename "$0") down"
exit 1
}
if [ $# -lt 1 ]; then
usage
fi
case "$1" in
"up")
if [ $# -ne 2 ]; then
usage
fi
country=$2
chosen=$(find "$CONFIGS_DIR" -name "mullvad-$country*.conf" | sort -R | head -1)
if [ "$chosen" = "" ]; then
echo "Country $country not found."
exit 1
fi
# Make tailscale work with mullvad
awk -f - "$chosen" <<EOF > "$RUNTIME_CONFIG"
/\[Interface\]/ {
print;
print "PostUp = ip rule add table 52";
print "PreDown = ip rule del table 52";
next
}
1
EOF
wg-quick up "$RUNTIME_CONFIG"
;;
"down")
wg-quick down "$RUNTIME_CONFIG"
rm "$RUNTIME_CONFIG"
;;
*)
usage
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment