In the Barrett house, our wireless router is running DD-WRT with the OpenVPN client to a 3rd-party VPN provider always enabled. To hinder tracking by ISPs and the NSA, I try always to connect to a VPN on Android. When I enable it while connected to my home network, however, I notice a performance hit because I am recursively tunneling through a tunnel already being created by our wireless router. Using Tasker (root is not required) in conjunction with OpenVPN Connect, I have automated the connection and disconnection of the VPN service on my Android device. However, I do not take the extra performance overhead of the second connection when I am at home. Here is how you can automatically connect to VPN on Android using this same strategy.
Assumptions
You must have a subscription to an OpenVPN-compatible service. You should configure OpenVPN Connect on your Android phone per the instructions provided by your VPN service. You must also purchase and install Tasker before continuing.
I am not sure why, but it seems that Tasker must also have the Location Android permission enabled for the profiles to be triggered correctly.
Tasker Tasks
Configure the following two tasks with one action each in Tasker. 1Note: Items in curly brackets {} should be treated as variables and replaced.
“VPN Connect” Task
Add action type “System”->“Send Intent” and configure as follows.
- Action: net.openvpn.openvpn.CONNECT
- Cat: None
- Mime Type: {blank}
- Data: {blank}
- Extra: net.openvpn.openvpn.AUTOSTART_PROFILE_NAME:PC {your_profile_name}
- Extra: net.openvpn.openvpn.AUTOCONNECT:true
- Extra: net.openvpn.openvpn.APP_SECTION:PC
- Package: net.openvpn.openvpn
- Class: net.openvpn.unified.MainActivity
- Target: Activity
- Continue Task After Error: {unchecked}
- If: {blank}
- Label: {blank}
“VPN Disconnect” Task
Add action type “System”->“Send Intent” and configure as follows.
- Action: net.openvpn.openvpn.DISCONNECT
- Cat: None
- Mime Type: {blank}
- Data: {blank}
- Extra: net.openvpn.openvpn.STOP:true
- Extra: {blank}
- Extra: {blank}
- Package: net.openvpn.openvpn
- Class: net.openvpn.unified.MainActivity
- Target: Activity
- Continue Task After Error: {unchecked}
- If: {blank}
- Label: {blank}
Tasker Profiles
Configure the following two profiles in Tasker and link them to the task specified.
“Wi-Fi Disconnected” State Profile
Add profile and set as follows. Link this profile to the “VPN Connect” task.
- SSID: {blank}
- MAC: {blank}
- IP: {blank}
- Active: Yes
- Inverted: {checked}
“Wi-Fi Connected” State Profile
Add profile and configure as follows. Link this profile to the “VPN Disconnect” task.
- SSID: {blank}
- MAC: {blank}
- IP: {blank}
- Active: Yes
- Inverted: {unchecked}
via levgen
Hi.
{myOpenVpnProfileName} is this an ovpn file and where is it located.. I get openvpn profile not found error in the OpenVPN connect app
@Martin, I should have clarified that a bit better. You first have to import the opvn profile into the OpenVPN app on Android as you normally would from the settings menu. Once you’ve got the profile imported, verify you can connect from within OpenVPN by tapping “connect”. Then, in Tasker, {myOpenVpnProfileName} refers to the name (either default or customized when you imported the profile) of the profile in OpenVPN, no directory/location information needed. Let me know if I can help clarify further.
Tried your walkthrough but having a bit of troubles.
– My Tasker version is 4.8u5
– My OpenVPN for Android is 0.6.57
– My device is a Samsung Tab A, that is rooted.
Both downloaded off of the Google Playstore July 11, 2016.
OpenVPN for Android I imported my .ovpn file and can manually connect / disconnect my VPN just fine.
I have entered everything verbatim (aside variables as you specified) with the exception of:
•Continue Task After Error:
•Label: VPN Connect
Both the “VPN Connect” Task and “VPN Disconnect” Task do not have these settings to populate. Not sure if this would adversely affect operation.
But when I go to test each task by manually executing them in the Tasker app, the OpenVPN doesn’t start, nor if I manually start my openvpn will the Disconnect task end openvpn operation. The 5seconds of wait time does work.
Is there anything that may have changed since this writing back in January 2016?
Also tried this on my non rooted Samsung S5, same versions for OpenVPN and Tasker.
Hi @Jared,
It is quite possible that something has changed. I do not currently use this system (I just leave OpenVPN always connected, not using Tasker). I do have on my to-do list to do a revised write-up of this process, but I am not sure when I will have time to do that. Sorry, I can’t think of anything to offer to you to try until I dig in and retry on my own setup, which I don’t have time for in the near future. Good luck! Check back in awhile (few months) for a possible update to the post.
Working fine here, using the same settings you specified except with a similar app called Macrodroid
@chuck Great to hear! Thanks for the feedback!
Works great on my Google Pixel XL! Thanks for the code!
Thanks! Glad to hear it!
This is working on my Pixel after connecting Private Internet Access with OpenVPN. However, the “VPN Connect” Task causes the OpenVPN appear to appear briefly on screen; is there a way to prevent this? Also, from within the PIA app itself, there are settings for MACE ad blocking, is that available from OpenVPN this way as well?
Hi Steve,
I do not know of a way to prevent briefly showing the OpenVPN app on connect. However, I have been using this system for months and I hardly ever notice it. I am using a Nexus 6P, and the connect almost always happens when my screen is off as I am driving away from my home. So, it’s not really an issue for my usage.
The ad-blocking feature sounds like a custom feature for PIA. Adblocking really works best at the browser level, anyway. Blocking at the network level (like via a VPN) can only block entire domains but can’t effect stuff inside of https tunnels. If you want a great adblocking solution on Android, I recommend the Firefox browser with uBlock Origin and some of the lists I have (shameless plug for my side project) over at https://filterlists.com
Hope that helps!
Am new to all this but can this be done on Android with another vpn app? let me share my problem,I have a vpn that doesn’t connect on the first try so it attempt to connect two more times automatically,I would want to add more automatic re-connecting tries. am rooted xposed installed so any instructions would be nice. [email protected]
Hi Tyrone, The method described in this post is strictly for the official OpenVPN app. That being said, most good VPN services support OpenVPN. Even if the service you use has their own app, you might be able to connect to it via the OpenVPN app as well. I cannot provide specific instructions for that, though. You will need to contact your VPN provider. Hope that helps.
Thanks a million times!
Glad it helped!
Or an easier method would be to have a reserved ip for your phone and not send it’s traffic through the VPN on the router.
Hi Brian, That’s a great idea! It may not be supported on all router / router OSs, though.
This approach does not work in the latest version of OpenVPN for Android. The developer disabled the use of intents to connect and disconnect the VPN. Instead there is a openvpn tasker plugin(found on play store) now that can control openvpn for android.
Hi Sam,
Thanks. This article is targeting OpenVPN Connect, however, which still supports intents as described. OpenVPN for Android is a different app which this article does not claim to support.
Thank you so much for this. Its great.
When I implemented this, I noticed a couple of minor things about the profiles. The wifi connected profile should have the invert box Unchecked, and the wifi disconnected should have it checked, your instructions had them reversed. Also, on mine, the wifi connected profile also had to have the Active parameter set to “Any”. This was because while the VPN is connected, it wasn’t detecting that I reconnected to Wifi.
Hi John, Thanks for the tips. You are correct! I just updated the piece.
I can’t thank you enough for this tutorial. I made the decision to move from a Nexus phone to the Moto X4 and I am sincerely missing root (there is not yet a safe way to do this with the X4). I should have just bought a Pixel.
This made me turn to an alternative method for ad-blocking – AB-Solution on my ASUS router at home. I found myself needing to set up my own VPN on the ASUS so I could remain connected to my home network while away. While it works fine, it was not automated. Tasker was the obvious solution, but I was concerned it would not function properly without root.
Your instructions worked wonderfully. The only difference being the “Continue After Error” and “Label” options when creating tasks were not available. Labeling the task was available for me, however, through another menu with Tasker.
Device: Moto X4 Android One Edition
Android Version: 7.1.1
Tasker Version: 5.0u7
OpenVPN Connect Version: 1.1.23
Hi, this is great!!!
Is it possible to connect vpn whenever on wifi and disconnect if in cellular data?
I want to use vpn even at home when connected through wifi.
Tanks!
Hi Julio,
In this case, I just wouldn’t use Tasker at all and keep the VPN connected all the time (whether on cellular or WiFi).
Very good. Thanks for the helpful information. As an extra step, I also configured a profile to connect to my VPN whenever I’m on a cellular network. As a previous post indicated, I also use an Asus router with ab-solutions installed so I always want the benefits that has to offer to my mobile devices.
Cheers.
Sounds like a winning setup. Thanks for sharing!
Just what I needed to Connect or Disconnect to Open VPN when I want to via Tasker.
Thank you for sharing.
Regards Rob
Hi Rob, glad it helped! Let me know if you had any issues with it. I am working on an updated version of this post since it seems to be a popular one that has helped quite a few folks.
I tried this, but use it a bit differently, only want the VPN to connect when on a specific WiFi. However when the WiFi connects, I see OpenVPN connects, but then immediately disconnect?
Hi Toni,
I like that specific use case and am actually doing something very similar myself currently. I’m working on a re-write of this article, so check back soon.
Turns out it was my fault, I missed setting the Wi-Fi “Any” setting.
Also, it seems possible to merge the two profiles by adding the connect task as an exit condition when you leave the home profile (in my case the disconnect task)?
Ah, ok, glad to hear you figured it out.
Hmm, I’ll give that suggestion a shot. You might be right.
Thank you for this. I configured exactly as you outlined, but my profile is not connecting. When my home WiFi disconnects the openVPN Connect app opens, but my profile does not load. I double-checked my spelling and syntax. Please help!
Sorry, Greg. I am not sure to suggest. I am working on testing this method out fully again and re-writing this article with more options and details. Maybe check back soon and try again if I get that finished. Good luck!
hi! thax for this, one question there´s a way to use with Datally (Google´s App)? for block apps to use mobile data?
Since Datally sets up a local VPN server, using the Android’s VPN feature, I do not think there is a way to also use an actual OpenVPN client as well. Sorry.
Thanx for your reply maybe I’ll try openVPN
Thx Collin,
worked for me for two years.
But unfortunally today came an update for the OpenVPNconnect to version 3.0.0 and now this solution isn’t working anymore.
Maybe you can check the settings for this new version?
thx
raimund
Thanks for the info. I just added a notice to the top of the article. I am unaware of any new solution for the new version, but please let us know here if you find one. Sorry.
I think the newest OpenVPN update broke this, the tasks no longer trigger the connection.
Thanks for the info. I just added a notice to the top of the article. I am unaware of any new solution for the new version, but please let us know here if you find one. Sorry.
I sent an email to the developer asking if there was documentation on intents available in OpenVPN Connect 3.0.
We’ll see what kind of response I get.
I also wrote the developer linking this page as reference. Hopefully he can be able to help.
Please use following options for new version of OpenVPN Connect application:
[u][b]1. CONNECT[/b][/u]
[b][i]a) Access Server module[/i][/b]
Action: net.openvpn.openvpn.CONNECT
[b][i]OR[/i][/b]
Action: android.intent.action.VIEW
Cat: None
Mime Type: {blank}
Data: {blank}
Extra: net.openvpn.openvpn.AUTOSTART_PROFILE_NAME:AS {your_profile_name}
Extra: net.openvpn.openvpn.AUTOCONNECT:true
Extra: net.openvpn.openvpn.APP_SECTION:AS
Package: net.openvpn.openvpn
Class: net.openvpn.unified.MainActivity
Target: Activity
[b][i]
b) OVPN Profile module[/i][/b]
Action: net.openvpn.openvpn.CONNECT
[i][b]OR[/b][/i]
Action: android.intent.action.VIEW
Cat: None
Mime Type: {blank}
Data: {blank}
Extra: net.openvpn.openvpn.AUTOSTART_PROFILE_NAME:PC {your_profile_name}
Extra: net.openvpn.openvpn.AUTOCONNECT:true
Extra: net.openvpn.openvpn.APP_SECTION:PC
Package: net.openvpn.openvpn
Class: net.openvpn.unified.MainActivity
Target: Activity
[b][i]
2. DISCONNECT[/i][/b]
Action: net.openvpn.openvpn.DISCONNECT
Cat: None
Mime Type: {blank}
Data: {blank}
Extra: net.openvpn.openvpn.STOP:true
Extra: {blank}
Extra: {blank}
Package: net.openvpn.openvpn
Class: net.openvpn.unified.MainActivity
Target: Activity
Great work!
Thank you for this quick solution!!!!
rai
The OVPN profile module solution works like a charm for me. @levgen Thank you very much.. you are a life saver!
Hi,
i try to adopt levgen’s answer to Automate (tasker-alternative), but can’t get it work. OpenVPN Connect starts, but do nothing (doesnt change to OVPN Profile or anything else).
Disconnect seems to work, but i’m wondering, why the switch stays at a circle.
By the way: I use an OVPN profile.
Any ideas?
Andy
Hi Andy,
I never used Automate tool. Could you provide options which you used for previous version of OpenVPN Connect application. After that I will back soon with related data.
It looks like the most recent upgrade to openvpn broke this. It was working for me and then when openvpn upgraded, it stopped working.
I guess it would help if i read the new comments!
Forgot to mention that levgen’s solution works for me too. Thanks!
in ios version of openvpn app i am able to define connection via cellular data only or by wifi only or both with simple option “connect via” in app settings.
thus there is no need for tasker bloatware, tasker is slow and allows data leak during switching.
was it to help tasker sales or help track us by these 3 letters organizations?
why openvpn consortium created such mess for android users?
Huh, interesting point. I’m not familiar with how the iOS version works. I’d be happy to not have to mess with Tasker to get this to work on Android.
OK, doesn’t work for me, I just get a white screen
Hi Collin, I’m trying this on my Nexus 6P, and this is more of a Tasker issue but when ever I connect to my home WiFi, it does not recognise that and still says “Not Wifi Connected” in green.
Ah, I found the answer on reddit, https://www.reddit.com/r/tasker/comments/6ovz03/tasker_not_recognizing_wifi_when_vpn_connected/
I need to select “any” on the Wifi page
Ah, thanks for sharing.
Thanks for posting this. I have it all working except – it takes a few minutes for vpn to finally disconnect. Not sure why. Any ideas?
Hmm, really not sure. Sorry. I honestly gave up on trying to keep this automation working because it was so fragile (connect/disconnect at random times or not at all when I had hoped). I hope to re-visit at some point and will update this post if I do.
Do you know what I need to add in order to add a password?
My latest openvpn setup requires a password every time I connect (it isn’t possible to save it). So now when with your method tasker sends a connect to vpn a popup will show with an input field for the password.
Can the password be added in tasker somewhere?
Hmm, I’m never had to cache a password before. I’m not sure. Sorry. Good luck. Let us know if you figure it out!
This setup still works on Android 9. I did however had to introduce an extra wait for 5 s to prevent a too early startup of openvpn before the 4G connection had time to settle.
actually i reduced the wait down to 1 s.