Controlling WireGuard using Tasker on Android
For various reasons the IP I use to connect to my home WireGuard VPN changes depending on whether I am in the house or not. WireGuard only does a DNS lookup when you start the tunnel so if the IP changes, the tunnel will stop passing traffic until you manually cycle it.
Me being me, I wanted to automate this so I wouldn't have to manually cycle the VPN. I used Tasker to set up a profile that would toggle my VPN any time Wifi connected or disconnected.
Setting up WireGuard control in Tasker
This works on Android 15 with WireGuard v1.0.20231018 and Tasker 6.3.13.
Task Action: Send Intent
Setting | Value |
---|---|
Action | com.wireguard.android.action.SET_TUNNEL_DOWN or com.wireguard.android.action.SET_TUNNEL_UP |
Extra | tunnel:MyVPNName |
Package | com.wireguard.android |
Target | Broadcast Receiver |
Replace MyVPNName with the tunnel name you configured in WireGuard.
On top of this you must configure WireGuard to allow external apps to toggle tunnels. You can find this in WireGuard settings: "Allow remote control apps"
You also need to grant Tasker permission in Android settings to allow control of WireGuard tunnels:
Automation
Here is an example of an automation you can use to automatically restart WireGuard when your Wifi disconnects or connects. It keeps track of the VPN state so it won't turn the VPN on if it wasn't already activated.
First, create a variable %VPN_State to track if the VPN is active or not:
Then create a task that updates the %VPN_State variable when the VPN is off:
Create a profile that populates the VPN state variable depending on the VPN state. Note the use of start and exit tasks:
Create a task that will restart WireGuard if %VPN_State is 1 (on):
Create a profile to call the restart VPN task when Wifi is connected:
Repeat the last step to create a profile to call the restart VPN task when Wifi is not connected.