summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/utils
diff options
context:
space:
mode:
authorcyberta <cyberta@riseup.net>2020-01-24 08:34:24 -0800
committercyberta <cyberta@riseup.net>2020-01-24 08:34:24 -0800
commit721d222a457ec0dfec28bc4ee4908b50f04904fc (patch)
treea83724b8cda98136d5e270eeef7296f7323375b9 /app/src/main/java/se/leap/bitmaskclient/utils
parent95bd478d62c9ebf3e313df4915c392dfd094d615 (diff)
parentdf563345352a26d627415740dcf2ef58d724b868 (diff)
Merge branch 'no-dns-fallback' into 'master'
No dns fallback See merge request leap/bitmask_android!99
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/utils')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java102
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java111
2 files changed, 111 insertions, 102 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java b/app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java
new file mode 100644
index 00000000..2e3ef596
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java
@@ -0,0 +1,102 @@
+package se.leap.bitmaskclient.utils;
+
+/*
+ * Copyright (C) 2006-2008 Alfresco Software Limited.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ * As a special exception to the terms and conditions of version 2.0 of
+ * the GPL, you may redistribute this Program in connection with Free/Libre
+ * and Open Source Software ("FLOSS") applications as described in Alfresco's
+ * FLOSS exception. You should have recieved a copy of the text describing
+ * the FLOSS exception, and it is also available here:
+ * http://www.alfresco.com/legal/licensing"
+ */
+
+import java.util.StringTokenizer;
+
+/**
+ * TCP/IP Address Utility Class
+ *
+ * @author gkspencer
+ */
+public class IPAddress {
+
+
+ /**
+ * Convert a TCP/IP address string into a byte array
+ *
+ * @param addr String
+ * @return byte[]
+ */
+ public static byte[] asBytes(String addr) {
+
+ // Convert the TCP/IP address string to an integer value
+ int ipInt = parseNumericAddress(addr);
+ if (ipInt == 0)
+ return null;
+
+ // Convert to bytes
+ byte[] ipByts = new byte[4];
+
+ ipByts[3] = (byte) (ipInt & 0xFF);
+ ipByts[2] = (byte) ((ipInt >> 8) & 0xFF);
+ ipByts[1] = (byte) ((ipInt >> 16) & 0xFF);
+ ipByts[0] = (byte) ((ipInt >> 24) & 0xFF);
+
+ // Return the TCP/IP bytes
+ return ipByts;
+ }
+ /**
+ * Check if the specified address is a valid numeric TCP/IP address and return as an integer value
+ *
+ * @param ipaddr String
+ * @return int
+ */
+ private static int parseNumericAddress(String ipaddr) {
+
+ // Check if the string is valid
+ if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
+ return 0;
+
+ // Check the address string, should be n.n.n.n format
+ StringTokenizer token = new StringTokenizer(ipaddr,".");
+ if (token.countTokens() != 4)
+ return 0;
+
+ int ipInt = 0;
+ while (token.hasMoreTokens()) {
+
+ // Get the current token and convert to an integer value
+ String ipNum = token.nextToken();
+
+ try {
+ // Validate the current address part
+ int ipVal = Integer.valueOf(ipNum).intValue();
+ if (ipVal < 0 || ipVal > 255)
+ return 0;
+
+ // Add to the integer address
+ ipInt = (ipInt << 8) + ipVal;
+ }
+ catch (NumberFormatException ex) {
+ return 0;
+ }
+ }
+
+ // Return the integer address
+ return ipInt;
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
index 40bb2eca..de2058c7 100644
--- a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
@@ -2,7 +2,6 @@ package se.leap.bitmaskclient.utils;
import android.content.Context;
import android.content.SharedPreferences;
-import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -24,6 +23,7 @@ import se.leap.bitmaskclient.Provider;
import static android.content.Context.MODE_PRIVATE;
import static se.leap.bitmaskclient.Constants.ALWAYS_ON_SHOW_DIALOG;
import static se.leap.bitmaskclient.Constants.DEFAULT_SHARED_PREFS_BATTERY_SAVER;
+import static se.leap.bitmaskclient.Constants.EXCLUDED_APPS;
import static se.leap.bitmaskclient.Constants.LAST_USED_PROFILE;
import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION;
import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED;
@@ -33,7 +33,6 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.Constants.SU_PERMISSION;
import static se.leap.bitmaskclient.Constants.USE_PLUGGABLE_TRANSPORTS;
-import static se.leap.bitmaskclient.Constants.EXCLUDED_APPS;
/**
* Created by cyberta on 18.03.18.
@@ -48,6 +47,8 @@ public class PreferenceHelper {
Provider provider = new Provider();
try {
provider.setMainUrl(new URL(preferences.getString(Provider.MAIN_URL, "")));
+ provider.setProviderIp(preferences.getString(Provider.PROVIDER_IP, ""));
+ provider.setProviderApiIp(preferences.getString(Provider.PROVIDER_API_IP, ""));
provider.define(new JSONObject(preferences.getString(Provider.KEY, "")));
provider.setCaCert(preferences.getString(Provider.CA_CERT, ""));
provider.setVpnCertificate(preferences.getString(PROVIDER_VPN_CERTIFICATE, ""));
@@ -64,73 +65,12 @@ public class PreferenceHelper {
return preferences.getString(toFetch + "." + providerDomain, "");
}
- public static String getProviderName(String provider) {
- return getProviderName(null, provider);
- }
-
- public static String getProviderName(@Nullable SharedPreferences preferences) {
- return getProviderName(preferences,null);
- }
-
- public static String getProviderName(@Nullable SharedPreferences preferences, @Nullable String provider) {
- if (provider == null && preferences != null) {
- provider = preferences.getString(Provider.KEY, "");
- }
- try {
- JSONObject providerJson = new JSONObject(provider);
- String lang = Locale.getDefault().getLanguage();
- return providerJson.getJSONObject(Provider.NAME).getString(lang);
- } catch (JSONException e) {
- try {
- JSONObject providerJson = new JSONObject(provider);
- return providerJson.getJSONObject(Provider.NAME).getString("en");
- } catch (JSONException e2) {
- return null;
- }
- } catch (NullPointerException npe) {
- return null;
- }
- }
-
- public static String getProviderDomain(SharedPreferences preferences) {
- return getProviderDomain(preferences, null);
- }
-
- public static String getProviderDomain(String provider) {
- return getProviderDomain(null, provider);
- }
-
- public static String getProviderDomain(@Nullable SharedPreferences preferences, @Nullable String provider) {
- if (provider == null && preferences != null) {
- provider = preferences.getString(Provider.KEY, "");
- }
- try {
- JSONObject providerJson = new JSONObject(provider);
- return providerJson.getString(Provider.DOMAIN);
- } catch (JSONException | NullPointerException e) {
- return null;
- }
- }
-
- public static String getDescription(SharedPreferences preferences) {
- try {
- JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, ""));
- String lang = Locale.getDefault().getLanguage();
- return providerJson.getJSONObject(Provider.DESCRIPTION).getString(lang);
- } catch (JSONException e) {
- try {
- JSONObject providerJson = new JSONObject(preferences.getString(Provider.KEY, ""));
- return providerJson.getJSONObject(Provider.DESCRIPTION).getString("en");
- } catch (JSONException e1) {
- return null;
- }
- }
- }
-
// TODO: replace commit with apply after refactoring EIP
//FIXME: don't save private keys in shared preferences! use the keystore
public static void storeProviderInPreferences(SharedPreferences preferences, Provider provider) {
preferences.edit().putBoolean(PROVIDER_CONFIGURED, true).
+ putString(Provider.PROVIDER_IP, provider.getProviderIp()).
+ putString(Provider.PROVIDER_API_IP, provider.getProviderApiIp()).
putString(Provider.MAIN_URL, provider.getMainUrlString()).
putString(Provider.KEY, provider.getDefinitionString()).
putString(Provider.CA_CERT, provider.getCaCert()).
@@ -141,6 +81,8 @@ public class PreferenceHelper {
String providerDomain = provider.getDomain();
preferences.edit().putBoolean(PROVIDER_CONFIGURED, true).
+ putString(Provider.PROVIDER_IP + "." + providerDomain, provider.getProviderIp()).
+ putString(Provider.PROVIDER_API_IP + "." + providerDomain, provider.getProviderApiIp()).
putString(Provider.MAIN_URL + "." + providerDomain, provider.getMainUrlString()).
putString(Provider.KEY + "." + providerDomain, provider.getDefinitionString()).
putString(Provider.CA_CERT + "." + providerDomain, provider.getCaCert()).
@@ -167,48 +109,13 @@ public class PreferenceHelper {
return VpnProfile.fromJson(lastConnectedProfileJson);
}
-
-
-
- public static void clearDataOfLastProvider(SharedPreferences preferences) {
- clearDataOfLastProvider(preferences, false);
- }
-
- @Deprecated
- public static void clearDataOfLastProvider(SharedPreferences preferences, boolean commit) {
- Map<String, ?> allEntries = preferences.getAll();
- List<String> lastProvidersKeys = new ArrayList<>();
- for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
- //sort out all preferences that don't belong to the last provider
- if (entry.getKey().startsWith(Provider.KEY + ".") ||
- entry.getKey().startsWith(Provider.CA_CERT + ".") ||
- entry.getKey().startsWith(Provider.CA_CERT_FINGERPRINT + "." )||
- entry.getKey().equals(PREFERENCES_APP_VERSION)
- ) {
- continue;
- }
- lastProvidersKeys.add(entry.getKey());
- }
-
- SharedPreferences.Editor preferenceEditor = preferences.edit();
- for (String key : lastProvidersKeys) {
- preferenceEditor.remove(key);
- }
- if (commit) {
- preferenceEditor.commit();
- } else {
- preferenceEditor.apply();
- }
- }
-
public static void deleteProviderDetailsFromPreferences(@NonNull SharedPreferences preferences, String providerDomain) {
preferences.edit().
remove(Provider.KEY + "." + providerDomain).
remove(Provider.CA_CERT + "." + providerDomain).
- remove(Provider.CA_CERT_FINGERPRINT + "." + providerDomain).
+ remove(Provider.PROVIDER_IP + "." + providerDomain).
+ remove(Provider.PROVIDER_API_IP + "." + providerDomain).
remove(Provider.MAIN_URL + "." + providerDomain).
- remove(Provider.KEY + "." + providerDomain).
- remove(Provider.CA_CERT + "." + providerDomain).
remove(PROVIDER_EIP_DEFINITION + "." + providerDomain).
remove(PROVIDER_PRIVATE_KEY + "." + providerDomain).
remove(PROVIDER_VPN_CERTIFICATE + "." + providerDomain).