diff options
author | Will Szumski <will@cowboycoders.org> | 2016-11-24 20:40:52 +0000 |
---|---|---|
committer | yvesf <yvesf-git@xapek.org> | 2016-11-28 19:53:44 +0100 |
commit | 04912777fdbd9799710756c2478f2d5927675e23 (patch) | |
tree | b1a6ec3e8f892dade973d4c5e7cf7bae8fd95b05 /src/main/java/org | |
parent | d8e779e7d384be5c9f2a977a43a02b70de69e352 (diff) | |
download | andiodine-04912777fdbd9799710756c2478f2d5927675e23.tar.gz andiodine-04912777fdbd9799710756c2478f2d5927675e23.zip |
Use stable API to detect system nameservers
Motivation:
__system_get_property is a hidden api and has already been removed from the ndk once
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/xapek/andiodine/IodineVpnService.java | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/main/java/org/xapek/andiodine/IodineVpnService.java b/src/main/java/org/xapek/andiodine/IodineVpnService.java index 8d5afe9..ac0f45c 100644 --- a/src/main/java/org/xapek/andiodine/IodineVpnService.java +++ b/src/main/java/org/xapek/andiodine/IodineVpnService.java @@ -1,10 +1,16 @@ package org.xapek.andiodine; +import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkInfo; import android.net.VpnService; +import android.os.Build; import android.os.ParcelFileDescriptor; import android.util.Log; @@ -12,8 +18,10 @@ import org.xapek.andiodine.config.ConfigDatabase; import org.xapek.andiodine.config.IodineConfiguration; import java.io.IOException; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.List; public class IodineVpnService extends VpnService implements Runnable { private class IodineVpnException extends Exception { @@ -203,13 +211,53 @@ public class IodineVpnService extends VpnService implements Runnable { } } + @TargetApi(21) + private Network getActiveNetwork() { + ConnectivityManager cm = + (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo defaultNetworkInfo = cm.getActiveNetworkInfo(); + for (Network n : cm.getAllNetworks()) { + NetworkInfo info = cm.getNetworkInfo(n); + if (info.getType() == defaultNetworkInfo.getType() && + info.getSubtype() == defaultNetworkInfo.getSubtype() && + info.isConnected()) { + return n; + } + } + return null; + } + @Override public void run() { try { Log.d(TAG, "VPN Thread enter"); setStatus(ACTION_STATUS_CONNECT, mConfiguration.getId(), null); - String tunnelNameserver = IodineClient.getPropertyNetDns1(); + String tunnelNameserver = ""; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + ConnectivityManager cm = + (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + Network activeNetwork = getActiveNetwork(); + if (activeNetwork == null) { + String errorMessage = getString(R.string.vpnservice_error_no_active_network); + setStatus(ACTION_STATUS_ERROR, mConfiguration.getId(), errorMessage); + Log.e(TAG, "No active network. Aborting..."); + return; + } + LinkProperties prop = cm.getLinkProperties(activeNetwork); + List<InetAddress> servers = prop.getDnsServers(); + for (InetAddress candidate : servers) { + Log.d(TAG, "detected dns server: " + candidate); + if (candidate instanceof Inet4Address) { + tunnelNameserver = candidate.getHostAddress(); + break; + } + } + } else { + tunnelNameserver = IodineClient.getPropertyNetDns1(); + } + if (tunnelNameserver.isEmpty()) { String errorMessage = getString(R.string.vpnservice_error_dns_detect_failed); setStatus(ACTION_STATUS_ERROR, mConfiguration.getId(), errorMessage); |