Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mietzen/ddf7eb32fd8326cf2909fc673d3cd65a to your computer and use it in GitHub Desktop.
Save mietzen/ddf7eb32fd8326cf2909fc673d3cd65a to your computer and use it in GitHub Desktop.
Use this script to refresh your mullvad wireguard publickey on OpenWRT:
#!/bin/bash
OLD_PUBLIC_KEY=$(uci get network.mullvad.private_key | wg pubkey)
logger -p notice -t "Changing Wireguard Publickey" "Old Publickey=$OLD_PUBLIC_KEY"
logger -p notice -t "Changing Wireguard Publickey" "Old Privatekey=$(uci get network.mullvad.private_key)"
logger -p notice -t "Changing Wireguard Publickey" "Old IPs=$(uci get network.mullvad.addresses)"
PRIVATE_KEY=$(wg genkey)
logger -p notice -t "Changing Wireguard Publickey" "New Privatekey=$PRIVATE_KEY"
PUBLIC_KEY=$(echo "$PRIVATE_KEY" | wg pubkey)
logger -p notice -t "Changing Wireguard Publickey" "New Publickey=$PUBLIC_KEY"
IPS=($(curl -sSL https://api.mullvad.net/app/v1/replace-wireguard-key -H "Content-Type: application/json" -H "Authorization: Token $1" -d "{\"old\":\"$OLD_PUBLIC_KEY\",\"new\":\"$PUBLIC_KEY\"}" | jq -r '.ipv4_address, .ipv6_address'))
IPV4=${IPS[0]}
IPV6=${IPS[1]}
logger -p notice -t "Changing Wireguard Publickey" "New IPs=$IPV4 $IPV6"
uci set network.mullvad.private_key=$PRIVATE_KEY
uci set network.mullvad.addresses="$IPV4 $IPV6"
uci commit network
/etc/init.d/network restart
logger -p notice -t "Changing Wireguard Publickey" "Key change successful"
exit 0
@mietzen
Copy link
Author

mietzen commented May 7, 2022

Install these packages:
opkg install bash curl jq

This will only work with keys that don't have port forwarding enabled.

Usage:
./openwrt-refresh-mullvad-wireguard-publickey.sh <YOUR-TOKEN>

Put it in a cronjob and execute it every 24h.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment