From dd6e5a29f40f38481dec5545ff4fe1cabd5d6384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 12 Dec 2014 10:43:54 +0100 Subject: Beginning the tests for EIP. --- app/src/main/java/se/leap/bitmaskclient/eip/EIP.java | 4 ++-- .../java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'app/src/main/java') 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 533b0281..a86b9c6a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -354,8 +354,8 @@ public final class EIP extends IntentService { } private void checkCertValidity() { - VpnCertificateValidator validator = new VpnCertificateValidator(); - int resultCode = validator.isValid(preferences.getString(CERTIFICATE, "")) ? + VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(CERTIFICATE, "")); + int resultCode = validator.isValid() ? Activity.RESULT_OK : Activity.RESULT_CANCELED; tellToReceiver(ACTION_CHECK_CERT_VALIDITY, resultCode); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java index 6487f6c1..0bbe9db4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -28,7 +28,13 @@ import se.leap.bitmaskclient.ConfigHelper; public class VpnCertificateValidator { public final static String TAG = VpnCertificateValidator.class.getSimpleName(); - public boolean isValid(String certificate) { + private String certificate; + + public VpnCertificateValidator(String certificate) { + this.certificate = certificate; + } + + public boolean isValid() { if(!certificate.isEmpty()) { X509Certificate certificate_x509 = ConfigHelper.parseX509CertificateFromString(certificate); return isValid(certificate_x509); -- cgit v1.2.3 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 --- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 239 ++++----------------- .../java/se/leap/bitmaskclient/eip/Gateway.java | 11 +- .../se/leap/bitmaskclient/eip/GatewaysManager.java | 184 ++++++++++++++++ 3 files changed, 230 insertions(+), 204 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java (limited to 'app/src/main/java') 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 From b3ea137781af2eacd0a5b75f24030cfbc3c9d962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 13 Jan 2015 13:48:34 +0100 Subject: Reusing methods to download certificate. This makes sure that if a a certificate is not valid, a new one is downloaded. Unit tests don't apply to this, I've already added one to the certificate validator, whose result will determine if a new certificate will be downloaded. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 21 +++++---- .../java/se/leap/bitmaskclient/EipFragment.java | 54 ++++++++++------------ 2 files changed, 36 insertions(+), 39 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 4e6120ab..94be7cd1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -296,11 +296,14 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn providerApiCommand(Bundle.EMPTY, R.string.logout_message, ProviderAPI.LOG_OUT); } - private void downloadAuthedUserCertificate() { - Bundle parameters = new Bundle(); - parameters.putString(ConfigurationWizard.TYPE_OF_CERTIFICATE, ConfigurationWizard.AUTHED_CERTIFICATE); - - providerApiCommand(parameters, R.string.downloading_certificate_message, ProviderAPI.DOWNLOAD_CERTIFICATE); + protected void downloadVpnCertificate() { + boolean is_authenticated = !LeapSRPSession.getToken().isEmpty(); + boolean allowed_anon = preferences.getBoolean(Constants.ALLOWED_ANON, false); + if(allowed_anon || is_authenticated) + providerApiCommand(Bundle.EMPTY, R.string.downloading_certificate_message, ProviderAPI.DOWNLOAD_CERTIFICATE); + else + sessionDialog(Bundle.EMPTY); + } private Bundle bundleParameters(String username, String password) { @@ -312,8 +315,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn return parameters; } - private void providerApiCommand(Bundle parameters, int progressbar_message_resId, String providerApi_action) { - if(eip_fragment != null) { + protected void providerApiCommand(Bundle parameters, int progressbar_message_resId, String providerApi_action) { + if(eip_fragment != null && progressbar_message_resId != 0) { eip_fragment.progress_bar.setVisibility(ProgressBar.VISIBLE); setStatusMessage(progressbar_message_resId); } @@ -322,7 +325,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn startService(command); } - protected Intent prepareProviderAPICommand(Bundle parameters, String action) { + private Intent prepareProviderAPICommand(Bundle parameters, String action) { providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); @@ -371,7 +374,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); updateViewHidingProgressBar(resultCode); - downloadAuthedUserCertificate(); + downloadVpnCertificate(); } else if(resultCode == ProviderAPI.FAILED_LOGIN) { updateViewHidingProgressBar(resultCode); sessionDialog(resultData); diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 54432033..bbcaa577 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -45,7 +45,7 @@ public class EipFragment extends Fragment implements Observer { @InjectView(R.id.eipProgress) ProgressBar progress_bar; - private static Activity parent_activity; + private static Dashboard dashboard; private static EIPReceiver mEIPReceiver; private static EipStatus eip_status; private boolean is_starting_to_connect; @@ -53,11 +53,9 @@ public class EipFragment extends Fragment implements Observer { @Override public void onAttach(Activity activity) { super.onAttach(activity); - parent_activity = activity; - Dashboard dashboard = (Dashboard) parent_activity; - Intent provider_API_command = dashboard.prepareProviderAPICommand(Bundle.EMPTY, ProviderAPI.DOWNLOAD_EIP_SERVICE); - parent_activity.startService(provider_API_command); + dashboard = (Dashboard) activity; + dashboard.providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.DOWNLOAD_EIP_SERVICE); } @Override @@ -130,7 +128,6 @@ public class EipFragment extends Fragment implements Observer { startEipFromScratch(); else if(canLogInToStartEIP()) { Log.d(TAG, "Can Log In to start EIP"); - Dashboard dashboard = (Dashboard) parent_activity; Bundle bundle = new Bundle(); bundle.putBoolean(IS_PENDING, true); dashboard.sessionDialog(bundle); @@ -160,16 +157,16 @@ public class EipFragment extends Fragment implements Observer { } private void askPendingStartCancellation() { - AlertDialog.Builder alertBuilder = new AlertDialog.Builder(parent_activity); - alertBuilder.setTitle(parent_activity.getString(R.string.eip_cancel_connect_title)) - .setMessage(parent_activity.getString(R.string.eip_cancel_connect_text)) + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(dashboard); + alertBuilder.setTitle(dashboard.getString(R.string.eip_cancel_connect_title)) + .setMessage(dashboard.getString(R.string.eip_cancel_connect_text)) .setPositiveButton((R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { askToStopEIP(); } }) - .setNegativeButton(parent_activity.getString(R.string.no), new DialogInterface.OnClickListener() { + .setNegativeButton(dashboard.getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { eip_switch.setChecked(true); @@ -182,7 +179,7 @@ public class EipFragment extends Fragment implements Observer { is_starting_to_connect = true; progress_bar.setVisibility(View.VISIBLE); eip_switch.setVisibility(View.VISIBLE); - String status = parent_activity.getString(R.string.eip_status_start_pending); + String status = dashboard.getString(R.string.eip_status_start_pending); status_message.setText(status); if(!eip_switch.isChecked()) { @@ -195,15 +192,15 @@ public class EipFragment extends Fragment implements Observer { private void stopEIP() { if(eip_status.isConnecting()) VoidVpnService.stop(); - Intent disconnect_vpn = new Intent(parent_activity, DisconnectVPN.class); - parent_activity.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); + Intent disconnect_vpn = new Intent(dashboard, DisconnectVPN.class); + dashboard.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); eip_status.setDisconnecting(); } protected void askToStopEIP() { hideProgressBar(); - String status = parent_activity.getString(R.string.eip_state_not_connected); + String status = dashboard.getString(R.string.eip_state_not_connected); status_message.setText(status); eipCommand(Constants.ACTION_STOP_EIP); @@ -217,10 +214,10 @@ public class EipFragment extends Fragment implements Observer { */ private void eipCommand(String action){ // TODO validate "action"...how do we get the list of intent-filters for a class via Android API? - Intent vpn_intent = new Intent(parent_activity.getApplicationContext(), EIP.class); + Intent vpn_intent = new Intent(dashboard.getApplicationContext(), EIP.class); vpn_intent.setAction(action); vpn_intent.putExtra(Constants.RECEIVER_TAG, mEIPReceiver); - parent_activity.startService(vpn_intent); + dashboard.startService(vpn_intent); } @Override @@ -228,7 +225,7 @@ public class EipFragment extends Fragment implements Observer { if(observable instanceof EipStatus) { eip_status = (EipStatus) observable; final EipStatus eip_status = (EipStatus) observable; - parent_activity.runOnUiThread(new Runnable() { + dashboard.runOnUiThread(new Runnable() { @Override public void run() { handleNewState(eip_status); @@ -254,13 +251,13 @@ public class EipFragment extends Fragment implements Observer { Log.d(TAG, "setConnectedUi? " + eip_status.isConnected()); adjustSwitch(); is_starting_to_connect = false; - status_message.setText(parent_activity.getString(R.string.eip_state_connected)); + status_message.setText(dashboard.getString(R.string.eip_state_connected)); } private void setDisconnectedUI(){ hideProgressBar(); adjustSwitch(); - status_message.setText(parent_activity.getString(R.string.eip_state_not_connected)); + status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { @@ -281,13 +278,18 @@ public class EipFragment extends Fragment implements Observer { private void setInProgressUI(EipStatus eip_status) { int localizedResId = eip_status.getLocalizedResId(); String logmessage = eip_status.getLogMessage(); - String prefix = parent_activity.getString(localizedResId); + String prefix = dashboard.getString(localizedResId); status_message.setText(prefix + " " + logmessage); is_starting_to_connect = false; adjustSwitch(); } + private void updatingCertificateUI() { + progress_bar.setVisibility(View.VISIBLE); + status_message.setText(getString(R.string.updating_certificate_message)); + } + private void hideProgressBar() { if(progress_bar != null) progress_bar.setVisibility(View.GONE); @@ -333,16 +335,8 @@ public class EipFragment extends Fragment implements Observer { case Activity.RESULT_OK: break; case Activity.RESULT_CANCELED: - Dashboard dashboard = (Dashboard) parent_activity; - - progress_bar.setVisibility(View.VISIBLE); - status_message.setText(getString(R.string.updating_certificate_message)); - if(LeapSRPSession.getToken().isEmpty() && !Dashboard.preferences.getBoolean(Constants.ALLOWED_ANON, false)) { - dashboard.sessionDialog(Bundle.EMPTY); - } else { - Intent provider_API_command = dashboard.prepareProviderAPICommand(Bundle.EMPTY, ProviderAPI.DOWNLOAD_CERTIFICATE); - parent_activity.startService(provider_API_command); - } + updatingCertificateUI(); + dashboard.downloadVpnCertificate(); break; } } -- cgit v1.2.3 From 744272dd1d43913c8112d1dedcb0b5a9023c491e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 14 Jan 2015 17:18:19 +0100 Subject: Connect if logged in after turning switch on --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 49 ++++++++++------------ .../java/se/leap/bitmaskclient/EipFragment.java | 20 ++++++++- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 5 +-- 3 files changed, 42 insertions(+), 32 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 94be7cd1..afe1a638 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -127,33 +127,28 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn case 91: // 0.6.0 without Bug #5999 case 101: // 0.8.0 if(!preferences.getString(Constants.KEY, "").isEmpty()) - updateEipService(); + eip_fragment.updateEipService(); break; } } catch (NameNotFoundException e) { Log.d(TAG, "Handle version didn't find any " + getPackageName() + " package"); } } - - @SuppressLint("CommitPrefEdits") + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ if ( requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { - if ( resultCode == RESULT_OK ) { - preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); - updateEipService(); - - if (data.hasExtra(Provider.KEY)) { - provider = data.getParcelableExtra(Provider.KEY); - preferences.edit().putBoolean(Constants.PROVIDER_CONFIGURED, true).commit(); - preferences.edit().putString(Provider.MAIN_URL, provider.mainUrl().toString()).apply(); - preferences.edit().putString(Provider.KEY, provider.definition().toString()).apply(); - } - buildDashboard(false); - invalidateOptionsMenu(); - if (data.hasExtra(SessionDialog.TAG)) { - sessionDialog(Bundle.EMPTY); - } + if ( resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { + provider = data.getParcelableExtra(Provider.KEY); + providerToPreferences(provider); + + buildDashboard(false); + invalidateOptionsMenu(); + if (data.hasExtra(SessionDialog.TAG)) { + sessionDialog(Bundle.EMPTY); + } + + preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { finish(); } else @@ -163,6 +158,13 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } } + @SuppressLint("CommitPrefEdits") + private void providerToPreferences(Provider provider) { + preferences.edit().putBoolean(Constants.PROVIDER_CONFIGURED, true).commit(); + preferences.edit().putString(Provider.MAIN_URL, provider.mainUrl().toString()).apply(); + preferences.edit().putString(Provider.KEY, provider.definition().toString()).apply(); + } + private void configErrorDialog() { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getAppContext()); alertBuilder.setTitle(getResources().getString(R.string.setup_error_title)); @@ -389,16 +391,15 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn setResult(RESULT_CANCELED); } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { updateViewHidingProgressBar(resultCode); - updateEipService(); + eip_fragment.updateEipService(); setResult(RESULT_OK); } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { updateViewHidingProgressBar(resultCode); setResult(RESULT_CANCELED); } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { + eip_fragment.updateEipService(); setResult(RESULT_OK); - - updateEipService(); } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { setResult(RESULT_CANCELED); } @@ -410,12 +411,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn invalidateOptionsMenu(); } - private void updateEipService() { - Intent updateEIP = new Intent(getApplicationContext(), EIP.class); - updateEIP.setAction(Constants.ACTION_UPDATE_EIP_SERVICE); - startService(updateEIP); - } - private void changeStatusMessage(final int previous_result_code) { ResultReceiver status_receiver = new ResultReceiver(new Handler()){ protected void onReceiveResult(int resultCode, Bundle resultData){ diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index bbcaa577..b2898249 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -49,6 +49,7 @@ public class EipFragment extends Fragment implements Observer { private static EIPReceiver mEIPReceiver; private static EipStatus eip_status; private boolean is_starting_to_connect; + private boolean wants_to_connect; @Override public void onAttach(Activity activity) { @@ -127,7 +128,7 @@ public class EipFragment extends Fragment implements Observer { if(canStartEIP()) startEipFromScratch(); else if(canLogInToStartEIP()) { - Log.d(TAG, "Can Log In to start EIP"); + wants_to_connect = true; Bundle bundle = new Bundle(); bundle.putBoolean(IS_PENDING, true); dashboard.sessionDialog(bundle); @@ -176,6 +177,7 @@ public class EipFragment extends Fragment implements Observer { } public void startEipFromScratch() { + wants_to_connect = false; is_starting_to_connect = true; progress_bar.setVisibility(View.VISIBLE); eip_switch.setVisibility(View.VISIBLE); @@ -205,6 +207,10 @@ public class EipFragment extends Fragment implements Observer { eipCommand(Constants.ACTION_STOP_EIP); } + + protected void updateEipService() { + eipCommand(Constants.ACTION_UPDATE_EIP_SERVICE); + } /** * Send a command to EIP @@ -339,7 +345,17 @@ public class EipFragment extends Fragment implements Observer { dashboard.downloadVpnCertificate(); break; } - } + } else if (request.equals(Constants.ACTION_UPDATE_EIP_SERVICE)) { + switch (resultCode) { + case Activity.RESULT_OK: + if(wants_to_connect) + startEipFromScratch(); + break; + case Activity.RESULT_CANCELED: + handleNewState(eip_status); + break; + } + } } } 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 a5cc817a..3b72a486 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -64,10 +64,8 @@ public final class EIP extends IntentService { context = getApplicationContext(); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); eip_definition = eipDefinitionFromPreferences(); - if(gateways_manager.isEmpty()) { - gateways_manager = new GatewaysManager(context, preferences); + if(gateways_manager.isEmpty()) gatewaysFromPreferences(); - } } @Override @@ -179,6 +177,7 @@ public final class EIP extends IntentService { private void gatewaysFromPreferences() { String gateways_string = preferences.getString(Gateway.TAG, ""); + gateways_manager = new GatewaysManager(context, preferences); gateways_manager.addFromString(gateways_string); preferences.edit().remove(Gateway.TAG).apply(); } -- cgit v1.2.3 From 250b56762857f2e52626236d3716459786ce126d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 15 Jan 2015 15:24:52 +0100 Subject: More tests, sometimes they don't pass sometimes do testIsValid depends on the speed of Runtime's exec: sometimes it's quick and passes (because it changes the date correctly) and sometimes is not. A waitFor doesn't fix the issue, it waits forever. --- app/src/main/java/se/leap/bitmaskclient/EipFragment.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index b2898249..588b137b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -37,7 +37,6 @@ public class EipFragment extends Fragment implements Observer { protected static final String STATUS_MESSAGE = TAG + ".status_message"; public static final String START_ON_BOOT = "start on boot"; - private View view; @InjectView(R.id.eipSwitch) Switch eip_switch; @InjectView(R.id.status_message) @@ -69,7 +68,7 @@ public class EipFragment extends Fragment implements Observer { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - view = inflater.inflate(R.layout.eip_service_fragment, container, false); + View view = inflater.inflate(R.layout.eip_service_fragment, container, false); ButterKnife.inject(this, view); if (eip_status.isConnecting()) -- cgit v1.2.3