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

六眼飞鱼酱① 提交于 2019-12-18 15:03:14

问题


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


回答1:


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")




回答2:


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
  }



回答3:


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



来源:https://stackoverflow.com/questions/24861034/how-to-find-the-status-of-vpn-connection-through-framework-apis-or-any-other-eff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!