Tuesday, October 4
Shadow

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

Leave a Reply