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 | |
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')
-rw-r--r-- | src/main/AndroidManifest.xml | 1 | ||||
-rw-r--r-- | src/main/java/org/xapek/andiodine/IodineVpnService.java | 50 | ||||
-rw-r--r-- | src/main/res/values-de/strings.xml | 1 | ||||
-rw-r--r-- | src/main/res/values/strings.xml | 1 |
4 files changed, 52 insertions, 1 deletions
diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 0bea991..5325d7f 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ android:targetSdkVersion="16"/> <uses-permission android:name="android.permission.INTERNET"/> + <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="false" 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); diff --git a/src/main/res/values-de/strings.xml b/src/main/res/values-de/strings.xml index 9ad840b..b39b207 100644 --- a/src/main/res/values-de/strings.xml +++ b/src/main/res/values-de/strings.xml @@ -51,6 +51,7 @@ <string name="vpnservice_error_unknown_error_string">Unbekannter Fehler. Grund: %s</string> <string name="vpnservice_error_dns_detect_failed">Es wurden keine gültigen IPv4-Nameserver erkannt</string> <string name="vpnservice_error_create_interface_string">Das Tunnel-Netzwerkgerät konnte nicht erstellt werden</string> + <string name="vpnservice_error_no_active_network">Kein aktives Netzwerk</string> <string name="idle">Leerlauf</string> <string name="warning">Warnung</string> <string name="error">Fehler</string> diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index bd9709b..95b2250 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -76,6 +76,7 @@ <string name="vpnservice_error_unknown_error_string">Unknown Error. Reason: %s</string> <string name="vpnservice_error_dns_detect_failed">No valid IPv4 name servers detected</string> <string name="vpnservice_error_create_interface_string">Failed to create tunnel network device</string> + <string name="vpnservice_error_no_active_network">No active network</string> <string name="idle">Idle</string> <string name="warning">Warning</string> <string name="error">Error</string> |