Why is this useful?
Well, in my Tutorial "Pihole and PiVPN on a Raspberry Pi", I showed you, how to install a wireguard based VPN Server (with PiVPN) and the Ad-Filter Pihole on any Debian based System and use them on your Phone, when you are on the go or in a public network.
But you always have to manually connect to your Home-VPN, once you want to enjoy an encrypted connection to your home-network with Pihole filtering on the go.
What we want to achieve:
- automatically connect to the Home-VPN once you leave the Home-Network
- automatically disconnect from the Home-VPN once you enter the Home-Network
We can achieve it with the Android Swiss Army Knife in Terms of automation: Tasker (Play Store)
Tasker is an app, which you can use to automate like nearly anything on your phone. For example, I used it years ago to switch off any sounds on my phone when I arrived my workplace (GPS based). When I left my workplace, my phone automatically enabled sounds again and send a text to my wife that I'm coming home to let her know.
It's such a powertool, I freakin love it.
How to use Tasker to auto-connect to your VPN
Before we start, we need to make sure we match all dependencies!
What we need:
- A set up and working Wireguard server (you can use my Tutorial)
- The Wireguard app with a working Profile and enabled third party App access
- Tasker installed as well
I presume that you are able to install Tasker on your own, so I skip this part. Important is just, that Tasker has all the permissions it needs.
Creating the first Tasker-Profile
So now we are going to create the first Tasker-Profile. It should disconnect your Home-VPN-Connection once we enter our Home-Wifi.
We open up the Tasker App and see just an empty screen.
To add a profile we tap the + button.
We choose "create" on the following dialogue.
Now Tasker asks for a profile-type. In our case we need to tap on "State", because our automation profile is dependant on the WIFI-State of our device.
We need to choose "Net" to open all the network options available and hit "Wifi Connected" next.
On the following dialogue we can set up all the dependancies of our Wifi-State. Let's fill in our Home Network's SSID.
You can also fill in your routers MAC address if needed. My Home SSID is that unique that it's specific enough in my case.
Once filled in, we hit the back arrow and Tasker asks us to create a "New Task". We tap the + button and name the new task "Wireguard off" or whatever you like.
Now we need to add the specific task Tasker has to do when our phone is connected to our homenetwork.
We hit the + button and choose "Tasker -> Tasker Function -> Wireguard Set Tunnel"
It asks us for "Tunnel up?", we choose "no" and now we need to enter the Tunnel's name.
This is the name of your Tunnel in the wireguard app. Mine is called "Home" so I'm going to enter it here.
The task should look like this:
Now tap the back arrows like 2 or 3 times and we are on the main screen of tasker again with the first profile done!
As you can see there is a switch. You can use this to enable or disable the profiles. Once switched off you need to make your new configuration "live" by tapping the ✔️.
Creating the second Tasker-Profile
Okay, now that we have the first Profile we are going to add the second one. We want to auto-connect to our VPN once we leave our Home-Wifi.
The steps are pretty similar, so I want to only point out the differences, as we already know where to dig in Tasker's configutation possibilities.
1.) We add a new Profile - create - State - Net - Wifi Connected
We enter our Wifi data needed for the state, but this time we also check the "Invert" option ✔️.
Invert means here that the State "Wifi Connected" gets inverted to "Wifi Disconnected"
2.) We add a new task - name it "wireguard on" - Tasker - Tasker function - Wireguard Set Tunnel
It asks us for “Tunnel up?”, we choose “yes” and now we need to enter the Tunnel’s name.
Now we go back - back into the main screen and see of both Profiles are enabled. We make the changed "live" by tapping the ✔️.
To see if the profiles work, let's just disable the wifi connection and see what's happening on Tasker's main screen:
The green text will show the current active Profiles. If you also have the "Key-Icon" on your statusbar you should also see that the Wireguard trigger works.
The Wireguard App has to be set to allow third party Apps controlling its Tunnels.
To do so open the Wireguard app and its Settings. Tab on the Advanced section to make it visible and check this option:
Tasker usually yells at you, if it's missing a permission, but you can also manually check in your Android's settings.
As the settings apps differ per OS, here is how it looks for me: