I have a third party VPN app on my non-rooted Android 4.4 device, and want to write a background service to monitor the VPN connection and alert the user if the VPN connection has been broken.
Is there a way to do this? I couldn't find any way using the VPNService API.
Thanks -D
Using NetworkCapabilities
worked for me. You have to loop over all existing networks and check which has VPN_TRANSPORT
ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = cm.getAllNetworks();
Log.i(TAG, "Network count: " + networks.length);
for(int i = 0; i < networks.length; i++) {
NetworkCapabilities caps = cm.getNetworkCapabilities(networks[i]);
Log.i(TAG, "Network " + i + ": " + networks[i].toString());
Log.i(TAG, "VPN transport is: " + caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN));
Log.i(TAG, "NOT_VPN capability is: " + caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN));
}
This is works for me: Tested from API 16 to 23:
List<String> networkList = new ArrayList<>();
try {
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
if (networkInterface.isUp())
networkList.add(networkInterface.getName());
}
} catch (Exception ex) {
Timber.d("isVpnUsing Network List didn't received");
}
return networkList.contains("tun0");
PS Also it can be another networkInterface with name "ppp0", but in my tests with different VPN apps it's always was "tun0"
Late answer, tested on Android 16 <-> 29
:
public boolean vpn() {
String iface = "";
try {
for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
if (networkInterface.isUp())
iface = networkInterface.getName();
Log.d("DEBUG", "IFACE NAME: " + iface);
if ( iface.contains("tun") || iface.contains("ppp") || iface.contains("pptp")) {
return true;
}
}
} catch (SocketException e1) {
e1.printStackTrace();
}
return false;
}
The above doesn't work with wireguard because the interface name can be anything (normally the connection's name).
An alternative to check if the active Network is using VPN:
Network activeNetwork = connectivityManager.getActiveNetwork();
NetworkCapabilities caps = connectivityManager.getNetworkCapabilities(activeNetwork);
boolean vpnInUse = caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
Here is kotlin coroutines flow
solution
val isVpnActiveFlow = callbackFlow {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
if (connectivityManager == null) {
channel.close(IllegalStateException("connectivity manager is null"))
return@callbackFlow
} else {
val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
channel.trySend(true)
}
override fun onLost(network: Network) {
channel.trySend(false)
}
}
connectivityManager.registerNetworkCallback(
//I have to investigate on this builder!
NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_VPN)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
.build(),
callback
)
awaitClose {
connectivityManager.unregisterNetworkCallback(callback)
}
}
}
Write a little app that hits an internal page or pings an internal site...
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(internal_url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
or
String str = "";
try
{
Process process = Runtime.getRuntime().exec(
"/system/bin/ping -c 8 " + internal_url);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
int i;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((i = reader.read(buffer)) > 0)
output.append(buffer, 0, i);
reader.close();
str = output.toString();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
How about checking the network infos? If one has a type of VPN, I would assume there's a VPN connection.
Requires the ACCESS_NETWORK_STATE permission.
List<NetworkInfo> connectedNetworks = new ArrayList<>();
if (Build.VERSION.SDK_INT >= 21)
{
Network[] networks = m_ConnectivityManager.getAllNetworks();
for (Network n : networks)
{
NetworkInfo ni = m_ConnectivityManager.getNetworkInfo(n);
if (ni.isConnectedOrConnecting())
{
connectedNetworks.add(ni);
}
}
}
else
{
NetworkInfo[] nis = m_ConnectivityManager.getAllNetworkInfo();
for (NetworkInfo ni : nis)
{
if (ni.isConnectedOrConnecting())
{
connectedNetworks.add(ni);
}
}
}
boolean bHasVPN = false;
if (Build.VERSION.SDK_INT >= 21)
{
for (NetworkInfo ni : connectedNetworks)
{
bHasVPN |= (ni.getType() == ConnectivityManager.TYPE_VPN);
}
}
This works for API < 21 too:
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo[] infos=manager.getAllNetworkInfo();
boolean VPNConnected = false;
for (NetworkInfo info : infos){
if ("VPN".equalsIgnoreCase(info.getTypeName()){
if (info.getState() == NetworkInfo.State.CONNECTED){
VPNConnected = true;
break;
}
}
}
I wrote a helper function for myself that works API 21 and above.
public static boolean vpnActive(Context context){
//this method doesn't work below API 21
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
return false;
boolean vpnInUse = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network activeNetwork = connectivityManager.getActiveNetwork();
NetworkCapabilities caps = connectivityManager.getNetworkCapabilities(activeNetwork);
return caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
}
Network[] networks = connectivityManager.getAllNetworks();
for(int i = 0; i < networks.length; i++) {
NetworkCapabilities caps = connectivityManager.getNetworkCapabilities(networks[i]);
if(caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
vpnInUse = true;
break;
}
}
return vpnInUse;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.