From a74d09929575e44f86d09283ae4633b0dcfcb566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 6 Jan 2015 10:11:50 +0100 Subject: Extracted GatewaysManager + coded its tests --- app/src/androidTest/assets/eip-service-test.json | 69 ++++++ app/src/androidTest/assets/gateway.json | 1 + app/src/androidTest/assets/secrets.json | 1 + .../java/se/leap/bitmaskclient/test/testEIP.java | 23 +- .../bitmaskclient/test/testGatewaysManager.java | 133 ++++++++++++ .../main/java/se/leap/bitmaskclient/eip/EIP.java | 239 ++++----------------- .../java/se/leap/bitmaskclient/eip/Gateway.java | 11 +- .../se/leap/bitmaskclient/eip/GatewaysManager.java | 184 ++++++++++++++++ 8 files changed, 455 insertions(+), 206 deletions(-) create mode 100644 app/src/androidTest/assets/eip-service-test.json create mode 100644 app/src/androidTest/assets/gateway.json create mode 100644 app/src/androidTest/assets/secrets.json create mode 100644 app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java (limited to 'app/src') diff --git a/app/src/androidTest/assets/eip-service-test.json b/app/src/androidTest/assets/eip-service-test.json new file mode 100644 index 00000000..78b49bae --- /dev/null +++ b/app/src/androidTest/assets/eip-service-test.json @@ -0,0 +1,69 @@ +{ + "gateways": [ + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "ports": [ + "443" + ], + "protocols": [ + "tcp", + "udp" + ], + "transport": [ + "openvpn" + ], + "user_ips": false + }, + "host": "millipede.demo.bitmask.net", + "ip_address": "198.252.153.84", + "location": "seattle__wa" + }, + { + "capabilities": { + "adblock": false, + "filter_dns": false, + "limited": false, + "ports": [ + "443" + ], + "protocols": [ + "tcp", + "udp" + ], + "transport": [ + "openvpn" + ], + "user_ips": false + }, + "host": "otter.demo.bitmask.net", + "ip_address": "46.165.242.169", + "location": "frankfurt" + } + ], + "locations": { + "frankfurt": { + "country_code": "DE", + "hemisphere": "N", + "name": "Frankfurt", + "timezone": "+1" + }, + "seattle__wa": { + "country_code": "US", + "hemisphere": "N", + "name": "Seattle, WA", + "timezone": "-7" + } + }, + "openvpn_configuration": { + "auth": "SHA1", + "cipher": "AES-128-CBC", + "keepalive": "10 30", + "tls-cipher": "DHE-RSA-AES128-SHA", + "tun-ipv6": true + }, + "serial": 1, + "version": 1 +} \ No newline at end of file diff --git a/app/src/androidTest/assets/gateway.json b/app/src/androidTest/assets/gateway.json new file mode 100644 index 00000000..51a19ec9 --- /dev/null +++ b/app/src/androidTest/assets/gateway.json @@ -0,0 +1 @@ +{"location":"seattle__wa","ip_address":"198.252.153.84","capabilities":{"limited":false,"ports":["443"],"adblock":false,"transport":["openvpn"],"filter_dns":false,"protocols":["tcp","udp"],"user_ips":false},"host":"millipede.demo.bitmask.net"} diff --git a/app/src/androidTest/assets/secrets.json b/app/src/androidTest/assets/secrets.json new file mode 100644 index 00000000..36ba5977 --- /dev/null +++ b/app/src/androidTest/assets/secrets.json @@ -0,0 +1 @@ +{"ca_cert":"-----BEGIN CERTIFICATE-----\nMIIFbzCCA1egAwIBAgIBATANBgkqhkiG9w0BAQ0FADBKMRgwFgYDVQQDDA9CaXRt\nYXNrIFJvb3QgQ0ExEDAOBgNVBAoMB0JpdG1hc2sxHDAaBgNVBAsME2h0dHBzOi8v\nYml0bWFzay5uZXQwHhcNMTIxMTA2MDAwMDAwWhcNMjIxMTA2MDAwMDAwWjBKMRgw\nFgYDVQQDDA9CaXRtYXNrIFJvb3QgQ0ExEDAOBgNVBAoMB0JpdG1hc2sxHDAaBgNV\nBAsME2h0dHBzOi8vYml0bWFzay5uZXQwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw\nggIKAoICAQC1eV4YvayaU+maJbWrD4OHo3d7S1BtDlcvkIRS1Fw3iYDjsyDkZxai\ndHp4EUasfNQ+EVtXUvtk6170EmLco6Elg8SJBQ27trE6nielPRPCfX3fQzETRfvB\n7tNvGw4Jn2YKiYoMD79kkjgy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aNJIo4e+no\nrURlp800wWabEPRocdBRQmyULBLxduBr2LIMzhgwGSz8b2wji\/l9ZA3PFY135bxClVzSzUIjuO3N\nrGUzHl2wAAyuAFDSUshzfkPBJRNt8aVBF5PQ3t93ZYmPAmv8LPZe875yX5ECgYEAsUEcwK\/ZNW7g\ndQPZR4iJNkC4Xu6cBZ6Cnn92swBheEYvLSoNlX0vDZ7aLE3\/jzQqrjzC8NP8sbH5jtbuvgeDXZX3\nAmGRp5j6C6A61ihAPmEVz3ZfN8SSfJ3vl\/\/PAIg6lyz0J+cy4Q7RkwSeuVQ72Hl4M8TEvmmKC3Af\nispy6Y0CgYEAgl1o2lo+ACyk+oVQPaaPqK3d7WOBFp4eR2nXFor\/vsx9igQOlZUgzRDQsR8jo1o9\nefOSBf87igrZGgssys89pWa2dnXnz5PMmzkKr6bw4D9Ez6u6Puc9UZhGw\/8wDYg6fSosdB9utspm\nM698ycef7jBNMDgmhpSvfw5GctoNQ4s=\n-----END RSA PRIVATE KEY-----"} diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java index be774ecb..d9235085 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java @@ -1,3 +1,19 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * 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 3 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, see . + */ package se.leap.bitmaskclient.test; import android.content.Context; @@ -10,6 +26,9 @@ import se.leap.bitmaskclient.Dashboard; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.eip.EIP; +/** + * @author parmegv + */ public class testEIP extends ServiceTestCase { private Context context; @@ -42,13 +61,13 @@ public class testEIP extends ServiceTestCase { } private void testEmptyCertificate() { - preferences.edit().putString(Constants.CERTIFICATE, ""); + preferences.edit().putString(Constants.CERTIFICATE, "").apply(); startService(Constants.ACTION_CHECK_CERT_VALIDITY); } private void testExpiredCertificate() { String expired_certificate = "expired certificate"; - preferences.edit().putString(Constants.CERTIFICATE, expired_certificate); + preferences.edit().putString(Constants.CERTIFICATE, expired_certificate).apply(); startService(Constants.ACTION_CHECK_CERT_VALIDITY); } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java new file mode 100644 index 00000000..98d4d6bc --- /dev/null +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java @@ -0,0 +1,133 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * 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 3 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, see . + */ +package se.leap.bitmaskclient.test; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.test.InstrumentationTestCase; +import android.test.suitebuilder.annotation.MediumTest; +import android.test.suitebuilder.annotation.SmallTest; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.io.InputStream; + +import se.leap.bitmaskclient.Dashboard; +import se.leap.bitmaskclient.eip.Gateway; +import se.leap.bitmaskclient.eip.GatewaysManager; + +/** + * @author parmegv + */ +public class testGatewaysManager extends InstrumentationTestCase { + + GatewaysManager gateways_manager; + Gateway gateway; + JSONObject eip_definition; + + Context context; + SharedPreferences preferences; + + @Override + protected void setUp() throws Exception { + mockGatewaysManager(); + mockRealGateway(); + super.setUp(); + } + + @MediumTest + public void testFromEipServiceJson() { + gateways_manager.fromEipServiceJson(eip_definition); + assertEquals(2, gateways_manager.size()); + gateways_manager.addFromString(gateway.toString()); + assertEquals(2, gateways_manager.size()); + } + + @SmallTest + public void testAddFromString() { + gateways_manager.addFromString(""); + gateways_manager.addFromString(gateway.toString()); + } + + @MediumTest + public void testRemoveDuplicate() { + gateways_manager.addFromString(gateway.toString()); + assertEquals(1, gateways_manager.size()); + + mockArtificialGateway(); + gateways_manager.addFromString(gateway.toString()); + assertEquals(1, gateways_manager.size()); + } + + @MediumTest + public void testToString() { + assertEquals("[]", gateways_manager.toString()); + + gateways_manager.addFromString(gateway.toString()); + assertEquals("["+gateway.toString()+"]", gateways_manager.toString()); + } + + @SmallTest + public void testIsEmpty() { + assertTrue(gateways_manager.isEmpty()); + gateways_manager.addFromString(""); + assertTrue(gateways_manager.isEmpty()); + gateways_manager.addFromString(gateway.toString()); + assertFalse(gateways_manager.isEmpty()); + } + + private void mockGatewaysManager() { + context = getInstrumentation().getContext(); + preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE); + gateways_manager = new GatewaysManager(context, preferences); + } + + private void mockRealGateway() { + try { + eip_definition = new JSONObject(fromAssets("eip-service-test.json")); + JSONObject secrets = new JSONObject(fromAssets("secrets.json")); + JSONObject gateway = new JSONObject(fromAssets("gateway.json")); + this.gateway = new Gateway(eip_definition, secrets, gateway); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void mockArtificialGateway() { + try { + eip_definition = new JSONObject(fromAssets("eip-service-test.json")); + JSONObject secrets = new JSONObject(fromAssets("secrets.json").replace("6u6", "7u7")); + JSONObject gateway = new JSONObject(fromAssets("gateway.json")); + this.gateway = new Gateway(eip_definition, secrets, gateway); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private String fromAssets(String filename) throws IOException, JSONException { + String result = ""; + InputStream is = context.getAssets().open(filename); + byte[] bytes = new byte[is.available()]; + if(is.read(bytes) > 0) { + result = new String(bytes); + } + return result; + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index a86b9c6a..a5cc817a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -16,46 +16,17 @@ */ package se.leap.bitmaskclient.eip; -import android.app.Activity; -import android.app.IntentService; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.os.ResultReceiver; +import android.app.*; +import android.content.*; +import android.os.*; import android.util.Log; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; +import org.json.*; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import de.blinkt.openvpn.*; +import se.leap.bitmaskclient.*; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; - -import de.blinkt.openvpn.LaunchVPN; -import de.blinkt.openvpn.VpnProfile; -import de.blinkt.openvpn.core.Connection; -import de.blinkt.openvpn.core.ProfileManager; -import se.leap.bitmaskclient.Dashboard; -import se.leap.bitmaskclient.EipFragment; -import se.leap.bitmaskclient.Provider; - -import static se.leap.bitmaskclient.eip.Constants.ACTION_CHECK_CERT_VALIDITY; -import static se.leap.bitmaskclient.eip.Constants.ACTION_IS_EIP_RUNNING; -import static se.leap.bitmaskclient.eip.Constants.ACTION_START_EIP; -import static se.leap.bitmaskclient.eip.Constants.ACTION_STOP_EIP; -import static se.leap.bitmaskclient.eip.Constants.ACTION_UPDATE_EIP_SERVICE; -import static se.leap.bitmaskclient.eip.Constants.CERTIFICATE; -import static se.leap.bitmaskclient.eip.Constants.KEY; -import static se.leap.bitmaskclient.eip.Constants.RECEIVER_TAG; -import static se.leap.bitmaskclient.eip.Constants.REQUEST_TAG; +import static se.leap.bitmaskclient.eip.Constants.*; /** * EIP is the abstract base class for interacting with and managing the Encrypted @@ -79,26 +50,25 @@ public final class EIP extends IntentService { private static SharedPreferences preferences; private static JSONObject eip_definition; - private static List gateways = new ArrayList<>(); - private static ProfileManager profile_manager; + private static GatewaysManager gateways_manager = new GatewaysManager(); private static Gateway gateway; - public EIP(){ - super(TAG); - } + public EIP(){ + super(TAG); + } - @Override - public void onCreate() { - super.onCreate(); + @Override + public void onCreate() { + super.onCreate(); - context = getApplicationContext(); + context = getApplicationContext(); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); - - profile_manager = ProfileManager.getInstance(context); - eip_definition = eipDefinitionFromPreferences(); - if(gateways.isEmpty()) - gateways = gatewaysFromPreferences(); - } + eip_definition = eipDefinitionFromPreferences(); + if(gateways_manager.isEmpty()) { + gateways_manager = new GatewaysManager(context, preferences); + gatewaysFromPreferences(); + } + } @Override protected void onHandleIntent(Intent intent) { @@ -123,18 +93,17 @@ public final class EIP extends IntentService { * It also sets up early routes. */ private void startEIP() { - if(gateways.isEmpty()) + if(gateways_manager.isEmpty()) updateEIPService(); earlyRoutes(); - GatewaySelector gateway_selector = new GatewaySelector(gateways); - gateway = gateway_selector.select(); + gateway = gateways_manager.select(); if(gateway != null && gateway.getProfile() != null) { mReceiver = EipFragment.getReceiver(); launchActiveGateway(); - tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK); + tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK); } else - tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED); + tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED); } /** @@ -184,173 +153,39 @@ public final class EIP extends IntentService { */ private void updateEIPService() { eip_definition = eipDefinitionFromPreferences(); - if(eip_definition != null) + if(eip_definition.length() > 0) updateGateways(); tellToReceiver(ACTION_UPDATE_EIP_SERVICE, Activity.RESULT_OK); } private JSONObject eipDefinitionFromPreferences() { + JSONObject result = new JSONObject(); try { String eip_definition_string = preferences.getString(KEY, ""); if(!eip_definition_string.isEmpty()) { - return new JSONObject(eip_definition_string); + result = new JSONObject(eip_definition_string); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } - return null; - } - - private List gatewaysFromPreferences() { - List result; - - String gateways_string = preferences.getString(Gateway.TAG, ""); - Type type_list_gateways = new TypeToken>() {}.getType(); - result = gateways_string.isEmpty() ? - new ArrayList() - : (List) new Gson().fromJson(gateways_string, type_list_gateways); - preferences.edit().remove(Gateway.TAG); - return result; - } - - /** - * Walk the list of gateways defined in eip-service.json and parse them into - * Gateway objects. - */ - private void updateGateways(){ - try { - JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways"); - for (int i = 0; i < gatewaysDefined.length(); i++) { - JSONObject gw = gatewaysDefined.getJSONObject(i); - if (isOpenVpnGateway(gw)) { - JSONObject secrets = secretsConfiguration(); - Gateway aux = new Gateway(eip_definition, secrets, gw); - if(!containsProfileWithSecrets(aux.getProfile())) { - addGateway(aux); - } - } - } - gatewaysToPreferences(); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - private boolean isOpenVpnGateway(JSONObject gateway) { - try { - String transport = gateway.getJSONObject("capabilities").getJSONArray("transport").toString(); - return transport.contains("openvpn"); - } catch (JSONException e) { - return false; - } - } - - - private JSONObject secretsConfiguration() { - JSONObject result = new JSONObject(); - try { - result.put(Provider.CA_CERT, preferences.getString(Provider.CA_CERT, "")); - result.put(Constants.PRIVATE_KEY, preferences.getString(Constants.PRIVATE_KEY, "")); - result.put(Constants.CERTIFICATE, preferences.getString(Constants.CERTIFICATE, "")); - } catch (JSONException e) { - e.printStackTrace(); - } return result; } - private void addGateway(Gateway gateway) { - VpnProfile profile = gateway.getProfile(); - removeGateway(gateway); - - profile_manager.addProfile(profile); - profile_manager.saveProfile(context, profile); - profile_manager.saveProfileList(context); - - gateways.add(gateway); - } - - private void removeGateway(Gateway gateway) { - VpnProfile profile = gateway.getProfile(); - removeDuplicatedProfile(profile); - removeDuplicatedGateway(profile); - } - - private void removeDuplicatedProfile(VpnProfile original) { - if(containsProfile(original)) { - VpnProfile remove = duplicatedProfile(original); - profile_manager.removeProfile(context, remove); - }if(containsProfile(original)) removeDuplicatedProfile(original); - } - - private boolean containsProfile(VpnProfile profile) { - Collection profiles = profile_manager.getProfiles(); - for(VpnProfile aux : profiles) { - if (sameConnections(profile.mConnections, aux.mConnections)) { - return true; - } - } - return false; - } - - private boolean containsProfileWithSecrets(VpnProfile profile) { - boolean result = false; - - if(containsProfile(profile)) { - Collection profiles = profile_manager.getProfiles(); - for(VpnProfile aux : profiles) { - result = result == false ? - sameConnections(profile.mConnections, aux.mConnections) - && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) - && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename) - : true; - } - } - return result; - } - - private VpnProfile duplicatedProfile(VpnProfile profile) { - VpnProfile duplicated = null; - Collection profiles = profile_manager.getProfiles(); - for(VpnProfile aux : profiles) { - if (sameConnections(profile.mConnections, aux.mConnections)) { - duplicated = aux; - } - } - if(duplicated != null) return duplicated; - else throw new NoSuchElementException(profile.getName()); - } - - private boolean sameConnections(Connection[] c1, Connection[] c2) { - int same_connections = 0; - for(Connection c1_aux : c1) { - for(Connection c2_aux : c2) - if(c2_aux.mServerName.equals(c1_aux.mServerName)) { - same_connections++; - break; - } - } - return c1.length == c2.length && c1.length == same_connections; - + private void updateGateways(){ + gateways_manager.fromEipServiceJson(eip_definition); + gatewaysToPreferences(); } - private void removeDuplicatedGateway(VpnProfile profile) { - Iterator it = gateways.iterator(); - List gateways_to_remove = new ArrayList<>(); - while(it.hasNext()) { - Gateway aux = it.next(); - if(sameConnections(aux.getProfile().mConnections, profile.mConnections)) { - gateways_to_remove.add(aux); - } - } - gateways.removeAll(gateways_to_remove); + private void gatewaysFromPreferences() { + String gateways_string = preferences.getString(Gateway.TAG, ""); + gateways_manager.addFromString(gateways_string); + preferences.edit().remove(Gateway.TAG).apply(); } private void gatewaysToPreferences() { - Type type_list_gateways = new TypeToken>() {}.getType(); - String gateways_string = new Gson().toJson(gateways, type_list_gateways); - preferences.edit().putString(Gateway.TAG, gateways_string).apply(); + String gateways_string = gateways_manager.toString(); + preferences.edit().putString(Gateway.TAG, gateways_string).commit(); } private void checkCertValidity() { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java index daf7d4a7..0d8a2f7b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -20,6 +20,8 @@ import android.app.Activity; import android.content.SharedPreferences; import android.util.Log; +import com.google.gson.Gson; + import org.json.JSONException; import org.json.JSONObject; @@ -40,7 +42,7 @@ import se.leap.bitmaskclient.Dashboard; */ public class Gateway { - public static String TAG = Gateway.class.getSimpleName(); + public final static String TAG = Gateway.class.getSimpleName(); private JSONObject general_configuration; private JSONObject secrets; @@ -53,7 +55,7 @@ public class Gateway { * Build a gateway object from a JSON OpenVPN gateway definition in eip-service.json * and create a VpnProfile belonging to it. */ - protected Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway){ + public Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway){ this.gateway = gateway; this.secrets = secrets; @@ -130,4 +132,9 @@ public class Gateway { public int getTimezone() { return timezone; } + + @Override + public String toString() { + return new Gson().toJson(this, Gateway.class); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java new file mode 100644 index 00000000..b1aa5a2f --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -0,0 +1,184 @@ +/** + * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * + * 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 3 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, see . + */ +package se.leap.bitmaskclient.eip; + +import android.content.*; +import android.util.Log; + +import com.google.gson.*; +import com.google.gson.reflect.*; + +import org.json.*; + +import java.lang.reflect.*; +import java.util.*; + +import de.blinkt.openvpn.*; +import de.blinkt.openvpn.core.*; +import se.leap.bitmaskclient.*; + +/** + * @author parmegv + */ +public class GatewaysManager { + + private Context context; + private SharedPreferences preferences; + private List gateways = new ArrayList<>(); + private ProfileManager profile_manager; + private Type list_type = new TypeToken>() {}.getType(); + + public GatewaysManager() {} + + public GatewaysManager(Context context, SharedPreferences preferences) { + this.context = context; + this.preferences = preferences; + profile_manager = ProfileManager.getInstance(context); + } + public Gateway select() { + GatewaySelector gateway_selector = new GatewaySelector(gateways); + return gateway_selector.select(); + } + + public boolean isEmpty() { + return gateways.isEmpty(); + } + + public int size() { + return gateways.size(); + } + + public void addFromString(String gateways) { + List gateways_list = new ArrayList(); + try { + gateways_list = new Gson().fromJson(gateways, list_type); + } catch(JsonSyntaxException e) { + gateways_list.add(new Gson().fromJson(gateways, Gateway.class)); + } + + if(gateways_list != null) { + for (Gateway gateway : gateways_list) + removeDuplicatedGateway(gateway); + this.gateways.addAll(gateways_list); + } else + Log.d("GatewaysManager", "No gateways added"); + } + + @Override + public String toString() { + return new Gson().toJson(gateways, list_type); + } + + public void fromEipServiceJson(JSONObject eip_definition) { + try { + JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways"); + for (int i = 0; i < gatewaysDefined.length(); i++) { + JSONObject gw = gatewaysDefined.getJSONObject(i); + if (isOpenVpnGateway(gw)) { + JSONObject secrets = secretsConfiguration(); + Gateway aux = new Gateway(eip_definition, secrets, gw); + if(!containsProfileWithSecrets(aux.getProfile())) { + addGateway(aux); + } + } + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + private boolean isOpenVpnGateway(JSONObject gateway) { + try { + String transport = gateway.getJSONObject("capabilities").getJSONArray("transport").toString(); + return transport.contains("openvpn"); + } catch (JSONException e) { + return false; + } + } + + private JSONObject secretsConfiguration() { + JSONObject result = new JSONObject(); + try { + result.put(Provider.CA_CERT, preferences.getString(Provider.CA_CERT, "")); + result.put(Constants.PRIVATE_KEY, preferences.getString(Constants.PRIVATE_KEY, "")); + result.put(Constants.CERTIFICATE, preferences.getString(Constants.CERTIFICATE, "")); + } catch (JSONException e) { + e.printStackTrace(); + } + return result; + } + + private boolean containsProfileWithSecrets(VpnProfile profile) { + boolean result = false; + + Collection profiles = profile_manager.getProfiles(); + for(VpnProfile aux : profiles) { + result = result || sameConnections(profile.mConnections, aux.mConnections) + && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) + && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename); + } + return result; + } + + private void addGateway(Gateway gateway) { + removeDuplicatedGateway(gateway); + + gateways.add(gateway); + + VpnProfile profile = gateway.getProfile(); + profile_manager.addProfile(profile); + //profile_manager.saveProfile(context, profile); + //profile_manager.saveProfileList(context); + } + + private void removeDuplicatedGateway(Gateway gateway) { + Iterator it = gateways.iterator(); + List gateways_to_remove = new ArrayList<>(); + while(it.hasNext()) { + Gateway aux = it.next(); + if(sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) { + gateways_to_remove.add(aux); + } + } + gateways.removeAll(gateways_to_remove); + removeDuplicatedProfiles(gateway.getProfile()); + } + + private void removeDuplicatedProfiles(VpnProfile original) { + Collection profiles = profile_manager.getProfiles(); + List remove_list = new ArrayList<>(); + for(VpnProfile aux : profiles) { + if (sameConnections(original.mConnections, aux.mConnections)) + remove_list.add(aux); + } + for (VpnProfile profile : remove_list) + profile_manager.removeProfile(context, profile); + } + + private boolean sameConnections(Connection[] c1, Connection[] c2) { + int same_connections = 0; + for(Connection c1_aux : c1) { + for(Connection c2_aux : c2) + if(c2_aux.mServerName.equals(c1_aux.mServerName)) { + same_connections++; + break; + } + } + return c1.length == c2.length && c1.length == same_connections; + } +} -- cgit v1.2.3