How to find the status of VPN connection through framework APIs or any other efficient method?
Asked Answered
P

3

13

So far:

I found the following solutions

  1. Using Broadcastreceiver

    The broadcast receiver is deprecated from ICS

  2. Using Ping or Traceroute

    It's definitely going to take lots of time and its not efficient

  3. Polling for DNS server changes

    It's definitely going to take lots of time and its not efficient

  4. Using ip-address

    Even though it won't take much time depending on the network connection it may vary

My conclusion:

So far all the solutions I found are either not efficient or not so reliable.

My questions:

If VPN is connected in android device then android OS must be aware of it.

Are there any public android framework APIs to read it because finding it locally is most efficient and reliable solution ?

Are there any other efficient and reliable ways to achieve it (like integrating a C or C plus plus library with NDK)?

Note:

I couldn't find any custom broadcast senders/AIDL from OpenVPN for Android as well

Perlman answered 21/7, 2014 at 8:51 Comment(1)
Several questions: What is the underlying problem which you are trying to solve? Why do you need to know whether you are on VPN or not? Are you implementing your own VPN or do you want to check status of VPN in some other type of application?Leonerd
B
9

You can try to check for tun0 INTERFACE, It is being start afther the establish command.

try {
    for( NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) {

        // Pass over dormant interfaces
        if(!intf.isUp() || intf.getInterfaceAddresses().size() == 0)
            continue;

            if ("tun0".equals(intf.getName())){
                // The VPN is up
                break;
            }
    }
}

this also might work:

(Collections.list(NetworkInterface.getByName("tun0")

Boone answered 6/12, 2015 at 9:2 Comment(5)
tun0 is not the only option. There is also ppp0 and I don't know which other types depending on the type of VPN connection.Shutz
It will work after bug fix :), if ("tun0".equals(intf.getName())){Birmingham
@Shutz on what device did you got ppp0 ?Boone
Samsung Galaxy Tab Active SM-T365Shutz
Sometime it will be "tun1",so I think startWith("tun") will be more appropriate.Snivel
D
3

Have you used - VpnService

As per documentation -

Prepare to establish a VPN connection. This method returns 'null' if the VPN application 
is 'already prepared'.

From here-

http://developer.android.com/reference/android/net/VpnService.html#prepare(android.content.Context)

 Intent intent = VpnService.prepare(getApplicationContext());
  if (intent == null) {
    // this means there is already a prepared VPN connection
  }
Devote answered 21/7, 2014 at 8:59 Comment(2)
As per the documentation Prepare is meant to be having a single VPN connection so If a vpn connection is already there then it will return nul if not then it will return a respective object But it always returns null even if there is not vpn connection at all.Perlman
Even though the solution didn't work out,its actually a good tryPerlman
G
0

I know the answer is ridiculously late, but I was just trying to figure this out and stumbled upon the command:

ifconfig tun0

This will return an IP address and other info if the VPN is connected and if it isn't.

tun0: No such device

Gauger answered 23/6, 2016 at 6:11 Comment(2)
This one seems like we have to try through ADB ,,,is it possible that I can run it in java ?Perlman
tun0 is not the only option. There is also ppp0 and I don't know which other types depending on the type of VPN connection.Shutz

© 2022 - 2024 — McMap. All rights reserved.