From 767bb8de42b9529b60858fc43e07b1d050ccd785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 9 Mar 2015 12:05:32 +0100 Subject: Eip status disconnects void vpn when disconnecting. --- app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 4ac3bd6a..f3f6e16f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -50,9 +50,11 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { current_status.setLocalizedResId(localizedResId); current_status.setLevel(level); current_status.setChanged(); - if(isConnected() || isDisconnected()) + if(isConnected() || isDisconnected()) { setConnectedOrDisconnected(); - else if(isConnecting()) + if(isDisconnected()) + VoidVpnService.stop(); + } else if(isConnecting()) setConnecting(); Log.d(TAG, "update state with level " + level); current_status.notifyObservers(); -- cgit v1.2.3 From 767f2982ec62e071396226415f5abda2fb6cb3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 9 Mar 2015 12:15:09 +0100 Subject: Extracted a new method, refactoring. --- .../main/java/se/leap/bitmaskclient/eip/EipStatus.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index f3f6e16f..c6735130 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -44,12 +44,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { @Override public void updateState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { - current_status = getInstance(); - current_status.setState(state); - current_status.setLogMessage(logmessage); - current_status.setLocalizedResId(localizedResId); - current_status.setLevel(level); - current_status.setChanged(); + updateStatus(state, logmessage, localizedResId, level); if(isConnected() || isDisconnected()) { setConnectedOrDisconnected(); if(isDisconnected()) @@ -57,7 +52,15 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { } else if(isConnecting()) setConnecting(); Log.d(TAG, "update state with level " + level); - current_status.notifyObservers(); + } + + private void updateStatus(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { + current_status = getInstance(); + current_status.setState(state); + current_status.setLogMessage(logmessage); + current_status.setLocalizedResId(localizedResId); + current_status.setLevel(level); + current_status.setChanged(); } public boolean wantsToDisconnect() { -- cgit v1.2.3 From 6e245043236476b0d440732a729a47304dddce96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 9 Mar 2015 14:58:33 +0100 Subject: Just say it's authenticated, nothing about certificates --- app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 1 - app/src/main/res/values/strings.xml | 2 -- 2 files changed, 3 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 6d368e11..a4197eaf 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -422,7 +422,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn switch(previous_result_code){ case ProviderAPI.SUCCESSFUL_LOGIN: setStatusMessage(R.string.succesful_authentication_message); break; case ProviderAPI.FAILED_LOGIN: setStatusMessage(R.string.authentication_failed_message); break; - case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: setStatusMessage(R.string.authed_secured_status); break; case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: setStatusMessage(R.string.incorrectly_downloaded_certificate_message); break; case ProviderAPI.SUCCESSFUL_LOGOUT: setStatusMessage(R.string.logged_out_message); break; case ProviderAPI.LOGOUT_FAILED: setStatusMessage(R.string.log_out_failed_message); break; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1608f487..709d92b5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,8 +16,6 @@ Provider: No provider configured Status unknown. - Connection secure using an anonymous certificate. - Connection secure using your own certificate. Encrypted Internet Select a service provider Add new Provider -- cgit v1.2.3 From 49f906005c2d80dab2751d4e6beff100e99586c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 10 Mar 2015 10:08:46 +0100 Subject: Logged in == leapsrpsession has a token --- app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 15 +++------------ .../main/java/se/leap/bitmaskclient/LeapSRPSession.java | 6 +++++- .../main/java/se/leap/bitmaskclient/eip/Constants.java | 1 - 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index a4197eaf..4fe51939 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -63,7 +63,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn EipFragment eip_fragment; private Provider provider; - private static boolean authed_eip; public ProviderAPIResultReceiver providerAPI_result_receiver; private boolean switching_provider; @@ -148,7 +147,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn sessionDialog(Bundle.EMPTY); } - preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { finish(); } else @@ -222,12 +220,11 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn if(!provider_json_string.isEmpty()) { provider_json = new JSONObject(provider_json_string); JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - boolean authed_eip = !LeapSRPSession.getToken().isEmpty(); boolean allow_registered_eip = service_description.getBoolean(Provider.ALLOW_REGISTRATION); preferences.edit().putBoolean(Constants.ALLOWED_REGISTERED, allow_registered_eip).apply(); if(allow_registered_eip) { - if(authed_eip) { + if(LeapSRPSession.loggedIn()) { menu.findItem(R.id.login_button).setVisible(false); menu.findItem(R.id.logout_button).setVisible(true); } else { @@ -265,7 +262,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn return true; case R.id.switch_provider: switching_provider = true; - if (preferences.getBoolean(Constants.AUTHED_EIP, false)) logOut(); + if (LeapSRPSession.loggedIn()) logOut(); else switchProvider(); return true; case R.id.login_button: @@ -299,7 +296,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } protected void downloadVpnCertificate() { - boolean is_authenticated = !LeapSRPSession.getToken().isEmpty(); + boolean is_authenticated = LeapSRPSession.loggedIn(); 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); @@ -373,18 +370,12 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn updateViewHidingProgressBar(resultCode); sessionDialog(resultData); } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { - authed_eip = true; - preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); - updateViewHidingProgressBar(resultCode); downloadVpnCertificate(); } else if(resultCode == ProviderAPI.FAILED_LOGIN) { updateViewHidingProgressBar(resultCode); sessionDialog(resultData); } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { - authed_eip = false; - preferences.edit().putBoolean(Constants.AUTHED_EIP, authed_eip).apply(); - updateViewHidingProgressBar(resultCode); if(switching_provider) switchProvider(); } else if(resultCode == ProviderAPI.LOGOUT_FAILED) { diff --git a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java index 989dc395..09855baf 100644 --- a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java +++ b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +package se.leap.bitmaskclient; import org.jboss.security.srp.SRPParameters; @@ -331,6 +331,10 @@ public class LeapSRPSession { return token; } + protected static boolean loggedIn() { + return !token.isEmpty(); + } + /** * @return a new SHA-256 digest. */ diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java index 12c2e015..f7206927 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java @@ -27,7 +27,6 @@ public interface Constants { public final static String TAG = Constants.class.getSimpleName(); - public final static String AUTHED_EIP = TAG + ".AUTHED_EIP"; public final static String ACTION_CHECK_CERT_VALIDITY = TAG + ".CHECK_CERT_VALIDITY"; public final static String ACTION_START_EIP = TAG + ".START_EIP"; public final static String ACTION_STOP_EIP = TAG + ".STOP_EIP"; -- cgit v1.2.3 From 8a53bf0c88498059751693695775788874714fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 10 Mar 2015 11:44:00 +0100 Subject: Point to the log if an error occurrs while connecting --- app/src/main/java/se/leap/bitmaskclient/EipFragment.java | 6 +++++- app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 588b137b..3023442a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -262,7 +262,11 @@ public class EipFragment extends Fragment implements Observer { private void setDisconnectedUI(){ hideProgressBar(); adjustSwitch(); - status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); + String last_log_message = eip_status.getLastLogMessage(dashboard.getApplicationContext()); + if(last_log_message.contains("error") || last_log_message.contains("ERROR")) + status_message.setText("An error occurred." + "\n" + "Please take a look at the log, from the menu"); + else + status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index c6735130..14158696 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -16,11 +16,12 @@ */ package se.leap.bitmaskclient.eip; -import android.util.Log; +import android.util.*; +import android.content.*; -import java.util.Observable; +import java.util.*; -import de.blinkt.openvpn.core.VpnStatus; +import de.blinkt.openvpn.core.*; public class EipStatus extends Observable implements VpnStatus.StateListener { public static String TAG = EipStatus.class.getSimpleName(); @@ -135,6 +136,11 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { EipStatus.level = level; } + public String getLastLogMessage(Context context) { + VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); + return log[log.length-1].getString(context); + } + @Override public String toString() { return "State: " + state + " Level: " + level.toString(); -- cgit v1.2.3 From 9732f263b3ea9af0c9610fa9b3f3010481a39e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 12 Mar 2015 11:47:28 +0100 Subject: Just show the log if an error occurred. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 19 ++++-- .../java/se/leap/bitmaskclient/EipFragment.java | 77 ++++++++++++---------- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 4fe51939..04385ddd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -249,16 +249,12 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override public boolean onOptionsItemSelected(MenuItem item){ - Intent intent; switch (item.getItemId()){ case R.id.about_leap: - intent = new Intent(this, AboutActivity.class); - startActivity(intent); + showAbout(); return true; case R.id.log_window: - Intent startLW = new Intent(getAppContext(), LogWindow.class); - startLW.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - startActivity(startLW); + showLog(); return true; case R.id.switch_provider: switching_provider = true; @@ -279,6 +275,17 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } } + public void showAbout() { + Intent intent = new Intent(this, AboutActivity.class); + startActivity(intent); + } + + public void showLog() { + Intent startLW = new Intent(getAppContext(), LogWindow.class); + //startLW.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + startActivity(startLW); + } + @Override public void signUp(String username, String password) { Bundle parameters = bundleParameters(username, password); diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 3023442a..680f21e1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -1,32 +1,33 @@ +/** + * Copyright (c) 2013 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; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.os.ResultReceiver; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ProgressBar; -import android.widget.Switch; -import android.widget.TextView; - -import java.util.Observable; -import java.util.Observer; - -import butterknife.ButterKnife; -import butterknife.InjectView; -import butterknife.OnCheckedChanged; -import de.blinkt.openvpn.activities.DisconnectVPN; -import se.leap.bitmaskclient.eip.Constants; -import se.leap.bitmaskclient.eip.EIP; -import se.leap.bitmaskclient.eip.EipStatus; -import se.leap.bitmaskclient.eip.VoidVpnService; +import android.app.*; +import android.content.*; +import android.os.*; +import android.util.*; +import android.view.*; +import android.widget.*; + +import java.util.*; + +import butterknife.*; +import de.blinkt.openvpn.activities.*; +import se.leap.bitmaskclient.eip.*; public class EipFragment extends Fragment implements Observer { @@ -193,6 +194,10 @@ public class EipFragment extends Fragment implements Observer { private void stopEIP() { if(eip_status.isConnecting()) VoidVpnService.stop(); + disconnect(); + } + + private void disconnect() { Intent disconnect_vpn = new Intent(dashboard, DisconnectVPN.class); dashboard.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); eip_status.setDisconnecting(); @@ -201,8 +206,8 @@ public class EipFragment extends Fragment implements Observer { protected void askToStopEIP() { hideProgressBar(); - String status = dashboard.getString(R.string.eip_state_not_connected); - status_message.setText(status); + String message = dashboard.getString(R.string.eip_state_not_connected); + status_message.setText(message); eipCommand(Constants.ACTION_STOP_EIP); } @@ -240,11 +245,10 @@ public class EipFragment extends Fragment implements Observer { } private void handleNewState(EipStatus eip_status) { - Log.d(TAG, "handleNewState: " + eip_status.toString()); if(eip_status.wantsToDisconnect()) setDisconnectedUI(); - else if(eip_status.isConnecting() || is_starting_to_connect) - setInProgressUI(eip_status); + else if(eip_status.isConnecting() || is_starting_to_connect) + setInProgressUI(eip_status); else if (eip_status.isConnected()) setConnectedUI(); else if (eip_status.isDisconnected() && !eip_status.isConnecting()) @@ -263,10 +267,11 @@ public class EipFragment extends Fragment implements Observer { hideProgressBar(); adjustSwitch(); String last_log_message = eip_status.getLastLogMessage(dashboard.getApplicationContext()); - if(last_log_message.contains("error") || last_log_message.contains("ERROR")) - status_message.setText("An error occurred." + "\n" + "Please take a look at the log, from the menu"); - else - status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); + if((last_log_message.contains("error") || last_log_message.contains("ERROR")) + && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ + dashboard.showLog(); + } + status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { -- cgit v1.2.3 From 4d0e03dc3e836418e8fc240e81e2a4d9ebc445a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 12 Mar 2015 11:57:23 +0100 Subject: Failed log out message a bit explained. Now that I fixed the internal inconsistencies of checking if the user is logged in, if this error happens it should be caused by the provider or the network itself. So I tell the user. --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 709d92b5..37969cda 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -58,7 +58,7 @@ Signing up Logging out from this session. Logged out. - Didn\'t log out. + Didn\'t log out. Try later, it may be a problem in the network or in the provider. Authentication succeeded. Authentication failed. Registration failed. -- cgit v1.2.3 From f62c2c0ef907744814b48a56d08a83a6fbe1bed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 12 Mar 2015 12:01:34 +0100 Subject: Suggest to wipe data if logging out fails repeatedly. --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 37969cda..032602cb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -58,7 +58,7 @@ Signing up Logging out from this session. Logged out. - Didn\'t log out. Try later, it may be a problem in the network or in the provider. + Didn\'t log out. Try later, it may be a problem in the network or in the provider. If the problem persists, then wipe Bitmask data from the Android settings Authentication succeeded. Authentication failed. Registration failed. -- cgit v1.2.3 From 037d65da739ed2630affe181894e640fa06d35b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 12 Mar 2015 12:54:18 +0100 Subject: Restore state on Configuration Wizard. --- .../java/se/leap/bitmaskclient/ConfigurationWizard.java | 13 +++++++++---- app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java | 2 +- .../java/se/leap/bitmaskclient/ConfigurationWizard.java | 13 ++++++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 27984147..d28bb690 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -95,7 +95,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); if(selected_provider != null) outState.putParcelable(Provider.KEY, selected_provider); - outState.putParcelable(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); super.onSaveInstanceState(outState); } @@ -112,7 +111,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download if(savedInstanceState != null) restoreState(savedInstanceState); - else setUpProviderAPIResultReceiver(); } @@ -121,8 +119,14 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download provider_name = savedInstanceState.getString(Provider.NAME, ""); selected_provider = savedInstanceState.getParcelable(Provider.KEY); progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); - providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); - providerAPI_result_receiver.setReceiver(this); + + if(fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { + if (selected_provider != null) + onItemSelectedUi(selected_provider); + if (progress > 0) + mProgressBar.setProgress(progress); + } + } @Override @@ -356,6 +360,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download DialogFragment newFragment = ProviderDetailFragment.newInstance(); newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); + setting_up_provider = false; } } diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index 0650f0ad..fa923948 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -675,7 +675,7 @@ public class ProviderAPI extends IntentService { String json_file_content = ""; URL provider_url = null; - int seconds_of_timeout = 1; + int seconds_of_timeout = 2; try { provider_url = new URL(string_url); URLConnection url_connection = provider_url.openConnection(); diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index abab106d..f5e78e65 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -96,7 +96,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); if(selected_provider != null) outState.putParcelable(Provider.KEY, selected_provider); - outState.putParcelable(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); super.onSaveInstanceState(outState); } @@ -113,7 +112,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download if (savedInstanceState != null) restoreState(savedInstanceState); - else setUpProviderAPIResultReceiver(); } @@ -121,9 +119,13 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download progressbar_text = savedInstanceState.getString(PROGRESSBAR_TEXT, ""); provider_name = savedInstanceState.getString(Provider.NAME, ""); selected_provider = savedInstanceState.getParcelable(Provider.KEY); - progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); - providerAPI_result_receiver = savedInstanceState.getParcelable(ProviderAPI.RECEIVER_KEY); - providerAPI_result_receiver.setReceiver(this); + + if(fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { + if (selected_provider != null) + onItemSelectedUi(selected_provider); + if (progress > 0) + mProgressBar.setProgress(progress); + } } @Override @@ -355,6 +357,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download DialogFragment newFragment = ProviderDetailFragment.newInstance(); newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); + setting_up_provider = false; } } -- cgit v1.2.3 From 06b98e2c28f93f2294f3e6f88ba3812e8b8751e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 12 Mar 2015 13:50:10 +0100 Subject: Show progress bar if connecting. --- app/src/main/java/se/leap/bitmaskclient/EipFragment.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 680f21e1..6dbdf961 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -294,16 +294,22 @@ public class EipFragment extends Fragment implements Observer { String logmessage = eip_status.getLogMessage(); String prefix = dashboard.getString(localizedResId); + showProgressBar(); status_message.setText(prefix + " " + logmessage); is_starting_to_connect = false; adjustSwitch(); } private void updatingCertificateUI() { - progress_bar.setVisibility(View.VISIBLE); + showProgressBar(); status_message.setText(getString(R.string.updating_certificate_message)); } + private void showProgressBar() { + if(progress_bar != null) + progress_bar.setVisibility(View.VISIBLE); + } + private void hideProgressBar() { if(progress_bar != null) progress_bar.setVisibility(View.GONE); -- cgit v1.2.3 From 6738a3f17cb38febfa1c698d6b420aed9e17ad74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 13 Mar 2015 14:22:25 +0100 Subject: Show user session status separately. Switching orientation doesn't hide that information, now we have two places to avoid it. --- .../java/se/leap/bitmaskclient/ProviderAPI.java | 100 ++++++------ .../main/java/se/leap/bitmaskclient/Dashboard.java | 179 ++++++++++++--------- .../java/se/leap/bitmaskclient/EipFragment.java | 22 +-- .../java/se/leap/bitmaskclient/LeapSRPSession.java | 3 +- .../main/java/se/leap/bitmaskclient/Provider.java | 8 + app/src/main/java/se/leap/bitmaskclient/User.java | 41 +++++ .../se/leap/bitmaskclient/UserSessionStatus.java | 65 ++++++++ .../se/leap/bitmaskclient/eip/VoidVpnService.java | 3 +- app/src/main/res/layout/dashboard.xml | 15 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 26 +-- 10 files changed, 309 insertions(+), 153 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/User.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index fa923948..9ce0e99f 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -49,8 +49,8 @@ public class ProviderAPI extends IntentService { TAG = ProviderAPI.class.getSimpleName(), SET_UP_PROVIDER = "setUpProvider", DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON", - SRP_REGISTER = "srpRegister", - SRP_AUTH = "srpAuth", + SIGN_UP = "srpRegister", + LOG_IN = "srpAuth", LOG_OUT = "logOut", DOWNLOAD_CERTIFICATE = "downloadUserAuthedCertificate", PARAMETERS = "parameters", @@ -118,11 +118,11 @@ public class ProviderAPI extends IntentService { return "{ \"" + ERRORS + "\" : \""+getResources().getString(toast_string_id)+"\" }"; } - @Override - protected void onHandleIntent(Intent command) { - final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); - String action = command.getAction(); - Bundle parameters = command.getBundleExtra(PARAMETERS); + @Override + protected void onHandleIntent(Intent command) { + final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); + String action = command.getAction(); + Bundle parameters = command.getBundleExtra(PARAMETERS); if(provider_api_url == null) { try { JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "no provider")); @@ -133,42 +133,48 @@ public class ProviderAPI extends IntentService { } } - if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { - Bundle result = setUpProvider(parameters); - if(go_ahead) { - if(result.getBoolean(RESULT_KEY)) { - receiver.send(PROVIDER_OK, result); - } else { - receiver.send(PROVIDER_NOK, result); - } - } - } else if (action.equalsIgnoreCase(SRP_REGISTER)) { - Bundle session_id_bundle = tryToRegister(parameters); - if(session_id_bundle.getBoolean(RESULT_KEY)) { - receiver.send(SUCCESSFUL_SIGNUP, session_id_bundle); - } else { - receiver.send(FAILED_SIGNUP, session_id_bundle); - } - } else if (action.equalsIgnoreCase(SRP_AUTH)) { - Bundle session_id_bundle = tryToAuthenticate(parameters); - if(session_id_bundle.getBoolean(RESULT_KEY)) { - receiver.send(SUCCESSFUL_LOGIN, session_id_bundle); - } else { - receiver.send(FAILED_LOGIN, session_id_bundle); - } - } else if (action.equalsIgnoreCase(LOG_OUT)) { - if(logOut()) { - receiver.send(SUCCESSFUL_LOGOUT, Bundle.EMPTY); - } else { - receiver.send(LOGOUT_FAILED, Bundle.EMPTY); - } - } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { - if(updateVpnCertificate()) { - receiver.send(CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); - } else { - receiver.send(INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); - } - } else if(action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { + if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { + Bundle result = setUpProvider(parameters); + if(go_ahead) { + if(result.getBoolean(RESULT_KEY)) { + receiver.send(PROVIDER_OK, result); + } else { + receiver.send(PROVIDER_NOK, result); + } + } + } else if (action.equalsIgnoreCase(SIGN_UP)) { + Bundle result = tryToRegister(parameters); + if(result.getBoolean(RESULT_KEY)) { + receiver.send(SUCCESSFUL_SIGNUP, result); + } else { + receiver.send(FAILED_SIGNUP, result); + } + } else if (action.equalsIgnoreCase(LOG_IN)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_IN); + Bundle result = tryToAuthenticate(parameters); + if(result.getBoolean(RESULT_KEY)) { + receiver.send(SUCCESSFUL_LOGIN, result); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_IN); + } else { + receiver.send(FAILED_LOGIN, result); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.NOT_LOGGED_IN); + } + } else if (action.equalsIgnoreCase(LOG_OUT)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_OUT); + if(logOut()) { + receiver.send(SUCCESSFUL_LOGOUT, Bundle.EMPTY); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_OUT); + } else { + receiver.send(LOGOUT_FAILED, Bundle.EMPTY); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.DIDNT_LOG_OUT); + } + } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { + if(updateVpnCertificate()) { + receiver.send(CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); + } else { + receiver.send(INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); + } + } else if(action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { Bundle result = getAndSetEipServiceJson(); if(result.getBoolean(RESULT_KEY)) { receiver.send(CORRECTLY_DOWNLOADED_EIP_SERVICE, result); @@ -176,13 +182,13 @@ public class ProviderAPI extends IntentService { receiver.send(INCORRECTLY_DOWNLOADED_EIP_SERVICE, result); } } - } + } private Bundle tryToRegister(Bundle task) { Bundle result = new Bundle(); int progress = 0; - String username = task.getString(SessionDialog.USERNAME); + String username = User.userName(); String password = task.getString(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { @@ -231,8 +237,8 @@ public class ProviderAPI extends IntentService { private Bundle tryToAuthenticate(Bundle task) { Bundle result = new Bundle(); int progress = 0; - - String username = task.getString(SessionDialog.USERNAME); + + String username = User.userName(); String password = task.getString(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { result = authenticate(username, password); diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 04385ddd..c6ac2477 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -28,6 +28,7 @@ import android.widget.*; import org.jetbrains.annotations.NotNull; import org.json.*; import java.net.*; +import java.util.*; import butterknife.*; import de.blinkt.openvpn.activities.*; @@ -40,7 +41,7 @@ import se.leap.bitmaskclient.eip.*; * @author Sean Leonard * @author parmegv */ -public class Dashboard extends Activity implements SessionDialog.SessionDialogInterface, ProviderAPIResultReceiver.Receiver { +public class Dashboard extends Activity implements SessionDialog.SessionDialogInterface, ProviderAPIResultReceiver.Receiver, Observer { protected static final int CONFIGURE_LEAP = 0; protected static final int SWITCH_PROVIDER = 1; @@ -60,46 +61,63 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @InjectView(R.id.providerName) TextView provider_name; + @InjectView(R.id.user_session_status) + TextView user_session_status_text_view; EipFragment eip_fragment; private Provider provider; + private UserSessionStatus user_session_status; public ProviderAPIResultReceiver providerAPI_result_receiver; private boolean switching_provider; @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - app = this; + super.onCreate(savedInstanceState); - PRNGFixes.apply(); + app = this; + user_session_status = UserSessionStatus.getInstance(); + user_session_status.addObserver(this); - preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); - handleVersion(); + PRNGFixes.apply(); - provider = getSavedProvider(savedInstanceState); + preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); + fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); + handleVersion(); + + restoreProvider(savedInstanceState); if (provider == null || provider.getName().isEmpty()) - startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP); - else - buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); + startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP); + else { + buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); + restoreSessionStatus(savedInstanceState); + } + } + + private void restoreProvider(Bundle savedInstanceState) { + if (savedInstanceState != null) { + if(savedInstanceState.containsKey(Provider.KEY)) + provider = savedInstanceState.getParcelable(Provider.KEY); + if(provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) + provider = getSavedProviderFromSharedPreferences(); + } + } + + private void restoreSessionStatus(Bundle savedInstanceState) { + if (savedInstanceState != null) + if(savedInstanceState.containsKey(UserSessionStatus.TAG)) { + UserSessionStatus.SessionStatus status = (UserSessionStatus.SessionStatus) savedInstanceState.getSerializable(UserSessionStatus.TAG); + user_session_status.updateStatus(status); + } } @Override protected void onSaveInstanceState(@NotNull Bundle outState) { if(provider != null) outState.putParcelable(Provider.KEY, provider); - super.onSaveInstanceState(outState); - } + if(user_session_status_text_view != null && user_session_status_text_view.getVisibility() == TextView.VISIBLE) + outState.putSerializable(UserSessionStatus.TAG, user_session_status.sessionStatus()); - private Provider getSavedProvider(Bundle savedInstanceState) { - Provider provider = null; - if(savedInstanceState != null) - provider = savedInstanceState.getParcelable(Provider.KEY); - else if(preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) - provider = getSavedProviderFromSharedPreferences(); - - return provider; + super.onSaveInstanceState(outState); } private Provider getSavedProviderFromSharedPreferences() { @@ -107,9 +125,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn try { provider = new Provider(new URL(preferences.getString(Provider.MAIN_URL, ""))); provider.define(new JSONObject(preferences.getString(Provider.KEY, ""))); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (MalformedURLException | JSONException e) { e.printStackTrace(); } @@ -136,8 +152,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ - if ( requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { - if ( resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { + if ( requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { + if ( resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { provider = data.getParcelableExtra(Provider.KEY); providerToPreferences(provider); @@ -147,13 +163,13 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn sessionDialog(Bundle.EMPTY); } - } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { + } else if (resultCode == RESULT_CANCELED && data.hasExtra(ACTION_QUIT)) { finish(); - } else - configErrorDialog(); - } else if(requestCode == EIP.DISCONNECT) { - EipStatus.getInstance().setConnectedOrDisconnected(); - } + } else + configErrorDialog(); + } else if(requestCode == EIP.DISCONNECT) { + EipStatus.getInstance().setConnectedOrDisconnected(); + } } @SuppressLint("CommitPrefEdits") @@ -190,55 +206,39 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn * service dependent UI elements to include. */ private void buildDashboard(boolean hide_and_turn_on_eip) { - setContentView(R.layout.dashboard); + setContentView(R.layout.dashboard); ButterKnife.inject(this); - provider_name.setText(provider.getDomain()); - if ( provider.hasEIP()){ + provider_name.setText(provider.getDomain()); + if ( provider.hasEIP()){ fragment_manager.removePreviousFragment(EipFragment.TAG); eip_fragment = new EipFragment(); - if (hide_and_turn_on_eip) { - preferences.edit().remove(Dashboard.START_ON_BOOT).apply(); - Bundle arguments = new Bundle(); - arguments.putBoolean(EipFragment.START_ON_BOOT, true); + if (hide_and_turn_on_eip) { + preferences.edit().remove(Dashboard.START_ON_BOOT).apply(); + Bundle arguments = new Bundle(); + arguments.putBoolean(EipFragment.START_ON_BOOT, true); if(eip_fragment != null) eip_fragment.setArguments(arguments); - } + } fragment_manager.replace(R.id.servicesCollection, eip_fragment, EipFragment.TAG); - if (hide_and_turn_on_eip) { - onBackPressed(); - } - } + if (hide_and_turn_on_eip) { + onBackPressed(); + } + } + handleNewUserSessionStatus(user_session_status); } @Override public boolean onPrepareOptionsMenu(Menu menu) { - JSONObject provider_json; - try { - String provider_json_string = preferences.getString(Provider.KEY, ""); - if(!provider_json_string.isEmpty()) { - provider_json = new JSONObject(provider_json_string); - JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - boolean allow_registered_eip = service_description.getBoolean(Provider.ALLOW_REGISTRATION); - preferences.edit().putBoolean(Constants.ALLOWED_REGISTERED, allow_registered_eip).apply(); - - if(allow_registered_eip) { - if(LeapSRPSession.loggedIn()) { - menu.findItem(R.id.login_button).setVisible(false); - menu.findItem(R.id.logout_button).setVisible(true); - } else { - menu.findItem(R.id.login_button).setVisible(true); - menu.findItem(R.id.logout_button).setVisible(false); - } - menu.findItem(R.id.signup_button).setVisible(true); - } - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return true; + if(provider.allowsRegistration()) { + menu.findItem(R.id.signup_button).setVisible(true); + + boolean logged_in = User.loggedIn(); + menu.findItem(R.id.login_button).setVisible(!logged_in); + menu.findItem(R.id.logout_button).setVisible(logged_in); + } + return true; } @Override @@ -282,25 +282,50 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn public void showLog() { Intent startLW = new Intent(getAppContext(), LogWindow.class); - //startLW.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(startLW); } @Override public void signUp(String username, String password) { - Bundle parameters = bundleParameters(username, password); - providerApiCommand(parameters, R.string.signingup_message, ProviderAPI.SRP_REGISTER); + User.setUserName(username); + Bundle parameters = bundlePassword(password); + providerApiCommand(parameters, R.string.signingup_message, ProviderAPI.SIGN_UP); } @Override public void logIn(String username, String password) { - Bundle parameters = bundleParameters(username, password); - providerApiCommand(parameters, R.string.authenticating_message, ProviderAPI.SRP_AUTH); + User.setUserName(username); + Bundle parameters = bundlePassword(password); + providerApiCommand(parameters, R.string.authenticating_message, ProviderAPI.LOG_IN); } public void logOut() { providerApiCommand(Bundle.EMPTY, R.string.logout_message, ProviderAPI.LOG_OUT); } + + @Override + public void update (Observable observable, Object data) { + if(observable instanceof UserSessionStatus) { + UserSessionStatus status = (UserSessionStatus) observable; + handleNewUserSessionStatus(status); + } + } + + private void handleNewUserSessionStatus(UserSessionStatus status) { + user_session_status = status; + if(provider.allowsRegistration()) + changeSessionStatusMessage(user_session_status.toString()); + } + + private void changeSessionStatusMessage(final String message) { + Log.d(TAG, message); + runOnUiThread(new Runnable() { + @Override + public void run() { + user_session_status_text_view.setText(message); + } + }); + } protected void downloadVpnCertificate() { boolean is_authenticated = LeapSRPSession.loggedIn(); @@ -312,10 +337,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } - private Bundle bundleParameters(String username, String password) { + private Bundle bundlePassword(String password) { Bundle parameters = new Bundle(); - if(!username.isEmpty()) - parameters.putString(SessionDialog.USERNAME, username); if(!password.isEmpty()) parameters.putString(SessionDialog.PASSWORD, password); return parameters; diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 6dbdf961..e152df46 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -23,6 +23,8 @@ import android.util.*; import android.view.*; import android.widget.*; +import org.jetbrains.annotations.NotNull; + import java.util.*; import butterknife.*; @@ -80,18 +82,20 @@ public class EipFragment extends Fragment implements Observer { Bundle arguments = getArguments(); if(arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) startEipFromScratch(); + if(savedInstanceState != null) restoreState(savedInstanceState); - if (savedInstanceState != null) { - status_message.setText(savedInstanceState.getString(STATUS_MESSAGE)); - if(savedInstanceState.getBoolean(IS_PENDING)) - eip_status.setConnecting(); - else if(savedInstanceState.getBoolean(IS_CONNECTED)) { - eip_status.setConnectedOrDisconnected(); - } - } return view; } + private void restoreState(@NotNull Bundle savedInstanceState) { + if(savedInstanceState.getBoolean(IS_PENDING)) + eip_status.setConnecting(); + else if(savedInstanceState.getBoolean(IS_CONNECTED)) + eip_status.setConnectedOrDisconnected(); + else + status_message.setText(savedInstanceState.getString(STATUS_MESSAGE)); + } + @Override public void onResume() { super.onResume(); @@ -103,14 +107,12 @@ public class EipFragment extends Fragment implements Observer { public void onSaveInstanceState(Bundle outState) { outState.putBoolean(IS_PENDING, eip_status.isConnecting()); outState.putBoolean(IS_CONNECTED, eip_status.isConnected()); - Log.d(TAG, "status message onSaveInstanceState = " + status_message.getText().toString()); outState.putString(STATUS_MESSAGE, status_message.getText().toString()); super.onSaveInstanceState(outState); } protected void saveStatus() { boolean is_on = eip_switch.isChecked(); - Log.d(TAG, "saveStatus: is_on = " + is_on); Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, is_on).commit(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java index 09855baf..49c115ae 100644 --- a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java +++ b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java @@ -44,7 +44,8 @@ public class LeapSRPSession { final public static String TOKEN = "token"; final public static String AUTHORIZATION_HEADER= "Authorization"; final public static String TAG= "Leap SRP session class tag"; - + + private User user; private SRPParameters params; private String username; private String password; diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index d7ff6633..ef25c5d2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -172,6 +172,14 @@ public final class Provider implements Parcelable { return false; } + public boolean allowsRegistration() { + try { + return definition.getJSONObject(Provider.SERVICE).getBoolean(Provider.ALLOW_REGISTRATION); + } catch (JSONException e) { + return false; + } + } + @Override public int describeContents() { return 0; diff --git a/app/src/main/java/se/leap/bitmaskclient/User.java b/app/src/main/java/se/leap/bitmaskclient/User.java new file mode 100644 index 00000000..e05a0573 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/User.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2013 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; + +public class User { + private static String user_name = "The user"; + private static User user; + + public static User getInstance() { + if(user == null) { + user = new User(); + } + return user; + } + + public static void setUserName(String user_name) { + User.user_name = user_name; + } + + private User() { } + + public static String userName() { return user_name; } + + public static boolean loggedIn() { + return LeapSRPSession.loggedIn(); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java new file mode 100644 index 00000000..bab57d04 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2013 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; + +import android.os.*; + +import java.util.*; + +public class UserSessionStatus extends Observable { + public static String TAG = UserSessionStatus.class.getSimpleName(); + private static UserSessionStatus current_status; + + public enum SessionStatus { + LOGGED_IN, + LOGGED_OUT, + NOT_LOGGED_IN, + DIDNT_LOG_OUT, + LOGGING_IN, + LOGGING_OUT + } + + private static SessionStatus session_status = SessionStatus.NOT_LOGGED_IN; + + public static UserSessionStatus getInstance() { + if(current_status == null) { + current_status = new UserSessionStatus(); + } + return current_status; + } + + private UserSessionStatus() { } + + private void sessionStatus(SessionStatus session_status) { + this.session_status = session_status; + } + + public SessionStatus sessionStatus() { return session_status; } + + public static void updateStatus(SessionStatus session_status) { + current_status = getInstance(); + current_status.sessionStatus(session_status); + current_status.setChanged(); + current_status.notifyObservers(); + } + + @Override + public String toString() { + return User.userName() + " is " + + session_status.toString().toLowerCase().replaceAll("_", " "); + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index 0c773208..0a707ab1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -42,7 +42,8 @@ public class VoidVpnService extends VpnService { public static boolean stop() { try { - fd.close(); + if(fd != null) + fd.close(); return true; } catch (IOException | NullPointerException e) { android.util.Log.d(TAG, "VoidVpnService didn't stop"); diff --git a/app/src/main/res/layout/dashboard.xml b/app/src/main/res/layout/dashboard.xml index 67a1122f..4a9dd502 100644 --- a/app/src/main/res/layout/dashboard.xml +++ b/app/src/main/res/layout/dashboard.xml @@ -16,7 +16,6 @@ android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:ellipsize="marquee" - android:fadingEdge="horizontal" android:singleLine="true" android:text="@string/provider_label" android:textAppearance="?android:attr/textAppearanceMedium" /> @@ -28,18 +27,22 @@ android:textSize="28sp" android:layout_marginLeft="10dp" android:ellipsize="marquee" - android:fadingEdge="horizontal" android:singleLine="true" android:text="@string/provider_label_none" android:textAppearance="?android:attr/textAppearanceMedium" /> - + android:layout_height="wrap_content" + android:textSize="18sp" + android:layout_marginLeft="10dp" + android:ellipsize="marquee" + android:singleLine="true" + android:textAppearance="?android:attr/textAppearanceMedium" + /> Date: Fri, 13 Mar 2015 14:56:27 +0100 Subject: Progress spinning bar for user session status Copy changes to xlarge layout. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 35 ++++++++++++++++++---- .../se/leap/bitmaskclient/UserSessionStatus.java | 5 ++++ app/src/main/res/layout-xlarge/dashboard.xml | 22 ++++++++++++++ app/src/main/res/layout/dashboard.xml | 32 +++++++++++++------- 4 files changed, 77 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index c6ac2477..28eb739c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -63,6 +63,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn TextView provider_name; @InjectView(R.id.user_session_status) TextView user_session_status_text_view; + @InjectView(R.id.user_session_status_progress) + ProgressBar user_session_status_progress_bar; EipFragment eip_fragment; private Provider provider; @@ -313,12 +315,16 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn private void handleNewUserSessionStatus(UserSessionStatus status) { user_session_status = status; - if(provider.allowsRegistration()) + if(provider.allowsRegistration()) { + if(user_session_status.inProgress()) + showUserSessionProgressBar(); + else + hideUserSessionProgressBar(); changeSessionStatusMessage(user_session_status.toString()); + } } private void changeSessionStatusMessage(final String message) { - Log.d(TAG, message); runOnUiThread(new Runnable() { @Override public void run() { @@ -327,6 +333,24 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn }); } + private void showUserSessionProgressBar() { + runOnUiThread(new Runnable() { + @Override + public void run() { + user_session_status_progress_bar.setVisibility(ProgressBar.VISIBLE); + } + }); + } + + private void hideUserSessionProgressBar() { + runOnUiThread(new Runnable() { + @Override + public void run() { + user_session_status_progress_bar.setVisibility(ProgressBar.GONE); + } + }); + } + protected void downloadVpnCertificate() { boolean is_authenticated = LeapSRPSession.loggedIn(); boolean allowed_anon = preferences.getBoolean(Constants.ALLOWED_ANON, false); @@ -428,12 +452,11 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } private void updateViewHidingProgressBar(int resultCode) { - changeStatusMessage(resultCode); - hideProgressBar(); + changeEipStatusMessage(resultCode); invalidateOptionsMenu(); } - private void changeStatusMessage(final int previous_result_code) { + private void changeEipStatusMessage(final int previous_result_code) { ResultReceiver status_receiver = new ResultReceiver(new Handler()){ protected void onReceiveResult(int resultCode, Bundle resultData){ super.onReceiveResult(resultCode, resultData); @@ -480,7 +503,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn startService(intent); } - private void hideProgressBar() { + private void hideEipProgressBar() { if(eip_fragment != null) { eip_fragment.progress_bar.setProgress(0); eip_fragment.progress_bar.setVisibility(ProgressBar.GONE); diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java index bab57d04..f533819b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -49,6 +49,11 @@ public class UserSessionStatus extends Observable { } public SessionStatus sessionStatus() { return session_status; } + + public boolean inProgress() { + return session_status == SessionStatus.LOGGING_IN + || session_status == SessionStatus.LOGGING_OUT; + } public static void updateStatus(SessionStatus session_status) { current_status = getInstance(); diff --git a/app/src/main/res/layout-xlarge/dashboard.xml b/app/src/main/res/layout-xlarge/dashboard.xml index bd644e1e..268bd3f9 100644 --- a/app/src/main/res/layout-xlarge/dashboard.xml +++ b/app/src/main/res/layout-xlarge/dashboard.xml @@ -52,6 +52,28 @@ + + + + + - - + + + + + Date: Fri, 13 Mar 2015 15:19:20 +0100 Subject: Say nothing about user sessions in eip status. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 71 ++-------------------- .../java/se/leap/bitmaskclient/EipFragment.java | 7 ++- app/src/main/java/se/leap/bitmaskclient/User.java | 2 +- 3 files changed, 13 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 28eb739c..74a87e63 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -182,7 +182,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } private void configErrorDialog() { - AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getAppContext()); + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getContext()); alertBuilder.setTitle(getResources().getString(R.string.setup_error_title)); alertBuilder .setMessage(getResources().getString(R.string.setup_error_text)) @@ -190,7 +190,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn .setPositiveButton(getResources().getString(R.string.setup_error_configure_button), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - startActivityForResult(new Intent(getAppContext(),ConfigurationWizard.class),CONFIGURE_LEAP); + startActivityForResult(new Intent(getContext(),ConfigurationWizard.class),CONFIGURE_LEAP); } }) .setNegativeButton(getResources().getString(R.string.setup_error_close_button), new DialogInterface.OnClickListener() { @@ -283,7 +283,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } public void showLog() { - Intent startLW = new Intent(getAppContext(), LogWindow.class); + Intent startLW = new Intent(getContext(), LogWindow.class); startActivity(startLW); } @@ -321,6 +321,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn else hideUserSessionProgressBar(); changeSessionStatusMessage(user_session_status.toString()); + invalidateOptionsMenu(); } } @@ -421,26 +422,20 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn String password = resultData.getString(SessionDialog.PASSWORD); logIn(username, password); } else if(resultCode == ProviderAPI.FAILED_SIGNUP) { - updateViewHidingProgressBar(resultCode); sessionDialog(resultData); } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { - updateViewHidingProgressBar(resultCode); downloadVpnCertificate(); } else if(resultCode == ProviderAPI.FAILED_LOGIN) { - updateViewHidingProgressBar(resultCode); sessionDialog(resultData); } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { - updateViewHidingProgressBar(resultCode); if(switching_provider) switchProvider(); } else if(resultCode == ProviderAPI.LOGOUT_FAILED) { - updateViewHidingProgressBar(resultCode); setResult(RESULT_CANCELED); } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { - updateViewHidingProgressBar(resultCode); eip_fragment.updateEipService(); + eip_fragment.handleNewVpnCertificate(); setResult(RESULT_OK); } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { - updateViewHidingProgressBar(resultCode); setResult(RESULT_CANCELED); } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { @@ -451,66 +446,12 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } } - private void updateViewHidingProgressBar(int resultCode) { - changeEipStatusMessage(resultCode); - invalidateOptionsMenu(); - } - - private void changeEipStatusMessage(final int previous_result_code) { - ResultReceiver status_receiver = new ResultReceiver(new Handler()){ - protected void onReceiveResult(int resultCode, Bundle resultData){ - super.onReceiveResult(resultCode, resultData); - String request = resultData.getString(Constants.REQUEST_TAG); - if (request.equalsIgnoreCase(Constants.ACTION_IS_EIP_RUNNING)){ - if (resultCode == Activity.RESULT_OK){ - switch(previous_result_code){ - case ProviderAPI.SUCCESSFUL_LOGIN: setStatusMessage(R.string.succesful_authentication_message); break; - case ProviderAPI.FAILED_LOGIN: setStatusMessage(R.string.authentication_failed_message); break; - case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: setStatusMessage(R.string.incorrectly_downloaded_certificate_message); break; - case ProviderAPI.SUCCESSFUL_LOGOUT: setStatusMessage(R.string.logged_out_message); break; - case ProviderAPI.LOGOUT_FAILED: setStatusMessage(R.string.log_out_failed_message); break; - - } - } - else if(resultCode == Activity.RESULT_CANCELED){ - switch(previous_result_code){ - case ProviderAPI.SUCCESSFUL_LOGIN: setStatusMessage(R.string.succesful_authentication_message); break; - case ProviderAPI.FAILED_LOGIN: setStatusMessage(R.string.authentication_failed_message); break; - case ProviderAPI.FAILED_SIGNUP: setStatusMessage(R.string.registration_failed_message); break; - case ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE: break; - case ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE: setStatusMessage(R.string.incorrectly_downloaded_certificate_message); break; - case ProviderAPI.SUCCESSFUL_LOGOUT: setStatusMessage(R.string.logged_out_message); break; - case ProviderAPI.LOGOUT_FAILED: setStatusMessage(R.string.log_out_failed_message); break; - } - } - } - - } - }; - eipIsRunning(status_receiver); - } - private void setStatusMessage(int string_resId) { if(eip_fragment != null && eip_fragment.status_message != null) eip_fragment.status_message.setText(string_resId); } - private void eipIsRunning(ResultReceiver eip_receiver){ - // TODO validate "action"...how do we get the list of intent-filters for a class via Android API? - Intent intent = new Intent(this, EIP.class); - intent.setAction(Constants.ACTION_IS_EIP_RUNNING); - intent.putExtra(Constants.RECEIVER_TAG, eip_receiver); - startService(intent); - } - - private void hideEipProgressBar() { - if(eip_fragment != null) { - eip_fragment.progress_bar.setProgress(0); - eip_fragment.progress_bar.setVisibility(ProgressBar.GONE); - } - } - - public static Context getAppContext() { + public static Context getContext() { return app; } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index e152df46..2057a80b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -116,6 +116,10 @@ public class EipFragment extends Fragment implements Observer { Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, is_on).commit(); } + void handleNewVpnCertificate() { + handleSwitch(!eip_switch.isEnabled()); + } + @OnCheckedChanged(R.id.eipSwitch) void handleSwitch(boolean isChecked) { if(isChecked) @@ -156,7 +160,8 @@ public class EipFragment extends Fragment implements Observer { askPendingStartCancellation(); } else if(eip_status.isConnected()) { askToStopEIP(); - } + } else + setDisconnectedUI(); } private void askPendingStartCancellation() { diff --git a/app/src/main/java/se/leap/bitmaskclient/User.java b/app/src/main/java/se/leap/bitmaskclient/User.java index e05a0573..6e20c165 100644 --- a/app/src/main/java/se/leap/bitmaskclient/User.java +++ b/app/src/main/java/se/leap/bitmaskclient/User.java @@ -17,7 +17,7 @@ package se.leap.bitmaskclient; public class User { - private static String user_name = "The user"; + private static String user_name = "You"; private static User user; public static User getInstance() { -- cgit v1.2.3 From 429295bd6ee5f2caa57c287e74392c80aed25f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 13 Mar 2015 15:28:59 +0100 Subject: If a provider fails to be configured, go back to CW. --- app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java | 3 ++- app/src/main/res/values/strings.xml | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index d28bb690..ed2a063c 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -215,7 +215,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download setResult(RESULT_OK); } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { hideProgressBar(); - + cancelSettingUpProvider(); + Toast.makeText(getApplicationContext(), R.string.provider_problem, Toast.LENGTH_LONG).show(); setResult(RESULT_CANCELED, mConfigState); } else if(resultCode == AboutActivity.VIEWED) { // Do nothing, right now diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 032602cb..897223ad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -71,4 +71,6 @@ No "Not running! Connection not secure!" Connection Secure. + It seems there is a problem with the provider. + Please try another provider, or contact yours. -- cgit v1.2.3 From b9120adba28b5d691375ece5a919357e54e86a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 13 Mar 2015 15:44:24 +0100 Subject: Restore provider from sharedconfs if necessary --- app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 74a87e63..face6ced 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -99,9 +99,9 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn if (savedInstanceState != null) { if(savedInstanceState.containsKey(Provider.KEY)) provider = savedInstanceState.getParcelable(Provider.KEY); - if(provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) - provider = getSavedProviderFromSharedPreferences(); } + if(provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) + provider = getSavedProviderFromSharedPreferences(); } private void restoreSessionStatus(Bundle savedInstanceState) { -- cgit v1.2.3 From 3a7b9c1c746bd26b4e689814c3a4ec6b3141a76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 13 Mar 2015 15:57:43 +0100 Subject: 0.9.3 RC1 --- CHANGELOG | 10 ++++++++++ app/src/main/AndroidManifest.xml | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d879089d..771c46e7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,13 @@ +0.9.3 +Bugs: +- If VPN connection fails, blocking VPN is turned off +- Don't try to log out if nothing is logged in. +- Give better user feedback to some problems +- Switching orientation doesn't hide any useful message + +Features: +- Show the user status progress and info in its own place + 0.9.2 Bugs: - Sign Up dialog works correctly diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e11b57ef..2bd630ee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,8 +17,8 @@ + android:versionCode="117" + android:versionName="0.9.3" > -- cgit v1.2.3 From bb2a944265da15eeba35106f2e2bcda288855527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 19 Mar 2015 15:07:55 +0100 Subject: Show browser warning on disconnect. Also reindent some code. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 2 +- .../java/se/leap/bitmaskclient/EipFragment.java | 428 +++++++++++---------- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 144 +++---- app/src/main/res/values/strings.xml | 1 + 4 files changed, 298 insertions(+), 277 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index face6ced..a8f6e110 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -407,7 +407,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } private void switchProvider() { - if (provider.hasEIP()) eip_fragment.askToStopEIP(); + if (provider.hasEIP()) eip_fragment.stopEipIfPossible(); preferences.edit().clear().apply(); switching_provider = false; diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 2057a80b..5e5b76b2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -32,7 +32,7 @@ import de.blinkt.openvpn.activities.*; import se.leap.bitmaskclient.eip.*; public class EipFragment extends Fragment implements Observer { - + public static String TAG = EipFragment.class.getSimpleName(); protected static final String IS_PENDING = TAG + ".is_pending"; @@ -55,36 +55,36 @@ public class EipFragment extends Fragment implements Observer { @Override public void onAttach(Activity activity) { - super.onAttach(activity); + super.onAttach(activity); dashboard = (Dashboard) activity; dashboard.providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.DOWNLOAD_EIP_SERVICE); } - + @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - eip_status = EipStatus.getInstance(); - eip_status.addObserver(this); - mEIPReceiver = new EIPReceiver(new Handler()); + super.onCreate(savedInstanceState); + eip_status = EipStatus.getInstance(); + eip_status.addObserver(this); + mEIPReceiver = new EIPReceiver(new Handler()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View 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()) - eip_switch.setVisibility(View.VISIBLE); + if (eip_status.isConnecting()) + eip_switch.setVisibility(View.VISIBLE); - Log.d(TAG, "onCreateView, eip_switch is checked? " + eip_switch.isChecked()); + Log.d(TAG, "onCreateView, eip_switch is checked? " + eip_switch.isChecked()); Bundle arguments = getArguments(); - if(arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) - startEipFromScratch(); - if(savedInstanceState != null) restoreState(savedInstanceState); + if(arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) + startEipFromScratch(); + if(savedInstanceState != null) restoreState(savedInstanceState); - return view; + return view; } private void restoreState(@NotNull Bundle savedInstanceState) { @@ -98,22 +98,22 @@ public class EipFragment extends Fragment implements Observer { @Override public void onResume() { - super.onResume(); - eipCommand(Constants.ACTION_CHECK_CERT_VALIDITY); - handleNewState(eip_status); + super.onResume(); + eipCommand(Constants.ACTION_CHECK_CERT_VALIDITY); + handleNewState(eip_status); } - + @Override public void onSaveInstanceState(Bundle outState) { - outState.putBoolean(IS_PENDING, eip_status.isConnecting()); - outState.putBoolean(IS_CONNECTED, eip_status.isConnected()); - outState.putString(STATUS_MESSAGE, status_message.getText().toString()); - super.onSaveInstanceState(outState); + outState.putBoolean(IS_PENDING, eip_status.isConnecting()); + outState.putBoolean(IS_CONNECTED, eip_status.isConnected()); + outState.putString(STATUS_MESSAGE, status_message.getText().toString()); + super.onSaveInstanceState(outState); } protected void saveStatus() { - boolean is_on = eip_switch.isChecked(); - Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, is_on).commit(); + boolean is_on = eip_switch.isChecked(); + Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, is_on).commit(); } void handleNewVpnCertificate() { @@ -122,251 +122,271 @@ public class EipFragment extends Fragment implements Observer { @OnCheckedChanged(R.id.eipSwitch) void handleSwitch(boolean isChecked) { - if(isChecked) - handleSwitchOn(); - else - handleSwitchOff(); - - saveStatus(); + if(isChecked) + handleSwitchOn(); + else + handleSwitchOff(); + + saveStatus(); } private void handleSwitchOn() { - if(canStartEIP()) - startEipFromScratch(); - else if(canLogInToStartEIP()) { - wants_to_connect = true; - Bundle bundle = new Bundle(); - bundle.putBoolean(IS_PENDING, true); - dashboard.sessionDialog(bundle); - } - } - + if(canStartEIP()) + startEipFromScratch(); + else if(canLogInToStartEIP()) { + wants_to_connect = true; + Bundle bundle = new Bundle(); + bundle.putBoolean(IS_PENDING, true); + dashboard.sessionDialog(bundle); + } + } + private boolean canStartEIP() { - boolean certificateExists = !Dashboard.preferences.getString(Constants.CERTIFICATE, "").isEmpty(); - boolean isAllowedAnon = Dashboard.preferences.getBoolean(Constants.ALLOWED_ANON, false); - return (isAllowedAnon || certificateExists) && !eip_status.isConnected() && !eip_status.isConnecting(); + boolean certificateExists = !Dashboard.preferences.getString(Constants.CERTIFICATE, "").isEmpty(); + boolean isAllowedAnon = Dashboard.preferences.getBoolean(Constants.ALLOWED_ANON, false); + return (isAllowedAnon || certificateExists) && !eip_status.isConnected() && !eip_status.isConnecting(); } - + private boolean canLogInToStartEIP() { - boolean isAllowedRegistered = Dashboard.preferences.getBoolean(Constants.ALLOWED_REGISTERED, false); - boolean isLoggedIn = !LeapSRPSession.getToken().isEmpty(); - Log.d(TAG, "Allow registered? " + isAllowedRegistered); - Log.d(TAG, "Is logged in? " + isLoggedIn); - return isAllowedRegistered && !isLoggedIn && !eip_status.isConnecting() && !eip_status.isConnected(); + boolean isAllowedRegistered = Dashboard.preferences.getBoolean(Constants.ALLOWED_REGISTERED, false); + boolean isLoggedIn = !LeapSRPSession.getToken().isEmpty(); + Log.d(TAG, "Allow registered? " + isAllowedRegistered); + Log.d(TAG, "Is logged in? " + isLoggedIn); + return isAllowedRegistered && !isLoggedIn && !eip_status.isConnecting() && !eip_status.isConnected(); } private void handleSwitchOff() { - if(eip_status.isConnecting()) { - askPendingStartCancellation(); - } else if(eip_status.isConnected()) { - askToStopEIP(); - } else - setDisconnectedUI(); - } - - private void askPendingStartCancellation() { - 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(dashboard.getString(R.string.no), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - eip_switch.setChecked(true); - } - }) - .show(); + if(eip_status.isConnecting()) { + askPendingStartCancellation(); + } else if(eip_status.isConnected()) { + askToStopEIP(); + } else + setDisconnectedUI(); + } + + private void askPendingStartCancellation() { + 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(dashboard.getString(R.string.no), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + eip_switch.setChecked(true); + } + }) + .show(); } public void startEipFromScratch() { wants_to_connect = false; is_starting_to_connect = true; progress_bar.setVisibility(View.VISIBLE); - eip_switch.setVisibility(View.VISIBLE); - String status = dashboard.getString(R.string.eip_status_start_pending); - status_message.setText(status); - - if(!eip_switch.isChecked()) { - eip_switch.setChecked(true); - } + eip_switch.setVisibility(View.VISIBLE); + String status = dashboard.getString(R.string.eip_status_start_pending); + status_message.setText(status); + + if(!eip_switch.isChecked()) { + eip_switch.setChecked(true); + } saveStatus(); - eipCommand(Constants.ACTION_START_EIP); + eipCommand(Constants.ACTION_START_EIP); } - private void stopEIP() { - if(eip_status.isConnecting()) - VoidVpnService.stop(); - disconnect(); + private void stop() { + if(eip_status.isConnecting()) + VoidVpnService.stop(); + disconnect(); } private void disconnect() { - Intent disconnect_vpn = new Intent(dashboard, DisconnectVPN.class); - dashboard.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); - eip_status.setDisconnecting(); + Intent disconnect_vpn = new Intent(dashboard, DisconnectVPN.class); + dashboard.startActivityForResult(disconnect_vpn, EIP.DISCONNECT); + eip_status.setDisconnecting(); } - protected void askToStopEIP() { + protected void stopEipIfPossible() { + hideProgressBar(); - String message = dashboard.getString(R.string.eip_state_not_connected); - status_message.setText(message); + String message = dashboard.getString(R.string.eip_state_not_connected); + status_message.setText(message); + + eipCommand(Constants.ACTION_STOP_EIP); + } - eipCommand(Constants.ACTION_STOP_EIP); + private void askToStopEIP() { + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(dashboard); + alertBuilder.setTitle(dashboard.getString(R.string.eip_cancel_connect_title)) + .setMessage(dashboard.getString(R.string.eip_warning_browser_inconsistency)) + .setPositiveButton((R.string.yes), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + stopEipIfPossible(); + } + }) + .setNegativeButton(dashboard.getString(R.string.no), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + eip_switch.setChecked(true); + } + }) + .show(); } protected void updateEipService() { eipCommand(Constants.ACTION_UPDATE_EIP_SERVICE); } - + /** * Send a command to EIP - * + * * @param action A valid String constant from EIP class representing an Intent * filter for the EIP class */ 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(dashboard.getApplicationContext(), EIP.class); - vpn_intent.setAction(action); - vpn_intent.putExtra(Constants.RECEIVER_TAG, mEIPReceiver); - dashboard.startService(vpn_intent); + // TODO validate "action"...how do we get the list of intent-filters for a class via Android API? + Intent vpn_intent = new Intent(dashboard.getApplicationContext(), EIP.class); + vpn_intent.setAction(action); + vpn_intent.putExtra(Constants.RECEIVER_TAG, mEIPReceiver); + dashboard.startService(vpn_intent); } - + @Override public void update (Observable observable, Object data) { - if(observable instanceof EipStatus) { - eip_status = (EipStatus) observable; - final EipStatus eip_status = (EipStatus) observable; - dashboard.runOnUiThread(new Runnable() { - @Override - public void run() { - handleNewState(eip_status); - } - }); - } + if(observable instanceof EipStatus) { + eip_status = (EipStatus) observable; + final EipStatus eip_status = (EipStatus) observable; + dashboard.runOnUiThread(new Runnable() { + @Override + public void run() { + handleNewState(eip_status); + } + }); + } } private void handleNewState(EipStatus eip_status) { - if(eip_status.wantsToDisconnect()) - setDisconnectedUI(); - else if(eip_status.isConnecting() || is_starting_to_connect) - setInProgressUI(eip_status); - else if (eip_status.isConnected()) - setConnectedUI(); - else if (eip_status.isDisconnected() && !eip_status.isConnecting()) - setDisconnectedUI(); + if(eip_status.wantsToDisconnect()) + setDisconnectedUI(); + else if(eip_status.isConnecting() || is_starting_to_connect) + setInProgressUI(eip_status); + else if (eip_status.isConnected()) + setConnectedUI(); + else if (eip_status.isDisconnected() && !eip_status.isConnecting()) + setDisconnectedUI(); } private void setConnectedUI() { - hideProgressBar(); - Log.d(TAG, "setConnectedUi? " + eip_status.isConnected()); - adjustSwitch(); - is_starting_to_connect = false; - status_message.setText(dashboard.getString(R.string.eip_state_connected)); + hideProgressBar(); + Log.d(TAG, "setConnectedUi? " + eip_status.isConnected()); + adjustSwitch(); + is_starting_to_connect = false; + status_message.setText(dashboard.getString(R.string.eip_state_connected)); } private void setDisconnectedUI(){ - hideProgressBar(); - adjustSwitch(); - String last_log_message = eip_status.getLastLogMessage(dashboard.getApplicationContext()); - if((last_log_message.contains("error") || last_log_message.contains("ERROR")) - && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ - dashboard.showLog(); - } + hideProgressBar(); + adjustSwitch(); + String last_log_message = eip_status.getLastLogMessage(dashboard.getApplicationContext()); + if((last_log_message.contains("error") || last_log_message.contains("ERROR")) + && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ + dashboard.showLog(); + } status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { - if(eip_status.isConnected() || eip_status.isConnecting() || is_starting_to_connect) { - Log.d(TAG, "adjustSwitch, isConnected || isConnecting, is checked"); - if(!eip_switch.isChecked()) { - eip_switch.setChecked(true); - } - } else { - Log.d(TAG, "adjustSwitch, !isConnected && !isConnecting? " + eip_status.toString()); + if(eip_status.isConnected() || eip_status.isConnecting() || is_starting_to_connect) { + Log.d(TAG, "adjustSwitch, isConnected || isConnecting, is checked"); + if(!eip_switch.isChecked()) { + eip_switch.setChecked(true); + } + } else { + Log.d(TAG, "adjustSwitch, !isConnected && !isConnecting? " + eip_status.toString()); - if(eip_switch.isChecked()) { - eip_switch.setChecked(false); - } - } + if(eip_switch.isChecked()) { + eip_switch.setChecked(false); + } + } } private void setInProgressUI(EipStatus eip_status) { - int localizedResId = eip_status.getLocalizedResId(); - String logmessage = eip_status.getLogMessage(); - String prefix = dashboard.getString(localizedResId); + int localizedResId = eip_status.getLocalizedResId(); + String logmessage = eip_status.getLogMessage(); + String prefix = dashboard.getString(localizedResId); - showProgressBar(); - status_message.setText(prefix + " " + logmessage); + showProgressBar(); + status_message.setText(prefix + " " + logmessage); is_starting_to_connect = false; - adjustSwitch(); + adjustSwitch(); } private void updatingCertificateUI() { - showProgressBar(); + showProgressBar(); status_message.setText(getString(R.string.updating_certificate_message)); } private void showProgressBar() { - if(progress_bar != null) - progress_bar.setVisibility(View.VISIBLE); + if(progress_bar != null) + progress_bar.setVisibility(View.VISIBLE); } private void hideProgressBar() { - if(progress_bar != null) - progress_bar.setVisibility(View.GONE); - } - - protected class EIPReceiver extends ResultReceiver { - - protected EIPReceiver(Handler handler){ - super(handler); - } - - @Override - protected void onReceiveResult(int resultCode, Bundle resultData) { - super.onReceiveResult(resultCode, resultData); - - String request = resultData.getString(Constants.REQUEST_TAG); - - if (request.equals(Constants.ACTION_START_EIP)) { - switch (resultCode){ - case Activity.RESULT_OK: - break; - case Activity.RESULT_CANCELED: - - break; - } - } else if (request.equals(Constants.ACTION_STOP_EIP)) { - switch (resultCode){ - case Activity.RESULT_OK: - stopEIP(); - break; - case Activity.RESULT_CANCELED: - break; - } - } else if (request.equals(Constants.EIP_NOTIFICATION)) { - switch (resultCode){ - case Activity.RESULT_OK: - break; - case Activity.RESULT_CANCELED: - break; - } - } else if (request.equals(Constants.ACTION_CHECK_CERT_VALIDITY)) { - switch (resultCode) { - case Activity.RESULT_OK: - break; - case Activity.RESULT_CANCELED: - updatingCertificateUI(); - dashboard.downloadVpnCertificate(); - break; - } - } else if (request.equals(Constants.ACTION_UPDATE_EIP_SERVICE)) { + if(progress_bar != null) + progress_bar.setVisibility(View.GONE); + } + +protected class EIPReceiver extends ResultReceiver { + + protected EIPReceiver(Handler handler){ + super(handler); + } + + @Override + protected void onReceiveResult(int resultCode, Bundle resultData) { + super.onReceiveResult(resultCode, resultData); + + String request = resultData.getString(Constants.REQUEST_TAG); + + if (request.equals(Constants.ACTION_START_EIP)) { + switch (resultCode){ + case Activity.RESULT_OK: + break; + case Activity.RESULT_CANCELED: + + break; + } + } else if (request.equals(Constants.ACTION_STOP_EIP)) { + switch (resultCode){ + case Activity.RESULT_OK: + stop(); + break; + case Activity.RESULT_CANCELED: + break; + } + } else if (request.equals(Constants.EIP_NOTIFICATION)) { + switch (resultCode){ + case Activity.RESULT_OK: + break; + case Activity.RESULT_CANCELED: + break; + } + } else if (request.equals(Constants.ACTION_CHECK_CERT_VALIDITY)) { + switch (resultCode) { + case Activity.RESULT_OK: + break; + case Activity.RESULT_CANCELED: + updatingCertificateUI(); + dashboard.downloadVpnCertificate(); + break; + } + } else if (request.equals(Constants.ACTION_UPDATE_EIP_SERVICE)) { switch (resultCode) { case Activity.RESULT_OK: if(wants_to_connect) @@ -377,11 +397,11 @@ public class EipFragment extends Fragment implements Observer { break; } } - } } +} public static EIPReceiver getReceiver() { - return mEIPReceiver; + return mEIPReceiver; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 6f260f55..5f5dcabd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -37,79 +37,79 @@ public class VpnConfigGenerator { private final String new_line = System.getProperty("line.separator"); // Platform new line public VpnConfigGenerator(JSONObject general_configuration, JSONObject secrets, JSONObject gateway) { - this.general_configuration = general_configuration; - this.gateway = gateway; + this.general_configuration = general_configuration; + this.gateway = gateway; this.secrets = secrets; } - + public String generate() { - return - generalConfiguration() - + new_line - + gatewayConfiguration() - + new_line - + secretsConfiguration() - + new_line - + androidCustomizations(); + return + generalConfiguration() + + new_line + + gatewayConfiguration() + + new_line + + secretsConfiguration() + + new_line + + androidCustomizations(); } private String generalConfiguration() { - String common_options = ""; - try { - Iterator keys = general_configuration.keys(); - while ( keys.hasNext() ){ - String key = keys.next().toString(); - - common_options += key + " "; - for ( String word : general_configuration.getString(key).split(" ") ) - common_options += word + " "; - common_options += new_line; - - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - common_options += "client"; - - return common_options; + String common_options = ""; + try { + Iterator keys = general_configuration.keys(); + while ( keys.hasNext() ){ + String key = keys.next().toString(); + + common_options += key + " "; + for (String word : general_configuration.getString(key).split(" ")) + common_options += word + " "; + common_options += new_line; + + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + common_options += "client"; + + return common_options; } - + private String gatewayConfiguration() { - String remotes = ""; - - String ip_address_keyword = "ip_address"; - String remote_keyword = "remote"; - String ports_keyword = "ports"; - String protocol_keyword = "protocols"; - String capabilities_keyword = "capabilities"; - String udp = "udp"; - - try { - String ip_address = gateway.getString(ip_address_keyword); - JSONObject capabilities = gateway.getJSONObject(capabilities_keyword); - JSONArray ports = capabilities.getJSONArray(ports_keyword); - for (int i=0; iInitiating connection Cancel connection? There is a connection attempt in progress. Do you wish to cancel it? + In order to avoid leaking your personal information, please close your browser and start a private window after disconnecting the Encrypted VPN Internet Access. Thanks. Yes No "Not running! Connection not secure!" -- cgit v1.2.3 From 6dfae9951be275ccf33a05f4980a977b5b634e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 19 Mar 2015 15:27:28 +0100 Subject: Don't show user session progress in the eip fragment --- app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java | 1 + app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 6 +++--- app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java | 3 ++- app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index 9ce0e99f..47451a70 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -143,6 +143,7 @@ public class ProviderAPI extends IntentService { } } } else if (action.equalsIgnoreCase(SIGN_UP)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.SIGNING_UP); Bundle result = tryToRegister(parameters); if(result.getBoolean(RESULT_KEY)) { receiver.send(SUCCESSFUL_SIGNUP, result); diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index a8f6e110..c043821f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -291,18 +291,18 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn public void signUp(String username, String password) { User.setUserName(username); Bundle parameters = bundlePassword(password); - providerApiCommand(parameters, R.string.signingup_message, ProviderAPI.SIGN_UP); + providerApiCommand(parameters, 0, ProviderAPI.SIGN_UP); } @Override public void logIn(String username, String password) { User.setUserName(username); Bundle parameters = bundlePassword(password); - providerApiCommand(parameters, R.string.authenticating_message, ProviderAPI.LOG_IN); + providerApiCommand(parameters, 0, ProviderAPI.LOG_IN); } public void logOut() { - providerApiCommand(Bundle.EMPTY, R.string.logout_message, ProviderAPI.LOG_OUT); + providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.LOG_OUT); } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java index f533819b..db432a82 100644 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -30,7 +30,8 @@ public class UserSessionStatus extends Observable { NOT_LOGGED_IN, DIDNT_LOG_OUT, LOGGING_IN, - LOGGING_OUT + LOGGING_OUT, + SIGNING_UP } private static SessionStatus session_status = SessionStatus.NOT_LOGGED_IN; diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index 184173b2..3117ea6f 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -138,6 +138,7 @@ public class ProviderAPI extends IntentService { } } } else if (action.equalsIgnoreCase(SIGN_UP)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.SIGNING_UP); Bundle result = tryToRegister(parameters); if(result.getBoolean(RESULT_KEY)) { receiver.send(SUCCESSFUL_SIGNUP, result); -- cgit v1.2.3 From 2c44ba2678c6b3367a119d1e857dd5d5723f7681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 19 Mar 2015 15:56:30 +0100 Subject: Warn riseup users about credentials, as an error in the username field. I haven't still decided how to show that warning, I'm talking to gus about it. This is just the first idea, but the core mechanism is implemented at least. --- app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java | 8 ++++---- app/src/main/java/se/leap/bitmaskclient/Dashboard.java | 2 ++ app/src/main/java/se/leap/bitmaskclient/SessionDialog.java | 13 +++++++++---- app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java | 8 ++++---- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index 47451a70..57c4d067 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -199,11 +199,11 @@ public class ProviderAPI extends IntentService { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); } if(!validUsername(username)) { result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.USERNAME_MISSING, true); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); } } @@ -248,11 +248,11 @@ public class ProviderAPI extends IntentService { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); } if(!validUsername(username)) { result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.USERNAME_MISSING, true); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index c043821f..14d79bc0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -426,6 +426,8 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { downloadVpnCertificate(); } else if(resultCode == ProviderAPI.FAILED_LOGIN) { + if(provider.getName().equalsIgnoreCase("riseup")) + resultData.putBoolean(SessionDialog.ERRORS.CONFUSING_CREDENTIALS.toString(), true); sessionDialog(resultData); } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { if(switching_provider) switchProvider(); diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java index 9025564b..e32bca8e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java @@ -48,8 +48,11 @@ public class SessionDialog extends DialogFragment{ final public static String USERNAME = "username"; final public static String PASSWORD = "password"; - final public static String USERNAME_MISSING = "username missing"; - final public static String PASSWORD_INVALID_LENGTH = "password_invalid_length"; + public static enum ERRORS { + USERNAME_MISSING, + PASSWORD_INVALID_LENGTH, + CONFUSING_CREDENTIALS + } @InjectView(R.id.user_message) TextView user_message; @@ -105,13 +108,15 @@ public class SessionDialog extends DialogFragment{ private void setUp(Bundle arguments) { is_eip_pending = arguments.getBoolean(EipFragment.IS_PENDING, false); - if (arguments.containsKey(PASSWORD_INVALID_LENGTH)) + if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) password_field.setError(getString(R.string.error_not_valid_password_user_message)); + else if(arguments.containsKey(ERRORS.CONFUSING_CREDENTIALS.toString())) + username_field.setError("Perhaps you used your non LEAP credentials"); if (arguments.containsKey(USERNAME)) { String username = arguments.getString(USERNAME); username_field.setText(username); } - if (arguments.containsKey(USERNAME_MISSING)) { + if (arguments.containsKey(ERRORS.USERNAME_MISSING.toString())) { username_field.setError(getString(R.string.username_ask)); } if(arguments.containsKey(getString(R.string.user_message))) diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index 3117ea6f..c8fd0251 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -194,11 +194,11 @@ public class ProviderAPI extends IntentService { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); } if(!validUsername(username)) { result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.USERNAME_MISSING, true); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); } } @@ -244,11 +244,11 @@ public class ProviderAPI extends IntentService { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); } if(!validUsername(username)) { result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.USERNAME_MISSING, true); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); } } -- cgit v1.2.3 From d18be7ed1dfc645bd378c2eb0c152f7e0eda42e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 19 Mar 2015 16:56:32 +0100 Subject: Show gus message just for riseup login. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 34 ++++++++++++---------- .../java/se/leap/bitmaskclient/SessionDialog.java | 13 ++++----- app/src/main/res/values/strings.xml | 3 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 14d79bc0..7c5e5421 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -37,7 +37,7 @@ import se.leap.bitmaskclient.eip.*; /** * The main user facing Activity of Bitmask Android, consisting of status, controls, * and access to preferences. - * + * * @author Sean Leonard * @author parmegv */ @@ -202,7 +202,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn }) .show(); } - + /** * Inflates permanent UI elements of the View and contains logic for what * service dependent UI elements to include. @@ -248,7 +248,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn getMenuInflater().inflate(R.menu.client_dashboard, menu); return true; } - + @Override public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()){ @@ -279,7 +279,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn public void showAbout() { Intent intent = new Intent(this, AboutActivity.class); - startActivity(intent); + startActivity(intent); } public void showLog() { @@ -300,11 +300,11 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn Bundle parameters = bundlePassword(password); providerApiCommand(parameters, 0, ProviderAPI.LOG_IN); } - + public void logOut() { providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.LOG_OUT); } - + @Override public void update (Observable observable, Object data) { if(observable instanceof UserSessionStatus) { @@ -374,7 +374,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn eip_fragment.progress_bar.setVisibility(ProgressBar.VISIBLE); setStatusMessage(progressbar_message_resId); } - + Intent command = prepareProviderAPICommand(parameters, providerApi_action); startService(command); } @@ -382,9 +382,9 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn private Intent prepareProviderAPICommand(Bundle parameters, String action) { providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); - + Intent command = new Intent(this, ProviderAPI.class); - + command.putExtra(ProviderAPI.PARAMETERS, parameters); command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); command.setAction(action); @@ -394,12 +394,16 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn public void cancelLoginOrSignup() { EipStatus.getInstance().setConnectedOrDisconnected(); } - + public void sessionDialog(Bundle resultData) { - + FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); DialogFragment newFragment = new SessionDialog(); + if(provider.getName().equalsIgnoreCase("riseup")) { + resultData = resultData == Bundle.EMPTY ? new Bundle() : resultData; + resultData.putBoolean(SessionDialog.ERRORS.RISEUP_WARNING.toString(), true); + } if(resultData != null && !resultData.isEmpty()) { newFragment.setArguments(resultData); } @@ -408,7 +412,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn private void switchProvider() { if (provider.hasEIP()) eip_fragment.stopEipIfPossible(); - + preferences.edit().clear().apply(); switching_provider = false; startActivityForResult(new Intent(this, ConfigurationWizard.class), SWITCH_PROVIDER); @@ -426,8 +430,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { downloadVpnCertificate(); } else if(resultCode == ProviderAPI.FAILED_LOGIN) { - if(provider.getName().equalsIgnoreCase("riseup")) - resultData.putBoolean(SessionDialog.ERRORS.CONFUSING_CREDENTIALS.toString(), true); sessionDialog(resultData); } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { if(switching_provider) switchProvider(); @@ -456,7 +458,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn public static Context getContext() { return app; } - + @Override public void startActivityForResult(Intent intent, int requestCode) { intent.putExtra(Dashboard.REQUEST_CODE, requestCode); diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java index e32bca8e..91e56330 100644 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java @@ -26,8 +26,6 @@ import android.view.View; import android.widget.EditText; import android.widget.TextView; -import org.jetbrains.annotations.NotNull; - import butterknife.ButterKnife; import butterknife.InjectView; @@ -51,7 +49,7 @@ public class SessionDialog extends DialogFragment{ public static enum ERRORS { USERNAME_MISSING, PASSWORD_INVALID_LENGTH, - CONFUSING_CREDENTIALS + RISEUP_WARNING } @InjectView(R.id.user_message) @@ -110,9 +108,10 @@ public class SessionDialog extends DialogFragment{ is_eip_pending = arguments.getBoolean(EipFragment.IS_PENDING, false); if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) password_field.setError(getString(R.string.error_not_valid_password_user_message)); - else if(arguments.containsKey(ERRORS.CONFUSING_CREDENTIALS.toString())) - username_field.setError("Perhaps you used your non LEAP credentials"); - if (arguments.containsKey(USERNAME)) { + else if(arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { + user_message.setVisibility(TextView.VISIBLE); + user_message.setText(R.string.login_riseup_warning); + } if (arguments.containsKey(USERNAME)) { String username = arguments.getString(USERNAME); username_field.setText(username); } @@ -121,7 +120,7 @@ public class SessionDialog extends DialogFragment{ } if(arguments.containsKey(getString(R.string.user_message))) user_message.setText(arguments.getString(getString(R.string.user_message))); - else + else if(user_message.getVisibility() != TextView.VISIBLE) user_message.setVisibility(View.GONE); if(!username_field.getText().toString().isEmpty() && password_field.isFocusable()) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 632bc1f8..d1e33928 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -59,11 +59,10 @@ Logging out from this session. Logged out. Didn\'t log out. Try later, it may be a problem in the network or in the provider. If the problem persists, then wipe Bitmask data from the Android settings + "Riseup users: You need to create a separate account to use the VPN service" Authentication succeeded. Authentication failed. Registration failed. - Your own cert has been correctly downloaded. - Your own cert has incorrectly been downloaded. Initiating connection Cancel connection? There is a connection attempt in progress. Do you wish to cancel it? -- cgit v1.2.3 From 966ac406322f0c6b36b2fe746cd3f738bd0f5a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 20 Mar 2015 13:22:19 +0100 Subject: Detect error in last 5 lines of the log. --- app/src/main/java/se/leap/bitmaskclient/EipFragment.java | 5 +---- .../main/java/se/leap/bitmaskclient/eip/EipStatus.java | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 5e5b76b2..ed507de4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -53,7 +53,6 @@ public class EipFragment extends Fragment implements Observer { private boolean is_starting_to_connect; private boolean wants_to_connect; - @Override public void onAttach(Activity activity) { super.onAttach(activity); @@ -293,12 +292,10 @@ public class EipFragment extends Fragment implements Observer { private void setDisconnectedUI(){ hideProgressBar(); adjustSwitch(); - String last_log_message = eip_status.getLastLogMessage(dashboard.getApplicationContext()); - if((last_log_message.contains("error") || last_log_message.contains("ERROR")) + if(eip_status.errorInLast(5, dashboard.getApplicationContext()) && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ dashboard.showLog(); } - status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 14158696..adbbaef3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -136,9 +136,19 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { EipStatus.level = level; } - public String getLastLogMessage(Context context) { - VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); - return log[log.length-1].getString(context); + public boolean errorInLast(int lines, Context context) { + boolean result = false; + String[] error_keywords = {"error", "ERROR", "fatal", "FATAL"}; + + VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); + String message = ""; + for (int i = 0; i < lines; i++) { + message = log[log.length-1].getString(context); + for(int j = 0; j < error_keywords.length; j++) + if(message.contains(error_keywords[i])) + result = true; + } + return result; } @Override -- cgit v1.2.3 From 7c17d2654d9b6f32580e71e22ea08fca1893aa93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 23 Mar 2015 14:45:07 +0100 Subject: Ammend. --- .../java/se/leap/bitmaskclient/EipFragment.java | 7 ++-- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 8 +++- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 9 ++-- .../se/leap/bitmaskclient/eip/VoidVpnLauncher.java | 15 +++++-- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 49 ++++++++-------------- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 3 +- 6 files changed, 46 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index ed507de4..43e0c2aa 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -197,9 +197,9 @@ public class EipFragment extends Fragment implements Observer { eipCommand(Constants.ACTION_START_EIP); } - private void stop() { - if(eip_status.isConnecting()) - VoidVpnService.stop(); + public void stop() { + Intent intent = new Intent(dashboard.getApplicationContext(), VoidVpnService.class); + dashboard.stopService(intent); disconnect(); } @@ -296,6 +296,7 @@ public class EipFragment extends Fragment implements Observer { && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ dashboard.showLog(); } + status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { 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 3b72a486..5cac4588 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -126,8 +126,12 @@ public final class EIP extends IntentService { private void stopEIP() { EipStatus eip_status = EipStatus.getInstance(); int result_code = Activity.RESULT_CANCELED; - if(eip_status.isConnected() || eip_status.isConnecting()) - result_code = Activity.RESULT_OK; + if(eip_status.isConnected() || eip_status.isConnecting()) { + Intent intent = new Intent(); + intent.setComponent(VoidVpnLauncher.getService()); + stopService(intent); + result_code = Activity.RESULT_OK; + } tellToReceiver(ACTION_STOP_EIP, result_code); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index adbbaef3..3b9dad1c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -22,6 +22,7 @@ import android.content.*; import java.util.*; import de.blinkt.openvpn.core.*; +import se.leap.bitmaskclient.EipFragment; public class EipStatus extends Observable implements VpnStatus.StateListener { public static String TAG = EipStatus.class.getSimpleName(); @@ -48,8 +49,6 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { updateStatus(state, logmessage, localizedResId, level); if(isConnected() || isDisconnected()) { setConnectedOrDisconnected(); - if(isDisconnected()) - VoidVpnService.stop(); } else if(isConnecting()) setConnecting(); Log.d(TAG, "update state with level " + level); @@ -142,10 +141,10 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; - for (int i = 0; i < lines; i++) { - message = log[log.length-1].getString(context); + for (int i = 1; i <= lines && i < log.length; i++) { + message = log[log.length-i].getString(context); for(int j = 0; j < error_keywords.length; j++) - if(message.contains(error_keywords[i])) + if(message.contains(error_keywords[j])) result = true; } return result; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java index d79d8003..e7ce74d4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java @@ -1,6 +1,7 @@ package se.leap.bitmaskclient.eip; import android.app.Activity; +import android.content.ComponentName; import android.content.Intent; import android.net.VpnService; import android.os.Bundle; @@ -9,6 +10,8 @@ public class VoidVpnLauncher extends Activity { private static final int VPN_USER_PERMISSION = 71; + private static ComponentName service; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -27,11 +30,17 @@ public class VoidVpnLauncher extends Activity { protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == VPN_USER_PERMISSION) { if(resultCode == RESULT_OK) { - Intent void_vpn_service = new Intent(getApplicationContext(), VoidVpnService.class); - void_vpn_service.setAction(Constants.START_BLOCKING_VPN_PROFILE); - startService(void_vpn_service); + Intent intent = new Intent(getApplicationContext(), VoidVpnService.class); + intent.setAction(Constants.START_BLOCKING_VPN_PROFILE); + android.util.Log.d(VoidVpnLauncher.class.getSimpleName(), "VoidVpnService starting from Launcher"); + + service = startService(intent); } } finish(); } + + public static ComponentName getService() { + return service; + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index 0a707ab1..367a9f1d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -13,42 +13,29 @@ public class VoidVpnService extends VpnService { @Override public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent != null ? intent.getAction() : ""; - if (action == Constants.START_BLOCKING_VPN_PROFILE) { - new Thread(new Runnable() { - public void run() { - Builder builder = new Builder(); - builder.setSession("Blocking until running"); - builder.addAddress("10.42.0.8",16); - builder.addRoute("0.0.0.0", 1); - builder.addRoute("192.168.1.0", 24); - builder.addDnsServer("10.42.0.1"); - try { - fd = builder.establish(); - } catch (Exception e) { - e.printStackTrace(); - } - android.util.Log.d(TAG, "VoidVpnService set up"); - } - }).run(); - } - return 0; - } + String action = intent != null ? intent.getAction() : ""; + if (action == Constants.START_BLOCKING_VPN_PROFILE) { + start(); + } - @Override - public void onRevoke() { - super.onRevoke(); + return 0; } - public static boolean stop() { + protected void start() { + Builder builder = new Builder(); + builder.setSession("Blocking until running"); + builder.addAddress("10.42.0.8", 16); + builder.addRoute("0.0.0.0", 1); + builder.addRoute("192.168.1.0", 24); + builder.addDnsServer("10.42.0.1"); try { - if(fd != null) - fd.close(); - return true; - } catch (IOException | NullPointerException e) { - android.util.Log.d(TAG, "VoidVpnService didn't stop"); + fd = builder.establish(); + if (fd != null) + android.util.Log.d(TAG, "VoidVpnService set up"); + else + android.util.Log.d(TAG, "VoidVpnService failed to set up"); + } catch (Exception e) { e.printStackTrace(); - return false; } } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 5f5dcabd..c29cc180 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -148,6 +148,7 @@ public class VpnConfigGenerator { + new_line + "persist-tun" + new_line - + "auth-retry nointeract"; + + "auth-retry nointeract" + + "fragment 1400"; } } -- cgit v1.2.3 From ea748b512ecb4eaff6ad7e212d9356a1caf7ed98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 23 Mar 2015 14:48:12 +0100 Subject: 0.9.3RC2 --- CHANGELOG | 2 ++ app/src/main/AndroidManifest.xml | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 771c46e7..12fdeb6e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,8 @@ Bugs: Features: - Show the user status progress and info in its own place +- Remember riseup's users that this is a different service +- Advice users to be cautious when surfing the web after disconnecting the VPN 0.9.2 Bugs: diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2bd630ee..07315bdd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,8 +17,8 @@ + android:versionCode="118" + android:versionName="0.9.3RC2" > -- cgit v1.2.3 From 92a48eda9956193cfb143875a849ff979e16e4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 12:53:51 +0100 Subject: Revert "Ammend." This reverts commit 7c17d2654d9b6f32580e71e22ea08fca1893aa93. --- .../java/se/leap/bitmaskclient/EipFragment.java | 7 ++-- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 8 +--- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 9 ++-- .../se/leap/bitmaskclient/eip/VoidVpnLauncher.java | 15 ++----- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 49 ++++++++++++++-------- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 3 +- 6 files changed, 45 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 43e0c2aa..ed507de4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -197,9 +197,9 @@ public class EipFragment extends Fragment implements Observer { eipCommand(Constants.ACTION_START_EIP); } - public void stop() { - Intent intent = new Intent(dashboard.getApplicationContext(), VoidVpnService.class); - dashboard.stopService(intent); + private void stop() { + if(eip_status.isConnecting()) + VoidVpnService.stop(); disconnect(); } @@ -296,7 +296,6 @@ public class EipFragment extends Fragment implements Observer { && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ dashboard.showLog(); } - status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { 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 5cac4588..3b72a486 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -126,12 +126,8 @@ public final class EIP extends IntentService { private void stopEIP() { EipStatus eip_status = EipStatus.getInstance(); int result_code = Activity.RESULT_CANCELED; - if(eip_status.isConnected() || eip_status.isConnecting()) { - Intent intent = new Intent(); - intent.setComponent(VoidVpnLauncher.getService()); - stopService(intent); - result_code = Activity.RESULT_OK; - } + if(eip_status.isConnected() || eip_status.isConnecting()) + result_code = Activity.RESULT_OK; tellToReceiver(ACTION_STOP_EIP, result_code); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 3b9dad1c..adbbaef3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -22,7 +22,6 @@ import android.content.*; import java.util.*; import de.blinkt.openvpn.core.*; -import se.leap.bitmaskclient.EipFragment; public class EipStatus extends Observable implements VpnStatus.StateListener { public static String TAG = EipStatus.class.getSimpleName(); @@ -49,6 +48,8 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { updateStatus(state, logmessage, localizedResId, level); if(isConnected() || isDisconnected()) { setConnectedOrDisconnected(); + if(isDisconnected()) + VoidVpnService.stop(); } else if(isConnecting()) setConnecting(); Log.d(TAG, "update state with level " + level); @@ -141,10 +142,10 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; - for (int i = 1; i <= lines && i < log.length; i++) { - message = log[log.length-i].getString(context); + for (int i = 0; i < lines; i++) { + message = log[log.length-1].getString(context); for(int j = 0; j < error_keywords.length; j++) - if(message.contains(error_keywords[j])) + if(message.contains(error_keywords[i])) result = true; } return result; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java index e7ce74d4..d79d8003 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java @@ -1,7 +1,6 @@ package se.leap.bitmaskclient.eip; import android.app.Activity; -import android.content.ComponentName; import android.content.Intent; import android.net.VpnService; import android.os.Bundle; @@ -10,8 +9,6 @@ public class VoidVpnLauncher extends Activity { private static final int VPN_USER_PERMISSION = 71; - private static ComponentName service; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -30,17 +27,11 @@ public class VoidVpnLauncher extends Activity { protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == VPN_USER_PERMISSION) { if(resultCode == RESULT_OK) { - Intent intent = new Intent(getApplicationContext(), VoidVpnService.class); - intent.setAction(Constants.START_BLOCKING_VPN_PROFILE); - android.util.Log.d(VoidVpnLauncher.class.getSimpleName(), "VoidVpnService starting from Launcher"); - - service = startService(intent); + Intent void_vpn_service = new Intent(getApplicationContext(), VoidVpnService.class); + void_vpn_service.setAction(Constants.START_BLOCKING_VPN_PROFILE); + startService(void_vpn_service); } } finish(); } - - public static ComponentName getService() { - return service; - } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index 367a9f1d..0a707ab1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -13,29 +13,42 @@ public class VoidVpnService extends VpnService { @Override public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent != null ? intent.getAction() : ""; - if (action == Constants.START_BLOCKING_VPN_PROFILE) { - start(); - } + String action = intent != null ? intent.getAction() : ""; + if (action == Constants.START_BLOCKING_VPN_PROFILE) { + new Thread(new Runnable() { + public void run() { + Builder builder = new Builder(); + builder.setSession("Blocking until running"); + builder.addAddress("10.42.0.8",16); + builder.addRoute("0.0.0.0", 1); + builder.addRoute("192.168.1.0", 24); + builder.addDnsServer("10.42.0.1"); + try { + fd = builder.establish(); + } catch (Exception e) { + e.printStackTrace(); + } + android.util.Log.d(TAG, "VoidVpnService set up"); + } + }).run(); + } + return 0; + } - return 0; + @Override + public void onRevoke() { + super.onRevoke(); } - protected void start() { - Builder builder = new Builder(); - builder.setSession("Blocking until running"); - builder.addAddress("10.42.0.8", 16); - builder.addRoute("0.0.0.0", 1); - builder.addRoute("192.168.1.0", 24); - builder.addDnsServer("10.42.0.1"); + public static boolean stop() { try { - fd = builder.establish(); - if (fd != null) - android.util.Log.d(TAG, "VoidVpnService set up"); - else - android.util.Log.d(TAG, "VoidVpnService failed to set up"); - } catch (Exception e) { + if(fd != null) + fd.close(); + return true; + } catch (IOException | NullPointerException e) { + android.util.Log.d(TAG, "VoidVpnService didn't stop"); e.printStackTrace(); + return false; } } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index c29cc180..5f5dcabd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -148,7 +148,6 @@ public class VpnConfigGenerator { + new_line + "persist-tun" + new_line - + "auth-retry nointeract" - + "fragment 1400"; + + "auth-retry nointeract"; } } -- cgit v1.2.3 From 4d1ee9cd65d10161d3e818e99cbd06b93e211277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 12:58:18 +0100 Subject: Error in last lines fixed. --- app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java | 4 ++-- app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index adbbaef3..f9ddc0d2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -143,9 +143,9 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; for (int i = 0; i < lines; i++) { - message = log[log.length-1].getString(context); + message = log[log.length-i].getString(context); for(int j = 0; j < error_keywords.length; j++) - if(message.contains(error_keywords[i])) + if(message.contains(error_keywords[j])) result = true; } return result; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 5f5dcabd..25c85585 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -148,6 +148,6 @@ public class VpnConfigGenerator { + new_line + "persist-tun" + new_line - + "auth-retry nointeract"; + + "auth-retry nointeract remove"; } } -- cgit v1.2.3 From 7dba6442fb80cb55fe66f3d9510f71b80cb9e2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 13:06:55 +0100 Subject: Show log if an error is reported in last 5 log messages. --- app/src/main/java/se/leap/bitmaskclient/EipFragment.java | 1 + app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java | 2 +- app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index ed507de4..8b532e24 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -296,6 +296,7 @@ public class EipFragment extends Fragment implements Observer { && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ dashboard.showLog(); } + status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } private void adjustSwitch() { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index f9ddc0d2..17e290b8 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -142,7 +142,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; - for (int i = 0; i < lines; i++) { + for (int i = 1; i <= lines && log.length > i; i++) { message = log[log.length-i].getString(context); for(int j = 0; j < error_keywords.length; j++) if(message.contains(error_keywords[j])) diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 25c85585..5af8b8f1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -148,6 +148,6 @@ public class VpnConfigGenerator { + new_line + "persist-tun" + new_line - + "auth-retry nointeract remove"; + + "auth-retry nointeract" } } -- cgit v1.2.3 From 4d3e6e733d4f7f0c310642da2c43a35057018bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 13:12:32 +0100 Subject: Silly typo. --- app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 5af8b8f1..5f5dcabd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -148,6 +148,6 @@ public class VpnConfigGenerator { + new_line + "persist-tun" + new_line - + "auth-retry nointeract" + + "auth-retry nointeract"; } } -- cgit v1.2.3 From e4eeef39a6219d2abba26af5a588bc51c4f5af5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 17:40:50 +0100 Subject: 9 out of 10 times, in a row, void vpn is closed. --- .../java/se/leap/bitmaskclient/EipFragment.java | 1 + .../java/se/leap/bitmaskclient/eip/EipStatus.java | 2 -- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 32 ++++++++++++++-------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 8b532e24..ac869b18 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -295,6 +295,7 @@ public class EipFragment extends Fragment implements Observer { if(eip_status.errorInLast(5, dashboard.getApplicationContext()) && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ dashboard.showLog(); + VoidVpnService.stop(); } status_message.setText(dashboard.getString(R.string.eip_state_not_connected)); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 17e290b8..d1ed0d27 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -48,8 +48,6 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { updateStatus(state, logmessage, localizedResId, level); if(isConnected() || isDisconnected()) { setConnectedOrDisconnected(); - if(isDisconnected()) - VoidVpnService.stop(); } else if(isConnecting()) setConnecting(); Log.d(TAG, "update state with level " + level); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index 0a707ab1..f87f35e2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -11,12 +11,14 @@ public class VoidVpnService extends VpnService { static final String TAG = VoidVpnService.class.getSimpleName(); static ParcelFileDescriptor fd; + static Thread thread; + @Override public int onStartCommand(Intent intent, int flags, int startId) { String action = intent != null ? intent.getAction() : ""; if (action == Constants.START_BLOCKING_VPN_PROFILE) { - new Thread(new Runnable() { - public void run() { + thread = new Thread(new Runnable() { + public void run() { Builder builder = new Builder(); builder.setSession("Blocking until running"); builder.addAddress("10.42.0.8",16); @@ -25,30 +27,38 @@ public class VoidVpnService extends VpnService { builder.addDnsServer("10.42.0.1"); try { fd = builder.establish(); + } catch (Exception e) { e.printStackTrace(); } - android.util.Log.d(TAG, "VoidVpnService set up"); + android.util.Log.d(TAG, "VoidVpnService set up: fd = " + fd.toString()); } - }).run(); - } + }); + thread.run(); + } return 0; } @Override public void onRevoke() { super.onRevoke(); + closeFd(); + } + + public static void stop() { + if(thread != null) + thread.interrupt(); + closeFd(); } - public static boolean stop() { + private static void closeFd() { try { - if(fd != null) + if(fd != null) { + android.util.Log.d(TAG, "VoidVpnService closing fd = " + fd.toString()); fd.close(); - return true; - } catch (IOException | NullPointerException e) { - android.util.Log.d(TAG, "VoidVpnService didn't stop"); + } + } catch (IOException e) { e.printStackTrace(); - return false; } } } -- cgit v1.2.3 From 1e300837b2b2188bc682af7b1dfadfa16942725b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 17:57:20 +0100 Subject: Remove unused imports. Using intellij tool. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 40 +++++++++++++++------- .../java/se/leap/bitmaskclient/EipFragment.java | 37 ++++++++++++++------ .../main/java/se/leap/bitmaskclient/Provider.java | 4 --- .../se/leap/bitmaskclient/ProviderListAdapter.java | 5 --- .../se/leap/bitmaskclient/ProviderManager.java | 1 - .../se/leap/bitmaskclient/ProviderRenderer.java | 3 -- .../bitmaskclient/ProviderRendererBuilder.java | 6 ---- .../se/leap/bitmaskclient/UserSessionStatus.java | 4 +-- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 35 +++++++++++++------ .../java/se/leap/bitmaskclient/eip/EipStatus.java | 8 ++--- .../java/se/leap/bitmaskclient/eip/Gateway.java | 3 -- .../se/leap/bitmaskclient/eip/GatewaysManager.java | 26 +++++++++----- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 3 -- 13 files changed, 100 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 7c5e5421..bfa0e123 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -17,22 +17,38 @@ package se.leap.bitmaskclient; import android.annotation.SuppressLint; -import android.app.*; -import android.content.*; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.DialogFragment; +import android.app.FragmentTransaction; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; -import android.os.*; +import android.os.Bundle; +import android.os.Handler; import android.util.Log; -import android.view.*; -import android.widget.*; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.ProgressBar; +import android.widget.TextView; import org.jetbrains.annotations.NotNull; -import org.json.*; -import java.net.*; -import java.util.*; - -import butterknife.*; -import de.blinkt.openvpn.activities.*; -import se.leap.bitmaskclient.eip.*; +import org.json.JSONException; +import org.json.JSONObject; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Observable; +import java.util.Observer; + +import butterknife.ButterKnife; +import butterknife.InjectView; +import de.blinkt.openvpn.activities.LogWindow; +import se.leap.bitmaskclient.eip.Constants; +import se.leap.bitmaskclient.eip.EIP; +import se.leap.bitmaskclient.eip.EipStatus; /** * The main user facing Activity of Bitmask Android, consisting of status, controls, diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index ac869b18..153d473b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -16,20 +16,35 @@ */ package se.leap.bitmaskclient; -import android.app.*; -import android.content.*; -import android.os.*; -import android.util.*; -import android.view.*; -import android.widget.*; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Fragment; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.ResultReceiver; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ProgressBar; +import android.widget.Switch; +import android.widget.TextView; import org.jetbrains.annotations.NotNull; -import java.util.*; - -import butterknife.*; -import de.blinkt.openvpn.activities.*; -import se.leap.bitmaskclient.eip.*; +import java.util.Observable; +import java.util.Observer; + +import butterknife.ButterKnife; +import butterknife.InjectView; +import butterknife.OnCheckedChanged; +import de.blinkt.openvpn.activities.DisconnectVPN; +import se.leap.bitmaskclient.eip.Constants; +import se.leap.bitmaskclient.eip.EIP; +import se.leap.bitmaskclient.eip.EipStatus; +import se.leap.bitmaskclient.eip.VoidVpnService; public class EipFragment extends Fragment implements Observer { diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index ef25c5d2..f2587968 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -16,9 +16,6 @@ */ package se.leap.bitmaskclient; -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences; import android.os.Parcel; import android.os.Parcelable; @@ -27,7 +24,6 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.File; -import java.io.Serializable; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index c63e2edb..e7a6a536 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -1,18 +1,13 @@ package se.leap.bitmaskclient; -import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TwoLineListItem; import com.pedrogomez.renderers.AdapteeCollection; import com.pedrogomez.renderers.RendererAdapter; import com.pedrogomez.renderers.RendererBuilder; -import java.util.Collection; -import java.util.List; import java.util.Set; public class ProviderListAdapter extends RendererAdapter { diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java index 69bd7c1e..e0dd0989 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -10,7 +10,6 @@ import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java index 6e194e84..60ef9791 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java @@ -4,15 +4,12 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; import android.widget.TextView; import com.pedrogomez.renderers.Renderer; import butterknife.ButterKnife; import butterknife.InjectView; -import butterknife.OnItemClick; -import butterknife.OnItemSelected; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java index 7366e68e..4a4f3901 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java @@ -1,15 +1,9 @@ package se.leap.bitmaskclient; -import android.content.Context; - import com.pedrogomez.renderers.Renderer; import com.pedrogomez.renderers.RendererBuilder; import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import javax.inject.Inject; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java index db432a82..2779efd0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -16,9 +16,7 @@ */ package se.leap.bitmaskclient; -import android.os.*; - -import java.util.*; +import java.util.Observable; public class UserSessionStatus extends Observable { public static String TAG = UserSessionStatus.class.getSimpleName(); 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 3b72a486..58cd2cee 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -16,17 +16,30 @@ */ package se.leap.bitmaskclient.eip; -import android.app.*; -import android.content.*; -import android.os.*; -import android.util.Log; - -import org.json.*; - -import de.blinkt.openvpn.*; -import se.leap.bitmaskclient.*; - -import static se.leap.bitmaskclient.eip.Constants.*; +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 org.json.JSONException; +import org.json.JSONObject; + +import de.blinkt.openvpn.LaunchVPN; +import se.leap.bitmaskclient.Dashboard; +import se.leap.bitmaskclient.EipFragment; + +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; /** * EIP is the abstract base class for interacting with and managing the Encrypted diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index d1ed0d27..902ef8da 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -16,12 +16,12 @@ */ package se.leap.bitmaskclient.eip; -import android.util.*; -import android.content.*; +import android.content.Context; +import android.util.Log; -import java.util.*; +import java.util.Observable; -import de.blinkt.openvpn.core.*; +import de.blinkt.openvpn.core.VpnStatus; public class EipStatus extends Observable implements VpnStatus.StateListener { public static String TAG = EipStatus.class.getSimpleName(); 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 0d8a2f7b..bc978684 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -16,8 +16,6 @@ */ package se.leap.bitmaskclient.eip; -import android.app.Activity; -import android.content.SharedPreferences; import android.util.Log; import com.google.gson.Gson; @@ -30,7 +28,6 @@ import java.io.StringReader; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; -import se.leap.bitmaskclient.Dashboard; /** * Gateway provides objects defining gateways and their metadata. diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index 401cd701..d79a599b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -16,20 +16,28 @@ */ package se.leap.bitmaskclient.eip; -import android.content.*; +import android.content.Context; +import android.content.SharedPreferences; import android.util.Log; -import com.google.gson.*; -import com.google.gson.reflect.*; +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.google.gson.reflect.TypeToken; -import org.json.*; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; -import java.lang.reflect.*; -import java.util.*; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; -import de.blinkt.openvpn.*; -import de.blinkt.openvpn.core.*; -import se.leap.bitmaskclient.*; +import de.blinkt.openvpn.VpnProfile; +import de.blinkt.openvpn.core.Connection; +import de.blinkt.openvpn.core.ProfileManager; +import se.leap.bitmaskclient.Provider; /** * @author parmegv diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 5f5dcabd..1fe7df9f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -16,9 +16,6 @@ */ package se.leap.bitmaskclient.eip; -import android.content.SharedPreferences; -import android.util.Log; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -- cgit v1.2.3 From 26efe3b9b2e1cbfe87f88239e1a3c9264ae1e1db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 17:59:08 +0100 Subject: Revert "Remove unused imports." This reverts commit 1e300837b2b2188bc682af7b1dfadfa16942725b. I don't want big lists of imports, I like wildcards. --- .../main/java/se/leap/bitmaskclient/Dashboard.java | 40 +++++++--------------- .../java/se/leap/bitmaskclient/EipFragment.java | 37 ++++++-------------- .../main/java/se/leap/bitmaskclient/Provider.java | 4 +++ .../se/leap/bitmaskclient/ProviderListAdapter.java | 5 +++ .../se/leap/bitmaskclient/ProviderManager.java | 1 + .../se/leap/bitmaskclient/ProviderRenderer.java | 3 ++ .../bitmaskclient/ProviderRendererBuilder.java | 6 ++++ .../se/leap/bitmaskclient/UserSessionStatus.java | 4 ++- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 35 ++++++------------- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 8 ++--- .../java/se/leap/bitmaskclient/eip/Gateway.java | 3 ++ .../se/leap/bitmaskclient/eip/GatewaysManager.java | 26 +++++--------- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 3 ++ 13 files changed, 75 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index bfa0e123..7c5e5421 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -17,38 +17,22 @@ package se.leap.bitmaskclient; import android.annotation.SuppressLint; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.DialogFragment; -import android.app.FragmentTransaction; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.SharedPreferences; +import android.app.*; +import android.content.*; import android.content.pm.PackageManager.NameNotFoundException; -import android.os.Bundle; -import android.os.Handler; +import android.os.*; import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.ProgressBar; -import android.widget.TextView; +import android.view.*; +import android.widget.*; import org.jetbrains.annotations.NotNull; -import org.json.JSONException; -import org.json.JSONObject; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Observable; -import java.util.Observer; - -import butterknife.ButterKnife; -import butterknife.InjectView; -import de.blinkt.openvpn.activities.LogWindow; -import se.leap.bitmaskclient.eip.Constants; -import se.leap.bitmaskclient.eip.EIP; -import se.leap.bitmaskclient.eip.EipStatus; +import org.json.*; +import java.net.*; +import java.util.*; + +import butterknife.*; +import de.blinkt.openvpn.activities.*; +import se.leap.bitmaskclient.eip.*; /** * The main user facing Activity of Bitmask Android, consisting of status, controls, diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 153d473b..ac869b18 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -16,35 +16,20 @@ */ package se.leap.bitmaskclient; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.os.ResultReceiver; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ProgressBar; -import android.widget.Switch; -import android.widget.TextView; +import android.app.*; +import android.content.*; +import android.os.*; +import android.util.*; +import android.view.*; +import android.widget.*; import org.jetbrains.annotations.NotNull; -import java.util.Observable; -import java.util.Observer; - -import butterknife.ButterKnife; -import butterknife.InjectView; -import butterknife.OnCheckedChanged; -import de.blinkt.openvpn.activities.DisconnectVPN; -import se.leap.bitmaskclient.eip.Constants; -import se.leap.bitmaskclient.eip.EIP; -import se.leap.bitmaskclient.eip.EipStatus; -import se.leap.bitmaskclient.eip.VoidVpnService; +import java.util.*; + +import butterknife.*; +import de.blinkt.openvpn.activities.*; +import se.leap.bitmaskclient.eip.*; public class EipFragment extends Fragment implements Observer { diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index f2587968..ef25c5d2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -16,6 +16,9 @@ */ package se.leap.bitmaskclient; +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; import android.os.Parcel; import android.os.Parcelable; @@ -24,6 +27,7 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.File; +import java.io.Serializable; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index e7a6a536..c63e2edb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -1,13 +1,18 @@ package se.leap.bitmaskclient; +import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TwoLineListItem; import com.pedrogomez.renderers.AdapteeCollection; import com.pedrogomez.renderers.RendererAdapter; import com.pedrogomez.renderers.RendererBuilder; +import java.util.Collection; +import java.util.List; import java.util.Set; public class ProviderListAdapter extends RendererAdapter { diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java index e0dd0989..69bd7c1e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -10,6 +10,7 @@ import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java index 60ef9791..6e194e84 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java @@ -4,12 +4,15 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.TextView; import com.pedrogomez.renderers.Renderer; import butterknife.ButterKnife; import butterknife.InjectView; +import butterknife.OnItemClick; +import butterknife.OnItemSelected; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java index 4a4f3901..7366e68e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java @@ -1,9 +1,15 @@ package se.leap.bitmaskclient; +import android.content.Context; + import com.pedrogomez.renderers.Renderer; import com.pedrogomez.renderers.RendererBuilder; import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import javax.inject.Inject; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java index 2779efd0..db432a82 100644 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -16,7 +16,9 @@ */ package se.leap.bitmaskclient; -import java.util.Observable; +import android.os.*; + +import java.util.*; public class UserSessionStatus extends Observable { public static String TAG = UserSessionStatus.class.getSimpleName(); 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 58cd2cee..3b72a486 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -16,30 +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 org.json.JSONException; -import org.json.JSONObject; - -import de.blinkt.openvpn.LaunchVPN; -import se.leap.bitmaskclient.Dashboard; -import se.leap.bitmaskclient.EipFragment; - -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 android.app.*; +import android.content.*; +import android.os.*; +import android.util.Log; + +import org.json.*; + +import de.blinkt.openvpn.*; +import se.leap.bitmaskclient.*; + +import static se.leap.bitmaskclient.eip.Constants.*; /** * EIP is the abstract base class for interacting with and managing the Encrypted diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index 902ef8da..d1ed0d27 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -16,12 +16,12 @@ */ package se.leap.bitmaskclient.eip; -import android.content.Context; -import android.util.Log; +import android.util.*; +import android.content.*; -import java.util.Observable; +import java.util.*; -import de.blinkt.openvpn.core.VpnStatus; +import de.blinkt.openvpn.core.*; public class EipStatus extends Observable implements VpnStatus.StateListener { public static String TAG = EipStatus.class.getSimpleName(); 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 bc978684..0d8a2f7b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -16,6 +16,8 @@ */ package se.leap.bitmaskclient.eip; +import android.app.Activity; +import android.content.SharedPreferences; import android.util.Log; import com.google.gson.Gson; @@ -28,6 +30,7 @@ import java.io.StringReader; import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; +import se.leap.bitmaskclient.Dashboard; /** * Gateway provides objects defining gateways and their metadata. diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index d79a599b..401cd701 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -16,28 +16,20 @@ */ package se.leap.bitmaskclient.eip; -import android.content.Context; -import android.content.SharedPreferences; +import android.content.*; import android.util.Log; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; -import com.google.gson.reflect.TypeToken; +import com.google.gson.*; +import com.google.gson.reflect.*; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.*; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; +import java.lang.reflect.*; +import java.util.*; -import de.blinkt.openvpn.VpnProfile; -import de.blinkt.openvpn.core.Connection; -import de.blinkt.openvpn.core.ProfileManager; -import se.leap.bitmaskclient.Provider; +import de.blinkt.openvpn.*; +import de.blinkt.openvpn.core.*; +import se.leap.bitmaskclient.*; /** * @author parmegv diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 1fe7df9f..5f5dcabd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -16,6 +16,9 @@ */ package se.leap.bitmaskclient.eip; +import android.content.SharedPreferences; +import android.util.Log; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -- cgit v1.2.3 From 9bb172e96cfd741e528e692118ee32fc609c6da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 18:59:27 +0100 Subject: Cleaned imports, use wildcards when appropriate --- .../leap/bitmaskclient/test/ConnectionManager.java | 11 +++----- .../se/leap/bitmaskclient/test/FromAssets.java | 9 ++---- .../test/testDashboardIntegration.java | 4 +-- .../java/se/leap/bitmaskclient/test/testEIP.java | 14 ++++----- .../bitmaskclient/test/testGatewaysManager.java | 19 +++++-------- .../bitmaskclient/test/testLeapSRPSession.java | 19 +++++-------- .../se/leap/bitmaskclient/ConfigurationWizard.java | 6 ++-- .../se/leap/bitmaskclient/NewProviderDialog.java | 21 +++++--------- .../java/se/leap/bitmaskclient/ProviderAPI.java | 8 +++--- .../leap/bitmaskclient/ProviderDetailFragment.java | 5 ++-- .../se/leap/bitmaskclient/ProviderListContent.java | 23 +++++---------- .../java/se/leap/bitmaskclient/AboutActivity.java | 10 +++---- .../java/se/leap/bitmaskclient/ConfigHelper.java | 26 ++++++----------- .../main/java/se/leap/bitmaskclient/Dashboard.java | 8 +++--- .../leap/bitmaskclient/DownloadFailedDialog.java | 9 ++---- .../java/se/leap/bitmaskclient/EipFragment.java | 2 +- .../bitmaskclient/FragmentManagerEnhanced.java | 4 +-- .../java/se/leap/bitmaskclient/LeapSRPSession.java | 14 ++++----- .../java/se/leap/bitmaskclient/OnBootReceiver.java | 9 ++---- .../main/java/se/leap/bitmaskclient/PRNGFixes.java | 21 ++++---------- .../main/java/se/leap/bitmaskclient/Provider.java | 23 +++++---------- .../bitmaskclient/ProviderAPIResultReceiver.java | 4 +-- .../se/leap/bitmaskclient/ProviderListAdapter.java | 15 ++-------- .../se/leap/bitmaskclient/ProviderManager.java | 27 +++++------------- .../se/leap/bitmaskclient/ProviderRenderer.java | 20 +++++-------- .../bitmaskclient/ProviderRendererBuilder.java | 12 ++------ .../java/se/leap/bitmaskclient/SessionDialog.java | 19 +++++-------- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 1 - .../java/se/leap/bitmaskclient/eip/Gateway.java | 18 ++++-------- .../se/leap/bitmaskclient/eip/GatewaySelector.java | 6 +--- .../se/leap/bitmaskclient/eip/GatewaysManager.java | 2 +- .../se/leap/bitmaskclient/eip/VoidVpnLauncher.java | 8 +++--- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 8 +++--- .../bitmaskclient/eip/VpnCertificateValidator.java | 10 +++---- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 13 ++------- .../se/leap/bitmaskclient/ConfigurationWizard.java | 4 +-- .../se/leap/bitmaskclient/NewProviderDialog.java | 19 ++++--------- .../java/se/leap/bitmaskclient/ProviderAPI.java | 8 +++--- .../leap/bitmaskclient/ProviderDetailFragment.java | 33 +++++++++++----------- .../se/leap/bitmaskclient/ProviderListContent.java | 22 +++++---------- 40 files changed, 180 insertions(+), 334 deletions(-) diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java index e67dd820..96abc7eb 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java @@ -1,12 +1,9 @@ package se.leap.bitmaskclient.test; -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.wifi.WifiManager; -import android.util.Log; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; +import android.content.*; +import android.net.*; +import android.net.wifi.*; +import java.lang.reflect.*; public class ConnectionManager { static void setMobileDataEnabled(boolean enabled, Context context) { diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java index 4f771922..fd8b9a7e 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java @@ -1,11 +1,8 @@ package se.leap.bitmaskclient.test; -import android.content.Context; - -import org.json.JSONException; - -import java.io.IOException; -import java.io.InputStream; +import android.content.*; +import org.json.*; +import java.io.*; public class FromAssets { diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java index 91b93d42..57213bc1 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java @@ -1,10 +1,10 @@ package se.leap.bitmaskclient.test; -import android.content.Context; +import android.content.*; import android.test.*; import com.robotium.solo.*; -import java.io.IOException; +import java.io.*; import de.blinkt.openvpn.activities.*; import se.leap.bitmaskclient.*; 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 d9235085..b504af99 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testEIP.java @@ -16,15 +16,13 @@ */ package se.leap.bitmaskclient.test; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.test.ServiceTestCase; -import android.test.suitebuilder.annotation.MediumTest; -import se.leap.bitmaskclient.Dashboard; -import se.leap.bitmaskclient.eip.Constants; -import se.leap.bitmaskclient.eip.EIP; +import android.content.*; +import android.test.*; +import android.test.suitebuilder.annotation.*; + +import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.eip.*; /** * @author parmegv diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java index c4303251..addbc53c 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testGatewaysManager.java @@ -16,20 +16,15 @@ */ 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 android.app.*; +import android.content.*; +import android.test.*; +import android.test.suitebuilder.annotation.*; -import junit.framework.Test; +import org.json.*; -import org.json.JSONObject; - -import se.leap.bitmaskclient.Dashboard; -import se.leap.bitmaskclient.eip.Gateway; -import se.leap.bitmaskclient.eip.GatewaysManager; +import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.eip.*; /** * @author parmegv diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testLeapSRPSession.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testLeapSRPSession.java index d7f4bfb3..e25d4e56 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testLeapSRPSession.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testLeapSRPSession.java @@ -1,20 +1,15 @@ package se.leap.bitmaskclient.test; -import java.io.UnsupportedEncodingException; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Arrays; +import java.io.*; +import java.math.*; +import java.security.*; +import java.util.*; -import org.jboss.security.srp.SRPParameters; +import android.test.suitebuilder.annotation.*; -import android.test.suitebuilder.annotation.SmallTest; +import se.leap.bitmaskclient.*; -import se.leap.bitmaskclient.ConfigHelper; -import se.leap.bitmaskclient.LeapSRPSession; - -import junit.framework.TestCase; +import junit.framework.*; public class testLeapSRPSession extends TestCase { diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index ed2a063c..63e5272e 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -28,17 +28,17 @@ import java.net.*; import java.util.*; import butterknife.*; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.*; import org.json.*; -import javax.inject.Inject; +import javax.inject.*; import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; import se.leap.bitmaskclient.ProviderDetailFragment.ProviderDetailFragmentInterface; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import se.leap.bitmaskclient.eip.Constants; +import se.leap.bitmaskclient.eip.*; /** * Activity that builds and shows the list of known available providers. diff --git a/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java b/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java index 8fe1c3eb..01c13ca3 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java +++ b/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java @@ -14,22 +14,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +package se.leap.bitmaskclient; -import butterknife.ButterKnife; -import butterknife.InjectView; +import butterknife.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.Toast; +import android.app.*; +import android.content.*; +import android.os.*; +import android.view.*; +import android.widget.*; /** * Implements the new custom provider dialog. diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index 57c4d067..f17c7151 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -16,19 +16,19 @@ */ package se.leap.bitmaskclient; -import android.app.IntentService; +import android.app.*; import android.content.*; import android.os.*; import android.util.*; import java.io.*; -import java.math.BigInteger; +import java.math.*; import java.net.*; import java.security.*; import java.security.cert.*; -import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.*; import java.util.*; import javax.net.ssl.*; -import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.*; import org.json.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java index 4b46e48f..09e415c5 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java @@ -2,15 +2,14 @@ package se.leap.bitmaskclient; import org.json.*; -import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.eip.Constants; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import android.app.*; import android.content.*; -import android.os.Bundle; +import android.os.*; import android.view.*; -import android.widget.TextView; +import android.widget.*; public class ProviderDetailFragment extends DialogFragment { diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java index 8c8fb69f..48273d6a 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java @@ -16,17 +16,8 @@ */ package se.leap.bitmaskclient; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.net.URL; -import java.net.MalformedURLException; - -import org.json.JSONException; -import org.json.JSONObject; +import java.util.*; +import java.net.*; /** * Models the provider list shown in the ConfigurationWizard. @@ -55,20 +46,20 @@ public class ProviderListContent { /** * A provider item. - */ + */ public static class ProviderItem { final public static String CUSTOM = "custom"; final public static String DANGER_ON = "danger_on"; private String provider_main_url; - private String name; - + private String name; + /** * @param name of the provider * @param provider_main_url used to download provider.json file of the provider - */ + */ public ProviderItem(String name, String provider_main_url) { this.name = name; - this.provider_main_url = provider_main_url; + this.provider_main_url = provider_main_url; } public String name() { return name; } diff --git a/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java b/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java index 6c4e517b..fa79d009 100644 --- a/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java @@ -1,10 +1,10 @@ package se.leap.bitmaskclient; -import android.app.Activity; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager.NameNotFoundException; -import android.os.Bundle; -import android.widget.TextView; +import android.app.*; +import android.content.pm.*; +import android.content.pm.PackageManager.*; +import android.os.*; +import android.widget.*; public class AboutActivity extends Activity { diff --git a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java index c0f0b0c3..b20cb1ae 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java @@ -16,26 +16,16 @@ */ package se.leap.bitmaskclient; -import android.util.Base64; +import android.util.*; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.*; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.math.BigInteger; -import java.security.KeyFactory; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.security.interfaces.RSAPrivateKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; +import java.io.*; +import java.math.*; +import java.security.*; +import java.security.cert.*; +import java.security.interfaces.*; +import java.security.spec.*; /** * Stores constants, and implements auxiliary methods used across all LEAP Android classes. diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 7c5e5421..7e2fa0b5 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -16,16 +16,16 @@ */ package se.leap.bitmaskclient; -import android.annotation.SuppressLint; +import android.annotation.*; import android.app.*; import android.content.*; -import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PackageManager.*; import android.os.*; -import android.util.Log; +import android.util.*; import android.view.*; import android.widget.*; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.*; import org.json.*; import java.net.*; import java.util.*; diff --git a/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java b/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java index a44253c6..b26205bb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java @@ -16,12 +16,9 @@ */ package se.leap.bitmaskclient; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.content.DialogInterface; -import android.os.Bundle; +import android.app.*; +import android.content.*; +import android.os.*; /** * Implements a dialog to show why a download failed. diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index ac869b18..a5f3118c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -23,7 +23,7 @@ import android.util.*; import android.view.*; import android.widget.*; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.*; import java.util.*; diff --git a/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java b/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java index 49af9274..9975b127 100644 --- a/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java +++ b/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java @@ -16,9 +16,7 @@ */ package se.leap.bitmaskclient; -import android.app.Fragment; -import android.app.FragmentManager; -import android.app.FragmentTransaction; +import android.app.*; public class FragmentManagerEnhanced { diff --git a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java index 49c115ae..d3085fa3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java +++ b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java @@ -17,14 +17,12 @@ package se.leap.bitmaskclient; -import org.jboss.security.srp.SRPParameters; - -import java.io.UnsupportedEncodingException; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Arrays; +import org.jboss.security.srp.*; + +import java.io.*; +import java.math.*; +import java.security.*; +import java.util.*; /** * Implements all SRP algorithm logic. diff --git a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java index 96b87085..806d5602 100644 --- a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java +++ b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java @@ -1,12 +1,9 @@ package se.leap.bitmaskclient; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.util.Log; +import android.content.*; +import android.util.*; -import se.leap.bitmaskclient.eip.Constants; +import se.leap.bitmaskclient.eip.*; public class OnBootReceiver extends BroadcastReceiver { diff --git a/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java b/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java index a046f01f..33d2971d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java +++ b/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java @@ -12,24 +12,13 @@ package se.leap.bitmaskclient; * Source: http://android-developers.blogspot.de/2013/08/some-securerandom-thoughts.html */ -import android.os.Build; +import android.os.*; import android.os.Process; -import android.util.Log; - -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.security.NoSuchAlgorithmException; +import android.util.*; + +import java.io.*; +import java.security.*; import java.security.Provider; -import java.security.SecureRandom; -import java.security.SecureRandomSpi; -import java.security.Security; /** * Fixes for the output of the default PRNG having low entropy. diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index ef25c5d2..f45eccdd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -16,22 +16,13 @@ */ package se.leap.bitmaskclient; -import android.app.Activity; -import android.content.Context; -import android.content.SharedPreferences; -import android.os.Parcel; -import android.os.Parcelable; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.File; -import java.io.Serializable; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Arrays; -import java.util.Locale; +import android.os.*; + +import org.json.*; + +import java.io.*; +import java.net.*; +import java.util.*; /** * @author Sean Leonard diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderAPIResultReceiver.java b/app/src/main/java/se/leap/bitmaskclient/ProviderAPIResultReceiver.java index 7e4e95d3..4e6333c0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderAPIResultReceiver.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderAPIResultReceiver.java @@ -16,9 +16,7 @@ */ package se.leap.bitmaskclient; -import android.os.Bundle; -import android.os.Handler; -import android.os.ResultReceiver; +import android.os.*; /** * Implements the ResultReceiver needed by Activities using ProviderAPI to receive the results of its operations. diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java index c63e2edb..f0103986 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java @@ -1,19 +1,10 @@ package se.leap.bitmaskclient; -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TwoLineListItem; +import android.view.*; -import com.pedrogomez.renderers.AdapteeCollection; -import com.pedrogomez.renderers.RendererAdapter; -import com.pedrogomez.renderers.RendererBuilder; +import com.pedrogomez.renderers.*; -import java.util.Collection; -import java.util.List; -import java.util.Set; +import java.util.*; public class ProviderListAdapter extends RendererAdapter { private static boolean[] hidden = null; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java index 69bd7c1e..56b38a29 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -1,25 +1,12 @@ package se.leap.bitmaskclient; -import android.content.res.AssetManager; - -import com.pedrogomez.renderers.AdapteeCollection; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; +import android.content.res.*; +import com.pedrogomez.renderers.*; +import org.json.*; + +import java.io.*; +import java.net.*; +import java.util.*; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java index 6e194e84..4a5d0db1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRenderer.java @@ -1,18 +1,12 @@ package se.leap.bitmaskclient; -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import com.pedrogomez.renderers.Renderer; - -import butterknife.ButterKnife; -import butterknife.InjectView; -import butterknife.OnItemClick; -import butterknife.OnItemSelected; +import android.content.*; +import android.view.*; +import android.widget.*; + +import com.pedrogomez.renderers.*; + +import butterknife.*; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java index 7366e68e..1b42e349 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java @@ -1,15 +1,7 @@ package se.leap.bitmaskclient; -import android.content.Context; - -import com.pedrogomez.renderers.Renderer; -import com.pedrogomez.renderers.RendererBuilder; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import javax.inject.Inject; +import com.pedrogomez.renderers.*; +import java.util.*; /** * Created by parmegv on 4/12/14. diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java index 91e56330..6346fc36 100644 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java @@ -16,18 +16,13 @@ */ package se.leap.bitmaskclient; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.DialogFragment; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.EditText; -import android.widget.TextView; - -import butterknife.ButterKnife; -import butterknife.InjectView; +import android.app.*; +import android.content.*; +import android.os.*; +import android.view.*; +import android.widget.*; + +import butterknife.*; /** * Implements the log in dialog, currently without progress dialog. 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 3b72a486..6335f818 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -19,7 +19,6 @@ package se.leap.bitmaskclient.eip; import android.app.*; import android.content.*; import android.os.*; -import android.util.Log; import org.json.*; 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 0d8a2f7b..10ddc0ef 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -16,21 +16,15 @@ */ package se.leap.bitmaskclient.eip; -import android.app.Activity; -import android.content.SharedPreferences; -import android.util.Log; +import android.util.*; -import com.google.gson.Gson; +import com.google.gson.*; -import org.json.JSONException; -import org.json.JSONObject; +import org.json.*; +import java.io.*; -import java.io.IOException; -import java.io.StringReader; - -import de.blinkt.openvpn.VpnProfile; -import de.blinkt.openvpn.core.ConfigParser; -import se.leap.bitmaskclient.Dashboard; +import de.blinkt.openvpn.*; +import de.blinkt.openvpn.core.*; /** * Gateway provides objects defining gateways and their metadata. diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java index 39ae7ca6..11823511 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java @@ -1,10 +1,6 @@ package se.leap.bitmaskclient.eip; -import java.util.Calendar; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.TreeMap; +import java.util.*; public class GatewaySelector { List gateways; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index 401cd701..af5670f5 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -17,7 +17,7 @@ package se.leap.bitmaskclient.eip; import android.content.*; -import android.util.Log; +import android.util.*; import com.google.gson.*; import com.google.gson.reflect.*; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java index d79d8003..39a802c6 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java @@ -1,9 +1,9 @@ package se.leap.bitmaskclient.eip; -import android.app.Activity; -import android.content.Intent; -import android.net.VpnService; -import android.os.Bundle; +import android.app.*; +import android.content.*; +import android.net.*; +import android.os.*; public class VoidVpnLauncher extends Activity { diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index f87f35e2..e864288a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -1,10 +1,10 @@ package se.leap.bitmaskclient.eip; -import android.content.Intent; -import android.net.VpnService; -import android.os.ParcelFileDescriptor; +import android.content.*; +import android.net.*; +import android.os.*; -import java.io.IOException; +import java.io.*; public class VoidVpnService extends VpnService { 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 0bbe9db4..27971c3d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -16,14 +16,12 @@ */ package se.leap.bitmaskclient.eip; -import android.util.Log; +import android.util.*; -import java.security.cert.CertificateExpiredException; -import java.security.cert.CertificateNotYetValidException; -import java.security.cert.X509Certificate; -import java.util.Calendar; +import java.security.cert.*; +import java.util.*; -import se.leap.bitmaskclient.ConfigHelper; +import se.leap.bitmaskclient.*; public class VpnCertificateValidator { public final static String TAG = VpnCertificateValidator.class.getSimpleName(); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 5f5dcabd..8b1bc793 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -16,16 +16,9 @@ */ package se.leap.bitmaskclient.eip; -import android.content.SharedPreferences; -import android.util.Log; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Iterator; - -import se.leap.bitmaskclient.Provider; +import org.json.*; +import java.util.*; +import se.leap.bitmaskclient.*; public class VpnConfigGenerator { diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index f5e78e65..2b35080b 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -28,10 +28,10 @@ import java.net.*; import java.util.*; import butterknife.*; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.*; import org.json.*; -import javax.inject.Inject; +import javax.inject.*; import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; diff --git a/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java b/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java index f6709c22..96437eb2 100644 --- a/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java +++ b/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java @@ -16,20 +16,13 @@ */ package se.leap.bitmaskclient; -import butterknife.ButterKnife; -import butterknife.InjectView; +import butterknife.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.Toast; +import android.app.*; +import android.content.*; +import android.os.*; +import android.view.*; +import android.widget.*; /** * Implements the new custom provider dialog. diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index c8fd0251..7f0ce001 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -16,19 +16,19 @@ */ package se.leap.bitmaskclient; -import android.app.IntentService; +import android.app.*; import android.content.*; import android.os.*; import android.util.*; import java.io.*; -import java.math.BigInteger; +import java.math.*; import java.net.*; import java.security.*; import java.security.cert.*; -import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.*; import java.util.*; import javax.net.ssl.*; -import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.*; import org.json.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java b/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java index 97ce5245..0f978290 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java @@ -1,17 +1,16 @@ -package se.leap.bitmaskclient; - -import org.json.*; - -import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.eip.Constants; -import se.leap.bitmaskclient.ProviderListContent.ProviderItem; - -import android.app.*; -import android.content.*; -import android.os.Bundle; -import android.view.*; -import android.widget.TextView; - +package se.leap.bitmaskclient; + +import org.json.*; + +import se.leap.bitmaskclient.eip.*; +import se.leap.bitmaskclient.ProviderListContent.ProviderItem; + +import android.app.*; +import android.content.*; +import android.os.*; +import android.view.*; +import android.widget.*; + public class ProviderDetailFragment extends DialogFragment { final public static String TAG = "providerDetailFragment"; @@ -61,7 +60,7 @@ public class ProviderDetailFragment extends DialogFragment { private boolean anon_allowed(JSONObject provider_json) { try { JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - return service_description.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); + return service_description.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); } catch (JSONException e) { return false; } @@ -80,8 +79,8 @@ public class ProviderDetailFragment extends DialogFragment { public void onCancel(DialogInterface dialog) { super.onCancel(dialog); SharedPreferences.Editor editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); - editor.remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); - interface_with_configuration_wizard.showAllProviders(); + editor.remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); + interface_with_configuration_wizard.showAllProviders(); } public static DialogFragment newInstance() { diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java b/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java index a5402fd8..64e5be07 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java @@ -16,17 +16,9 @@ */ package se.leap.bitmaskclient; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.net.URL; -import java.net.MalformedURLException; - -import org.json.JSONException; -import org.json.JSONObject; +import java.io.*; +import java.util.*; +import java.net.*; /** * Models the provider list shown in the ConfigurationWizard. @@ -55,19 +47,19 @@ public class ProviderListContent { /** * A provider item. - */ + */ public static class ProviderItem { final public static String CUSTOM = "custom"; private String provider_main_url; - private String name; + private String name; /** * @param name of the provider * @param provider_main_url used to download provider.json file of the provider - */ + */ public ProviderItem(String name, String provider_main_url) { this.name = name; - this.provider_main_url = provider_main_url; + this.provider_main_url = provider_main_url; } public String name() { return name; } -- cgit v1.2.3 From b87f082fe0e9c05cf45c56b510a2c4000133b572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 19:07:43 +0100 Subject: Indent everything with IntelliJ --- .../leap/bitmaskclient/test/ConnectionManager.java | 27 +- .../se/leap/bitmaskclient/test/FromAssets.java | 5 +- .../test/testConfigurationWizard.java | 63 +- .../test/testDashboardIntegration.java | 113 +- .../bitmaskclient/test/testGatewaysManager.java | 4 +- .../bitmaskclient/test/testLeapSRPSession.java | 1094 ++++++------- .../test/testVpnCertificateValidator.java | 10 +- .../se/leap/bitmaskclient/ConfigurationWizard.java | 374 ++--- .../se/leap/bitmaskclient/NewProviderDialog.java | 74 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 1691 ++++++++++---------- .../leap/bitmaskclient/ProviderDetailFragment.java | 168 +- .../se/leap/bitmaskclient/ProviderListContent.java | 99 +- .../java/se/leap/bitmaskclient/AboutActivity.java | 38 +- .../java/se/leap/bitmaskclient/ConfigHelper.java | 291 ++-- .../main/java/se/leap/bitmaskclient/Dashboard.java | 282 ++-- .../leap/bitmaskclient/DownloadFailedDialog.java | 105 +- .../java/se/leap/bitmaskclient/EipFragment.java | 150 +- .../bitmaskclient/FragmentManagerEnhanced.java | 32 +- .../java/se/leap/bitmaskclient/LeapSRPSession.java | 613 +++---- .../java/se/leap/bitmaskclient/OnBootReceiver.java | 22 +- .../main/java/se/leap/bitmaskclient/PRNGFixes.java | 17 +- .../main/java/se/leap/bitmaskclient/Provider.java | 157 +- .../bitmaskclient/ProviderAPIResultReceiver.java | 34 +- .../se/leap/bitmaskclient/ProviderListAdapter.java | 132 +- .../se/leap/bitmaskclient/ProviderManager.java | 38 +- .../bitmaskclient/ProviderRendererBuilder.java | 16 +- .../java/se/leap/bitmaskclient/SessionDialog.java | 140 +- app/src/main/java/se/leap/bitmaskclient/User.java | 23 +- .../se/leap/bitmaskclient/UserSessionStatus.java | 47 +- .../java/se/leap/bitmaskclient/eip/Constants.java | 6 +- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 150 +- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 95 +- .../java/se/leap/bitmaskclient/eip/Gateway.java | 100 +- .../se/leap/bitmaskclient/eip/GatewaySelector.java | 46 +- .../se/leap/bitmaskclient/eip/GatewaysManager.java | 69 +- .../se/leap/bitmaskclient/eip/VoidVpnLauncher.java | 36 +- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 48 +- .../bitmaskclient/eip/VpnCertificateValidator.java | 46 +- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 10 +- .../se/leap/bitmaskclient/ConfigurationWizard.java | 431 ++--- .../se/leap/bitmaskclient/NewProviderDialog.java | 14 +- .../java/se/leap/bitmaskclient/ProviderAPI.java | 1675 +++++++++---------- .../leap/bitmaskclient/ProviderDetailFragment.java | 203 +-- .../se/leap/bitmaskclient/ProviderListContent.java | 155 +- 44 files changed, 4549 insertions(+), 4394 deletions(-) diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java index 96abc7eb..a664c885 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/ConnectionManager.java @@ -3,26 +3,27 @@ package se.leap.bitmaskclient.test; import android.content.*; import android.net.*; import android.net.wifi.*; + import java.lang.reflect.*; public class ConnectionManager { static void setMobileDataEnabled(boolean enabled, Context context) { - final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - Method[] methods = conman.getClass().getMethods(); - for (Method method : methods) { - if (method.getName().equals("setMobileDataEnabled")) { - method.setAccessible(true); - try { - method.invoke(conman, enabled); - } catch (InvocationTargetException | IllegalAccessException e) { - e.printStackTrace(); - } + final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + Method[] methods = conman.getClass().getMethods(); + for (Method method : methods) { + if (method.getName().equals("setMobileDataEnabled")) { + method.setAccessible(true); + try { + method.invoke(conman, enabled); + } catch (InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } + } } - } } static void setWifiEnabled(boolean enabled, Context context) { - WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); - wifiManager.setWifiEnabled(enabled); + WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + wifiManager.setWifiEnabled(enabled); } } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java index fd8b9a7e..6a4c1ee2 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/FromAssets.java @@ -1,7 +1,9 @@ package se.leap.bitmaskclient.test; import android.content.*; + import org.json.*; + import java.io.*; public class FromAssets { @@ -11,11 +13,12 @@ public class FromAssets { public FromAssets(Context context) { this.context = context; } + public String toString(String filename) throws IOException, JSONException { String result = ""; InputStream is = context.getAssets().open(filename); byte[] bytes = new byte[is.available()]; - if(is.read(bytes) > 0) { + if (is.read(bytes) > 0) { result = new String(bytes); } return result; diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java index 755f83a7..7a19409b 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java @@ -2,41 +2,44 @@ package se.leap.bitmaskclient.test; import android.test.*; import android.widget.*; + import com.robotium.solo.*; + import java.io.*; + import se.leap.bitmaskclient.*; public class testConfigurationWizard extends ActivityInstrumentationTestCase2 { - private Solo solo; - private static int added_providers; - - public testConfigurationWizard() { - super(ConfigurationWizard.class); - } + private Solo solo; + private static int added_providers; + + public testConfigurationWizard() { + super(ConfigurationWizard.class); + } public testConfigurationWizard(Solo solo) { super(ConfigurationWizard.class); this.solo = solo; } - @Override - protected void setUp() throws Exception { - super.setUp(); + @Override + protected void setUp() throws Exception { + super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); - ConnectionManager.setMobileDataEnabled(true, solo.getCurrentActivity().getApplicationContext()); - } + ConnectionManager.setMobileDataEnabled(true, solo.getCurrentActivity().getApplicationContext()); + } - @Override - protected void tearDown() throws Exception { - solo.finishOpenedActivities(); - } - - public void testListProviders() { - assertEquals(solo.getCurrentViews(ListView.class).size(), 1); + @Override + protected void tearDown() throws Exception { + solo.finishOpenedActivities(); + } - assertEquals("Number of available providers differ", predefinedProviders() + added_providers, shownProviders()); - } + public void testListProviders() { + assertEquals(solo.getCurrentViews(ListView.class).size(), 1); + + assertEquals("Number of available providers differ", predefinedProviders() + added_providers, shownProviders()); + } private int shownProviders() { return solo.getCurrentViews(ListView.class).get(0).getCount(); @@ -53,10 +56,10 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2 { - private Solo solo; + private Solo solo; private Context context; - public testDashboardIntegration() { - super(Dashboard.class); - } + public testDashboardIntegration() { + super(Dashboard.class); + } - @Override - protected void setUp() throws Exception { - super.setUp(); + @Override + protected void setUp() throws Exception { + super.setUp(); context = getInstrumentation().getContext(); - solo = new Solo(getInstrumentation(), getActivity()); - ConnectionManager.setMobileDataEnabled(true, context); + solo = new Solo(getInstrumentation(), getActivity()); + ConnectionManager.setMobileDataEnabled(true, context); solo.unlockScreen(); - if(solo.searchText(solo.getString(R.string.configuration_wizard_title))) + if (solo.searchText(solo.getString(R.string.configuration_wizard_title))) new testConfigurationWizard(solo).toDashboard("demo.bitmask.net"); - } - - @Override - protected void tearDown() throws Exception { - solo.finishOpenedActivities(); - } - - /** - * This test will fail if Android does not trust VPN connection. - * I cannot automate that dialog. - */ - public void testOnOffOpenVpn() { - solo.clickOnView(solo.getView(R.id.eipSwitch)); - turningEipOn(); - - solo.clickOnView(solo.getView(R.id.eipSwitch)); - turningEipOff(); - - solo.clickOnView(solo.getView(R.id.eipSwitch)); - turningEipOn(); - - solo.clickOnView(solo.getView(R.id.eipSwitch)); - turningEipOff(); + } + + @Override + protected void tearDown() throws Exception { + solo.finishOpenedActivities(); + } + + /** + * This test will fail if Android does not trust VPN connection. + * I cannot automate that dialog. + */ + public void testOnOffOpenVpn() { + solo.clickOnView(solo.getView(R.id.eipSwitch)); + turningEipOn(); + + solo.clickOnView(solo.getView(R.id.eipSwitch)); + turningEipOff(); + + solo.clickOnView(solo.getView(R.id.eipSwitch)); + turningEipOn(); + + solo.clickOnView(solo.getView(R.id.eipSwitch)); + turningEipOff(); /*solo.clickOnView(solo.getView(R.id.eipSwitch)); turningEipOn(); @@ -57,14 +58,14 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2Yd*s T`-n."; - salt = "40c3f47b99ce8dc9"; - expected_A = trim(new BigInteger("490b5de7a287c59cefe267441a186ec24f63210fbf28877305f5896eaec5a7245d304ecb2b09d91066e627d7b2c8bf9e5271d882361a435355d1c2d1ac9d3069877189a01d64b2dd73a569e9e96b9a99767dbc02e04c839b09444f48430b113c1827c20b684ae33f5018051169f5acf4913ebd76a205c6f1aa2cc75747687d56", 16).toByteArray()); - String x_string = "9665839759b4fb9684e7438daecbd6e7129b4ebd3e4a107916e9a64bbbf399c9"; - expected_x = trim(new BigInteger(x_string, 16).toByteArray()); - assertEquals(new BigInteger(1, expected_x).toString(16), x_string); - a = "a72111a2"; - B = "6574ddce3e33c44a77198fa8b3656627e4a24c8786948e79f0c2588febaa485c94b1deb5e420bd3b46f9a34c7862525452ca7a0542c52f939d9f277a013aeceef7353a7741440f6dd2f6c2f1dc07fa5ca003e305c89c876a3035bd04f546b711d44da06a3ba827bc8affbf9ed46de1bfbc670ef9ed7c0bb8cdc588285d13849e"; - expected_M1 = trim(new BigInteger("03bbcf57aeaec89a3a254bb9650a924ea86aa0fdd83fd7274a75b7083f221cf0", 16).toByteArray()); - expected_M2 = trim(new BigInteger("082cf49ad5a34cc5ca571e3d063aec4bd96e7b96a6d951295180631650a84587", 16).toByteArray()); - - a_byte = new BigInteger(a, 16).toByteArray(); - client = new LeapSRPSession(username, password, a_byte); - - x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); - assertTrue(Arrays.equals(x, expected_x)); - assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); - - A = client.exponential(); - assertTrue(Arrays.equals(A, expected_A)); - - M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); - - assertTrue(Arrays.equals(M1, expected_M1)); - - verified = client.verify(expected_M2); - assertTrue(verified); + username = "parmegv"; + password = "wR\"P}x@_,:k$`YYd*s T`-n."; + salt = "40c3f47b99ce8dc9"; + expected_A = trim(new BigInteger("490b5de7a287c59cefe267441a186ec24f63210fbf28877305f5896eaec5a7245d304ecb2b09d91066e627d7b2c8bf9e5271d882361a435355d1c2d1ac9d3069877189a01d64b2dd73a569e9e96b9a99767dbc02e04c839b09444f48430b113c1827c20b684ae33f5018051169f5acf4913ebd76a205c6f1aa2cc75747687d56", 16).toByteArray()); + String x_string = "9665839759b4fb9684e7438daecbd6e7129b4ebd3e4a107916e9a64bbbf399c9"; + expected_x = trim(new BigInteger(x_string, 16).toByteArray()); + assertEquals(new BigInteger(1, expected_x).toString(16), x_string); + a = "a72111a2"; + B = "6574ddce3e33c44a77198fa8b3656627e4a24c8786948e79f0c2588febaa485c94b1deb5e420bd3b46f9a34c7862525452ca7a0542c52f939d9f277a013aeceef7353a7741440f6dd2f6c2f1dc07fa5ca003e305c89c876a3035bd04f546b711d44da06a3ba827bc8affbf9ed46de1bfbc670ef9ed7c0bb8cdc588285d13849e"; + expected_M1 = trim(new BigInteger("03bbcf57aeaec89a3a254bb9650a924ea86aa0fdd83fd7274a75b7083f221cf0", 16).toByteArray()); + expected_M2 = trim(new BigInteger("082cf49ad5a34cc5ca571e3d063aec4bd96e7b96a6d951295180631650a84587", 16).toByteArray()); + + a_byte = new BigInteger(a, 16).toByteArray(); + client = new LeapSRPSession(username, password, a_byte); + + x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); + assertTrue(Arrays.equals(x, expected_x)); + assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); + + A = client.exponential(); + assertTrue(Arrays.equals(A, expected_A)); + + M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); + + assertTrue(Arrays.equals(M1, expected_M1)); + + verified = client.verify(expected_M2); + assertTrue(verified); /* Test 7: password with ! */ - username = "parmegvtest3"; - password = "holahola!"; - salt = "1bf48d42b9a7ed32"; - expected_A = trim(new BigInteger("4e2fbe8db5d07f33ff1f4303959b5396dcffc1460b6ce8866fd388415f27fe10f9042986ab8682cdcf9a033e651bca89173688989adad854c91bc1918f98d5c82525fb6f328a8cf74ce1436b23821cba5337aaa20a3e5631e4b957053d542f2b5fc456e888371c9d6b94360b37adb2793eca8db100c24887c459e36d729a98e1", 16).toByteArray()); - x_string = "363d1d62dda07b2d987a9739ddb5ec32fcad9c7322fb64e87937f2da86c45d9f"; - expected_x = trim(new BigInteger(x_string, 16).toByteArray()); - assertEquals(new BigInteger(1, expected_x).toString(16), x_string); - a = "16dd0cf5"; - B = "dd5c9c5e13eb5daa6e7303928b3c826cec520ccef429c0dcb785be34c330d5bb89c99d7d94842b6b5c19cac600f884c50b26989b105f397115df7f3d13c5c7c2f6327cc547fc854ae40f09f1f6a104968bd510243feb104eb559e085fe1d720770be2887a1f424c534a3ab962d82e92458f652328bcf9878f95fdcf463d06193"; - expected_M1 = trim(new BigInteger("a7ffbff753a547b877f8944339b707b3ce1998da27badf253d56bf39f35308a6", 16).toByteArray()); - expected_M2 = trim(new BigInteger("5cc3d7f0077e978c83acdef14a725af01488c1728f0cf32cd7013d24faf5d901", 16).toByteArray()); - - a_byte = new BigInteger(a, 16).toByteArray(); - client = new LeapSRPSession(username, password, a_byte); - - x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); - assertTrue(Arrays.equals(x, expected_x)); - assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); - - A = client.exponential(); - assertTrue(Arrays.equals(A, expected_A)); - - M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); - - assertTrue(Arrays.equals(M1, expected_M1)); - - verified = client.verify(expected_M2); - assertTrue(verified); + username = "parmegvtest3"; + password = "holahola!"; + salt = "1bf48d42b9a7ed32"; + expected_A = trim(new BigInteger("4e2fbe8db5d07f33ff1f4303959b5396dcffc1460b6ce8866fd388415f27fe10f9042986ab8682cdcf9a033e651bca89173688989adad854c91bc1918f98d5c82525fb6f328a8cf74ce1436b23821cba5337aaa20a3e5631e4b957053d542f2b5fc456e888371c9d6b94360b37adb2793eca8db100c24887c459e36d729a98e1", 16).toByteArray()); + x_string = "363d1d62dda07b2d987a9739ddb5ec32fcad9c7322fb64e87937f2da86c45d9f"; + expected_x = trim(new BigInteger(x_string, 16).toByteArray()); + assertEquals(new BigInteger(1, expected_x).toString(16), x_string); + a = "16dd0cf5"; + B = "dd5c9c5e13eb5daa6e7303928b3c826cec520ccef429c0dcb785be34c330d5bb89c99d7d94842b6b5c19cac600f884c50b26989b105f397115df7f3d13c5c7c2f6327cc547fc854ae40f09f1f6a104968bd510243feb104eb559e085fe1d720770be2887a1f424c534a3ab962d82e92458f652328bcf9878f95fdcf463d06193"; + expected_M1 = trim(new BigInteger("a7ffbff753a547b877f8944339b707b3ce1998da27badf253d56bf39f35308a6", 16).toByteArray()); + expected_M2 = trim(new BigInteger("5cc3d7f0077e978c83acdef14a725af01488c1728f0cf32cd7013d24faf5d901", 16).toByteArray()); + + a_byte = new BigInteger(a, 16).toByteArray(); + client = new LeapSRPSession(username, password, a_byte); + + x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); + assertTrue(Arrays.equals(x, expected_x)); + assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); + + A = client.exponential(); + assertTrue(Arrays.equals(A, expected_A)); + + M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); + + assertTrue(Arrays.equals(M1, expected_M1)); + + verified = client.verify(expected_M2); + assertTrue(verified); /* Test 8: username and password *was* failing in localhost testProviderAPI*/ - username = "gg"; - password = "password"; - a = "bc925bfb"; - salt = "ff9ebb44e947cf59"; - expected_A = trim(new BigInteger("8f434633414faeaf035a0dea8c1cb7876bb1f8ee80d6fee8ea43ae60c4f9658550d825c25f1ed5c6a5543358bbcb559b76958c8047a2e7e5fe0072bc1f16401bcfa77b57651ff50dd665c6f28c302b37c98495eff397a56befead2e5ceffaace45f2ec200520258adb66df751e815e464656d869454e360d98cbc70f9c64fd4c", 16).toByteArray()); - x_string = "9cad2eca264380dd0b48e3b405e109c1be0615ee6ec92e7105eff5bc3a309fd9"; - expected_x = trim(new BigInteger(x_string, 16).toByteArray()); - assertEquals(new BigInteger(1, expected_x).toString(16), x_string); - B = "9ca2cd50b4c41047e4aa9e4fac9078ae21175e51e04a23877d6c2044765e39959e9a6a3ede99d08a556c196f51a2be12117681b1ef9d0b0498fb2fa4e88649ab9403e743504e3aaefbce8c5cb474eef8f4724ccd076fd33857de510d8509b67f166d986443bc262d776ec20985f617a7aa86e490290ce5d66332c8b45742a527"; - expected_M1 = trim(new BigInteger("7a2f768791abaeb954eb7f001bb60d91e6f61e959c8fcdefb58de857af9edaac", 16).toByteArray()); - expected_M2 = trim(new BigInteger("d78da7e0a23c9b87a2f09cdee05c510c105b4a8d471b47402c38f4cdfa49fe6d", 16).toByteArray()); - - a_byte = new BigInteger(a, 16).toByteArray(); - client = new LeapSRPSession(username, password, a_byte); - - x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); - assertTrue(Arrays.equals(x, expected_x)); - assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); - - A = client.exponential(); - assertTrue(Arrays.equals(A, expected_A)); - - M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); - - assertTrue(Arrays.equals(M1, expected_M1)); - - verified = client.verify(expected_M2); - assertTrue(verified); + username = "gg"; + password = "password"; + a = "bc925bfb"; + salt = "ff9ebb44e947cf59"; + expected_A = trim(new BigInteger("8f434633414faeaf035a0dea8c1cb7876bb1f8ee80d6fee8ea43ae60c4f9658550d825c25f1ed5c6a5543358bbcb559b76958c8047a2e7e5fe0072bc1f16401bcfa77b57651ff50dd665c6f28c302b37c98495eff397a56befead2e5ceffaace45f2ec200520258adb66df751e815e464656d869454e360d98cbc70f9c64fd4c", 16).toByteArray()); + x_string = "9cad2eca264380dd0b48e3b405e109c1be0615ee6ec92e7105eff5bc3a309fd9"; + expected_x = trim(new BigInteger(x_string, 16).toByteArray()); + assertEquals(new BigInteger(1, expected_x).toString(16), x_string); + B = "9ca2cd50b4c41047e4aa9e4fac9078ae21175e51e04a23877d6c2044765e39959e9a6a3ede99d08a556c196f51a2be12117681b1ef9d0b0498fb2fa4e88649ab9403e743504e3aaefbce8c5cb474eef8f4724ccd076fd33857de510d8509b67f166d986443bc262d776ec20985f617a7aa86e490290ce5d66332c8b45742a527"; + expected_M1 = trim(new BigInteger("7a2f768791abaeb954eb7f001bb60d91e6f61e959c8fcdefb58de857af9edaac", 16).toByteArray()); + expected_M2 = trim(new BigInteger("d78da7e0a23c9b87a2f09cdee05c510c105b4a8d471b47402c38f4cdfa49fe6d", 16).toByteArray()); + + a_byte = new BigInteger(a, 16).toByteArray(); + client = new LeapSRPSession(username, password, a_byte); + + x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); + assertTrue(Arrays.equals(x, expected_x)); + assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); + + A = client.exponential(); + assertTrue(Arrays.equals(A, expected_A)); + + M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); + + assertTrue(Arrays.equals(M1, expected_M1)); + + verified = client.verify(expected_M2); + assertTrue(verified); /* Test 9: username and password *was* failing in localhost testProviderAPI*/ - username = "oo"; - password = "password"; - a = "1322ec50"; - salt = "a93c74934dcadd90"; - expected_A = trim(new BigInteger("c8e9f30a2f67977ee7e61e9ca5af8bd854b6cc98fe01dbe1b1a4cf002c1e2523b7e49f373a600ff85a84867817ec60fec532857812a07f5c6189f6172f133023af75ca4cb98b758bb84620d0aa3cfc74dc69e0507114c0aeab5a75c3ae3f07a919c5729420f03266c26ed41d1846e07de023ec68dd6830e9ebf129cf51abb571", 16).toByteArray()); - x_string = "20470538560c4beb4908e6bfe5b0e00da94223e361302a25c898cbdd3724020"; - expected_x = trim(new BigInteger(x_string, 16).toByteArray()); - assertEquals(new BigInteger(1, expected_x).toString(16), x_string); - B = "24f98ede155212bea8b1d8bacf8153735ee8114faa824c57c84df55f8d6072ab87f5ae885ce1062939dbaa68ca6e63147c1d2dc1f751e8be20d8a6f87287a2a83fcb1dc9b85dd406d438aeee5ccbc873603cb399627e26e6444e94b3d5d26764e866776c8960fe206bd33febeca9f55f6291dd2cb832eab69e5373f548adeefb"; - expected_M1 = trim(new BigInteger("1b35c705e563bd5239cdccc6627aa877c3023286f49b4b7c21341d2949ca2d15", 16).toByteArray()); - expected_M2 = trim(new BigInteger("a382025452bad8a6ccd0f703253fda90e7ea7bd0c2d466a389455080a4bd015d", 16).toByteArray()); - - a_byte = new BigInteger(a, 16).toByteArray(); - client = new LeapSRPSession(username, password, a_byte); - - x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); - assertTrue(Arrays.equals(x, expected_x)); - assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); - - A = client.exponential(); - assertTrue(Arrays.equals(A, expected_A)); - - M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); - - assertTrue(Arrays.equals(M1, expected_M1)); - - verified = client.verify(expected_M2); - assertTrue(verified); + username = "oo"; + password = "password"; + a = "1322ec50"; + salt = "a93c74934dcadd90"; + expected_A = trim(new BigInteger("c8e9f30a2f67977ee7e61e9ca5af8bd854b6cc98fe01dbe1b1a4cf002c1e2523b7e49f373a600ff85a84867817ec60fec532857812a07f5c6189f6172f133023af75ca4cb98b758bb84620d0aa3cfc74dc69e0507114c0aeab5a75c3ae3f07a919c5729420f03266c26ed41d1846e07de023ec68dd6830e9ebf129cf51abb571", 16).toByteArray()); + x_string = "20470538560c4beb4908e6bfe5b0e00da94223e361302a25c898cbdd3724020"; + expected_x = trim(new BigInteger(x_string, 16).toByteArray()); + assertEquals(new BigInteger(1, expected_x).toString(16), x_string); + B = "24f98ede155212bea8b1d8bacf8153735ee8114faa824c57c84df55f8d6072ab87f5ae885ce1062939dbaa68ca6e63147c1d2dc1f751e8be20d8a6f87287a2a83fcb1dc9b85dd406d438aeee5ccbc873603cb399627e26e6444e94b3d5d26764e866776c8960fe206bd33febeca9f55f6291dd2cb832eab69e5373f548adeefb"; + expected_M1 = trim(new BigInteger("1b35c705e563bd5239cdccc6627aa877c3023286f49b4b7c21341d2949ca2d15", 16).toByteArray()); + expected_M2 = trim(new BigInteger("a382025452bad8a6ccd0f703253fda90e7ea7bd0c2d466a389455080a4bd015d", 16).toByteArray()); + + a_byte = new BigInteger(a, 16).toByteArray(); + client = new LeapSRPSession(username, password, a_byte); + + x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); + assertTrue(Arrays.equals(x, expected_x)); + assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); + + A = client.exponential(); + assertTrue(Arrays.equals(A, expected_A)); + + M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); + + assertTrue(Arrays.equals(M1, expected_M1)); + + verified = client.verify(expected_M2); + assertTrue(verified); /* Test 10: derived from test 11, discovered that password bytes should be in ISO-8859-1*/ - username = "nostradamus"; - password = "$[[//jjiilajfewahug43a89y¿"; - a = "800f0819"; - salt = "cfb9ae3ec5433076"; - expected_A = trim(new BigInteger("2ab09ee2fa01058f2f72fd2142b129f2ec26313801052889bcc4af57ee2e4d5b92c90cdfd6ecd660e82c635b2a091ba1b164e5b371c911ce0c4e69686baa120c58e2e0af84b2adc10da6cdfb0b579a1685032c57fd6ed1306d9713a562eddf5c833725042e825fa1abc7017f74760cb53d8c755ffe628c510022c296d1cd3584", 16).toByteArray()); - x_string = "9736a5e386a18f35bb08cac0f7c70bdbe120f2efe019874d0eb23b85b1955858"; - expected_x = trim(new BigInteger(x_string, 16).toByteArray()); - assertEquals(new BigInteger(1, expected_x).toString(16), x_string); - B = "2d19fe17dca1bda01044a0f406547895c32a10df2b0e69676de911273a8685d294763c4d16b3663f722b8980126e2c659efd33ffc6435a9594a2539e726c48e365893b3374670bd1958c13f55c2defa8ea9c0f9ba1345a5dca0e78debba434c8b755353d066d42bc5dfe0403fdcacfe5efd25c685f883ee6766c710b775c50f2"; - expected_M1 = trim(new BigInteger("a33feada1771c6f53e9343f5b9e69d51d4f15043c95fb663b6dd5b1c7af6f66b", 16).toByteArray()); - expected_M2 = trim(new BigInteger("9e99f9adfbfaa7add3626ed6e6aea94c9fa60dab6b8d56ad0cc950548f577d32", 16).toByteArray()); - - a_byte = new BigInteger(a, 16).toByteArray(); - client = new LeapSRPSession(username, password, a_byte); - - x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); - assertTrue(Arrays.equals(x, expected_x)); - assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); - - A = client.exponential(); - assertTrue(Arrays.equals(A, expected_A)); - - M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); - - assertTrue(Arrays.equals(M1, expected_M1)); - - verified = client.verify(expected_M2); - assertTrue(verified); + username = "nostradamus"; + password = "$[[//jjiilajfewahug43a89y¿"; + a = "800f0819"; + salt = "cfb9ae3ec5433076"; + expected_A = trim(new BigInteger("2ab09ee2fa01058f2f72fd2142b129f2ec26313801052889bcc4af57ee2e4d5b92c90cdfd6ecd660e82c635b2a091ba1b164e5b371c911ce0c4e69686baa120c58e2e0af84b2adc10da6cdfb0b579a1685032c57fd6ed1306d9713a562eddf5c833725042e825fa1abc7017f74760cb53d8c755ffe628c510022c296d1cd3584", 16).toByteArray()); + x_string = "9736a5e386a18f35bb08cac0f7c70bdbe120f2efe019874d0eb23b85b1955858"; + expected_x = trim(new BigInteger(x_string, 16).toByteArray()); + assertEquals(new BigInteger(1, expected_x).toString(16), x_string); + B = "2d19fe17dca1bda01044a0f406547895c32a10df2b0e69676de911273a8685d294763c4d16b3663f722b8980126e2c659efd33ffc6435a9594a2539e726c48e365893b3374670bd1958c13f55c2defa8ea9c0f9ba1345a5dca0e78debba434c8b755353d066d42bc5dfe0403fdcacfe5efd25c685f883ee6766c710b775c50f2"; + expected_M1 = trim(new BigInteger("a33feada1771c6f53e9343f5b9e69d51d4f15043c95fb663b6dd5b1c7af6f66b", 16).toByteArray()); + expected_M2 = trim(new BigInteger("9e99f9adfbfaa7add3626ed6e6aea94c9fa60dab6b8d56ad0cc950548f577d32", 16).toByteArray()); + + a_byte = new BigInteger(a, 16).toByteArray(); + client = new LeapSRPSession(username, password, a_byte); + + x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); + assertTrue(Arrays.equals(x, expected_x)); + assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); + + A = client.exponential(); + assertTrue(Arrays.equals(A, expected_A)); + + M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); + + assertTrue(Arrays.equals(M1, expected_M1)); + + verified = client.verify(expected_M2); + assertTrue(verified); /* Test 11: username and password failing in localhost testProviderAPI*/ - username = "nostradamus"; - password = "$[['//\"jjiilajfewahug43a89y¿"; - a = "5bfbc968"; - salt = "1bcae1065951bbf5"; - expected_A = trim(new BigInteger("7a74c254d46dd6010a7090e574817a03f32ba13f98ed3c695d96f09c9d334e591771541400e68b6d27a19e734baccf3965ca79c0294ffbf553716b41fbca627c7cd3ea4a0d1c640c22411881696f59ad7ed8ce6ef7010e43f57fb3858aa4c3479dd41e4073afadb6a516c41f649b8cf30dea6366efa711c5106c83ea71b00da4", 16).toByteArray()); - x_string = "9834210874c883db35785ee6648079e13d22450c472d6469192ea775ff50c646"; - expected_x = trim(new BigInteger(x_string, 16).toByteArray()); - assertEquals(new BigInteger(1, expected_x).toString(16), x_string); - B = "285b00c034da5676dd8938ce6a7b717968fef2e5f479ecca6d95828a6ce809dd37893752c956245b5d13315987c50e57cc68aa4f770ff9ce977ddfd65052f278b90545286cf32b3d18307140514e0fe2269fc0437fb16104358f6fa127dc97281a017582759644862d736f48025f2b35cb1662067c11f2fcf0753e2f72c9e028"; - expected_M1 = trim(new BigInteger("fedbaff9d9a19efc4eea949b045297a6a3121cf371e2acdda85a2a1ca61c929d", 16).toByteArray()); - expected_M2 = trim(new BigInteger("ffccafa0febc1771a428082b30b7ce409856de4581c7d7d986f5b80015aba0d3", 16).toByteArray()); - - a_byte = new BigInteger(a, 16).toByteArray(); - client = new LeapSRPSession(username, password, a_byte); - - x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); - assertTrue(Arrays.equals(x, expected_x)); - assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); - - A = client.exponential(); - assertTrue(Arrays.equals(A, expected_A)); - - M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); - - assertTrue(Arrays.equals(M1, expected_M1)); - - verified = client.verify(expected_M2); - assertTrue(verified); - } - - public void testSignUpMath() throws NoSuchAlgorithmException{ - String username = "parmegvtest29"; - String password = "holahola2"; - byte[] salt = new BigInteger("67e8348d1500d26c", 16).toByteArray(); - - LeapSRPSession client = new LeapSRPSession(username, password); - - String expected_v = "12bea84e588ffa2f8fc5ae47cb5e751a8f2d9e8125268ad9ab483eff83f98cb08484350eb478bee582b8b72363ff8e7b12e9f332e86f7a0bd77689927c609d275471c6ad2cff8b1e7bbfc3664169c3b7bccb0b974154c1f1656b64274568015ca1b849c9d9890ae4437ed686341b432340809b81c30727ed2aadea8bdec6d101"; - - assertEquals(expected_v, client.calculateV(username, password, salt).toString(16)); + username = "nostradamus"; + password = "$[['//\"jjiilajfewahug43a89y¿"; + a = "5bfbc968"; + salt = "1bcae1065951bbf5"; + expected_A = trim(new BigInteger("7a74c254d46dd6010a7090e574817a03f32ba13f98ed3c695d96f09c9d334e591771541400e68b6d27a19e734baccf3965ca79c0294ffbf553716b41fbca627c7cd3ea4a0d1c640c22411881696f59ad7ed8ce6ef7010e43f57fb3858aa4c3479dd41e4073afadb6a516c41f649b8cf30dea6366efa711c5106c83ea71b00da4", 16).toByteArray()); + x_string = "9834210874c883db35785ee6648079e13d22450c472d6469192ea775ff50c646"; + expected_x = trim(new BigInteger(x_string, 16).toByteArray()); + assertEquals(new BigInteger(1, expected_x).toString(16), x_string); + B = "285b00c034da5676dd8938ce6a7b717968fef2e5f479ecca6d95828a6ce809dd37893752c956245b5d13315987c50e57cc68aa4f770ff9ce977ddfd65052f278b90545286cf32b3d18307140514e0fe2269fc0437fb16104358f6fa127dc97281a017582759644862d736f48025f2b35cb1662067c11f2fcf0753e2f72c9e028"; + expected_M1 = trim(new BigInteger("fedbaff9d9a19efc4eea949b045297a6a3121cf371e2acdda85a2a1ca61c929d", 16).toByteArray()); + expected_M2 = trim(new BigInteger("ffccafa0febc1771a428082b30b7ce409856de4581c7d7d986f5b80015aba0d3", 16).toByteArray()); + + a_byte = new BigInteger(a, 16).toByteArray(); + client = new LeapSRPSession(username, password, a_byte); + + x = client.calculatePasswordHash(username, password, trim(new BigInteger(salt, 16).toByteArray())); + assertTrue(Arrays.equals(x, expected_x)); + assertEquals(new BigInteger(1, expected_x).toString(16), new BigInteger(1, x).toString(16)); + + A = client.exponential(); + assertTrue(Arrays.equals(A, expected_A)); + + M1 = client.response(new BigInteger(salt, 16).toByteArray(), new BigInteger(B, 16).toByteArray()); + + assertTrue(Arrays.equals(M1, expected_M1)); + + verified = client.verify(expected_M2); + assertTrue(verified); + } + + public void testSignUpMath() throws NoSuchAlgorithmException { + String username = "parmegvtest29"; + String password = "holahola2"; + byte[] salt = new BigInteger("67e8348d1500d26c", 16).toByteArray(); + + LeapSRPSession client = new LeapSRPSession(username, password); + + String expected_v = "12bea84e588ffa2f8fc5ae47cb5e751a8f2d9e8125268ad9ab483eff83f98cb08484350eb478bee582b8b72363ff8e7b12e9f332e86f7a0bd77689927c609d275471c6ad2cff8b1e7bbfc3664169c3b7bccb0b974154c1f1656b64274568015ca1b849c9d9890ae4437ed686341b432340809b81c30727ed2aadea8bdec6d101"; + + assertEquals(expected_v, client.calculateV(username, password, salt).toString(16)); + } + + public byte[] trim(byte[] in) { + if (in.length == 0 || in[0] != 0) + return in; + + int len = in.length; + int i = 1; + while (in[i] == 0 && i < len) + ++i; + byte[] ret = new byte[len - i]; + System.arraycopy(in, i, ret, 0, len - i); + return ret; } - - public byte[] trim(byte[] in) { - if(in.length == 0 || in[0] != 0) - return in; - - int len = in.length; - int i = 1; - while(in[i] == 0 && i < len) - ++i; - byte[] ret = new byte[len - i]; - System.arraycopy(in, i, ret, 0, len - i); - return ret; - } } diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java index cd1d6c3b..672607be 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testVpnCertificateValidator.java @@ -56,11 +56,11 @@ public class testVpnCertificateValidator extends InstrumentationTestCase { } private void setTime(int year, int month, int day) { - shellCommand("adb shell chmod 666 /dev/alarm"); - Calendar c = Calendar.getInstance(); - c.set(year, month, day, 12, 00, 00); - SystemClock.setCurrentTimeMillis(c.getTimeInMillis()); - shellCommand("adb shell chmod 664 /dev/alarm"); + shellCommand("adb shell chmod 666 /dev/alarm"); + Calendar c = Calendar.getInstance(); + c.set(year, month, day, 12, 00, 00); + SystemClock.setCurrentTimeMillis(c.getTimeInMillis()); + shellCommand("adb shell chmod 664 /dev/alarm"); } private int shellCommand(String command) { diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java index 63e5272e..59d77d83 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributors - * + * * 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 @@ -28,6 +28,7 @@ import java.net.*; import java.util.*; import butterknife.*; + import org.jetbrains.annotations.*; import org.json.*; @@ -42,25 +43,28 @@ import se.leap.bitmaskclient.eip.*; /** * Activity that builds and shows the list of known available providers. - * + *

* It also allows the user to enter custom providers with a button. - * - * @author parmegv * + * @author parmegv */ public class ConfigurationWizard extends Activity -implements NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { + implements NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { - @InjectView(R.id.progressbar_configuration_wizard) ProgressBar mProgressBar; - @InjectView(R.id.progressbar_description) TextView progressbar_description; + @InjectView(R.id.progressbar_configuration_wizard) + ProgressBar mProgressBar; + @InjectView(R.id.progressbar_description) + TextView progressbar_description; - @InjectView(R.id.provider_list) ListView provider_list_view; - @Inject ProviderListAdapter adapter; + @InjectView(R.id.provider_list) + ListView provider_list_view; + @Inject + ProviderListAdapter adapter; private ProviderManager provider_manager; private Intent mConfigState = new Intent(); private Provider selected_provider; - + final public static String TAG = ConfigurationWizard.class.getSimpleName(); final protected static String PROVIDER_SET = "PROVIDER SET"; @@ -68,7 +72,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download final private static String PROGRESSBAR_TEXT = TAG + "PROGRESSBAR_TEXT"; final private static String PROGRESSBAR_NUMBER = TAG + "PROGRESSBAR_NUMBER"; - + public ProviderAPIResultReceiver providerAPI_result_receiver; private ProviderAPIBroadcastReceiver_Update providerAPI_broadcast_receiver_update; @@ -86,14 +90,14 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download adapter = new ProviderListAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); provider_list_view.setAdapter(adapter); } - + @Override protected void onSaveInstanceState(@NotNull Bundle outState) { - if(mProgressBar != null) + if (mProgressBar != null) outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); - if(progressbar_description != null) + if (progressbar_description != null) outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); - if(selected_provider != null) + if (selected_provider != null) outState.putParcelable(Provider.KEY, selected_provider); super.onSaveInstanceState(outState); } @@ -107,11 +111,11 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download setUpInitialUI(); - initProviderList(); + initProviderList(); - if(savedInstanceState != null) + if (savedInstanceState != null) restoreState(savedInstanceState); - setUpProviderAPIResultReceiver(); + setUpProviderAPIResultReceiver(); } private void restoreState(Bundle savedInstanceState) { @@ -120,7 +124,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download selected_provider = savedInstanceState.getParcelable(Provider.KEY); progress = savedInstanceState.getInt(PROGRESSBAR_NUMBER, -1); - if(fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { + if (fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { if (selected_provider != null) onItemSelectedUi(selected_provider); if (progress > 0) @@ -132,7 +136,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @Override protected void onPostResume() { super.onPostResume(); - if(!progressbar_text.isEmpty() && !provider_name.isEmpty() && progress != -1) { + if (!progressbar_text.isEmpty() && !provider_name.isEmpty() && progress != -1) { progressbar_description.setText(progressbar_text); mProgressBar.setProgress(progress); @@ -148,82 +152,81 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download hideProgressBar(); } - - private void hideProgressBar() { - mProgressBar.setVisibility(ProgressBar.INVISIBLE); - progressbar_description.setVisibility(TextView.INVISIBLE); + + private void hideProgressBar() { + mProgressBar.setVisibility(ProgressBar.INVISIBLE); + progressbar_description.setVisibility(TextView.INVISIBLE); } @Override protected void onDestroy() { - super.onDestroy(); - if(providerAPI_broadcast_receiver_update != null) - unregisterReceiver(providerAPI_broadcast_receiver_update); + super.onDestroy(); + if (providerAPI_broadcast_receiver_update != null) + unregisterReceiver(providerAPI_broadcast_receiver_update); } private void setUpProviderAPIResultReceiver() { providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); - providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); - - IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_PROGRESSBAR); - update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); + providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); + + IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_PROGRESSBAR); + update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); + registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); } @Override public void onReceiveResult(int resultCode, Bundle resultData) { - if(resultCode == ProviderAPI.PROVIDER_OK) { - mConfigState.setAction(PROVIDER_SET); + if (resultCode == ProviderAPI.PROVIDER_OK) { + mConfigState.setAction(PROVIDER_SET); try { String provider_json_string = preferences.getString(Provider.KEY, ""); - if(!provider_json_string.isEmpty()) - selected_provider.define(new JSONObject(provider_json_string)); + if (!provider_json_string.isEmpty()) + selected_provider.define(new JSONObject(provider_json_string)); } catch (JSONException e) { e.printStackTrace(); } - if (preferences.getBoolean(Constants.ALLOWED_ANON, false)){ - mConfigState.putExtra(SERVICES_RETRIEVED, true); - - downloadVpnCertificate(); - } else { - mProgressBar.incrementProgressBy(1); - hideProgressBar(); - - setResult(RESULT_OK); - - showProviderDetails(); - } - } else if(resultCode == ProviderAPI.PROVIDER_NOK) { - hideProgressBar(); - preferences.edit().remove(Provider.KEY).apply(); - setting_up_provider = false; - - setResult(RESULT_CANCELED, mConfigState); - - String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); - showDownloadFailedDialog(reason_to_fail); - } - else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { - mProgressBar.incrementProgressBy(1); - hideProgressBar(); - - showProviderDetails(); - - setResult(RESULT_OK); - } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { - hideProgressBar(); - cancelSettingUpProvider(); - Toast.makeText(getApplicationContext(), R.string.provider_problem, Toast.LENGTH_LONG).show(); - setResult(RESULT_CANCELED, mConfigState); - } else if(resultCode == AboutActivity.VIEWED) { - // Do nothing, right now - // I need this for CW to wait for the About activity to end before going back to Dashboard. - } - } - + if (preferences.getBoolean(Constants.ALLOWED_ANON, false)) { + mConfigState.putExtra(SERVICES_RETRIEVED, true); + + downloadVpnCertificate(); + } else { + mProgressBar.incrementProgressBy(1); + hideProgressBar(); + + setResult(RESULT_OK); + + showProviderDetails(); + } + } else if (resultCode == ProviderAPI.PROVIDER_NOK) { + hideProgressBar(); + preferences.edit().remove(Provider.KEY).apply(); + setting_up_provider = false; + + setResult(RESULT_CANCELED, mConfigState); + + String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); + showDownloadFailedDialog(reason_to_fail); + } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { + mProgressBar.incrementProgressBy(1); + hideProgressBar(); + + showProviderDetails(); + + setResult(RESULT_OK); + } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { + hideProgressBar(); + cancelSettingUpProvider(); + Toast.makeText(getApplicationContext(), R.string.provider_problem, Toast.LENGTH_LONG).show(); + setResult(RESULT_CANCELED, mConfigState); + } else if (resultCode == AboutActivity.VIEWED) { + // Do nothing, right now + // I need this for CW to wait for the About activity to end before going back to Dashboard. + } + } + @OnItemClick(R.id.provider_list) void onItemSelected(int position) { //TODO Code 2 pane view @@ -234,7 +237,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download private void onItemSelectedLogic(Provider selected_provider) { boolean danger_on = true; - if(preferences.contains(ProviderItem.DANGER_ON)) + if (preferences.contains(ProviderItem.DANGER_ON)) danger_on = preferences.getBoolean(ProviderItem.DANGER_ON, false); setUpProvider(selected_provider.mainUrl(), danger_on); } @@ -243,24 +246,24 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download startProgressBar(); adapter.hideAllBut(adapter.indexOf(provider)); } - + @Override public void onBackPressed() { - if(setting_up_provider) { - stopSettingUpProvider(); - } else { - askDashboardToQuitApp(); - super.onBackPressed(); - } - } - + if (setting_up_provider) { + stopSettingUpProvider(); + } else { + askDashboardToQuitApp(); + super.onBackPressed(); + } + } + private void stopSettingUpProvider() { - ProviderAPI.stop(); - mProgressBar.setVisibility(ProgressBar.GONE); - mProgressBar.setProgress(0); - progressbar_description.setVisibility(TextView.GONE); + ProviderAPI.stop(); + mProgressBar.setVisibility(ProgressBar.GONE); + mProgressBar.setProgress(0); + progressbar_description.setVisibility(TextView.GONE); - cancelSettingUpProvider(); + cancelSettingUpProvider(); } public void cancelSettingUpProvider() { @@ -268,11 +271,11 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download setting_up_provider = false; preferences.edit().remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); } - + private void askDashboardToQuitApp() { - Intent ask_quit = new Intent(); - ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); - setResult(RESULT_CANCELED, ask_quit); + Intent ask_quit = new Intent(); + ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); + setResult(RESULT_CANCELED, ask_quit); } private void startProgressBar() { @@ -281,88 +284,89 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download mProgressBar.setProgress(0); mProgressBar.setMax(3); - int measured_height = listItemHeight(); - mProgressBar.setTranslationY(measured_height); - progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); + int measured_height = listItemHeight(); + mProgressBar.setTranslationY(measured_height); + progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); } - + private int listItemHeight() { View listItem = adapter.getView(0, null, provider_list_view); listItem.setLayoutParams(new RelativeLayout.LayoutParams( - RelativeLayout.LayoutParams.WRAP_CONTENT, - RelativeLayout.LayoutParams.WRAP_CONTENT)); + RelativeLayout.LayoutParams.WRAP_CONTENT, + RelativeLayout.LayoutParams.WRAP_CONTENT)); WindowManager wm = (WindowManager) getApplicationContext() - .getSystemService(Context.WINDOW_SERVICE); + .getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int screenWidth = display.getWidth(); // deprecated int listViewWidth = screenWidth - 10 - 10; int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, - View.MeasureSpec.AT_MOST); + View.MeasureSpec.AT_MOST); listItem.measure(widthSpec, 0); return listItem.getMeasuredHeight(); } - + /** * Asks ProviderAPI to download an anonymous (anon) VPN certificate. */ private void downloadVpnCertificate() { - Intent provider_API_command = new Intent(this, ProviderAPI.class); + Intent provider_API_command = new Intent(this, ProviderAPI.class); - provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - startService(provider_API_command); + provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + startService(provider_API_command); } - + /** * Open the new provider dialog */ public void addAndSelectNewProvider() { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); new NewProviderDialog().show(fragment_transaction, NewProviderDialog.TAG); } - + /** * Open the new provider dialog with data */ public void addAndSelectNewProvider(String main_url, boolean danger_on) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - - DialogFragment newFragment = new NewProviderDialog(); - Bundle data = new Bundle(); - data.putString(Provider.MAIN_URL, main_url); - data.putBoolean(ProviderItem.DANGER_ON, danger_on); - newFragment.setArguments(data); - newFragment.show(fragment_transaction, NewProviderDialog.TAG); - } - + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); + + DialogFragment newFragment = new NewProviderDialog(); + Bundle data = new Bundle(); + data.putString(Provider.MAIN_URL, main_url); + data.putBoolean(ProviderItem.DANGER_ON, danger_on); + newFragment.setArguments(data); + newFragment.show(fragment_transaction, NewProviderDialog.TAG); + } + /** - * Once selected a provider, this fragment offers the user to log in, - * use it anonymously (if possible) + * Once selected a provider, this fragment offers the user to log in, + * use it anonymously (if possible) * or cancel his/her election pressing the back button. + * * @param reason_to_fail */ public void showDownloadFailedDialog(String reason_to_fail) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); - - DialogFragment newFragment = DownloadFailedDialog.newInstance(reason_to_fail); - newFragment.show(fragment_transaction, DownloadFailedDialog.TAG); + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); + + DialogFragment newFragment = DownloadFailedDialog.newInstance(reason_to_fail); + newFragment.show(fragment_transaction, DownloadFailedDialog.TAG); } /** - * Once selected a provider, this fragment offers the user to log in, - * use it anonymously (if possible) + * Once selected a provider, this fragment offers the user to log in, + * use it anonymously (if possible) * or cancel his/her election pressing the back button. */ private void showProviderDetails() { - if(setting_up_provider) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(ProviderDetailFragment.TAG); + if (setting_up_provider) { + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(ProviderDetailFragment.TAG); - DialogFragment newFragment = ProviderDetailFragment.newInstance(); - newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); - setting_up_provider = false; - } + DialogFragment newFragment = ProviderDetailFragment.newInstance(); + newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); + setting_up_provider = false; + } } public void showAndSelectProvider(String provider_main_url, boolean danger_on) { @@ -377,92 +381,94 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download } private void autoSelectProvider(Provider provider, boolean danger_on) { - preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); + preferences.edit().putBoolean(ProviderItem.DANGER_ON, danger_on).apply(); selected_provider = provider; onItemSelectedLogic(selected_provider); onItemSelectedUi(selected_provider); } - + /** * Asks ProviderAPI to download a new provider.json file + * * @param provider_main_url - * @param danger_on tells if HTTPS client should bypass certificate errors + * @param danger_on tells if HTTPS client should bypass certificate errors */ public void setUpProvider(URL provider_main_url, boolean danger_on) { - Intent provider_API_command = new Intent(this, ProviderAPI.class); - Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, provider_main_url.toString()); - parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); + Intent provider_API_command = new Intent(this, ProviderAPI.class); + Bundle parameters = new Bundle(); + parameters.putString(Provider.MAIN_URL, provider_main_url.toString()); + parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); - provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); - provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); + provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - startService(provider_API_command); - setting_up_provider = true; + startService(provider_API_command); + setting_up_provider = true; } public void retrySetUpProvider() { - cancelSettingUpProvider(); - if(!ProviderAPI.caCertDownloaded()) { - addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl(), ProviderAPI.lastDangerOn()); - } else { - Intent provider_API_command = new Intent(this, ProviderAPI.class); + cancelSettingUpProvider(); + if (!ProviderAPI.caCertDownloaded()) { + addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl(), ProviderAPI.lastDangerOn()); + } else { + Intent provider_API_command = new Intent(this, ProviderAPI.class); - provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - startService(provider_API_command); - } + startService(provider_API_command); + } } + @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.configuration_wizard_activity, menu); - return true; + getMenuInflater().inflate(R.menu.configuration_wizard_activity, menu); + return true; } - + @Override - public boolean onOptionsItemSelected(MenuItem item){ - switch (item.getItemId()){ - case R.id.about_leap: - startActivityForResult(new Intent(this, AboutActivity.class), 0); - return true; - case R.id.new_provider: - addAndSelectNewProvider(); - return true; - default: - return super.onOptionsItemSelected(item); - } + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.about_leap: + startActivityForResult(new Intent(this, AboutActivity.class), 0); + return true; + case R.id.new_provider: + addAndSelectNewProvider(); + return true; + default: + return super.onOptionsItemSelected(item); + } } public void showAllProviders() { adapter.showAllProviders(); } - + @Override public void login() { - Intent ask_login = new Intent(); - ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); + Intent ask_login = new Intent(); + ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); ask_login.putExtra(Provider.KEY, selected_provider); - setResult(RESULT_OK, ask_login); - setting_up_provider = false; - finish(); + setResult(RESULT_OK, ask_login); + setting_up_provider = false; + finish(); } @Override public void use_anonymously() { Intent pass_provider = new Intent(); pass_provider.putExtra(Provider.KEY, selected_provider); - setResult(RESULT_OK, pass_provider); - setting_up_provider = false; - finish(); + setResult(RESULT_OK, pass_provider); + setting_up_provider = false; + finish(); } public class ProviderAPIBroadcastReceiver_Update extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); - mProgressBar.setProgress(update); - } + @Override + public void onReceive(Context context, Intent intent) { + int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); + mProgressBar.setProgress(update); + } } } diff --git a/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java b/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java index 01c13ca3..c1426708 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java +++ b/app/src/debug/java/se/leap/bitmaskclient/NewProviderDialog.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -18,6 +18,7 @@ package se.leap.bitmaskclient; import butterknife.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; + import android.app.*; import android.content.*; import android.os.*; @@ -26,9 +27,8 @@ import android.widget.*; /** * Implements the new custom provider dialog. - * - * @author parmegv * + * @author parmegv */ public class NewProviderDialog extends DialogFragment { @@ -39,17 +39,17 @@ public class NewProviderDialog extends DialogFragment { @InjectView(R.id.danger_checkbox) CheckBox danger_checkbox; - public interface NewProviderDialogInterface { + public interface NewProviderDialogInterface { public void showAndSelectProvider(String url_provider, boolean danger_on); } - NewProviderDialogInterface interface_with_ConfigurationWizard; - + NewProviderDialogInterface interface_with_ConfigurationWizard; + @Override public void onAttach(Activity activity) { super.onAttach(activity); try { - interface_with_ConfigurationWizard = (NewProviderDialogInterface) activity; + interface_with_ConfigurationWizard = (NewProviderDialogInterface) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); @@ -57,59 +57,61 @@ public class NewProviderDialog extends DialogFragment { } @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - LayoutInflater inflater = getActivity().getLayoutInflater(); - View view = inflater.inflate(R.layout.new_provider_dialog, null); + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.new_provider_dialog, null); ButterKnife.inject(this, view); Bundle arguments = getArguments(); - if(arguments != null) { + if (arguments != null) { url_input_field.setText(arguments.getString(Provider.MAIN_URL, "")); danger_checkbox.setActivated(arguments.getBoolean(ProviderItem.DANGER_ON, false)); } - - builder.setView(view) - .setMessage(R.string.introduce_new_provider) - .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - saveProvider(); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }); - // Create the AlertDialog object and return it - return builder.create(); - } + + builder.setView(view) + .setMessage(R.string.introduce_new_provider) + .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + saveProvider(); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + } + }); + // Create the AlertDialog object and return it + return builder.create(); + } private void saveProvider() { String entered_url = url_input_field.getText().toString().trim(); - if(!entered_url.startsWith("https://")) { - if (entered_url.startsWith("http://")){ + if (!entered_url.startsWith("https://")) { + if (entered_url.startsWith("http://")) { entered_url = entered_url.substring("http://".length()); } entered_url = "https://".concat(entered_url); } boolean danger_on = danger_checkbox.isChecked(); - if(validURL(entered_url)) { + if (validURL(entered_url)) { interface_with_ConfigurationWizard.showAndSelectProvider(entered_url, danger_on); Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); } else { url_input_field.setText(""); danger_checkbox.setChecked(false); - Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show();; + Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show(); + ; } } /** * Checks if the entered url is valid or not. + * * @param entered_url * @return true if it's not empty nor contains only the protocol. */ - boolean validURL(String entered_url) { - //return !entered_url.isEmpty() && entered_url.matches("http[s]?://.+") && !entered_url.replaceFirst("http[s]?://", "").isEmpty(); - return android.util.Patterns.WEB_URL.matcher(entered_url).matches(); - } + boolean validURL(String entered_url) { + //return !entered_url.isEmpty() && entered_url.matches("http[s]?://.+") && !entered_url.replaceFirst("http[s]?://", "").isEmpty(); + return android.util.Patterns.WEB_URL.matcher(entered_url).matches(); + } } diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index f17c7151..50d09c55 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -20,6 +20,7 @@ import android.app.*; import android.content.*; import android.os.*; import android.util.*; + import java.io.*; import java.math.*; import java.net.*; @@ -27,7 +28,9 @@ import java.security.*; import java.security.cert.*; import java.security.interfaces.*; import java.util.*; + import javax.net.ssl.*; + import org.apache.http.client.*; import org.json.*; @@ -36,94 +39,90 @@ import se.leap.bitmaskclient.eip.*; /** * Implements HTTP api methods used to manage communications with the provider server. - * + *

* It's an IntentService because it downloads data from the Internet, so it operates in the background. - * + * * @author parmegv * @author MeanderingCode - * */ public class ProviderAPI extends IntentService { - + final public static String - TAG = ProviderAPI.class.getSimpleName(), - SET_UP_PROVIDER = "setUpProvider", - DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON", - SIGN_UP = "srpRegister", - LOG_IN = "srpAuth", - LOG_OUT = "logOut", - DOWNLOAD_CERTIFICATE = "downloadUserAuthedCertificate", - PARAMETERS = "parameters", - RESULT_KEY = "result", - RECEIVER_KEY = "receiver", - ERRORS = "errors", - UPDATE_PROGRESSBAR = "update_progressbar", - CURRENT_PROGRESS = "current_progress", - DOWNLOAD_EIP_SERVICE = TAG + ".DOWNLOAD_EIP_SERVICE" - ; + TAG = ProviderAPI.class.getSimpleName(), + SET_UP_PROVIDER = "setUpProvider", + DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON", + SIGN_UP = "srpRegister", + LOG_IN = "srpAuth", + LOG_OUT = "logOut", + DOWNLOAD_CERTIFICATE = "downloadUserAuthedCertificate", + PARAMETERS = "parameters", + RESULT_KEY = "result", + RECEIVER_KEY = "receiver", + ERRORS = "errors", + UPDATE_PROGRESSBAR = "update_progressbar", + CURRENT_PROGRESS = "current_progress", + DOWNLOAD_EIP_SERVICE = TAG + ".DOWNLOAD_EIP_SERVICE"; final public static int SUCCESSFUL_LOGIN = 3, - FAILED_LOGIN = 4, - SUCCESSFUL_SIGNUP = 5, - FAILED_SIGNUP = 6, - SUCCESSFUL_LOGOUT = 7, - LOGOUT_FAILED = 8, - CORRECTLY_DOWNLOADED_CERTIFICATE = 9, - INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, - PROVIDER_OK = 11, - PROVIDER_NOK = 12, - CORRECTLY_DOWNLOADED_EIP_SERVICE = 13, - INCORRECTLY_DOWNLOADED_EIP_SERVICE= 14 - ; - - private static boolean - CA_CERT_DOWNLOADED = false, - PROVIDER_JSON_DOWNLOADED = false, - EIP_SERVICE_JSON_DOWNLOADED = false - ; - + FAILED_LOGIN = 4, + SUCCESSFUL_SIGNUP = 5, + FAILED_SIGNUP = 6, + SUCCESSFUL_LOGOUT = 7, + LOGOUT_FAILED = 8, + CORRECTLY_DOWNLOADED_CERTIFICATE = 9, + INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, + PROVIDER_OK = 11, + PROVIDER_NOK = 12, + CORRECTLY_DOWNLOADED_EIP_SERVICE = 13, + INCORRECTLY_DOWNLOADED_EIP_SERVICE = 14; + + private static boolean + CA_CERT_DOWNLOADED = false, + PROVIDER_JSON_DOWNLOADED = false, + EIP_SERVICE_JSON_DOWNLOADED = false; + private static String last_provider_main_url; private static boolean last_danger_on = false; private static boolean go_ahead = true; private static SharedPreferences preferences; private static String provider_api_url; - + public static void stop() { - go_ahead = false; + go_ahead = false; + } + + public ProviderAPI() { + super("ProviderAPI"); + Log.v("ClassName", "Provider API"); } - public ProviderAPI() { - super("ProviderAPI"); - Log.v("ClassName", "Provider API"); - } - @Override public void onCreate() { - super.onCreate(); - - preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); - CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER) ); - } - - public static String lastProviderMainUrl() { - return last_provider_main_url; - } - + super.onCreate(); + + preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); + CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); + } + + public static String lastProviderMainUrl() { + return last_provider_main_url; + } + public static boolean lastDangerOn() { - return last_danger_on; + return last_danger_on; + } + + private String formatErrorMessage(final int toast_string_id) { + return "{ \"" + ERRORS + "\" : \"" + getResources().getString(toast_string_id) + "\" }"; } - - private String formatErrorMessage(final int toast_string_id) { - return "{ \"" + ERRORS + "\" : \""+getResources().getString(toast_string_id)+"\" }"; - } @Override protected void onHandleIntent(Intent command) { - final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); - String action = command.getAction(); - Bundle parameters = command.getBundleExtra(PARAMETERS); - if(provider_api_url == null) { + final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); + String action = command.getAction(); + Bundle parameters = command.getBundleExtra(PARAMETERS); + if (provider_api_url == null) { try { JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "no provider")); provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); @@ -132,52 +131,52 @@ public class ProviderAPI extends IntentService { go_ahead = false; } } - - if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { - Bundle result = setUpProvider(parameters); - if(go_ahead) { - if(result.getBoolean(RESULT_KEY)) { - receiver.send(PROVIDER_OK, result); - } else { - receiver.send(PROVIDER_NOK, result); - } - } - } else if (action.equalsIgnoreCase(SIGN_UP)) { - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.SIGNING_UP); - Bundle result = tryToRegister(parameters); - if(result.getBoolean(RESULT_KEY)) { - receiver.send(SUCCESSFUL_SIGNUP, result); - } else { - receiver.send(FAILED_SIGNUP, result); - } - } else if (action.equalsIgnoreCase(LOG_IN)) { - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_IN); - Bundle result = tryToAuthenticate(parameters); - if(result.getBoolean(RESULT_KEY)) { - receiver.send(SUCCESSFUL_LOGIN, result); - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_IN); - } else { - receiver.send(FAILED_LOGIN, result); - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.NOT_LOGGED_IN); - } - } else if (action.equalsIgnoreCase(LOG_OUT)) { - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_OUT); - if(logOut()) { - receiver.send(SUCCESSFUL_LOGOUT, Bundle.EMPTY); - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_OUT); - } else { - receiver.send(LOGOUT_FAILED, Bundle.EMPTY); - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.DIDNT_LOG_OUT); - } - } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { - if(updateVpnCertificate()) { - receiver.send(CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); - } else { - receiver.send(INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); - } - } else if(action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { + + if (action.equalsIgnoreCase(SET_UP_PROVIDER)) { + Bundle result = setUpProvider(parameters); + if (go_ahead) { + if (result.getBoolean(RESULT_KEY)) { + receiver.send(PROVIDER_OK, result); + } else { + receiver.send(PROVIDER_NOK, result); + } + } + } else if (action.equalsIgnoreCase(SIGN_UP)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.SIGNING_UP); + Bundle result = tryToRegister(parameters); + if (result.getBoolean(RESULT_KEY)) { + receiver.send(SUCCESSFUL_SIGNUP, result); + } else { + receiver.send(FAILED_SIGNUP, result); + } + } else if (action.equalsIgnoreCase(LOG_IN)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_IN); + Bundle result = tryToAuthenticate(parameters); + if (result.getBoolean(RESULT_KEY)) { + receiver.send(SUCCESSFUL_LOGIN, result); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_IN); + } else { + receiver.send(FAILED_LOGIN, result); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.NOT_LOGGED_IN); + } + } else if (action.equalsIgnoreCase(LOG_OUT)) { + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_OUT); + if (logOut()) { + receiver.send(SUCCESSFUL_LOGOUT, Bundle.EMPTY); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_OUT); + } else { + receiver.send(LOGOUT_FAILED, Bundle.EMPTY); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.DIDNT_LOG_OUT); + } + } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { + if (updateVpnCertificate()) { + receiver.send(CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); + } else { + receiver.send(INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); + } + } else if (action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { Bundle result = getAndSetEipServiceJson(); - if(result.getBoolean(RESULT_KEY)) { + if (result.getBoolean(RESULT_KEY)) { receiver.send(CORRECTLY_DOWNLOADED_EIP_SERVICE, result); } else { receiver.send(INCORRECTLY_DOWNLOADED_EIP_SERVICE, result); @@ -186,659 +185,674 @@ public class ProviderAPI extends IntentService { } private Bundle tryToRegister(Bundle task) { - Bundle result = new Bundle(); - int progress = 0; - - String username = User.userName(); - String password = task.getString(SessionDialog.PASSWORD); - - if(validUserLoginData(username, password)) { - result = register(username, password); - broadcastProgress(progress++); - } else { - if(!wellFormedPassword(password)) { - result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); - } - if(!validUsername(username)) { - result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); - } - } - - return result; - } - - private Bundle register(String username, String password) { - LeapSRPSession client = new LeapSRPSession(username, password); - byte[] salt = client.calculateNewSalt(); - - BigInteger password_verifier = client.calculateV(username, password, salt); - - JSONObject api_result = sendNewUserDataToSRPServer(provider_api_url, username, new BigInteger(1, salt).toString(16), password_verifier.toString(16)); - - Bundle result = new Bundle(); - if(api_result.has(ERRORS)) - result = authFailedNotification(api_result, username); - else { - result.putString(SessionDialog.USERNAME, username); - result.putString(SessionDialog.PASSWORD, password); - result.putBoolean(RESULT_KEY, true); - } - - return result; - } - /** - * Starts the authentication process using SRP protocol. - * - * @param task containing: username, password and api url. - * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if authentication was successful. - */ - private Bundle tryToAuthenticate(Bundle task) { - Bundle result = new Bundle(); - int progress = 0; + Bundle result = new Bundle(); + int progress = 0; String username = User.userName(); - String password = task.getString(SessionDialog.PASSWORD); - if(validUserLoginData(username, password)) { - result = authenticate(username, password); - broadcastProgress(progress++); - } else { - if(!wellFormedPassword(password)) { - result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); - } - if(!validUsername(username)) { - result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); - } - } - - return result; - } + String password = task.getString(SessionDialog.PASSWORD); + + if (validUserLoginData(username, password)) { + result = register(username, password); + broadcastProgress(progress++); + } else { + if (!wellFormedPassword(password)) { + result.putBoolean(RESULT_KEY, false); + result.putString(SessionDialog.USERNAME, username); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); + } + if (!validUsername(username)) { + result.putBoolean(RESULT_KEY, false); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); + } + } + + return result; + } + + private Bundle register(String username, String password) { + LeapSRPSession client = new LeapSRPSession(username, password); + byte[] salt = client.calculateNewSalt(); + + BigInteger password_verifier = client.calculateV(username, password, salt); + + JSONObject api_result = sendNewUserDataToSRPServer(provider_api_url, username, new BigInteger(1, salt).toString(16), password_verifier.toString(16)); + + Bundle result = new Bundle(); + if (api_result.has(ERRORS)) + result = authFailedNotification(api_result, username); + else { + result.putString(SessionDialog.USERNAME, username); + result.putString(SessionDialog.PASSWORD, password); + result.putBoolean(RESULT_KEY, true); + } + + return result; + } + + /** + * Starts the authentication process using SRP protocol. + * + * @param task containing: username, password and api url. + * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if authentication was successful. + */ + private Bundle tryToAuthenticate(Bundle task) { + Bundle result = new Bundle(); + int progress = 0; + + String username = User.userName(); + String password = task.getString(SessionDialog.PASSWORD); + if (validUserLoginData(username, password)) { + result = authenticate(username, password); + broadcastProgress(progress++); + } else { + if (!wellFormedPassword(password)) { + result.putBoolean(RESULT_KEY, false); + result.putString(SessionDialog.USERNAME, username); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); + } + if (!validUsername(username)) { + result.putBoolean(RESULT_KEY, false); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); + } + } + + return result; + } private Bundle authenticate(String username, String password) { - Bundle result = new Bundle(); - - LeapSRPSession client = new LeapSRPSession(username, password); - byte[] A = client.exponential(); - - JSONObject step_result = sendAToSRPServer(provider_api_url, username, new BigInteger(1, A).toString(16)); - try { - String salt = step_result.getString(LeapSRPSession.SALT); - byte[] Bbytes = new BigInteger(step_result.getString("B"), 16).toByteArray(); - byte[] M1 = client.response(new BigInteger(salt, 16).toByteArray(), Bbytes); - if(M1 != null) { - step_result = sendM1ToSRPServer(provider_api_url, username, M1); - setTokenIfAvailable(step_result); - byte[] M2 = new BigInteger(step_result.getString(LeapSRPSession.M2), 16).toByteArray(); - if(client.verify(M2)) { - result.putBoolean(RESULT_KEY, true); - } else { - authFailedNotification(step_result, username); - } - } else { - result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialog.USERNAME, username); - result.putString(getResources().getString(R.string.user_message), getResources().getString(R.string.error_srp_math_error_user_message)); - } - } catch (JSONException e) { - result = authFailedNotification(step_result, username); - e.printStackTrace(); - } - - return result; + Bundle result = new Bundle(); + + LeapSRPSession client = new LeapSRPSession(username, password); + byte[] A = client.exponential(); + + JSONObject step_result = sendAToSRPServer(provider_api_url, username, new BigInteger(1, A).toString(16)); + try { + String salt = step_result.getString(LeapSRPSession.SALT); + byte[] Bbytes = new BigInteger(step_result.getString("B"), 16).toByteArray(); + byte[] M1 = client.response(new BigInteger(salt, 16).toByteArray(), Bbytes); + if (M1 != null) { + step_result = sendM1ToSRPServer(provider_api_url, username, M1); + setTokenIfAvailable(step_result); + byte[] M2 = new BigInteger(step_result.getString(LeapSRPSession.M2), 16).toByteArray(); + if (client.verify(M2)) { + result.putBoolean(RESULT_KEY, true); + } else { + authFailedNotification(step_result, username); + } + } else { + result.putBoolean(RESULT_KEY, false); + result.putString(SessionDialog.USERNAME, username); + result.putString(getResources().getString(R.string.user_message), getResources().getString(R.string.error_srp_math_error_user_message)); + } + } catch (JSONException e) { + result = authFailedNotification(step_result, username); + e.printStackTrace(); + } + + return result; } private boolean setTokenIfAvailable(JSONObject authentication_step_result) { - try { - LeapSRPSession.setToken(authentication_step_result.getString(LeapSRPSession.TOKEN)); - CookieHandler.setDefault(null); // we don't need cookies anymore - } catch(JSONException e) { // - return false; - } - return true; - } - + try { + LeapSRPSession.setToken(authentication_step_result.getString(LeapSRPSession.TOKEN)); + CookieHandler.setDefault(null); // we don't need cookies anymore + } catch (JSONException e) { // + return false; + } + return true; + } + private Bundle authFailedNotification(JSONObject result, String username) { - Bundle user_notification_bundle = new Bundle(); - try{ - JSONObject error_message = result.getJSONObject(ERRORS); - String error_type = error_message.keys().next().toString(); - String message = error_message.get(error_type).toString(); - user_notification_bundle.putString(getResources().getString(R.string.user_message), message); - } catch(JSONException e) {} - - if(!username.isEmpty()) - user_notification_bundle.putString(SessionDialog.USERNAME, username); - user_notification_bundle.putBoolean(RESULT_KEY, false); - - return user_notification_bundle; - } - - /** - * Sets up an intent with the progress value passed as a parameter - * and sends it as a broadcast. - * @param progress - */ - private void broadcastProgress(int progress) { - Intent intentUpdate = new Intent(); - intentUpdate.setAction(UPDATE_PROGRESSBAR); - intentUpdate.addCategory(Intent.CATEGORY_DEFAULT); - intentUpdate.putExtra(CURRENT_PROGRESS, progress); - sendBroadcast(intentUpdate); - } - - /** - * Validates parameters entered by the user to log in - * @param username - * @param password - * @return true if both parameters are present and the entered password length is greater or equal to eight (8). - */ - private boolean validUserLoginData(String username, String password) { - return validUsername(username) && wellFormedPassword(password); - } + Bundle user_notification_bundle = new Bundle(); + try { + JSONObject error_message = result.getJSONObject(ERRORS); + String error_type = error_message.keys().next().toString(); + String message = error_message.get(error_type).toString(); + user_notification_bundle.putString(getResources().getString(R.string.user_message), message); + } catch (JSONException e) { + } + + if (!username.isEmpty()) + user_notification_bundle.putString(SessionDialog.USERNAME, username); + user_notification_bundle.putBoolean(RESULT_KEY, false); + + return user_notification_bundle; + } + + /** + * Sets up an intent with the progress value passed as a parameter + * and sends it as a broadcast. + * + * @param progress + */ + private void broadcastProgress(int progress) { + Intent intentUpdate = new Intent(); + intentUpdate.setAction(UPDATE_PROGRESSBAR); + intentUpdate.addCategory(Intent.CATEGORY_DEFAULT); + intentUpdate.putExtra(CURRENT_PROGRESS, progress); + sendBroadcast(intentUpdate); + } + + /** + * Validates parameters entered by the user to log in + * + * @param username + * @param password + * @return true if both parameters are present and the entered password length is greater or equal to eight (8). + */ + private boolean validUserLoginData(String username, String password) { + return validUsername(username) && wellFormedPassword(password); + } private boolean validUsername(String username) { return username != null && !username.isEmpty(); } - /** - * Validates a password - * @param password - * @return true if the entered password length is greater or equal to eight (8). - */ - private boolean wellFormedPassword(String password) { - return password != null && password.length() >= 8; - } - - /** - * Sends an HTTP POST request to the authentication server with the SRP Parameter A. - * @param server_url - * @param username - * @param clientA First SRP parameter sent - * @return response from authentication server - */ - private JSONObject sendAToSRPServer(String server_url, String username, String clientA) { - Map parameters = new HashMap(); - parameters.put("login", username); - parameters.put("A", clientA); - return sendToServer(server_url + "/sessions.json", "POST", parameters); - } - - /** - * Sends an HTTP PUT request to the authentication server with the SRP Parameter M1 (or simply M). - * @param server_url - * @param username - * @param m1 Second SRP parameter sent - * @return response from authentication server - */ - private JSONObject sendM1ToSRPServer(String server_url, String username, byte[] m1) { - Map parameters = new HashMap(); - parameters.put("client_auth", new BigInteger(1, ConfigHelper.trim(m1)).toString(16)); - - return sendToServer(server_url + "/sessions/" + username +".json", "PUT", parameters); - } - - /** - * Sends an HTTP POST request to the api server to register a new user. - * @param server_url - * @param username - * @param salt - * @param password_verifier - * @return response from authentication server - */ + /** + * Validates a password + * + * @param password + * @return true if the entered password length is greater or equal to eight (8). + */ + private boolean wellFormedPassword(String password) { + return password != null && password.length() >= 8; + } + + /** + * Sends an HTTP POST request to the authentication server with the SRP Parameter A. + * + * @param server_url + * @param username + * @param clientA First SRP parameter sent + * @return response from authentication server + */ + private JSONObject sendAToSRPServer(String server_url, String username, String clientA) { + Map parameters = new HashMap(); + parameters.put("login", username); + parameters.put("A", clientA); + return sendToServer(server_url + "/sessions.json", "POST", parameters); + } + + /** + * Sends an HTTP PUT request to the authentication server with the SRP Parameter M1 (or simply M). + * + * @param server_url + * @param username + * @param m1 Second SRP parameter sent + * @return response from authentication server + */ + private JSONObject sendM1ToSRPServer(String server_url, String username, byte[] m1) { + Map parameters = new HashMap(); + parameters.put("client_auth", new BigInteger(1, ConfigHelper.trim(m1)).toString(16)); + + return sendToServer(server_url + "/sessions/" + username + ".json", "PUT", parameters); + } + + /** + * Sends an HTTP POST request to the api server to register a new user. + * + * @param server_url + * @param username + * @param salt + * @param password_verifier + * @return response from authentication server + */ private JSONObject sendNewUserDataToSRPServer(String server_url, String username, String salt, String password_verifier) { - Map parameters = new HashMap(); - parameters.put("user[login]", username); - parameters.put("user[password_salt]", salt); - parameters.put("user[password_verifier]", password_verifier); - Log.d(TAG, server_url); - Log.d(TAG, parameters.toString()); - return sendToServer(server_url + "/users.json", "POST", parameters); - } - - /** - * Executes an HTTP request expecting a JSON response. - * @param url - * @param request_method - * @param parameters - * @return response from authentication server - */ - private JSONObject sendToServer(String url, String request_method, Map parameters) { - JSONObject json_response; - HttpsURLConnection urlConnection = null; - try { - InputStream is = null; - urlConnection = (HttpsURLConnection)new URL(url).openConnection(); - urlConnection.setRequestMethod(request_method); - urlConnection.setChunkedStreamingMode(0); - urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - - DataOutputStream writer = new DataOutputStream(urlConnection.getOutputStream()); - writer.writeBytes(formatHttpParameters(parameters)); - writer.close(); - - is = urlConnection.getInputStream(); - String plain_response = new Scanner(is).useDelimiter("\\A").next(); - json_response = new JSONObject(plain_response); - } catch (ClientProtocolException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (IOException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (JSONException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (KeyManagementException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (KeyStoreException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (CertificateException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } - - return json_response; - } + Map parameters = new HashMap(); + parameters.put("user[login]", username); + parameters.put("user[password_salt]", salt); + parameters.put("user[password_verifier]", password_verifier); + Log.d(TAG, server_url); + Log.d(TAG, parameters.toString()); + return sendToServer(server_url + "/users.json", "POST", parameters); + } + + /** + * Executes an HTTP request expecting a JSON response. + * + * @param url + * @param request_method + * @param parameters + * @return response from authentication server + */ + private JSONObject sendToServer(String url, String request_method, Map parameters) { + JSONObject json_response; + HttpsURLConnection urlConnection = null; + try { + InputStream is = null; + urlConnection = (HttpsURLConnection) new URL(url).openConnection(); + urlConnection.setRequestMethod(request_method); + urlConnection.setChunkedStreamingMode(0); + urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); + + DataOutputStream writer = new DataOutputStream(urlConnection.getOutputStream()); + writer.writeBytes(formatHttpParameters(parameters)); + writer.close(); + + is = urlConnection.getInputStream(); + String plain_response = new Scanner(is).useDelimiter("\\A").next(); + json_response = new JSONObject(plain_response); + } catch (ClientProtocolException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (IOException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (JSONException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (KeyManagementException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (KeyStoreException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (CertificateException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } + + return json_response; + } private JSONObject getErrorMessage(HttpsURLConnection urlConnection) { - JSONObject error_message = new JSONObject(); - if(urlConnection != null) { - InputStream error_stream = urlConnection.getErrorStream(); - if(error_stream != null) { - String error_response = new Scanner(error_stream).useDelimiter("\\A").next(); - Log.d("Error", error_response); - try { - error_message = new JSONObject(error_response); - } catch (JSONException e) { - Log.d(TAG, e.getMessage()); - e.printStackTrace(); - } - urlConnection.disconnect(); - } - } - return error_message; - } - - private String formatHttpParameters(Map parameters) throws UnsupportedEncodingException { - StringBuilder result = new StringBuilder(); - boolean first = true; - - Iterator parameter_iterator = parameters.keySet().iterator(); - while(parameter_iterator.hasNext()) { - if(first) - first = false; - else - result.append("&&"); - - String key = parameter_iterator.next(); - String value = parameters.get(key); - - result.append(URLEncoder.encode(key, "UTF-8")); - result.append("="); - result.append(URLEncoder.encode(value, "UTF-8")); - } - - return result.toString(); - } - - - - - /** - * Downloads a provider.json from a given URL, adding a new provider using the given name. - * @param task containing a boolean meaning if the provider is custom or not, another boolean meaning if the user completely trusts this provider, the provider name and its provider.json url. - * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful. - */ - private Bundle setUpProvider(Bundle task) { - int progress = 0; - Bundle current_download = new Bundle(); - - if(task != null && task.containsKey(ProviderItem.DANGER_ON) && task.containsKey(Provider.MAIN_URL)) { - last_danger_on = task.getBoolean(ProviderItem.DANGER_ON); - last_provider_main_url = task.getString(Provider.MAIN_URL); - CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; + JSONObject error_message = new JSONObject(); + if (urlConnection != null) { + InputStream error_stream = urlConnection.getErrorStream(); + if (error_stream != null) { + String error_response = new Scanner(error_stream).useDelimiter("\\A").next(); + Log.d("Error", error_response); + try { + error_message = new JSONObject(error_response); + } catch (JSONException e) { + Log.d(TAG, e.getMessage()); + e.printStackTrace(); + } + urlConnection.disconnect(); + } + } + return error_message; + } + + private String formatHttpParameters(Map parameters) throws UnsupportedEncodingException { + StringBuilder result = new StringBuilder(); + boolean first = true; + + Iterator parameter_iterator = parameters.keySet().iterator(); + while (parameter_iterator.hasNext()) { + if (first) + first = false; + else + result.append("&&"); + + String key = parameter_iterator.next(); + String value = parameters.get(key); + + result.append(URLEncoder.encode(key, "UTF-8")); + result.append("="); + result.append(URLEncoder.encode(value, "UTF-8")); + } + + return result.toString(); + } + + + /** + * Downloads a provider.json from a given URL, adding a new provider using the given name. + * + * @param task containing a boolean meaning if the provider is custom or not, another boolean meaning if the user completely trusts this provider, the provider name and its provider.json url. + * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful. + */ + private Bundle setUpProvider(Bundle task) { + int progress = 0; + Bundle current_download = new Bundle(); + + if (task != null && task.containsKey(ProviderItem.DANGER_ON) && task.containsKey(Provider.MAIN_URL)) { + last_danger_on = task.getBoolean(ProviderItem.DANGER_ON); + last_provider_main_url = task.getString(Provider.MAIN_URL); + CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; go_ahead = true; - } - - if(!PROVIDER_JSON_DOWNLOADED) - current_download = getAndSetProviderJson(last_provider_main_url, last_danger_on); - if(PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { - broadcastProgress(progress++); - PROVIDER_JSON_DOWNLOADED = true; - current_download = downloadCACert(last_danger_on); - - if(CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { - broadcastProgress(progress++); - CA_CERT_DOWNLOADED = true; - current_download = getAndSetEipServiceJson(); - if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { - broadcastProgress(progress++); - EIP_SERVICE_JSON_DOWNLOADED = true; - } - } - } - - return current_download; - } - - private Bundle downloadCACert(boolean danger_on) { - Bundle result = new Bundle(); - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - String ca_cert_url = provider_json.getString(Provider.CA_CERT_URI); - String cert_string = downloadWithCommercialCA(ca_cert_url, danger_on); - - if(validCertificate(cert_string) && go_ahead) { - preferences.edit().putString(Provider.CA_CERT, cert_string).commit(); - result.putBoolean(RESULT_KEY, true); - } else { - String reason_to_fail = pickErrorMessage(cert_string); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - } catch (JSONException e) { - String reason_to_fail = formatErrorMessage(R.string.malformed_url); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - - return result; - } - - public static boolean caCertDownloaded() { - return CA_CERT_DOWNLOADED; - } + } + + if (!PROVIDER_JSON_DOWNLOADED) + current_download = getAndSetProviderJson(last_provider_main_url, last_danger_on); + if (PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { + broadcastProgress(progress++); + PROVIDER_JSON_DOWNLOADED = true; + current_download = downloadCACert(last_danger_on); + + if (CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { + broadcastProgress(progress++); + CA_CERT_DOWNLOADED = true; + current_download = getAndSetEipServiceJson(); + if (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { + broadcastProgress(progress++); + EIP_SERVICE_JSON_DOWNLOADED = true; + } + } + } + + return current_download; + } + + private Bundle downloadCACert(boolean danger_on) { + Bundle result = new Bundle(); + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + String ca_cert_url = provider_json.getString(Provider.CA_CERT_URI); + String cert_string = downloadWithCommercialCA(ca_cert_url, danger_on); + + if (validCertificate(cert_string) && go_ahead) { + preferences.edit().putString(Provider.CA_CERT, cert_string).commit(); + result.putBoolean(RESULT_KEY, true); + } else { + String reason_to_fail = pickErrorMessage(cert_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + } catch (JSONException e) { + String reason_to_fail = formatErrorMessage(R.string.malformed_url); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + + return result; + } + + public static boolean caCertDownloaded() { + return CA_CERT_DOWNLOADED; + } private boolean validCertificate(String cert_string) { - boolean result = false; - if(!ConfigHelper.checkErroneousDownload(cert_string)) { - X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(cert_string); - try { - if(certificate != null) { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - String fingerprint = provider_json.getString(Provider.CA_CERT_FINGERPRINT); - String encoding = fingerprint.split(":")[0]; - String expected_fingerprint = fingerprint.split(":")[1]; - String real_fingerprint = base64toHex(Base64.encodeToString( - MessageDigest.getInstance(encoding).digest(certificate.getEncoded()), - Base64.DEFAULT)); - - result = real_fingerprint.trim().equalsIgnoreCase(expected_fingerprint.trim()); - } else - result = false; - } catch (JSONException e) { - result = false; - } catch (NoSuchAlgorithmException e) { - result = false; - } catch (CertificateEncodingException e) { - result = false; - } - } - - return result; + boolean result = false; + if (!ConfigHelper.checkErroneousDownload(cert_string)) { + X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(cert_string); + try { + if (certificate != null) { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + String fingerprint = provider_json.getString(Provider.CA_CERT_FINGERPRINT); + String encoding = fingerprint.split(":")[0]; + String expected_fingerprint = fingerprint.split(":")[1]; + String real_fingerprint = base64toHex(Base64.encodeToString( + MessageDigest.getInstance(encoding).digest(certificate.getEncoded()), + Base64.DEFAULT)); + + result = real_fingerprint.trim().equalsIgnoreCase(expected_fingerprint.trim()); + } else + result = false; + } catch (JSONException e) { + result = false; + } catch (NoSuchAlgorithmException e) { + result = false; + } catch (CertificateEncodingException e) { + result = false; + } + } + + return result; } private String base64toHex(String base64_input) { - byte[] byteArray = Base64.decode(base64_input, Base64.DEFAULT); - int readBytes = byteArray.length; - StringBuffer hexData = new StringBuffer(); - int onebyte; - for (int i=0; i < readBytes; i++) { - onebyte = ((0x000000ff & byteArray[i]) | 0xffffff00); - hexData.append(Integer.toHexString(onebyte).substring(6)); - } - return hexData.toString(); - } - private Bundle getAndSetProviderJson(String provider_main_url, boolean danger_on) { - Bundle result = new Bundle(); - - if(go_ahead) { - String provider_dot_json_string = downloadWithCommercialCA(provider_main_url + "/provider.json", danger_on); - - try { - JSONObject provider_json = new JSONObject(provider_dot_json_string); - provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); - String name = provider_json.getString(Provider.NAME); - //TODO setProviderName(name); - - preferences.edit().putString(Provider.KEY, provider_json.toString()).commit(); - preferences.edit().putBoolean(Constants.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_ANON)).commit(); - preferences.edit().putBoolean(Constants.ALLOWED_REGISTERED, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_REGISTERED)).commit(); - - result.putBoolean(RESULT_KEY, true); - } catch (JSONException e) { - //TODO Error message should be contained in that provider_dot_json_string - String reason_to_fail = pickErrorMessage(provider_dot_json_string); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - } - return result; - } - - private Bundle getAndSetEipServiceJson() { - Bundle result = new Bundle(); - String eip_service_json_string = ""; - if(go_ahead) { - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; - eip_service_json_string = downloadWithProviderCA(eip_service_url, true); - JSONObject eip_service_json = new JSONObject(eip_service_json_string); - eip_service_json.getInt(Provider.API_RETURN_SERIAL); - - preferences.edit().putString(Constants.KEY, eip_service_json.toString()).commit(); - - result.putBoolean(RESULT_KEY, true); - } catch (JSONException e) { - String reason_to_fail = pickErrorMessage(eip_service_json_string); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - } - return result; - } - - /** - * Interprets the error message as a JSON object and extract the "errors" keyword pair. - * If the error message is not a JSON object, then it is returned untouched. - * @param string_json_error_message - * @return final error message - */ - private String pickErrorMessage(String string_json_error_message) { - String error_message = ""; - try { - JSONObject json_error_message = new JSONObject(string_json_error_message); - error_message = json_error_message.getString(ERRORS); - } catch (JSONException e) { - // TODO Auto-generated catch block - error_message = string_json_error_message; - } - - return error_message; - } - - /** - * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. - * - * If danger_on flag is true, SSL exceptions will be managed by futher methods that will try to use some bypass methods. - * @param string_url - * @param danger_on if the user completely trusts this provider - * @return - */ - private String downloadWithCommercialCA(String string_url, boolean danger_on) { - - String json_file_content = ""; - - URL provider_url = null; - int seconds_of_timeout = 2; - try { - provider_url = new URL(string_url); - URLConnection url_connection = provider_url.openConnection(); - url_connection.setConnectTimeout(seconds_of_timeout*1000); - if(!LeapSRPSession.getToken().isEmpty()) - url_connection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token = " + LeapSRPSession.getToken()); - json_file_content = new Scanner(url_connection.getInputStream()).useDelimiter("\\A").next(); - } catch (MalformedURLException e) { - json_file_content = formatErrorMessage(R.string.malformed_url); - } catch(SocketTimeoutException e) { - json_file_content = formatErrorMessage(R.string.server_unreachable_message); - } catch (SSLHandshakeException e) { - if(provider_url != null) { - json_file_content = downloadWithProviderCA(string_url, danger_on); - } else { - json_file_content = formatErrorMessage(R.string.certificate_error); - } - } catch(ConnectException e) { - json_file_content = formatErrorMessage(R.string.service_is_down_error); - } catch (FileNotFoundException e) { - json_file_content = formatErrorMessage(R.string.malformed_url); - } catch (Exception e) { - if(provider_url != null && danger_on) { - json_file_content = downloadWithProviderCA(string_url, danger_on); - } - } - - return json_file_content; - } - - /** - * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. - * @param url_string as a string - * @param danger_on true to download CA certificate in case it has not been downloaded. - * @return an empty string if it fails, the url content if not. - */ - private String downloadWithProviderCA(String url_string, boolean danger_on) { - String json_file_content = ""; - - try { - URL url = new URL(url_string); - // Tell the URLConnection to use a SocketFactory from our SSLContext - HttpsURLConnection urlConnection = - (HttpsURLConnection)url.openConnection(); - urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - if(!LeapSRPSession.getToken().isEmpty()) - urlConnection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token=" + LeapSRPSession.getToken()); - json_file_content = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (UnknownHostException e) { - e.printStackTrace(); - json_file_content = formatErrorMessage(R.string.server_unreachable_message); - } catch (IOException e) { - // The downloaded certificate doesn't validate our https connection. - if(danger_on) { - json_file_content = downloadWithoutCA(url_string); - } else { - json_file_content = formatErrorMessage(R.string.certificate_error); - } - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchElementException e) { - e.printStackTrace(); - json_file_content = formatErrorMessage(R.string.server_unreachable_message); - } - return json_file_content; - } - - private javax.net.ssl.SSLSocketFactory getProviderSSLSocketFactory() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException { - String provider_cert_string = preferences.getString(Provider.CA_CERT,""); - - java.security.cert.Certificate provider_certificate = ConfigHelper.parseX509CertificateFromString(provider_cert_string); - - // Create a KeyStore containing our trusted CAs - String keyStoreType = KeyStore.getDefaultType(); - KeyStore keyStore = KeyStore.getInstance(keyStoreType); - keyStore.load(null, null); - keyStore.setCertificateEntry("provider_ca_certificate", provider_certificate); - - // Create a TrustManager that trusts the CAs in our KeyStore - String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); - TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); - tmf.init(keyStore); - - // Create an SSLContext that uses our TrustManager - SSLContext context = SSLContext.getInstance("TLS"); - context.init(null, tmf.getTrustManagers(), null); - - return context.getSocketFactory(); - } - - /** - * Downloads the string that's in the url with any certificate. - */ - private String downloadWithoutCA(String url_string) { - String string = ""; - try { - - HostnameVerifier hostnameVerifier = new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; - } - }; - - class DefaultTrustManager implements X509TrustManager { - - @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} - - @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - } - - SSLContext context = SSLContext.getInstance("TLS"); - context.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); - - URL url = new URL(url_string); - HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection(); - urlConnection.setSSLSocketFactory(context.getSocketFactory()); - urlConnection.setHostnameVerifier(hostnameVerifier); - string = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); - System.out.println("String ignoring certificate = " + string); - } catch (FileNotFoundException e) { - e.printStackTrace(); - string = formatErrorMessage(R.string.malformed_url); - } catch (IOException e) { - // The downloaded certificate doesn't validate our https connection. - e.printStackTrace(); - string = formatErrorMessage(R.string.certificate_error); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return string; - } - + byte[] byteArray = Base64.decode(base64_input, Base64.DEFAULT); + int readBytes = byteArray.length; + StringBuffer hexData = new StringBuffer(); + int onebyte; + for (int i = 0; i < readBytes; i++) { + onebyte = ((0x000000ff & byteArray[i]) | 0xffffff00); + hexData.append(Integer.toHexString(onebyte).substring(6)); + } + return hexData.toString(); + } + + private Bundle getAndSetProviderJson(String provider_main_url, boolean danger_on) { + Bundle result = new Bundle(); + + if (go_ahead) { + String provider_dot_json_string = downloadWithCommercialCA(provider_main_url + "/provider.json", danger_on); + + try { + JSONObject provider_json = new JSONObject(provider_dot_json_string); + provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); + String name = provider_json.getString(Provider.NAME); + //TODO setProviderName(name); + + preferences.edit().putString(Provider.KEY, provider_json.toString()).commit(); + preferences.edit().putBoolean(Constants.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_ANON)).commit(); + preferences.edit().putBoolean(Constants.ALLOWED_REGISTERED, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_REGISTERED)).commit(); + + result.putBoolean(RESULT_KEY, true); + } catch (JSONException e) { + //TODO Error message should be contained in that provider_dot_json_string + String reason_to_fail = pickErrorMessage(provider_dot_json_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + } + return result; + } + + private Bundle getAndSetEipServiceJson() { + Bundle result = new Bundle(); + String eip_service_json_string = ""; + if (go_ahead) { + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; + eip_service_json_string = downloadWithProviderCA(eip_service_url, true); + JSONObject eip_service_json = new JSONObject(eip_service_json_string); + eip_service_json.getInt(Provider.API_RETURN_SERIAL); + + preferences.edit().putString(Constants.KEY, eip_service_json.toString()).commit(); + + result.putBoolean(RESULT_KEY, true); + } catch (JSONException e) { + String reason_to_fail = pickErrorMessage(eip_service_json_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + } + return result; + } + + /** + * Interprets the error message as a JSON object and extract the "errors" keyword pair. + * If the error message is not a JSON object, then it is returned untouched. + * + * @param string_json_error_message + * @return final error message + */ + private String pickErrorMessage(String string_json_error_message) { + String error_message = ""; + try { + JSONObject json_error_message = new JSONObject(string_json_error_message); + error_message = json_error_message.getString(ERRORS); + } catch (JSONException e) { + // TODO Auto-generated catch block + error_message = string_json_error_message; + } + + return error_message; + } + + /** + * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. + *

+ * If danger_on flag is true, SSL exceptions will be managed by futher methods that will try to use some bypass methods. + * + * @param string_url + * @param danger_on if the user completely trusts this provider + * @return + */ + private String downloadWithCommercialCA(String string_url, boolean danger_on) { + + String json_file_content = ""; + + URL provider_url = null; + int seconds_of_timeout = 2; + try { + provider_url = new URL(string_url); + URLConnection url_connection = provider_url.openConnection(); + url_connection.setConnectTimeout(seconds_of_timeout * 1000); + if (!LeapSRPSession.getToken().isEmpty()) + url_connection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token = " + LeapSRPSession.getToken()); + json_file_content = new Scanner(url_connection.getInputStream()).useDelimiter("\\A").next(); + } catch (MalformedURLException e) { + json_file_content = formatErrorMessage(R.string.malformed_url); + } catch (SocketTimeoutException e) { + json_file_content = formatErrorMessage(R.string.server_unreachable_message); + } catch (SSLHandshakeException e) { + if (provider_url != null) { + json_file_content = downloadWithProviderCA(string_url, danger_on); + } else { + json_file_content = formatErrorMessage(R.string.certificate_error); + } + } catch (ConnectException e) { + json_file_content = formatErrorMessage(R.string.service_is_down_error); + } catch (FileNotFoundException e) { + json_file_content = formatErrorMessage(R.string.malformed_url); + } catch (Exception e) { + if (provider_url != null && danger_on) { + json_file_content = downloadWithProviderCA(string_url, danger_on); + } + } + + return json_file_content; + } + + /** + * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. + * + * @param url_string as a string + * @param danger_on true to download CA certificate in case it has not been downloaded. + * @return an empty string if it fails, the url content if not. + */ + private String downloadWithProviderCA(String url_string, boolean danger_on) { + String json_file_content = ""; + + try { + URL url = new URL(url_string); + // Tell the URLConnection to use a SocketFactory from our SSLContext + HttpsURLConnection urlConnection = + (HttpsURLConnection) url.openConnection(); + urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); + if (!LeapSRPSession.getToken().isEmpty()) + urlConnection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token=" + LeapSRPSession.getToken()); + json_file_content = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnknownHostException e) { + e.printStackTrace(); + json_file_content = formatErrorMessage(R.string.server_unreachable_message); + } catch (IOException e) { + // The downloaded certificate doesn't validate our https connection. + if (danger_on) { + json_file_content = downloadWithoutCA(url_string); + } else { + json_file_content = formatErrorMessage(R.string.certificate_error); + } + } catch (KeyStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyManagementException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchElementException e) { + e.printStackTrace(); + json_file_content = formatErrorMessage(R.string.server_unreachable_message); + } + return json_file_content; + } + + private javax.net.ssl.SSLSocketFactory getProviderSSLSocketFactory() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException { + String provider_cert_string = preferences.getString(Provider.CA_CERT, ""); + + java.security.cert.Certificate provider_certificate = ConfigHelper.parseX509CertificateFromString(provider_cert_string); + + // Create a KeyStore containing our trusted CAs + String keyStoreType = KeyStore.getDefaultType(); + KeyStore keyStore = KeyStore.getInstance(keyStoreType); + keyStore.load(null, null); + keyStore.setCertificateEntry("provider_ca_certificate", provider_certificate); + + // Create a TrustManager that trusts the CAs in our KeyStore + String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); + tmf.init(keyStore); + + // Create an SSLContext that uses our TrustManager + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, tmf.getTrustManagers(), null); + + return context.getSocketFactory(); + } + + /** + * Downloads the string that's in the url with any certificate. + */ + private String downloadWithoutCA(String url_string) { + String string = ""; + try { + + HostnameVerifier hostnameVerifier = new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + + class DefaultTrustManager implements X509TrustManager { + + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + } + + SSLContext context = SSLContext.getInstance("TLS"); + context.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom()); + + URL url = new URL(url_string); + HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); + urlConnection.setSSLSocketFactory(context.getSocketFactory()); + urlConnection.setHostnameVerifier(hostnameVerifier); + string = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); + System.out.println("String ignoring certificate = " + string); + } catch (FileNotFoundException e) { + e.printStackTrace(); + string = formatErrorMessage(R.string.malformed_url); + } catch (IOException e) { + // The downloaded certificate doesn't validate our https connection. + e.printStackTrace(); + string = formatErrorMessage(R.string.certificate_error); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyManagementException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return string; + } + /** * Logs out from the api url retrieved from the task. + * * @return true if there were no exceptions */ private boolean logOut() { @@ -847,117 +861,116 @@ public class ProviderAPI extends IntentService { HttpsURLConnection urlConnection = null; int responseCode = 0; int progress = 0; - try { - - urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection(); - urlConnection.setRequestMethod("DELETE"); - urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - - responseCode = urlConnection.getResponseCode(); - broadcastProgress(progress++); - LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IndexOutOfBoundsException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IOException e) { - // TODO Auto-generated catch block - try { - if(urlConnection != null) { - responseCode = urlConnection.getResponseCode(); - if(responseCode == 401) { - broadcastProgress(progress++); - LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); - return true; - } - } - } catch (IOException e1) { - e1.printStackTrace(); - } - - e.printStackTrace(); - return false; - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return true; - } - + try { + + urlConnection = (HttpsURLConnection) new URL(delete_url).openConnection(); + urlConnection.setRequestMethod("DELETE"); + urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); + + responseCode = urlConnection.getResponseCode(); + broadcastProgress(progress++); + LeapSRPSession.setToken(""); + Log.d(TAG, Integer.toString(responseCode)); + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IndexOutOfBoundsException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IOException e) { + // TODO Auto-generated catch block + try { + if (urlConnection != null) { + responseCode = urlConnection.getResponseCode(); + if (responseCode == 401) { + broadcastProgress(progress++); + LeapSRPSession.setToken(""); + Log.d(TAG, Integer.toString(responseCode)); + return true; + } + } + } catch (IOException e1) { + e1.printStackTrace(); + } + + e.printStackTrace(); + return false; + } catch (KeyManagementException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return true; + } + /** * Downloads a new OpenVPN certificate, attaching authenticated cookie for authenticated certificate. - * - * @return true if certificate was downloaded correctly, false if provider.json or danger_on flag are not present in SharedPreferences, or if the certificate url could not be parsed as a URI, or if there was an SSL error. + * + * @return true if certificate was downloaded correctly, false if provider.json or danger_on flag are not present in SharedPreferences, or if the certificate url could not be parsed as a URI, or if there was an SSL error. */ private boolean updateVpnCertificate() { - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - - String provider_main_url = provider_json.getString(Provider.API_URL); - URL new_cert_string_url = new URL(provider_main_url + "/" + provider_json.getString(Provider.API_VERSION) + "/" + Constants.CERTIFICATE); + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + + String provider_main_url = provider_json.getString(Provider.API_URL); + URL new_cert_string_url = new URL(provider_main_url + "/" + provider_json.getString(Provider.API_VERSION) + "/" + Constants.CERTIFICATE); - boolean danger_on = preferences.getBoolean(ProviderItem.DANGER_ON, false); + boolean danger_on = preferences.getBoolean(ProviderItem.DANGER_ON, false); - String cert_string = downloadWithProviderCA(new_cert_string_url.toString(), danger_on); + String cert_string = downloadWithProviderCA(new_cert_string_url.toString(), danger_on); - if(cert_string.isEmpty() || ConfigHelper.checkErroneousDownload(cert_string)) - return false; - else - return loadCertificate(cert_string); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } + if (cert_string.isEmpty() || ConfigHelper.checkErroneousDownload(cert_string)) + return false; + else + return loadCertificate(cert_string); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } } private boolean loadCertificate(String cert_string) { - try { - // API returns concatenated cert & key. Split them for OpenVPN options - String certificateString = null, keyString = null; - String[] certAndKey = cert_string.split("(?<=-\n)"); - for (int i=0; i < certAndKey.length-1; i++){ - if ( certAndKey[i].contains("KEY") ) { - keyString = certAndKey[i++] + certAndKey[i]; - } - else if ( certAndKey[i].contains("CERTIFICATE") ) { - certificateString = certAndKey[i++] + certAndKey[i]; - } - } - RSAPrivateKey keyCert = ConfigHelper.parseRsaKeyFromString(keyString); - keyString = Base64.encodeToString( keyCert.getEncoded(), Base64.DEFAULT ); - preferences.edit().putString(Constants.PRIVATE_KEY, "-----BEGIN RSA PRIVATE KEY-----\n"+keyString+"-----END RSA PRIVATE KEY-----").commit(); - - X509Certificate certCert = ConfigHelper.parseX509CertificateFromString(certificateString); - certificateString = Base64.encodeToString( certCert.getEncoded(), Base64.DEFAULT); - - preferences.edit().putString(Constants.CERTIFICATE, "-----BEGIN CERTIFICATE-----\n"+certificateString+"-----END CERTIFICATE-----").commit(); - - return true; - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } + try { + // API returns concatenated cert & key. Split them for OpenVPN options + String certificateString = null, keyString = null; + String[] certAndKey = cert_string.split("(?<=-\n)"); + for (int i = 0; i < certAndKey.length - 1; i++) { + if (certAndKey[i].contains("KEY")) { + keyString = certAndKey[i++] + certAndKey[i]; + } else if (certAndKey[i].contains("CERTIFICATE")) { + certificateString = certAndKey[i++] + certAndKey[i]; + } + } + RSAPrivateKey keyCert = ConfigHelper.parseRsaKeyFromString(keyString); + keyString = Base64.encodeToString(keyCert.getEncoded(), Base64.DEFAULT); + preferences.edit().putString(Constants.PRIVATE_KEY, "-----BEGIN RSA PRIVATE KEY-----\n" + keyString + "-----END RSA PRIVATE KEY-----").commit(); + + X509Certificate certCert = ConfigHelper.parseX509CertificateFromString(certificateString); + certificateString = Base64.encodeToString(certCert.getEncoded(), Base64.DEFAULT); + + preferences.edit().putString(Constants.CERTIFICATE, "-----BEGIN CERTIFICATE-----\n" + certificateString + "-----END CERTIFICATE-----").commit(); + + return true; + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } } } diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java index 09e415c5..5a7362d2 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java @@ -14,96 +14,98 @@ import android.widget.*; public class ProviderDetailFragment extends DialogFragment { final public static String TAG = "providerDetailFragment"; - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - try { - - LayoutInflater inflater = getActivity().getLayoutInflater(); - View provider_detail_view = inflater.inflate(R.layout.provider_detail_fragment, null); - - JSONObject provider_json = new JSONObject(getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, getActivity().MODE_PRIVATE).getString(Provider.KEY, "")); - - final TextView domain = (TextView)provider_detail_view.findViewById(R.id.provider_detail_domain); - domain.setText(provider_json.getString(Provider.DOMAIN)); - final TextView name = (TextView)provider_detail_view.findViewById(R.id.provider_detail_name); - name.setText(provider_json.getJSONObject(Provider.NAME).getString("en")); - final TextView description = (TextView)provider_detail_view.findViewById(R.id.provider_detail_description); - description.setText(provider_json.getJSONObject(Provider.DESCRIPTION).getString("en")); - - builder.setView(provider_detail_view); - builder.setTitle(R.string.provider_details_fragment_title); - - if(anon_allowed(provider_json)) { - builder.setPositiveButton(R.string.use_anonymously_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - interface_with_configuration_wizard.use_anonymously(); - } - }); - } - - if(registration_allowed(provider_json)) { - builder.setNegativeButton(R.string.signup_or_login_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - interface_with_configuration_wizard.login(); - } - }); - } - - return builder.create(); - } catch (JSONException e) { - return null; - } - } - - private boolean anon_allowed(JSONObject provider_json) { - try { - JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - return service_description.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); - } catch (JSONException e) { - return false; - } - } - - private boolean registration_allowed(JSONObject provider_json) { - try { - JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - return service_description.has(Provider.ALLOW_REGISTRATION) && service_description.getBoolean(Provider.ALLOW_REGISTRATION); - } catch (JSONException e) { - return false; - } - } - - @Override - public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - SharedPreferences.Editor editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); - editor.remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); - interface_with_configuration_wizard.showAllProviders(); - } - - public static DialogFragment newInstance() { - ProviderDetailFragment provider_detail_fragment = new ProviderDetailFragment(); - return provider_detail_fragment; - } - + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + try { + + LayoutInflater inflater = getActivity().getLayoutInflater(); + View provider_detail_view = inflater.inflate(R.layout.provider_detail_fragment, null); + + JSONObject provider_json = new JSONObject(getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, getActivity().MODE_PRIVATE).getString(Provider.KEY, "")); + + final TextView domain = (TextView) provider_detail_view.findViewById(R.id.provider_detail_domain); + domain.setText(provider_json.getString(Provider.DOMAIN)); + final TextView name = (TextView) provider_detail_view.findViewById(R.id.provider_detail_name); + name.setText(provider_json.getJSONObject(Provider.NAME).getString("en")); + final TextView description = (TextView) provider_detail_view.findViewById(R.id.provider_detail_description); + description.setText(provider_json.getJSONObject(Provider.DESCRIPTION).getString("en")); + + builder.setView(provider_detail_view); + builder.setTitle(R.string.provider_details_fragment_title); + + if (anon_allowed(provider_json)) { + builder.setPositiveButton(R.string.use_anonymously_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + interface_with_configuration_wizard.use_anonymously(); + } + }); + } + + if (registration_allowed(provider_json)) { + builder.setNegativeButton(R.string.signup_or_login_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + interface_with_configuration_wizard.login(); + } + }); + } + + return builder.create(); + } catch (JSONException e) { + return null; + } + } + + private boolean anon_allowed(JSONObject provider_json) { + try { + JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); + return service_description.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); + } catch (JSONException e) { + return false; + } + } + + private boolean registration_allowed(JSONObject provider_json) { + try { + JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); + return service_description.has(Provider.ALLOW_REGISTRATION) && service_description.getBoolean(Provider.ALLOW_REGISTRATION); + } catch (JSONException e) { + return false; + } + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + SharedPreferences.Editor editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); + editor.remove(Provider.KEY).remove(ProviderItem.DANGER_ON).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); + interface_with_configuration_wizard.showAllProviders(); + } + + public static DialogFragment newInstance() { + ProviderDetailFragment provider_detail_fragment = new ProviderDetailFragment(); + return provider_detail_fragment; + } + @Override public void onAttach(Activity activity) { super.onAttach(activity); try { - interface_with_configuration_wizard = (ProviderDetailFragmentInterface) activity; + interface_with_configuration_wizard = (ProviderDetailFragmentInterface) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement LogInDialogListener"); } } - - public interface ProviderDetailFragmentInterface { - public void login(); - public void use_anonymously(); - public void showAllProviders(); - } - - ProviderDetailFragmentInterface interface_with_configuration_wizard; + + public interface ProviderDetailFragmentInterface { + public void login(); + + public void use_anonymously(); + + public void showAllProviders(); + } + + ProviderDetailFragmentInterface interface_with_configuration_wizard; } diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java index 48273d6a..0a05e839 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderListContent.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -14,64 +14,69 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +package se.leap.bitmaskclient; import java.util.*; import java.net.*; /** * Models the provider list shown in the ConfigurationWizard. - * - * @author parmegv * + * @author parmegv */ public class ProviderListContent { - public static List ITEMS = new ArrayList(); + public static List ITEMS = new ArrayList(); + + public static Map ITEM_MAP = new HashMap(); + + /** + * Adds a new provider item to the end of the items map, and to the items list. + * + * @param item + */ + public static void addItem(ProviderItem item) { + ITEMS.add(item); + ITEM_MAP.put(String.valueOf(ITEMS.size()), item); + } + + public static void removeItem(ProviderItem item) { + ITEMS.remove(item); + ITEM_MAP.remove(item); + } + + /** + * A provider item. + */ + public static class ProviderItem { + final public static String CUSTOM = "custom"; + final public static String DANGER_ON = "danger_on"; + private String provider_main_url; + private String name; - public static Map ITEM_MAP = new HashMap(); + /** + * @param name of the provider + * @param provider_main_url used to download provider.json file of the provider + */ + public ProviderItem(String name, String provider_main_url) { + this.name = name; + this.provider_main_url = provider_main_url; + } - /** - * Adds a new provider item to the end of the items map, and to the items list. - * @param item - */ - public static void addItem(ProviderItem item) { - ITEMS.add(item); - ITEM_MAP.put(String.valueOf(ITEMS.size()), item); - } - public static void removeItem(ProviderItem item) { - ITEMS.remove(item); - ITEM_MAP.remove(item); - } + public String name() { + return name; + } - /** - * A provider item. - */ - public static class ProviderItem { - final public static String CUSTOM = "custom"; - final public static String DANGER_ON = "danger_on"; - private String provider_main_url; - private String name; + public String providerMainUrl() { + return provider_main_url; + } - /** - * @param name of the provider - * @param provider_main_url used to download provider.json file of the provider - */ - public ProviderItem(String name, String provider_main_url) { - this.name = name; - this.provider_main_url = provider_main_url; - } - - public String name() { return name; } - - public String providerMainUrl() { return provider_main_url; } - - public String domain() { - try { - return new URL(provider_main_url).getHost(); - } catch (MalformedURLException e) { - return provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); - } - } - } + public String domain() { + try { + return new URL(provider_main_url).getHost(); + } catch (MalformedURLException e) { + return provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); + } + } + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java b/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java index fa79d009..ce762fcc 100644 --- a/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/AboutActivity.java @@ -6,30 +6,30 @@ import android.content.pm.PackageManager.*; import android.os.*; import android.widget.*; -public class AboutActivity extends Activity { - +public class AboutActivity extends Activity { + final public static String TAG = "aboutFragment"; final public static int VIEWED = 0; @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.about); - TextView ver = (TextView) findViewById(R.id.version); - - String version; - String name="Openvpn"; - try { - PackageInfo packageinfo = getPackageManager().getPackageInfo(getPackageName(), 0); - version = packageinfo.versionName; - name = getString(R.string.app); - } catch (NameNotFoundException e) { - version = "error fetching version"; - } - - - ver.setText(getString(R.string.version_info,name,version)); - setResult(VIEWED); + super.onCreate(savedInstanceState); + setContentView(R.layout.about); + TextView ver = (TextView) findViewById(R.id.version); + + String version; + String name = "Openvpn"; + try { + PackageInfo packageinfo = getPackageManager().getPackageInfo(getPackageName(), 0); + version = packageinfo.versionName; + name = getString(R.string.app); + } catch (NameNotFoundException e) { + version = "error fetching version"; + } + + + ver.setText(getString(R.string.version_info, name, version)); + setResult(VIEWED); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java index b20cb1ae..4929f040 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -29,156 +29,157 @@ import java.security.spec.*; /** * Stores constants, and implements auxiliary methods used across all LEAP Android classes. - * + * * @author parmegv * @author MeanderingCode - * */ public class ConfigHelper { private static KeyStore keystore_trusted; final public static String NG_1024 = - "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3"; + "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3"; final public static BigInteger G = new BigInteger("2"); - - public static boolean checkErroneousDownload(String downloaded_string) { - try { - if(new JSONObject(downloaded_string).has(ProviderAPI.ERRORS) || downloaded_string.isEmpty()) { - return true; - } else { - return false; - } - } catch(JSONException e) { - return false; - } - } - - /** - * Treat the input as the MSB representation of a number, - * and lop off leading zero elements. For efficiency, the - * input is simply returned if no leading zeroes are found. - * - * @param in array to be trimmed - */ - public static byte[] trim(byte[] in) { - if(in.length == 0 || in[0] != 0) - return in; - - int len = in.length; - int i = 1; - while(in[i] == 0 && i < len) - ++i; - byte[] ret = new byte[len - i]; - System.arraycopy(in, i, ret, 0, len - i); - return ret; - } - - public static X509Certificate parseX509CertificateFromString(String certificate_string) { - java.security.cert.Certificate certificate = null; - CertificateFactory cf; - try { - cf = CertificateFactory.getInstance("X.509"); - - certificate_string = certificate_string.replaceFirst("-----BEGIN CERTIFICATE-----", "").replaceFirst("-----END CERTIFICATE-----", "").trim(); - byte[] cert_bytes = Base64.decode(certificate_string, Base64.DEFAULT); - InputStream caInput = new ByteArrayInputStream(cert_bytes); - try { - certificate = cf.generateCertificate(caInput); - System.out.println("ca=" + ((X509Certificate) certificate).getSubjectDN()); - } finally { - caInput.close(); - } - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - return null; - } catch (IllegalArgumentException e) { - return null; - } - - return (X509Certificate) certificate; - } - - protected static RSAPrivateKey parseRsaKeyFromString(String RsaKeyString) { - RSAPrivateKey key = null; - try { - KeyFactory kf = KeyFactory.getInstance("RSA", "BC"); - - RsaKeyString = RsaKeyString.replaceFirst("-----BEGIN RSA PRIVATE KEY-----", "").replaceFirst("-----END RSA PRIVATE KEY-----", ""); - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec( Base64.decode(RsaKeyString, Base64.DEFAULT) ); - key = (RSAPrivateKey) kf.generatePrivate(keySpec); - } catch (InvalidKeySpecException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; - } catch (NoSuchProviderException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; - } - - return key; - } - - /** - * Adds a new X509 certificate given its input stream and its provider name - * @param provider used to store the certificate in the keystore - * @param inputStream from which X509 certificate must be generated. - */ - public static void addTrustedCertificate(String provider, InputStream inputStream) { - CertificateFactory cf; - try { - cf = CertificateFactory.getInstance("X.509"); - X509Certificate cert = - (X509Certificate)cf.generateCertificate(inputStream); - keystore_trusted.setCertificateEntry(provider, cert); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * Adds a new X509 certificate given in its string from and using its provider name - * @param provider used to store the certificate in the keystore - * @param certificate - */ - public static void addTrustedCertificate(String provider, String certificate) { - - try { - X509Certificate cert = ConfigHelper.parseX509CertificateFromString(certificate); - if(keystore_trusted == null) { - keystore_trusted = KeyStore.getInstance("BKS"); - keystore_trusted.load(null); - } - keystore_trusted.setCertificateEntry(provider, cert); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * @return class wide keystore - */ - public static KeyStore getKeystore() { - return keystore_trusted; - } + + public static boolean checkErroneousDownload(String downloaded_string) { + try { + if (new JSONObject(downloaded_string).has(ProviderAPI.ERRORS) || downloaded_string.isEmpty()) { + return true; + } else { + return false; + } + } catch (JSONException e) { + return false; + } + } + + /** + * Treat the input as the MSB representation of a number, + * and lop off leading zero elements. For efficiency, the + * input is simply returned if no leading zeroes are found. + * + * @param in array to be trimmed + */ + public static byte[] trim(byte[] in) { + if (in.length == 0 || in[0] != 0) + return in; + + int len = in.length; + int i = 1; + while (in[i] == 0 && i < len) + ++i; + byte[] ret = new byte[len - i]; + System.arraycopy(in, i, ret, 0, len - i); + return ret; + } + + public static X509Certificate parseX509CertificateFromString(String certificate_string) { + java.security.cert.Certificate certificate = null; + CertificateFactory cf; + try { + cf = CertificateFactory.getInstance("X.509"); + + certificate_string = certificate_string.replaceFirst("-----BEGIN CERTIFICATE-----", "").replaceFirst("-----END CERTIFICATE-----", "").trim(); + byte[] cert_bytes = Base64.decode(certificate_string, Base64.DEFAULT); + InputStream caInput = new ByteArrayInputStream(cert_bytes); + try { + certificate = cf.generateCertificate(caInput); + System.out.println("ca=" + ((X509Certificate) certificate).getSubjectDN()); + } finally { + caInput.close(); + } + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + return null; + } catch (IllegalArgumentException e) { + return null; + } + + return (X509Certificate) certificate; + } + + protected static RSAPrivateKey parseRsaKeyFromString(String RsaKeyString) { + RSAPrivateKey key = null; + try { + KeyFactory kf = KeyFactory.getInstance("RSA", "BC"); + + RsaKeyString = RsaKeyString.replaceFirst("-----BEGIN RSA PRIVATE KEY-----", "").replaceFirst("-----END RSA PRIVATE KEY-----", ""); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(RsaKeyString, Base64.DEFAULT)); + key = (RSAPrivateKey) kf.generatePrivate(keySpec); + } catch (InvalidKeySpecException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } catch (NoSuchProviderException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + + return key; + } + + /** + * Adds a new X509 certificate given its input stream and its provider name + * + * @param provider used to store the certificate in the keystore + * @param inputStream from which X509 certificate must be generated. + */ + public static void addTrustedCertificate(String provider, InputStream inputStream) { + CertificateFactory cf; + try { + cf = CertificateFactory.getInstance("X.509"); + X509Certificate cert = + (X509Certificate) cf.generateCertificate(inputStream); + keystore_trusted.setCertificateEntry(provider, cert); + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * Adds a new X509 certificate given in its string from and using its provider name + * + * @param provider used to store the certificate in the keystore + * @param certificate + */ + public static void addTrustedCertificate(String provider, String certificate) { + + try { + X509Certificate cert = ConfigHelper.parseX509CertificateFromString(certificate); + if (keystore_trusted == null) { + keystore_trusted = KeyStore.getInstance("BKS"); + keystore_trusted.load(null); + } + keystore_trusted.setCertificateEntry(provider, cert); + } catch (KeyStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * @return class wide keystore + */ + public static KeyStore getKeystore() { + return keystore_trusted; + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 7e2fa0b5..2e9aa74c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -27,6 +27,7 @@ import android.widget.*; import org.jetbrains.annotations.*; import org.json.*; + import java.net.*; import java.util.*; @@ -88,7 +89,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn restoreProvider(savedInstanceState); if (provider == null || provider.getName().isEmpty()) - startActivityForResult(new Intent(this,ConfigurationWizard.class),CONFIGURE_LEAP); + startActivityForResult(new Intent(this, ConfigurationWizard.class), CONFIGURE_LEAP); else { buildDashboard(getIntent().getBooleanExtra(ON_BOOT, false)); restoreSessionStatus(savedInstanceState); @@ -97,16 +98,16 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn private void restoreProvider(Bundle savedInstanceState) { if (savedInstanceState != null) { - if(savedInstanceState.containsKey(Provider.KEY)) + if (savedInstanceState.containsKey(Provider.KEY)) provider = savedInstanceState.getParcelable(Provider.KEY); } - if(provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) + if (provider == null && preferences.getBoolean(Constants.PROVIDER_CONFIGURED, false)) provider = getSavedProviderFromSharedPreferences(); } private void restoreSessionStatus(Bundle savedInstanceState) { if (savedInstanceState != null) - if(savedInstanceState.containsKey(UserSessionStatus.TAG)) { + if (savedInstanceState.containsKey(UserSessionStatus.TAG)) { UserSessionStatus.SessionStatus status = (UserSessionStatus.SessionStatus) savedInstanceState.getSerializable(UserSessionStatus.TAG); user_session_status.updateStatus(status); } @@ -114,9 +115,9 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override protected void onSaveInstanceState(@NotNull Bundle outState) { - if(provider != null) + if (provider != null) outState.putParcelable(Provider.KEY, provider); - if(user_session_status_text_view != null && user_session_status_text_view.getVisibility() == TextView.VISIBLE) + if (user_session_status_text_view != null && user_session_status_text_view.getVisibility() == TextView.VISIBLE) outState.putSerializable(UserSessionStatus.TAG, user_session_status.sessionStatus()); super.onSaveInstanceState(outState); @@ -135,27 +136,27 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } private void handleVersion() { - try { - int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; - int lastDetectedVersion = preferences.getInt(APP_VERSION, 0); - preferences.edit().putInt(APP_VERSION, versionCode).apply(); - - switch(versionCode) { - case 91: // 0.6.0 without Bug #5999 - case 101: // 0.8.0 - if(!preferences.getString(Constants.KEY, "").isEmpty()) - eip_fragment.updateEipService(); - break; - } - } catch (NameNotFoundException e) { - Log.d(TAG, "Handle version didn't find any " + getPackageName() + " package"); - } + try { + int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; + int lastDetectedVersion = preferences.getInt(APP_VERSION, 0); + preferences.edit().putInt(APP_VERSION, versionCode).apply(); + + switch (versionCode) { + case 91: // 0.6.0 without Bug #5999 + case 101: // 0.8.0 + if (!preferences.getString(Constants.KEY, "").isEmpty()) + eip_fragment.updateEipService(); + break; + } + } catch (NameNotFoundException e) { + Log.d(TAG, "Handle version didn't find any " + getPackageName() + " package"); + } } @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data){ - if ( requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { - if ( resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == CONFIGURE_LEAP || requestCode == SWITCH_PROVIDER) { + if (resultCode == RESULT_OK && data.hasExtra(Provider.KEY)) { provider = data.getParcelableExtra(Provider.KEY); providerToPreferences(provider); @@ -169,7 +170,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn finish(); } else configErrorDialog(); - } else if(requestCode == EIP.DISCONNECT) { + } else if (requestCode == EIP.DISCONNECT) { EipStatus.getInstance().setConnectedOrDisconnected(); } } @@ -182,25 +183,25 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn } private void configErrorDialog() { - AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getContext()); - alertBuilder.setTitle(getResources().getString(R.string.setup_error_title)); - alertBuilder - .setMessage(getResources().getString(R.string.setup_error_text)) - .setCancelable(false) - .setPositiveButton(getResources().getString(R.string.setup_error_configure_button), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - startActivityForResult(new Intent(getContext(),ConfigurationWizard.class),CONFIGURE_LEAP); - } - }) - .setNegativeButton(getResources().getString(R.string.setup_error_close_button), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - preferences.edit().remove(Provider.KEY).remove(Constants.PROVIDER_CONFIGURED).apply(); - finish(); - } - }) - .show(); + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getContext()); + alertBuilder.setTitle(getResources().getString(R.string.setup_error_title)); + alertBuilder + .setMessage(getResources().getString(R.string.setup_error_text)) + .setCancelable(false) + .setPositiveButton(getResources().getString(R.string.setup_error_configure_button), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + startActivityForResult(new Intent(getContext(), ConfigurationWizard.class), CONFIGURE_LEAP); + } + }) + .setNegativeButton(getResources().getString(R.string.setup_error_close_button), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + preferences.edit().remove(Provider.KEY).remove(Constants.PROVIDER_CONFIGURED).apply(); + finish(); + } + }) + .show(); } /** @@ -212,7 +213,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn ButterKnife.inject(this); provider_name.setText(provider.getDomain()); - if ( provider.hasEIP()){ + if (provider.hasEIP()) { fragment_manager.removePreviousFragment(EipFragment.TAG); eip_fragment = new EipFragment(); @@ -220,7 +221,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn preferences.edit().remove(Dashboard.START_ON_BOOT).apply(); Bundle arguments = new Bundle(); arguments.putBoolean(EipFragment.START_ON_BOOT, true); - if(eip_fragment != null) eip_fragment.setArguments(arguments); + if (eip_fragment != null) eip_fragment.setArguments(arguments); } fragment_manager.replace(R.id.servicesCollection, eip_fragment, EipFragment.TAG); @@ -233,7 +234,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override public boolean onPrepareOptionsMenu(Menu menu) { - if(provider.allowsRegistration()) { + if (provider.allowsRegistration()) { menu.findItem(R.id.signup_button).setVisible(true); boolean logged_in = User.loggedIn(); @@ -245,78 +246,78 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.client_dashboard, menu); - return true; + getMenuInflater().inflate(R.menu.client_dashboard, menu); + return true; } @Override - public boolean onOptionsItemSelected(MenuItem item){ - switch (item.getItemId()){ - case R.id.about_leap: - showAbout(); - return true; - case R.id.log_window: - showLog(); - return true; - case R.id.switch_provider: - switching_provider = true; - if (LeapSRPSession.loggedIn()) logOut(); - else switchProvider(); - return true; - case R.id.login_button: - sessionDialog(Bundle.EMPTY); - return true; - case R.id.logout_button: - logOut(); - return true; - case R.id.signup_button: - sessionDialog(Bundle.EMPTY); - return true; - default: - return super.onOptionsItemSelected(item); - } + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.about_leap: + showAbout(); + return true; + case R.id.log_window: + showLog(); + return true; + case R.id.switch_provider: + switching_provider = true; + if (LeapSRPSession.loggedIn()) logOut(); + else switchProvider(); + return true; + case R.id.login_button: + sessionDialog(Bundle.EMPTY); + return true; + case R.id.logout_button: + logOut(); + return true; + case R.id.signup_button: + sessionDialog(Bundle.EMPTY); + return true; + default: + return super.onOptionsItemSelected(item); + } } public void showAbout() { - Intent intent = new Intent(this, AboutActivity.class); - startActivity(intent); + Intent intent = new Intent(this, AboutActivity.class); + startActivity(intent); } public void showLog() { - Intent startLW = new Intent(getContext(), LogWindow.class); - startActivity(startLW); + Intent startLW = new Intent(getContext(), LogWindow.class); + startActivity(startLW); } @Override public void signUp(String username, String password) { User.setUserName(username); - Bundle parameters = bundlePassword(password); - providerApiCommand(parameters, 0, ProviderAPI.SIGN_UP); + Bundle parameters = bundlePassword(password); + providerApiCommand(parameters, 0, ProviderAPI.SIGN_UP); } @Override public void logIn(String username, String password) { User.setUserName(username); - Bundle parameters = bundlePassword(password); - providerApiCommand(parameters, 0, ProviderAPI.LOG_IN); + Bundle parameters = bundlePassword(password); + providerApiCommand(parameters, 0, ProviderAPI.LOG_IN); } public void logOut() { - providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.LOG_OUT); + providerApiCommand(Bundle.EMPTY, 0, ProviderAPI.LOG_OUT); } @Override - public void update (Observable observable, Object data) { - if(observable instanceof UserSessionStatus) { + public void update(Observable observable, Object data) { + if (observable instanceof UserSessionStatus) { UserSessionStatus status = (UserSessionStatus) observable; - handleNewUserSessionStatus(status); + handleNewUserSessionStatus(status); } } private void handleNewUserSessionStatus(UserSessionStatus status) { user_session_status = status; - if(provider.allowsRegistration()) { - if(user_session_status.inProgress()) + if (provider.allowsRegistration()) { + if (user_session_status.inProgress()) showUserSessionProgressBar(); else hideUserSessionProgressBar(); @@ -355,7 +356,7 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn protected void downloadVpnCertificate() { boolean is_authenticated = LeapSRPSession.loggedIn(); boolean allowed_anon = preferences.getBoolean(Constants.ALLOWED_ANON, false); - if(allowed_anon || is_authenticated) + if (allowed_anon || is_authenticated) providerApiCommand(Bundle.EMPTY, R.string.downloading_certificate_message, ProviderAPI.DOWNLOAD_CERTIFICATE); else sessionDialog(Bundle.EMPTY); @@ -364,50 +365,50 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn private Bundle bundlePassword(String password) { Bundle parameters = new Bundle(); - if(!password.isEmpty()) - parameters.putString(SessionDialog.PASSWORD, password); - return parameters; + if (!password.isEmpty()) + parameters.putString(SessionDialog.PASSWORD, password); + return parameters; } protected void providerApiCommand(Bundle parameters, int progressbar_message_resId, String providerApi_action) { - if(eip_fragment != null && progressbar_message_resId != 0) { + if (eip_fragment != null && progressbar_message_resId != 0) { eip_fragment.progress_bar.setVisibility(ProgressBar.VISIBLE); setStatusMessage(progressbar_message_resId); } - Intent command = prepareProviderAPICommand(parameters, providerApi_action); - startService(command); + Intent command = prepareProviderAPICommand(parameters, providerApi_action); + startService(command); } private Intent prepareProviderAPICommand(Bundle parameters, String action) { - providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); - providerAPI_result_receiver.setReceiver(this); + providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); + providerAPI_result_receiver.setReceiver(this); - Intent command = new Intent(this, ProviderAPI.class); + Intent command = new Intent(this, ProviderAPI.class); - command.putExtra(ProviderAPI.PARAMETERS, parameters); - command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - command.setAction(action); - return command; + command.putExtra(ProviderAPI.PARAMETERS, parameters); + command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + command.setAction(action); + return command; } public void cancelLoginOrSignup() { - EipStatus.getInstance().setConnectedOrDisconnected(); + EipStatus.getInstance().setConnectedOrDisconnected(); } public void sessionDialog(Bundle resultData) { - FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); + FragmentTransaction transaction = fragment_manager.removePreviousFragment(SessionDialog.TAG); - DialogFragment newFragment = new SessionDialog(); - if(provider.getName().equalsIgnoreCase("riseup")) { + DialogFragment newFragment = new SessionDialog(); + if (provider.getName().equalsIgnoreCase("riseup")) { resultData = resultData == Bundle.EMPTY ? new Bundle() : resultData; resultData.putBoolean(SessionDialog.ERRORS.RISEUP_WARNING.toString(), true); } - if(resultData != null && !resultData.isEmpty()) { - newFragment.setArguments(resultData); - } - newFragment.show(transaction, SessionDialog.TAG); + if (resultData != null && !resultData.isEmpty()) { + newFragment.setArguments(resultData); + } + newFragment.show(transaction, SessionDialog.TAG); } private void switchProvider() { @@ -420,43 +421,42 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override public void onReceiveResult(int resultCode, Bundle resultData) { - Log.d(TAG, "onReceiveResult"); - if(resultCode == ProviderAPI.SUCCESSFUL_SIGNUP) { - String username = resultData.getString(SessionDialog.USERNAME); - String password = resultData.getString(SessionDialog.PASSWORD); - logIn(username, password); - } else if(resultCode == ProviderAPI.FAILED_SIGNUP) { - sessionDialog(resultData); - } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { - downloadVpnCertificate(); - } else if(resultCode == ProviderAPI.FAILED_LOGIN) { - sessionDialog(resultData); - } else if(resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { - if(switching_provider) switchProvider(); - } else if(resultCode == ProviderAPI.LOGOUT_FAILED) { - setResult(RESULT_CANCELED); - } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { - eip_fragment.updateEipService(); - eip_fragment.handleNewVpnCertificate(); - setResult(RESULT_OK); - } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { - setResult(RESULT_CANCELED); - } - else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { - eip_fragment.updateEipService(); - setResult(RESULT_OK); - } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { - setResult(RESULT_CANCELED); - } + Log.d(TAG, "onReceiveResult"); + if (resultCode == ProviderAPI.SUCCESSFUL_SIGNUP) { + String username = resultData.getString(SessionDialog.USERNAME); + String password = resultData.getString(SessionDialog.PASSWORD); + logIn(username, password); + } else if (resultCode == ProviderAPI.FAILED_SIGNUP) { + sessionDialog(resultData); + } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGIN) { + downloadVpnCertificate(); + } else if (resultCode == ProviderAPI.FAILED_LOGIN) { + sessionDialog(resultData); + } else if (resultCode == ProviderAPI.SUCCESSFUL_LOGOUT) { + if (switching_provider) switchProvider(); + } else if (resultCode == ProviderAPI.LOGOUT_FAILED) { + setResult(RESULT_CANCELED); + } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { + eip_fragment.updateEipService(); + eip_fragment.handleNewVpnCertificate(); + setResult(RESULT_OK); + } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { + setResult(RESULT_CANCELED); + } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE) { + eip_fragment.updateEipService(); + setResult(RESULT_OK); + } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE) { + setResult(RESULT_CANCELED); + } } private void setStatusMessage(int string_resId) { - if(eip_fragment != null && eip_fragment.status_message != null) - eip_fragment.status_message.setText(string_resId); + if (eip_fragment != null && eip_fragment.status_message != null) + eip_fragment.status_message.setText(string_resId); } public static Context getContext() { - return app; + return app; } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java b/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java index b26205bb..da32dbd4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/DownloadFailedDialog.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -22,67 +22,68 @@ import android.os.*; /** * Implements a dialog to show why a download failed. - * - * @author parmegv * + * @author parmegv */ public class DownloadFailedDialog extends DialogFragment { - public static String TAG = "downloaded_failed_dialog"; - private String reason_to_fail; - /** - * @return a new instance of this DialogFragment. - */ - public static DialogFragment newInstance(String reason_to_fail) { - DownloadFailedDialog dialog_fragment = new DownloadFailedDialog(); - dialog_fragment.reason_to_fail = reason_to_fail; - return dialog_fragment; - } + public static String TAG = "downloaded_failed_dialog"; + private String reason_to_fail; + + /** + * @return a new instance of this DialogFragment. + */ + public static DialogFragment newInstance(String reason_to_fail) { + DownloadFailedDialog dialog_fragment = new DownloadFailedDialog(); + dialog_fragment.reason_to_fail = reason_to_fail; + return dialog_fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setMessage(reason_to_fail) + .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dismiss(); + interface_with_ConfigurationWizard.retrySetUpProvider(); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + interface_with_ConfigurationWizard.cancelSettingUpProvider(); + dialog.dismiss(); + } + }); - builder.setMessage(reason_to_fail) - .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dismiss(); - interface_with_ConfigurationWizard.retrySetUpProvider(); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - interface_with_ConfigurationWizard.cancelSettingUpProvider(); - dialog.dismiss(); - } - }); + // Create the AlertDialog object and return it + return builder.create(); + } - // Create the AlertDialog object and return it - return builder.create(); - } + public interface DownloadFailedDialogInterface { + public void retrySetUpProvider(); - public interface DownloadFailedDialogInterface { - public void retrySetUpProvider(); - public void cancelSettingUpProvider(); - } + public void cancelSettingUpProvider(); + } - DownloadFailedDialogInterface interface_with_ConfigurationWizard; + DownloadFailedDialogInterface interface_with_ConfigurationWizard; - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - interface_with_ConfigurationWizard = (DownloadFailedDialogInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement NoticeDialogListener"); - } - } + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + interface_with_ConfigurationWizard = (DownloadFailedDialogInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement NoticeDialogListener"); + } + } - @Override - public void onCancel(DialogInterface dialog) { - interface_with_ConfigurationWizard.cancelSettingUpProvider(); - dialog.dismiss(); - } + @Override + public void onCancel(DialogInterface dialog) { + interface_with_ConfigurationWizard.cancelSettingUpProvider(); + dialog.dismiss(); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index a5f3118c..3ad658f5 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -79,17 +79,17 @@ public class EipFragment extends Fragment implements Observer { Log.d(TAG, "onCreateView, eip_switch is checked? " + eip_switch.isChecked()); Bundle arguments = getArguments(); - if(arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) + if (arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) startEipFromScratch(); - if(savedInstanceState != null) restoreState(savedInstanceState); + if (savedInstanceState != null) restoreState(savedInstanceState); return view; } private void restoreState(@NotNull Bundle savedInstanceState) { - if(savedInstanceState.getBoolean(IS_PENDING)) + if (savedInstanceState.getBoolean(IS_PENDING)) eip_status.setConnecting(); - else if(savedInstanceState.getBoolean(IS_CONNECTED)) + else if (savedInstanceState.getBoolean(IS_CONNECTED)) eip_status.setConnectedOrDisconnected(); else status_message.setText(savedInstanceState.getString(STATUS_MESSAGE)); @@ -121,7 +121,7 @@ public class EipFragment extends Fragment implements Observer { @OnCheckedChanged(R.id.eipSwitch) void handleSwitch(boolean isChecked) { - if(isChecked) + if (isChecked) handleSwitchOn(); else handleSwitchOff(); @@ -130,9 +130,9 @@ public class EipFragment extends Fragment implements Observer { } private void handleSwitchOn() { - if(canStartEIP()) + if (canStartEIP()) startEipFromScratch(); - else if(canLogInToStartEIP()) { + else if (canLogInToStartEIP()) { wants_to_connect = true; Bundle bundle = new Bundle(); bundle.putBoolean(IS_PENDING, true); @@ -155,9 +155,9 @@ public class EipFragment extends Fragment implements Observer { } private void handleSwitchOff() { - if(eip_status.isConnecting()) { + if (eip_status.isConnecting()) { askPendingStartCancellation(); - } else if(eip_status.isConnected()) { + } else if (eip_status.isConnected()) { askToStopEIP(); } else setDisconnectedUI(); @@ -190,7 +190,7 @@ public class EipFragment extends Fragment implements Observer { String status = dashboard.getString(R.string.eip_status_start_pending); status_message.setText(status); - if(!eip_switch.isChecked()) { + if (!eip_switch.isChecked()) { eip_switch.setChecked(true); } saveStatus(); @@ -198,7 +198,7 @@ public class EipFragment extends Fragment implements Observer { } private void stop() { - if(eip_status.isConnecting()) + if (eip_status.isConnecting()) VoidVpnService.stop(); disconnect(); } @@ -245,10 +245,10 @@ public class EipFragment extends Fragment implements Observer { /** * Send a command to EIP * - * @param action A valid String constant from EIP class representing an Intent - * filter for the EIP class + * @param action A valid String constant from EIP class representing an Intent + * filter for the EIP class */ - private void eipCommand(String action){ + 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(dashboard.getApplicationContext(), EIP.class); vpn_intent.setAction(action); @@ -257,8 +257,8 @@ public class EipFragment extends Fragment implements Observer { } @Override - public void update (Observable observable, Object data) { - if(observable instanceof EipStatus) { + public void update(Observable observable, Object data) { + if (observable instanceof EipStatus) { eip_status = (EipStatus) observable; final EipStatus eip_status = (EipStatus) observable; dashboard.runOnUiThread(new Runnable() { @@ -271,9 +271,9 @@ public class EipFragment extends Fragment implements Observer { } private void handleNewState(EipStatus eip_status) { - if(eip_status.wantsToDisconnect()) + if (eip_status.wantsToDisconnect()) setDisconnectedUI(); - else if(eip_status.isConnecting() || is_starting_to_connect) + else if (eip_status.isConnecting() || is_starting_to_connect) setInProgressUI(eip_status); else if (eip_status.isConnected()) setConnectedUI(); @@ -289,11 +289,11 @@ public class EipFragment extends Fragment implements Observer { status_message.setText(dashboard.getString(R.string.eip_state_connected)); } - private void setDisconnectedUI(){ + private void setDisconnectedUI() { hideProgressBar(); adjustSwitch(); - if(eip_status.errorInLast(5, dashboard.getApplicationContext()) - && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))){ + if (eip_status.errorInLast(5, dashboard.getApplicationContext()) + && !status_message.getText().toString().equalsIgnoreCase(dashboard.getString(R.string.eip_state_not_connected))) { dashboard.showLog(); VoidVpnService.stop(); } @@ -301,15 +301,15 @@ public class EipFragment extends Fragment implements Observer { } private void adjustSwitch() { - if(eip_status.isConnected() || eip_status.isConnecting() || is_starting_to_connect) { + if (eip_status.isConnected() || eip_status.isConnecting() || is_starting_to_connect) { Log.d(TAG, "adjustSwitch, isConnected || isConnecting, is checked"); - if(!eip_switch.isChecked()) { + if (!eip_switch.isChecked()) { eip_switch.setChecked(true); } } else { Log.d(TAG, "adjustSwitch, !isConnected && !isConnecting? " + eip_status.toString()); - if(eip_switch.isChecked()) { + if (eip_switch.isChecked()) { eip_switch.setChecked(false); } } @@ -332,72 +332,72 @@ public class EipFragment extends Fragment implements Observer { } private void showProgressBar() { - if(progress_bar != null) + if (progress_bar != null) progress_bar.setVisibility(View.VISIBLE); } private void hideProgressBar() { - if(progress_bar != null) + if (progress_bar != null) progress_bar.setVisibility(View.GONE); } -protected class EIPReceiver extends ResultReceiver { + protected class EIPReceiver extends ResultReceiver { - protected EIPReceiver(Handler handler){ - super(handler); - } + protected EIPReceiver(Handler handler) { + super(handler); + } - @Override - protected void onReceiveResult(int resultCode, Bundle resultData) { - super.onReceiveResult(resultCode, resultData); + @Override + protected void onReceiveResult(int resultCode, Bundle resultData) { + super.onReceiveResult(resultCode, resultData); - String request = resultData.getString(Constants.REQUEST_TAG); + String request = resultData.getString(Constants.REQUEST_TAG); - if (request.equals(Constants.ACTION_START_EIP)) { - switch (resultCode){ - case Activity.RESULT_OK: - break; - case Activity.RESULT_CANCELED: + if (request.equals(Constants.ACTION_START_EIP)) { + switch (resultCode) { + case Activity.RESULT_OK: + break; + case Activity.RESULT_CANCELED: - break; - } - } else if (request.equals(Constants.ACTION_STOP_EIP)) { - switch (resultCode){ - case Activity.RESULT_OK: - stop(); - break; - case Activity.RESULT_CANCELED: - break; - } - } else if (request.equals(Constants.EIP_NOTIFICATION)) { - switch (resultCode){ - case Activity.RESULT_OK: - break; - case Activity.RESULT_CANCELED: - break; - } - } else if (request.equals(Constants.ACTION_CHECK_CERT_VALIDITY)) { - switch (resultCode) { - case Activity.RESULT_OK: - break; - case Activity.RESULT_CANCELED: - updatingCertificateUI(); - 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; + break; + } + } else if (request.equals(Constants.ACTION_STOP_EIP)) { + switch (resultCode) { + case Activity.RESULT_OK: + stop(); + break; + case Activity.RESULT_CANCELED: + break; + } + } else if (request.equals(Constants.EIP_NOTIFICATION)) { + switch (resultCode) { + case Activity.RESULT_OK: + break; + case Activity.RESULT_CANCELED: + break; + } + } else if (request.equals(Constants.ACTION_CHECK_CERT_VALIDITY)) { + switch (resultCode) { + case Activity.RESULT_OK: + break; + case Activity.RESULT_CANCELED: + updatingCertificateUI(); + 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; + } } } } -} public static EIPReceiver getReceiver() { diff --git a/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java b/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java index 9975b127..8ba7fa34 100644 --- a/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java +++ b/app/src/main/java/se/leap/bitmaskclient/FragmentManagerEnhanced.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -21,26 +21,26 @@ import android.app.*; public class FragmentManagerEnhanced { private FragmentManager generic_fragment_manager; - + public FragmentManagerEnhanced(FragmentManager generic_fragment_manager) { - this.generic_fragment_manager = generic_fragment_manager; + this.generic_fragment_manager = generic_fragment_manager; } - + public FragmentTransaction removePreviousFragment(String tag) { - FragmentTransaction transaction = generic_fragment_manager.beginTransaction(); - Fragment previous_fragment = generic_fragment_manager.findFragmentByTag(tag); - if (previous_fragment != null) { - transaction.remove(previous_fragment); - } - transaction.addToBackStack(null); - - return transaction; + FragmentTransaction transaction = generic_fragment_manager.beginTransaction(); + Fragment previous_fragment = generic_fragment_manager.findFragmentByTag(tag); + if (previous_fragment != null) { + transaction.remove(previous_fragment); + } + transaction.addToBackStack(null); + + return transaction; } public void replace(int containerViewId, Fragment fragment, String tag) { - FragmentTransaction transaction = generic_fragment_manager.beginTransaction(); - - transaction.replace(containerViewId, fragment, tag).commit(); + FragmentTransaction transaction = generic_fragment_manager.beginTransaction(); + + transaction.replace(containerViewId, fragment, tag).commit(); } public FragmentTransaction beginTransaction() { @@ -48,6 +48,6 @@ public class FragmentManagerEnhanced { } public Fragment findFragmentByTag(String tag) { - return generic_fragment_manager.findFragmentByTag(tag); + return generic_fragment_manager.findFragmentByTag(tag); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java index d3085fa3..b961350e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java +++ b/app/src/main/java/se/leap/bitmaskclient/LeapSRPSession.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -26,329 +26,336 @@ import java.util.*; /** * Implements all SRP algorithm logic. - * + *

* It's derived from JBoss implementation, with adjustments to make it work with LEAP platform. - * - * @author parmegv * + * @author parmegv */ public class LeapSRPSession { - - private static String token = ""; - - final public static String SALT = "salt"; - final public static String M1 = "M1"; - final public static String M2 = "M2"; - final public static String TOKEN = "token"; - final public static String AUTHORIZATION_HEADER= "Authorization"; - final public static String TAG= "Leap SRP session class tag"; + + private static String token = ""; + + final public static String SALT = "salt"; + final public static String M1 = "M1"; + final public static String M2 = "M2"; + final public static String TOKEN = "token"; + final public static String AUTHORIZATION_HEADER = "Authorization"; + final public static String TAG = "Leap SRP session class tag"; private User user; - private SRPParameters params; - private String username; - private String password; - private BigInteger N; - private byte[] N_bytes; - private BigInteger g; - private BigInteger x; - private BigInteger v; - private BigInteger a; - private BigInteger A; - private byte[] K; - private SecureRandom pseudoRng; - /** The M1 = H(H(N) xor H(g) | H(U) | s | A | B | K) hash */ - private MessageDigest clientHash; - /** The M2 = H(A | M | K) hash */ - private MessageDigest serverHash; - - private static int A_LEN; - - - /** Creates a new SRP server session object from the username, password - verifier, - @param username, the user ID - @param password, the user clear text password - @param params, the SRP parameters for the session - */ - public LeapSRPSession(String username, String password) - { - this(username, password, null); - } - - /** Creates a new SRP server session object from the username, password - verifier, - @param username, the user ID - @param password, the user clear text password - @param params, the SRP parameters for the session - @param abytes, the random exponent used in the A public key - */ - public LeapSRPSession(String username, String password, byte[] abytes) { - - params = new SRPParameters(new BigInteger(ConfigHelper.NG_1024, 16).toByteArray(), ConfigHelper.G.toByteArray(), BigInteger.ZERO.toByteArray(), "SHA-256"); - this.g = new BigInteger(1, params.g); - N_bytes = ConfigHelper.trim(params.N); - this.N = new BigInteger(1, N_bytes); - this.username = username; - this.password = password; - - try { - pseudoRng = SecureRandom.getInstance("SHA1PRNG"); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - if( abytes != null ) { - A_LEN = 8*abytes.length; - /* TODO Why did they put this condition? - if( 8*abytes.length != A_LEN ) + private SRPParameters params; + private String username; + private String password; + private BigInteger N; + private byte[] N_bytes; + private BigInteger g; + private BigInteger x; + private BigInteger v; + private BigInteger a; + private BigInteger A; + private byte[] K; + private SecureRandom pseudoRng; + /** + * The M1 = H(H(N) xor H(g) | H(U) | s | A | B | K) hash + */ + private MessageDigest clientHash; + /** + * The M2 = H(A | M | K) hash + */ + private MessageDigest serverHash; + + private static int A_LEN; + + + /** + * Creates a new SRP server session object from the username, password + * verifier, + * + * @param username, the user ID + * @param password, the user clear text password + * @param params, the SRP parameters for the session + */ + public LeapSRPSession(String username, String password) { + this(username, password, null); + } + + /** + * Creates a new SRP server session object from the username, password + * verifier, + * + * @param username, the user ID + * @param password, the user clear text password + * @param params, the SRP parameters for the session + * @param abytes, the random exponent used in the A public key + */ + public LeapSRPSession(String username, String password, byte[] abytes) { + + params = new SRPParameters(new BigInteger(ConfigHelper.NG_1024, 16).toByteArray(), ConfigHelper.G.toByteArray(), BigInteger.ZERO.toByteArray(), "SHA-256"); + this.g = new BigInteger(1, params.g); + N_bytes = ConfigHelper.trim(params.N); + this.N = new BigInteger(1, N_bytes); + this.username = username; + this.password = password; + + try { + pseudoRng = SecureRandom.getInstance("SHA1PRNG"); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if (abytes != null) { + A_LEN = 8 * abytes.length; + /* TODO Why did they put this condition? + if( 8*abytes.length != A_LEN ) throw new IllegalArgumentException("The abytes param must be " +(A_LEN/8)+" in length, abytes.length="+abytes.length); */ - this.a = new BigInteger(abytes); - } - else - A_LEN = 64; - - serverHash = newDigest(); - clientHash = newDigest(); - } - - /** - * Calculates the parameter x of the SRP-6a algorithm. - * @param username - * @param password - * @param salt the salt of the user - * @return x - */ - public byte[] calculatePasswordHash(String username, String password, byte[] salt) - { - //password = password.replaceAll("\\\\", "\\\\\\\\"); - // Calculate x = H(s | H(U | ':' | password)) - MessageDigest x_digest = newDigest(); - // Try to convert the username to a byte[] using ISO-8859-1 - byte[] user = null; - byte[] password_bytes = null; - byte[] colon = {}; - String encoding = "ISO-8859-1"; - try { - user = ConfigHelper.trim(username.getBytes(encoding)); - colon = ConfigHelper.trim(":".getBytes(encoding)); - password_bytes = ConfigHelper.trim(password.getBytes(encoding)); - } - catch(UnsupportedEncodingException e) { - // Use the default platform encoding - user = ConfigHelper.trim(username.getBytes()); - colon = ConfigHelper.trim(":".getBytes()); - password_bytes = ConfigHelper.trim(password.getBytes()); - } - - // Build the hash - x_digest.update(user); - x_digest.update(colon); - x_digest.update(password_bytes); - byte[] h = x_digest.digest(); - - x_digest.reset(); - x_digest.update(salt); - x_digest.update(h); - byte[] x_digest_bytes = x_digest.digest(); - - return x_digest_bytes; - } + this.a = new BigInteger(abytes); + } else + A_LEN = 64; + + serverHash = newDigest(); + clientHash = newDigest(); + } + + /** + * Calculates the parameter x of the SRP-6a algorithm. + * + * @param username + * @param password + * @param salt the salt of the user + * @return x + */ + public byte[] calculatePasswordHash(String username, String password, byte[] salt) { + //password = password.replaceAll("\\\\", "\\\\\\\\"); + // Calculate x = H(s | H(U | ':' | password)) + MessageDigest x_digest = newDigest(); + // Try to convert the username to a byte[] using ISO-8859-1 + byte[] user = null; + byte[] password_bytes = null; + byte[] colon = {}; + String encoding = "ISO-8859-1"; + try { + user = ConfigHelper.trim(username.getBytes(encoding)); + colon = ConfigHelper.trim(":".getBytes(encoding)); + password_bytes = ConfigHelper.trim(password.getBytes(encoding)); + } catch (UnsupportedEncodingException e) { + // Use the default platform encoding + user = ConfigHelper.trim(username.getBytes()); + colon = ConfigHelper.trim(":".getBytes()); + password_bytes = ConfigHelper.trim(password.getBytes()); + } + + // Build the hash + x_digest.update(user); + x_digest.update(colon); + x_digest.update(password_bytes); + byte[] h = x_digest.digest(); + + x_digest.reset(); + x_digest.update(salt); + x_digest.update(h); + byte[] x_digest_bytes = x_digest.digest(); + + return x_digest_bytes; + } public byte[] calculateNewSalt() { - try { - BigInteger salt = new BigInteger(64, SecureRandom.getInstance("SHA1PRNG")); - return ConfigHelper.trim(salt.toByteArray()); - } catch(NoSuchAlgorithmException e) { - e.printStackTrace(); - } - return null; + try { + BigInteger salt = new BigInteger(64, SecureRandom.getInstance("SHA1PRNG")); + return ConfigHelper.trim(salt.toByteArray()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return null; } - /** - * Calculates the parameter V of the SRP-6a algorithm. - * @return the value of V - */ + + /** + * Calculates the parameter V of the SRP-6a algorithm. + * + * @return the value of V + */ public BigInteger calculateV(String username, String password, byte[] salt) { - byte[] x_bytes = calculatePasswordHash(username, password, ConfigHelper.trim(salt)); - x = new BigInteger(1, x_bytes); - BigInteger v = g.modPow(x, N); // g^x % N - return v; - } - - /** - * Calculates the trimmed xor from two BigInteger numbers - * @param b1 the positive source to build first BigInteger - * @param b2 the positive source to build second BigInteger - * @param length - * @return - */ - public byte[] xor(byte[] b1, byte[] b2) - { - //TODO Check if length matters in the order, when b2 is smaller than b1 or viceversa - byte[] xor_digest = new BigInteger(1, b1).xor(new BigInteger(1, b2)).toByteArray(); - return ConfigHelper.trim(xor_digest); - } - - /** - * @returns The exponential residue (parameter A) to be sent to the server. - */ - public byte[] exponential() { - byte[] Abytes = null; - if(A == null) { + byte[] x_bytes = calculatePasswordHash(username, password, ConfigHelper.trim(salt)); + x = new BigInteger(1, x_bytes); + BigInteger v = g.modPow(x, N); // g^x % N + return v; + } + + /** + * Calculates the trimmed xor from two BigInteger numbers + * + * @param b1 the positive source to build first BigInteger + * @param b2 the positive source to build second BigInteger + * @param length + * @return + */ + public byte[] xor(byte[] b1, byte[] b2) { + //TODO Check if length matters in the order, when b2 is smaller than b1 or viceversa + byte[] xor_digest = new BigInteger(1, b1).xor(new BigInteger(1, b2)).toByteArray(); + return ConfigHelper.trim(xor_digest); + } + + /** + * @returns The exponential residue (parameter A) to be sent to the server. + */ + public byte[] exponential() { + byte[] Abytes = null; + if (A == null) { /* If the random component of A has not been specified use a random number */ - if( a == null ) { - BigInteger one = BigInteger.ONE; - do { - a = new BigInteger(A_LEN, pseudoRng); - } while(a.compareTo(one) <= 0); - } - A = g.modPow(a, N); - Abytes = ConfigHelper.trim(A.toByteArray()); - } - return Abytes; - } - - /** - * Calculates the parameter M1, to be sent to the SRP server. - * It also updates hashes of client and server for further calculations in other methods. - * It uses a predefined k. - * @param salt_bytes - * @param Bbytes the parameter received from the server, in bytes - * @return the parameter M1 - * @throws NoSuchAlgorithmException - */ + if (a == null) { + BigInteger one = BigInteger.ONE; + do { + a = new BigInteger(A_LEN, pseudoRng); + } while (a.compareTo(one) <= 0); + } + A = g.modPow(a, N); + Abytes = ConfigHelper.trim(A.toByteArray()); + } + return Abytes; + } + + /** + * Calculates the parameter M1, to be sent to the SRP server. + * It also updates hashes of client and server for further calculations in other methods. + * It uses a predefined k. + * + * @param salt_bytes + * @param Bbytes the parameter received from the server, in bytes + * @return the parameter M1 + * @throws NoSuchAlgorithmException + */ public byte[] response(byte[] salt_bytes, byte[] Bbytes) { - // Calculate x = H(s | H(U | ':' | password)) - byte[] M1 = null; - if(new BigInteger(1, Bbytes).mod(new BigInteger(1, N_bytes)) != BigInteger.ZERO) { - this.v = calculateV(username, password, salt_bytes); - // H(N) - byte[] digest_of_n = newDigest().digest(N_bytes); - - // H(g) - byte[] digest_of_g = newDigest().digest(params.g); - - // clientHash = H(N) xor H(g) - byte[] xor_digest = xor(digest_of_n, digest_of_g); - clientHash.update(xor_digest); - - // clientHash = H(N) xor H(g) | H(U) - byte[] username_digest = newDigest().digest(ConfigHelper.trim(username.getBytes())); - username_digest = ConfigHelper.trim(username_digest); - clientHash.update(username_digest); - - // clientHash = H(N) xor H(g) | H(U) | s - clientHash.update(ConfigHelper.trim(salt_bytes)); - - K = null; - - // clientHash = H(N) xor H(g) | H(U) | A - byte[] Abytes = ConfigHelper.trim(A.toByteArray()); - clientHash.update(Abytes); - - // clientHash = H(N) xor H(g) | H(U) | s | A | B - Bbytes = ConfigHelper.trim(Bbytes); - clientHash.update(Bbytes); - - // Calculate S = (B - kg^x) ^ (a + u * x) % N - BigInteger S = calculateS(Bbytes); - byte[] S_bytes = ConfigHelper.trim(S.toByteArray()); - - // K = SessionHash(S) - MessageDigest sessionDigest = newDigest(); - K = ConfigHelper.trim(sessionDigest.digest(S_bytes)); - - // clientHash = H(N) xor H(g) | H(U) | A | B | K - clientHash.update(K); - - M1 = ConfigHelper.trim(clientHash.digest()); - - // serverHash = Astr + M + K - serverHash.update(Abytes); - serverHash.update(M1); - serverHash.update(K); - - } - return M1; - } - - /** - * It calculates the parameter S used by response() to obtain session hash K. - * @param Bbytes the parameter received from the server, in bytes - * @return the parameter S - */ - private BigInteger calculateS(byte[] Bbytes) { - byte[] Abytes = ConfigHelper.trim(A.toByteArray()); - Bbytes = ConfigHelper.trim(Bbytes); - byte[] u_bytes = getU(Abytes, Bbytes); - - BigInteger B = new BigInteger(1, Bbytes); - BigInteger u = new BigInteger(1, u_bytes); - String k_string = "bf66c44a428916cad64aa7c679f3fd897ad4c375e9bbb4cbf2f5de241d618ef0"; - BigInteger k = new BigInteger(k_string, 16); - BigInteger B_minus_v = B.subtract(k.multiply(v)); - BigInteger a_ux = a.add(u.multiply(x)); - BigInteger S = B_minus_v.modPow(a_ux, N); - return S; - } - - /** - * It calculates the parameter u used by calculateS to obtain S. - * @param Abytes the exponential residue sent to the server - * @param Bbytes the parameter received from the server, in bytes - * @return - */ - public byte[] getU(byte[] Abytes, byte[] Bbytes) { - MessageDigest u_digest = newDigest(); - u_digest.update(ConfigHelper.trim(Abytes)); - u_digest.update(ConfigHelper.trim(Bbytes)); - byte[] u_digest_bytes = u_digest.digest(); - return ConfigHelper.trim(new BigInteger(1, u_digest_bytes).toByteArray()); - } - - /** - * @param M2 The server's response to the client's challenge - * @returns True if and only if the server's response was correct. - */ - public boolean verify(byte[] M2) - { - // M2 = H(A | M1 | K) - M2 = ConfigHelper.trim(M2); - byte[] myM2 = ConfigHelper.trim(serverHash.digest()); - boolean valid = Arrays.equals(M2, myM2); - return valid; - } - - protected static void setToken(String token) { - LeapSRPSession.token = token; - } - - protected static String getToken() { - return token; - } + // Calculate x = H(s | H(U | ':' | password)) + byte[] M1 = null; + if (new BigInteger(1, Bbytes).mod(new BigInteger(1, N_bytes)) != BigInteger.ZERO) { + this.v = calculateV(username, password, salt_bytes); + // H(N) + byte[] digest_of_n = newDigest().digest(N_bytes); + + // H(g) + byte[] digest_of_g = newDigest().digest(params.g); + + // clientHash = H(N) xor H(g) + byte[] xor_digest = xor(digest_of_n, digest_of_g); + clientHash.update(xor_digest); + + // clientHash = H(N) xor H(g) | H(U) + byte[] username_digest = newDigest().digest(ConfigHelper.trim(username.getBytes())); + username_digest = ConfigHelper.trim(username_digest); + clientHash.update(username_digest); + + // clientHash = H(N) xor H(g) | H(U) | s + clientHash.update(ConfigHelper.trim(salt_bytes)); + + K = null; + + // clientHash = H(N) xor H(g) | H(U) | A + byte[] Abytes = ConfigHelper.trim(A.toByteArray()); + clientHash.update(Abytes); + + // clientHash = H(N) xor H(g) | H(U) | s | A | B + Bbytes = ConfigHelper.trim(Bbytes); + clientHash.update(Bbytes); + + // Calculate S = (B - kg^x) ^ (a + u * x) % N + BigInteger S = calculateS(Bbytes); + byte[] S_bytes = ConfigHelper.trim(S.toByteArray()); + + // K = SessionHash(S) + MessageDigest sessionDigest = newDigest(); + K = ConfigHelper.trim(sessionDigest.digest(S_bytes)); + + // clientHash = H(N) xor H(g) | H(U) | A | B | K + clientHash.update(K); + + M1 = ConfigHelper.trim(clientHash.digest()); + + // serverHash = Astr + M + K + serverHash.update(Abytes); + serverHash.update(M1); + serverHash.update(K); + + } + return M1; + } + + /** + * It calculates the parameter S used by response() to obtain session hash K. + * + * @param Bbytes the parameter received from the server, in bytes + * @return the parameter S + */ + private BigInteger calculateS(byte[] Bbytes) { + byte[] Abytes = ConfigHelper.trim(A.toByteArray()); + Bbytes = ConfigHelper.trim(Bbytes); + byte[] u_bytes = getU(Abytes, Bbytes); + + BigInteger B = new BigInteger(1, Bbytes); + BigInteger u = new BigInteger(1, u_bytes); + String k_string = "bf66c44a428916cad64aa7c679f3fd897ad4c375e9bbb4cbf2f5de241d618ef0"; + BigInteger k = new BigInteger(k_string, 16); + BigInteger B_minus_v = B.subtract(k.multiply(v)); + BigInteger a_ux = a.add(u.multiply(x)); + BigInteger S = B_minus_v.modPow(a_ux, N); + return S; + } + + /** + * It calculates the parameter u used by calculateS to obtain S. + * + * @param Abytes the exponential residue sent to the server + * @param Bbytes the parameter received from the server, in bytes + * @return + */ + public byte[] getU(byte[] Abytes, byte[] Bbytes) { + MessageDigest u_digest = newDigest(); + u_digest.update(ConfigHelper.trim(Abytes)); + u_digest.update(ConfigHelper.trim(Bbytes)); + byte[] u_digest_bytes = u_digest.digest(); + return ConfigHelper.trim(new BigInteger(1, u_digest_bytes).toByteArray()); + } + + /** + * @param M2 The server's response to the client's challenge + * @returns True if and only if the server's response was correct. + */ + public boolean verify(byte[] M2) { + // M2 = H(A | M1 | K) + M2 = ConfigHelper.trim(M2); + byte[] myM2 = ConfigHelper.trim(serverHash.digest()); + boolean valid = Arrays.equals(M2, myM2); + return valid; + } + + protected static void setToken(String token) { + LeapSRPSession.token = token; + } + + protected static String getToken() { + return token; + } protected static boolean loggedIn() { - return !token.isEmpty(); + return !token.isEmpty(); } - /** - * @return a new SHA-256 digest. - */ - public MessageDigest newDigest() - { - MessageDigest md = null; - try { - md = MessageDigest.getInstance("SHA-256"); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - return md; - } + /** + * @return a new SHA-256 digest. + */ + public MessageDigest newDigest() { + MessageDigest md = null; + try { + md = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return md; + } public byte[] getK() { - return K; + return K; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java index 806d5602..073d727f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java +++ b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java @@ -8,21 +8,21 @@ import se.leap.bitmaskclient.eip.*; public class OnBootReceiver extends BroadcastReceiver { SharedPreferences preferences; - + // Debug: am broadcast -a android.intent.action.BOOT_COMPLETED @Override public void onReceive(Context context, Intent intent) { - preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE); - boolean provider_configured = !preferences.getString(Provider.KEY, "").isEmpty(); - boolean start_on_boot = preferences.getBoolean(Dashboard.START_ON_BOOT, false); + preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE); + boolean provider_configured = !preferences.getString(Provider.KEY, "").isEmpty(); + boolean start_on_boot = preferences.getBoolean(Dashboard.START_ON_BOOT, false); Log.d("OnBootReceiver", "Provider configured " + String.valueOf(provider_configured)); Log.d("OnBootReceiver", "Start on boot " + String.valueOf(start_on_boot)); - if(provider_configured && start_on_boot) { - Intent dashboard_intent = new Intent(context, Dashboard.class); - dashboard_intent.setAction(Constants.ACTION_START_EIP); - dashboard_intent.putExtra(Dashboard.ON_BOOT, true); - dashboard_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(dashboard_intent); - } + if (provider_configured && start_on_boot) { + Intent dashboard_intent = new Intent(context, Dashboard.class); + dashboard_intent.setAction(Constants.ACTION_START_EIP); + dashboard_intent.putExtra(Dashboard.ON_BOOT, true); + dashboard_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(dashboard_intent); + } } } diff --git a/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java b/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java index 33d2971d..9e523751 100644 --- a/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java +++ b/app/src/main/java/se/leap/bitmaskclient/PRNGFixes.java @@ -22,7 +22,7 @@ import java.security.Provider; /** * Fixes for the output of the default PRNG having low entropy. - * + *

* The fixes need to be applied via {@link #apply()} before any use of Java * Cryptography Architecture primitives. A good place to invoke them is in the * application's {@code onCreate}. @@ -32,10 +32,13 @@ public final class PRNGFixes { private static final int VERSION_CODE_JELLY_BEAN = 16; private static final int VERSION_CODE_JELLY_BEAN_MR2 = 18; private static final byte[] BUILD_FINGERPRINT_AND_DEVICE_SERIAL = - getBuildFingerprintAndDeviceSerial(); + getBuildFingerprintAndDeviceSerial(); - /** Hidden constructor to prevent instantiation. */ - private PRNGFixes() {} + /** + * Hidden constructor to prevent instantiation. + */ + private PRNGFixes() { + } /** * Applies all fixes. @@ -102,7 +105,7 @@ public final class PRNGFixes { if ((secureRandomProviders == null) || (secureRandomProviders.length < 1) || (!LinuxPRNGSecureRandomProvider.class.equals( - secureRandomProviders[0].getClass()))) { + secureRandomProviders[0].getClass()))) { Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1); } @@ -127,7 +130,7 @@ public final class PRNGFixes { rng2.getProvider().getClass())) { throw new SecurityException( "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong" - + " Provider: " + rng2.getProvider().getClass()); + + " Provider: " + rng2.getProvider().getClass()); } } @@ -141,7 +144,7 @@ public final class PRNGFixes { super("LinuxPRNG", 1.0, "A Linux-specific random number provider that uses" - + " /dev/urandom"); + + " /dev/urandom"); // Although /dev/urandom is not a SHA-1 PRNG, some apps // explicitly request a SHA1PRNG SecureRandom and we thus need to // prevent them from getting the default implementation whose output diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index f45eccdd..ee06a586 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -27,46 +27,45 @@ import java.util.*; /** * @author Sean Leonard * @author Parménides GV - * */ public final class Provider implements Parcelable { - private JSONObject definition; // Represents our Provider's provider.json + private JSONObject definition; // Represents our Provider's provider.json private URL main_url; final public static String - API_URL = "api_uri", - API_VERSION = "api_version", - ALLOW_REGISTRATION = "allow_registration", - API_RETURN_SERIAL = "serial", - SERVICE = "service", - KEY = "provider", - CA_CERT = "ca_cert", - CA_CERT_URI = "ca_cert_uri", - CA_CERT_FINGERPRINT = "ca_cert_fingerprint", - NAME = "name", - DESCRIPTION = "description", - DOMAIN = "domain", - MAIN_URL = "main_url", - DOT_JSON_URL = "provider_json_url" - ; - - // Array of what API versions we understand - protected static final String[] API_VERSIONS = {"1"}; // I assume we might encounter arbitrary version "numbers" - // Some API pieces we want to know about - private static final String API_TERM_SERVICES = "services"; - private static final String API_TERM_NAME = "name"; - private static final String API_TERM_DOMAIN = "domain"; - private static final String API_TERM_DEFAULT_LANGUAGE = "default_language"; - protected static final String[] API_EIP_TYPES = {"openvpn"}; - - public Provider(URL main_url) { + API_URL = "api_uri", + API_VERSION = "api_version", + ALLOW_REGISTRATION = "allow_registration", + API_RETURN_SERIAL = "serial", + SERVICE = "service", + KEY = "provider", + CA_CERT = "ca_cert", + CA_CERT_URI = "ca_cert_uri", + CA_CERT_FINGERPRINT = "ca_cert_fingerprint", + NAME = "name", + DESCRIPTION = "description", + DOMAIN = "domain", + MAIN_URL = "main_url", + DOT_JSON_URL = "provider_json_url"; + + // Array of what API versions we understand + protected static final String[] API_VERSIONS = {"1"}; // I assume we might encounter arbitrary version "numbers" + // Some API pieces we want to know about + private static final String API_TERM_SERVICES = "services"; + private static final String API_TERM_NAME = "name"; + private static final String API_TERM_DOMAIN = "domain"; + private static final String API_TERM_DEFAULT_LANGUAGE = "default_language"; + protected static final String[] API_EIP_TYPES = {"openvpn"}; + + public Provider(URL main_url) { this.main_url = main_url; } public Provider(File provider_file) { } + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public Provider createFromParcel(Parcel in) { @@ -82,7 +81,7 @@ public final class Provider implements Parcelable { try { main_url = new URL(in.readString()); String definition_string = in.readString(); - if(definition_string != null) + if (definition_string != null) definition = new JSONObject((definition_string)); } catch (MalformedURLException e) { e.printStackTrace(); @@ -95,60 +94,62 @@ public final class Provider implements Parcelable { definition = provider_json; } - protected JSONObject definition() { return definition; } + protected JSONObject definition() { + return definition; + } - protected String getDomain(){ - return main_url.getHost(); - } + protected String getDomain() { + return main_url.getHost(); + } protected URL mainUrl() { return main_url; } - - protected String getName(){ - // Should we pass the locale in, or query the system here? - String lang = Locale.getDefault().getLanguage(); - String name = ""; - try { - if(definition != null) - name = definition.getJSONObject(API_TERM_NAME).getString(lang); + + protected String getName() { + // Should we pass the locale in, or query the system here? + String lang = Locale.getDefault().getLanguage(); + String name = ""; + try { + if (definition != null) + name = definition.getJSONObject(API_TERM_NAME).getString(lang); else throw new JSONException("Provider not defined"); - } catch (JSONException e) { - if(main_url != null) { + } catch (JSONException e) { + if (main_url != null) { String host = main_url.getHost(); name = host.substring(0, host.indexOf(".")); } - } - - return name; - } - - protected String getDescription(){ - String lang = Locale.getDefault().getLanguage(); - String desc = null; - try { - desc = definition.getJSONObject("description").getString(lang); - } catch (JSONException e) { - // TODO: handle exception!! - try { - desc = definition.getJSONObject("description").getString( definition.getString("default_language") ); - } catch (JSONException e2) { - // TODO: i can't believe you're doing it again! - } - } - - return desc; - } - - protected boolean hasEIP() { - try { + } + + return name; + } + + protected String getDescription() { + String lang = Locale.getDefault().getLanguage(); + String desc = null; + try { + desc = definition.getJSONObject("description").getString(lang); + } catch (JSONException e) { + // TODO: handle exception!! + try { + desc = definition.getJSONObject("description").getString(definition.getString("default_language")); + } catch (JSONException e2) { + // TODO: i can't believe you're doing it again! + } + } + + return desc; + } + + protected boolean hasEIP() { + try { JSONArray services = definition.getJSONArray(API_TERM_SERVICES); // returns ["openvpn"] - for (int i=0;i { - private static boolean[] hidden = null; - - public void hide(int position) { - hidden[getRealPosition(position)] = true; - notifyDataSetChanged(); - notifyDataSetInvalidated(); - } - - public void unHide(int position) { - hidden[getRealPosition(position)] = false; - notifyDataSetChanged(); - notifyDataSetInvalidated(); - } + private static boolean[] hidden = null; + + public void hide(int position) { + hidden[getRealPosition(position)] = true; + notifyDataSetChanged(); + notifyDataSetInvalidated(); + } + + public void unHide(int position) { + hidden[getRealPosition(position)] = false; + notifyDataSetChanged(); + notifyDataSetInvalidated(); + } public void showAllProviders() { - for(int i = 0; i < hidden.length; i++) + for (int i = 0; i < hidden.length; i++) hidden[i] = false; notifyDataSetChanged(); notifyDataSetInvalidated(); } - + public void hideAllBut(int position) { - for (int i = 0; i < hidden.length; i++) { + for (int i = 0; i < hidden.length; i++) { if (i != position) hidden[i] = true; else @@ -38,64 +38,66 @@ public class ProviderListAdapter extends RendererAdapter { notifyDataSetChanged(); notifyDataSetInvalidated(); } - - private int getRealPosition(int position) { - int hElements = getHiddenCountUpTo(position); - int diff = 0; - for(int i=0;i collection) { - super(layoutInflater, rendererBuilder, collection); - hidden = new boolean[collection.size()]; - for (int i = 0; i < collection.size(); i++) - hidden[i] = false; + super(layoutInflater, rendererBuilder, collection); + hidden = new boolean[collection.size()]; + for (int i = 0; i < collection.size(); i++) + hidden[i] = false; } - - @Override - public void add(Provider item) { - super.add(item); - if(getCollection().size() > hidden.length) { + + @Override + public void add(Provider item) { + super.add(item); + if (getCollection().size() > hidden.length) { boolean[] new_hidden = new boolean[hidden.length + 1]; System.arraycopy(hidden, 0, new_hidden, 0, hidden.length); new_hidden[hidden.length] = false; hidden = new_hidden; } - } - - @Override - public void remove(Provider item) { - super.remove(item); - boolean[] new_hidden = new boolean[hidden.length-1]; - System.arraycopy(hidden, 0, new_hidden, 0, hidden.length-1); - hidden = new_hidden; - } + } + + @Override + public void remove(Provider item) { + super.remove(item); + boolean[] new_hidden = new boolean[hidden.length - 1]; + System.arraycopy(hidden, 0, new_hidden, 0, hidden.length - 1); + hidden = new_hidden; + } protected int indexOf(Provider item) { int index = 0; diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java index 56b38a29..40fe8b5a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -1,7 +1,9 @@ package se.leap.bitmaskclient; import android.content.res.*; + import com.pedrogomez.renderers.*; + import org.json.*; import java.io.*; @@ -23,7 +25,7 @@ public class ProviderManager implements AdapteeCollection { final protected static String URLS = "urls"; public static ProviderManager getInstance(AssetManager assets_manager, File external_files_dir) { - if(instance == null) + if (instance == null) instance = new ProviderManager(assets_manager); instance.addCustomProviders(external_files_dir); @@ -46,10 +48,10 @@ public class ProviderManager implements AdapteeCollection { private Set providersFromAssets(String directory, String[] relative_file_paths) { Set providers = new HashSet(); try { - for(String file : relative_file_paths) { - String main_url = extractMainUrlFromInputStream(assets_manager.open(directory + "/" + file)); + for (String file : relative_file_paths) { + String main_url = extractMainUrlFromInputStream(assets_manager.open(directory + "/" + file)); providers.add(new Provider(new URL(main_url))); - } + } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { @@ -69,7 +71,7 @@ public class ProviderManager implements AdapteeCollection { private Set providersFromFiles(String[] files) { Set providers = new HashSet(); try { - for(String file : files) { + for (String file : files) { String main_url = extractMainUrlFromInputStream(new FileInputStream(external_files_dir.getAbsolutePath() + "/" + file)); providers.add(new Provider(new URL(main_url))); } @@ -87,7 +89,7 @@ public class ProviderManager implements AdapteeCollection { byte[] bytes = new byte[0]; try { bytes = new byte[input_stream_file_contents.available()]; - if(input_stream_file_contents.read(bytes) > 0) { + if (input_stream_file_contents.read(bytes) > 0) { JSONObject file_contents = new JSONObject(new String(bytes)); main_url = file_contents.getString(Provider.MAIN_URL); } @@ -114,7 +116,7 @@ public class ProviderManager implements AdapteeCollection { @Override public Provider get(int index) { Iterator iterator = providers().iterator(); - while(iterator.hasNext() && index > 0) { + while (iterator.hasNext() && index > 0) { iterator.next(); index--; } @@ -123,7 +125,7 @@ public class ProviderManager implements AdapteeCollection { @Override public void add(Provider element) { - if(!default_providers.contains(element)) + if (!default_providers.contains(element)) custom_providers.add(element); } @@ -150,17 +152,17 @@ public class ProviderManager implements AdapteeCollection { } protected void saveCustomProvidersToFile() { - try { - for (Provider provider : custom_providers) { + try { + for (Provider provider : custom_providers) { File provider_file = new File(external_files_dir, provider.getName() + ".json"); - if(!provider_file.exists()) { - FileWriter writer = new FileWriter(provider_file); - writer.write(provider.toJson().toString()); - writer.close(); + if (!provider_file.exists()) { + FileWriter writer = new FileWriter(provider_file); + writer.write(provider.toJson().toString()); + writer.close(); + } } - } - } catch (IOException e) { - e.printStackTrace(); - } + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java index 1b42e349..5a6e857d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderRendererBuilder.java @@ -1,17 +1,19 @@ package se.leap.bitmaskclient; import com.pedrogomez.renderers.*; + import java.util.*; /** * Created by parmegv on 4/12/14. */ - public class ProviderRendererBuilder extends RendererBuilder { - public ProviderRendererBuilder(Collection> prototypes) { - super(prototypes); - } - @Override - protected Class getPrototypeClass(Provider content) { - return ProviderRenderer.class; +public class ProviderRendererBuilder extends RendererBuilder { + public ProviderRendererBuilder(Collection> prototypes) { + super(prototypes); + } + + @Override + protected Class getPrototypeClass(Provider content) { + return ProviderRenderer.class; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java index 6346fc36..e92c6b7b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/SessionDialog.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +package se.leap.bitmaskclient; import android.app.*; import android.content.*; @@ -26,21 +26,21 @@ import butterknife.*; /** * Implements the log in dialog, currently without progress dialog. - * + *

* It returns to the previous fragment when finished, and sends username and password to the authenticate method. - * - * It also notifies the user if the password is not valid. - * - * @author parmegv + *

+ * It also notifies the user if the password is not valid. * + * @author parmegv */ -public class SessionDialog extends DialogFragment{ +public class SessionDialog extends DialogFragment { + - final public static String TAG = SessionDialog.class.getSimpleName(); final public static String USERNAME = "username"; final public static String PASSWORD = "password"; + public static enum ERRORS { USERNAME_MISSING, PASSWORD_INVALID_LENGTH, @@ -59,66 +59,67 @@ public class SessionDialog extends DialogFragment{ public SessionDialog() { setArguments(Bundle.EMPTY); } - - public AlertDialog onCreateDialog(Bundle savedInstanceState) { - - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - LayoutInflater inflater = getActivity().getLayoutInflater(); - View view = inflater.inflate(R.layout.session_dialog, null); - ButterKnife.inject(this, view); - - Bundle arguments = getArguments(); - if (arguments != Bundle.EMPTY) { + + public AlertDialog onCreateDialog(Bundle savedInstanceState) { + + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.session_dialog, null); + ButterKnife.inject(this, view); + + Bundle arguments = getArguments(); + if (arguments != Bundle.EMPTY) { setUp(arguments); - } - - builder.setView(view) - .setPositiveButton(R.string.login_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - String username = getEnteredUsername(); - String password = getEnteredPassword(); - dialog.dismiss(); - interface_with_Dashboard.logIn(username, password); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - interface_with_Dashboard.cancelLoginOrSignup(); - } - }) - .setNeutralButton(R.string.signup_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - String username = getEnteredUsername(); - String password = getEnteredPassword(); - dialog.dismiss(); - interface_with_Dashboard.signUp(username, password); - } - }); - - return builder.create(); - } + } + + builder.setView(view) + .setPositiveButton(R.string.login_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + String username = getEnteredUsername(); + String password = getEnteredPassword(); + dialog.dismiss(); + interface_with_Dashboard.logIn(username, password); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + interface_with_Dashboard.cancelLoginOrSignup(); + } + }) + .setNeutralButton(R.string.signup_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + String username = getEnteredUsername(); + String password = getEnteredPassword(); + dialog.dismiss(); + interface_with_Dashboard.signUp(username, password); + } + }); + + return builder.create(); + } private void setUp(Bundle arguments) { is_eip_pending = arguments.getBoolean(EipFragment.IS_PENDING, false); if (arguments.containsKey(ERRORS.PASSWORD_INVALID_LENGTH.toString())) password_field.setError(getString(R.string.error_not_valid_password_user_message)); - else if(arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { + else if (arguments.containsKey(ERRORS.RISEUP_WARNING.toString())) { user_message.setVisibility(TextView.VISIBLE); user_message.setText(R.string.login_riseup_warning); - } if (arguments.containsKey(USERNAME)) { + } + if (arguments.containsKey(USERNAME)) { String username = arguments.getString(USERNAME); username_field.setText(username); } if (arguments.containsKey(ERRORS.USERNAME_MISSING.toString())) { username_field.setError(getString(R.string.username_ask)); } - if(arguments.containsKey(getString(R.string.user_message))) + if (arguments.containsKey(getString(R.string.user_message))) user_message.setText(arguments.getString(getString(R.string.user_message))); - else if(user_message.getVisibility() != TextView.VISIBLE) + else if (user_message.getVisibility() != TextView.VISIBLE) user_message.setVisibility(View.GONE); - if(!username_field.getText().toString().isEmpty() && password_field.isFocusable()) + if (!username_field.getText().toString().isEmpty() && password_field.isFocusable()) password_field.requestFocus(); } @@ -131,26 +132,27 @@ public class SessionDialog extends DialogFragment{ return password_field.getText().toString(); } - - /** - * Interface used to communicate SessionDialog with Dashboard. - * - * @author parmegv - * - */ - public interface SessionDialogInterface { - public void logIn(String username, String password); - public void signUp(String username, String password); - public void cancelLoginOrSignup(); + + /** + * Interface used to communicate SessionDialog with Dashboard. + * + * @author parmegv + */ + public interface SessionDialogInterface { + public void logIn(String username, String password); + + public void signUp(String username, String password); + + public void cancelLoginOrSignup(); } - SessionDialogInterface interface_with_Dashboard; - + SessionDialogInterface interface_with_Dashboard; + @Override public void onAttach(Activity activity) { super.onAttach(activity); try { - interface_with_Dashboard = (SessionDialogInterface) activity; + interface_with_Dashboard = (SessionDialogInterface) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement LogInDialogListener"); @@ -159,8 +161,8 @@ public class SessionDialog extends DialogFragment{ @Override public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - if(is_eip_pending) - interface_with_Dashboard.cancelLoginOrSignup(); + super.onCancel(dialog); + if (is_eip_pending) + interface_with_Dashboard.cancelLoginOrSignup(); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/User.java b/app/src/main/java/se/leap/bitmaskclient/User.java index 6e20c165..4bbd9a91 100644 --- a/app/src/main/java/se/leap/bitmaskclient/User.java +++ b/app/src/main/java/se/leap/bitmaskclient/User.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -19,21 +19,24 @@ package se.leap.bitmaskclient; public class User { private static String user_name = "You"; private static User user; - + public static User getInstance() { - if(user == null) { - user = new User(); - } - return user; + if (user == null) { + user = new User(); + } + return user; } public static void setUserName(String user_name) { - User.user_name = user_name; + User.user_name = user_name; } - private User() { } - - public static String userName() { return user_name; } + private User() { + } + + public static String userName() { + return user_name; + } public static boolean loggedIn() { return LeapSRPSession.loggedIn(); diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java index db432a82..369df1a3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -25,47 +25,50 @@ public class UserSessionStatus extends Observable { private static UserSessionStatus current_status; public enum SessionStatus { - LOGGED_IN, - LOGGED_OUT, - NOT_LOGGED_IN, - DIDNT_LOG_OUT, - LOGGING_IN, - LOGGING_OUT, + LOGGED_IN, + LOGGED_OUT, + NOT_LOGGED_IN, + DIDNT_LOG_OUT, + LOGGING_IN, + LOGGING_OUT, SIGNING_UP } private static SessionStatus session_status = SessionStatus.NOT_LOGGED_IN; - + public static UserSessionStatus getInstance() { - if(current_status == null) { - current_status = new UserSessionStatus(); - } - return current_status; + if (current_status == null) { + current_status = new UserSessionStatus(); + } + return current_status; } - private UserSessionStatus() { } + private UserSessionStatus() { + } private void sessionStatus(SessionStatus session_status) { - this.session_status = session_status; + this.session_status = session_status; } - public SessionStatus sessionStatus() { return session_status; } + public SessionStatus sessionStatus() { + return session_status; + } public boolean inProgress() { return session_status == SessionStatus.LOGGING_IN || session_status == SessionStatus.LOGGING_OUT; } - + public static void updateStatus(SessionStatus session_status) { - current_status = getInstance(); - current_status.sessionStatus(session_status); - current_status.setChanged(); - current_status.notifyObservers(); + current_status = getInstance(); + current_status.sessionStatus(session_status); + current_status.setChanged(); + current_status.notifyObservers(); } @Override public String toString() { - return User.userName() + " is " - + session_status.toString().toLowerCase().replaceAll("_", " "); + return User.userName() + " is " + + session_status.toString().toLowerCase().replaceAll("_", " "); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java index f7206927..39ad7c08 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -17,16 +17,14 @@ package se.leap.bitmaskclient.eip; /** - * * Constants for intent passing, shared preferences * * @author Parménides GV - * */ public interface Constants { public final static String TAG = Constants.class.getSimpleName(); - + public final static String ACTION_CHECK_CERT_VALIDITY = TAG + ".CHECK_CERT_VALIDITY"; public final static String ACTION_START_EIP = TAG + ".START_EIP"; public final static String ACTION_STOP_EIP = TAG + ".STOP_EIP"; 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 6335f818..beed7948 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -33,7 +33,7 @@ import static se.leap.bitmaskclient.eip.Constants.*; * this IntentService. * Contains logic for parsing eip-service.json from the provider, configuring and selecting * gateways, and controlling {@link de.blinkt.openvpn.core.OpenVPNService} connections. - * + * * @author Sean Leonard * @author Parménides GV */ @@ -43,64 +43,64 @@ public final class EIP extends IntentService { public final static String SERVICE_API_PATH = "config/eip-service.json"; public static final int DISCONNECT = 15; - + private static Context context; private static ResultReceiver mReceiver; private static SharedPreferences preferences; - + private static JSONObject eip_definition; 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(); - - context = getApplicationContext(); + super.onCreate(); + + context = getApplicationContext(); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); - eip_definition = eipDefinitionFromPreferences(); - if(gateways_manager.isEmpty()) + eip_definition = eipDefinitionFromPreferences(); + if (gateways_manager.isEmpty()) gatewaysFromPreferences(); } @Override protected void onHandleIntent(Intent intent) { - String action = intent.getAction(); - mReceiver = intent.getParcelableExtra(RECEIVER_TAG); - - if ( action.equals(ACTION_START_EIP)) - startEIP(); - else if (action.equals(ACTION_STOP_EIP)) - stopEIP(); - else if (action.equals(ACTION_IS_EIP_RUNNING)) - isRunning(); + String action = intent.getAction(); + mReceiver = intent.getParcelableExtra(RECEIVER_TAG); + + if (action.equals(ACTION_START_EIP)) + startEIP(); + else if (action.equals(ACTION_STOP_EIP)) + stopEIP(); + else if (action.equals(ACTION_IS_EIP_RUNNING)) + isRunning(); else if (action.equals(ACTION_UPDATE_EIP_SERVICE)) - updateEIPService(); - else if (action.equals(ACTION_CHECK_CERT_VALIDITY)) - checkCertValidity(); + updateEIPService(); + else if (action.equals(ACTION_CHECK_CERT_VALIDITY)) + checkCertValidity(); } - + /** * Initiates an EIP connection by selecting a gateway and preparing and sending an * Intent to {@link de.blinkt.openvpn.LaunchVPN}. * It also sets up early routes. */ private void startEIP() { - if(gateways_manager.isEmpty()) - updateEIPService(); + if (gateways_manager.isEmpty()) + updateEIPService(); earlyRoutes(); - gateway = gateways_manager.select(); - if(gateway != null && gateway.getProfile() != null) { - mReceiver = EipFragment.getReceiver(); - launchActiveGateway(); - tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK); - } else - tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED); + gateway = gateways_manager.select(); + if (gateway != null && gateway.getProfile() != null) { + mReceiver = EipFragment.getReceiver(); + launchActiveGateway(); + tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK); + } else + tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED); } /** @@ -108,40 +108,40 @@ public final class EIP extends IntentService { * VpnService is started properly. */ private void earlyRoutes() { - Intent void_vpn_launcher = new Intent(context, VoidVpnLauncher.class); - void_vpn_launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(void_vpn_launcher); + Intent void_vpn_launcher = new Intent(context, VoidVpnLauncher.class); + void_vpn_launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(void_vpn_launcher); } - + private void launchActiveGateway() { - Intent intent = new Intent(this,LaunchVPN.class); - intent.setAction(Intent.ACTION_MAIN); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(LaunchVPN.EXTRA_NAME, gateway.getProfile().getName()); - intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); - startActivity(intent); + Intent intent = new Intent(this, LaunchVPN.class); + intent.setAction(Intent.ACTION_MAIN); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(LaunchVPN.EXTRA_NAME, gateway.getProfile().getName()); + intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); + startActivity(intent); } private void stopEIP() { - EipStatus eip_status = EipStatus.getInstance(); - int result_code = Activity.RESULT_CANCELED; - if(eip_status.isConnected() || eip_status.isConnecting()) - result_code = Activity.RESULT_OK; + EipStatus eip_status = EipStatus.getInstance(); + int result_code = Activity.RESULT_CANCELED; + if (eip_status.isConnected() || eip_status.isConnecting()) + result_code = Activity.RESULT_OK; - tellToReceiver(ACTION_STOP_EIP, result_code); + tellToReceiver(ACTION_STOP_EIP, result_code); } - + /** * Checks the last stored status notified by ics-openvpn * Sends Activity.RESULT_CANCELED to the ResultReceiver that made the * request if it's not connected, Activity.RESULT_OK otherwise. */ private void isRunning() { - EipStatus eip_status = EipStatus.getInstance(); - int resultCode = (eip_status.isConnected()) ? - Activity.RESULT_OK : - Activity.RESULT_CANCELED; - tellToReceiver(ACTION_IS_EIP_RUNNING, resultCode); + EipStatus eip_status = EipStatus.getInstance(); + int resultCode = (eip_status.isConnected()) ? + Activity.RESULT_OK : + Activity.RESULT_CANCELED; + tellToReceiver(ACTION_IS_EIP_RUNNING, resultCode); } /** @@ -149,27 +149,27 @@ public final class EIP extends IntentService { * TODO Implement API call to refresh eip-service.json from the provider */ private void updateEIPService() { - eip_definition = eipDefinitionFromPreferences(); - if(eip_definition.length() > 0) + eip_definition = eipDefinitionFromPreferences(); + if (eip_definition.length() > 0) updateGateways(); - tellToReceiver(ACTION_UPDATE_EIP_SERVICE, Activity.RESULT_OK); + 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()) { - result = new JSONObject(eip_definition_string); - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + try { + String eip_definition_string = preferences.getString(KEY, ""); + if (!eip_definition_string.isEmpty()) { + result = new JSONObject(eip_definition_string); + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return result; } - private void updateGateways(){ + private void updateGateways() { gateways_manager.fromEipServiceJson(eip_definition); gatewaysToPreferences(); } @@ -187,15 +187,15 @@ public final class EIP extends IntentService { } private void checkCertValidity() { - VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(CERTIFICATE, "")); - int resultCode = validator.isValid() ? - Activity.RESULT_OK : - Activity.RESULT_CANCELED; - tellToReceiver(ACTION_CHECK_CERT_VALIDITY, resultCode); + VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(CERTIFICATE, "")); + int resultCode = validator.isValid() ? + Activity.RESULT_OK : + Activity.RESULT_CANCELED; + tellToReceiver(ACTION_CHECK_CERT_VALIDITY, resultCode); } private void tellToReceiver(String action, int resultCode) { - if (mReceiver != null){ + if (mReceiver != null) { Bundle resultData = new Bundle(); resultData.putString(REQUEST_TAG, action); mReceiver.send(resultCode, resultData); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index d1ed0d27..e636c693 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -34,104 +34,105 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { private int localized_res_id; public static EipStatus getInstance() { - if(current_status == null) { - current_status = new EipStatus(); - VpnStatus.addStateListener(current_status); - } - return current_status; + if (current_status == null) { + current_status = new EipStatus(); + VpnStatus.addStateListener(current_status); + } + return current_status; } - private EipStatus() { } + private EipStatus() { + } @Override public void updateState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { - updateStatus(state, logmessage, localizedResId, level); - if(isConnected() || isDisconnected()) { - setConnectedOrDisconnected(); - } else if(isConnecting()) - setConnecting(); - Log.d(TAG, "update state with level " + level); + updateStatus(state, logmessage, localizedResId, level); + if (isConnected() || isDisconnected()) { + setConnectedOrDisconnected(); + } else if (isConnecting()) + setConnecting(); + Log.d(TAG, "update state with level " + level); } private void updateStatus(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { - current_status = getInstance(); - current_status.setState(state); - current_status.setLogMessage(logmessage); - current_status.setLocalizedResId(localizedResId); - current_status.setLevel(level); - current_status.setChanged(); + current_status = getInstance(); + current_status.setState(state); + current_status.setLogMessage(logmessage); + current_status.setLocalizedResId(localizedResId); + current_status.setLevel(level); + current_status.setChanged(); } public boolean wantsToDisconnect() { - return wants_to_disconnect; + return wants_to_disconnect; } public boolean isConnecting() { - return - !isConnected() && - !isDisconnected() && - !isPaused(); + return + !isConnected() && + !isDisconnected() && + !isPaused(); } public boolean isConnected() { - return level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED; + return level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED; } public boolean isDisconnected() { - return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; + return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; } public boolean isPaused() { - return level == VpnStatus.ConnectionStatus.LEVEL_VPNPAUSED; + return level == VpnStatus.ConnectionStatus.LEVEL_VPNPAUSED; } public void setConnecting() { - wants_to_disconnect = false; - current_status.setChanged(); - current_status.notifyObservers(); + wants_to_disconnect = false; + current_status.setChanged(); + current_status.notifyObservers(); } public void setConnectedOrDisconnected() { - Log.d(TAG, "setConnectedOrDisconnected()"); - wants_to_disconnect = false; - current_status.setChanged(); - current_status.notifyObservers(); + Log.d(TAG, "setConnectedOrDisconnected()"); + wants_to_disconnect = false; + current_status.setChanged(); + current_status.notifyObservers(); } public void setDisconnecting() { - wants_to_disconnect = false; + wants_to_disconnect = false; } public String getState() { - return state; + return state; } public String getLogMessage() { - return log_message; + return log_message; } public int getLocalizedResId() { - return localized_res_id; + return localized_res_id; } public VpnStatus.ConnectionStatus getLevel() { - return level; + return level; } private void setState(String state) { - this.state = state; + this.state = state; } private void setLogMessage(String log_message) { - this.log_message = log_message; + this.log_message = log_message; } private void setLocalizedResId(int localized_res_id) { - this.localized_res_id = localized_res_id; + this.localized_res_id = localized_res_id; } private void setLevel(VpnStatus.ConnectionStatus level) { - EipStatus.level = level; + EipStatus.level = level; } public boolean errorInLast(int lines, Context context) { @@ -141,9 +142,9 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; for (int i = 1; i <= lines && log.length > i; i++) { - message = log[log.length-i].getString(context); - for(int j = 0; j < error_keywords.length; j++) - if(message.contains(error_keywords[j])) + message = log[log.length - i].getString(context); + for (int j = 0; j < error_keywords.length; j++) + if (message.contains(error_keywords[j])) result = true; } return result; @@ -151,7 +152,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { @Override public String toString() { - return "State: " + state + " Level: " + level.toString(); + return "State: " + state + " Level: " + level.toString(); } } 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 10ddc0ef..b2c7af8c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -21,6 +21,7 @@ import android.util.*; import com.google.gson.*; import org.json.*; + import java.io.*; import de.blinkt.openvpn.*; @@ -30,12 +31,12 @@ import de.blinkt.openvpn.core.*; * Gateway provides objects defining gateways and their metadata. * Each instance contains a VpnProfile for OpenVPN specific data and member * variables describing capabilities and location (name) - * + * * @author Sean Leonard * @author Parménides GV */ public class Gateway { - + public final static String TAG = Gateway.class.getSimpleName(); private JSONObject general_configuration; @@ -45,86 +46,87 @@ public class Gateway { private String mName; private int timezone; private VpnProfile mVpnProfile; + /** * Build a gateway object from a JSON OpenVPN gateway definition in eip-service.json * and create a VpnProfile belonging to it. */ - public Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway){ + public Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway) { - this.gateway = gateway; + this.gateway = gateway; this.secrets = secrets; - general_configuration = getGeneralConfiguration(eip_definition); - timezone = getTimezone(eip_definition); - mName = locationAsName(eip_definition); + general_configuration = getGeneralConfiguration(eip_definition); + timezone = getTimezone(eip_definition); + mName = locationAsName(eip_definition); - mVpnProfile = createVPNProfile(); - mVpnProfile.mName = mName; + mVpnProfile = createVPNProfile(); + mVpnProfile.mName = mName; } private JSONObject getGeneralConfiguration(JSONObject eip_definition) { - try { - return eip_definition.getJSONObject("openvpn_configuration"); - } catch (JSONException e) { - return new JSONObject(); - } + try { + return eip_definition.getJSONObject("openvpn_configuration"); + } catch (JSONException e) { + return new JSONObject(); + } } private int getTimezone(JSONObject eip_definition) { - JSONObject location = getLocationInfo(eip_definition); - return location.optInt("timezone"); + JSONObject location = getLocationInfo(eip_definition); + return location.optInt("timezone"); } private String locationAsName(JSONObject eip_definition) { - JSONObject location = getLocationInfo(eip_definition); - return location.optString("name"); + JSONObject location = getLocationInfo(eip_definition); + return location.optString("name"); } private JSONObject getLocationInfo(JSONObject eip_definition) { - try { - JSONObject locations = eip_definition.getJSONObject("locations"); + try { + JSONObject locations = eip_definition.getJSONObject("locations"); - return locations.getJSONObject(gateway.getString("location")); - } catch (JSONException e) { - return new JSONObject(); - } + return locations.getJSONObject(gateway.getString("location")); + } catch (JSONException e) { + return new JSONObject(); + } } - + /** * Create and attach the VpnProfile to our gateway object */ - private VpnProfile createVPNProfile(){ - try { - ConfigParser cp = new ConfigParser(); - - VpnConfigGenerator vpn_configuration_generator = new VpnConfigGenerator(general_configuration, secrets, gateway); - String configuration = vpn_configuration_generator.generate(); - - cp.parseConfig(new StringReader(configuration)); - return cp.convertProfile(); - } catch (ConfigParser.ConfigParseError e) { - // FIXME We didn't get a VpnProfile! Error handling! and log level - Log.v(TAG,"Error creating VPNProfile"); - e.printStackTrace(); - return null; - } catch (IOException e) { - // FIXME We didn't get a VpnProfile! Error handling! and log level - Log.v(TAG,"Error creating VPNProfile"); - e.printStackTrace(); - return null; - } + private VpnProfile createVPNProfile() { + try { + ConfigParser cp = new ConfigParser(); + + VpnConfigGenerator vpn_configuration_generator = new VpnConfigGenerator(general_configuration, secrets, gateway); + String configuration = vpn_configuration_generator.generate(); + + cp.parseConfig(new StringReader(configuration)); + return cp.convertProfile(); + } catch (ConfigParser.ConfigParseError e) { + // FIXME We didn't get a VpnProfile! Error handling! and log level + Log.v(TAG, "Error creating VPNProfile"); + e.printStackTrace(); + return null; + } catch (IOException e) { + // FIXME We didn't get a VpnProfile! Error handling! and log level + Log.v(TAG, "Error creating VPNProfile"); + e.printStackTrace(); + return null; + } } public String getName() { - return mName; + return mName; } public VpnProfile getProfile() { - return mVpnProfile; + return mVpnProfile; } public int getTimezone() { - return timezone; + return timezone; } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java index 11823511..90c8f890 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java @@ -6,37 +6,37 @@ public class GatewaySelector { List gateways; public GatewaySelector(List gateways) { - this.gateways = gateways; + this.gateways = gateways; } public Gateway select() { - return closestGateway(); + return closestGateway(); } - + private Gateway closestGateway() { - TreeMap> offsets = calculateOffsets(); - return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next(); + TreeMap> offsets = calculateOffsets(); + return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next(); } - + private TreeMap> calculateOffsets() { - TreeMap> offsets = new TreeMap>(); - int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000; - for(Gateway gateway : gateways) { - int dist = timezoneDistance(localOffset, gateway.getTimezone()); - Set set = (offsets.get(dist) != null) ? - offsets.get(dist) : new HashSet(); - set.add(gateway); - offsets.put(dist, set); - } - return offsets; + TreeMap> offsets = new TreeMap>(); + int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000; + for (Gateway gateway : gateways) { + int dist = timezoneDistance(localOffset, gateway.getTimezone()); + Set set = (offsets.get(dist) != null) ? + offsets.get(dist) : new HashSet(); + set.add(gateway); + offsets.put(dist, set); + } + return offsets; } - + private int timezoneDistance(int local_timezone, int remote_timezone) { - // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12 - int dist = Math.abs(local_timezone - remote_timezone); - // Farther than 12 timezones and it's shorter around the "back" - if (dist > 12) - dist = 12 - (dist -12); // Well i'll be. Absolute values make equations do funny things. - return dist; + // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12 + int dist = Math.abs(local_timezone - remote_timezone); + // Farther than 12 timezones and it's shorter around the "back" + if (dist > 12) + dist = 12 - (dist - 12); // Well i'll be. Absolute values make equations do funny things. + return dist; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index af5670f5..b41fcfd4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -40,15 +40,18 @@ public class GatewaysManager { private SharedPreferences preferences; private List gateways = new ArrayList<>(); private ProfileManager profile_manager; - private Type list_type = new TypeToken>() {}.getType(); + private Type list_type = new TypeToken>() { + }.getType(); - public GatewaysManager() {} + 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(); @@ -66,13 +69,13 @@ public class GatewaysManager { List gateways_list = new ArrayList(); try { gateways_list = new Gson().fromJson(gateways, list_type); - } catch(JsonSyntaxException e) { + } catch (JsonSyntaxException e) { gateways_list.add(new Gson().fromJson(gateways, Gateway.class)); } - if(gateways_list != null) { + if (gateways_list != null) { for (Gateway gateway : gateways_list) - addGateway(gateway); + addGateway(gateway); this.gateways.addAll(gateways_list); } else Log.d("GatewaysManager", "No gateways added"); @@ -85,21 +88,21 @@ public class GatewaysManager { 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(); - } + 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) { @@ -127,10 +130,10 @@ public class GatewaysManager { boolean result = false; Collection profiles = profile_manager.getProfiles(); - for(VpnProfile aux : profiles) { + for (VpnProfile aux : profiles) { result = result || sameConnections(profile.mConnections, aux.mConnections) - && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) - && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename); + && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) + && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename); } return result; } @@ -149,9 +152,9 @@ public class GatewaysManager { private void removeDuplicatedGateway(Gateway gateway) { Iterator it = gateways.iterator(); List gateways_to_remove = new ArrayList<>(); - while(it.hasNext()) { + while (it.hasNext()) { Gateway aux = it.next(); - if(sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) { + if (sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) { gateways_to_remove.add(aux); } } @@ -160,21 +163,21 @@ public class GatewaysManager { } private void removeDuplicatedProfiles(VpnProfile original) { - Collection profiles = profile_manager.getProfiles(); + 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 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)) { + for (Connection c1_aux : c1) { + for (Connection c2_aux : c2) + if (c2_aux.mServerName.equals(c1_aux.mServerName)) { same_connections++; break; } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java index 39a802c6..5c9263b3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java @@ -11,27 +11,27 @@ public class VoidVpnLauncher extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setUp(); + super.onCreate(savedInstanceState); + setUp(); } - + public void setUp() { - Intent blocking_intent = VpnService.prepare(getApplicationContext()); // stops the VPN connection created by another application. - if(blocking_intent != null) - startActivityForResult(blocking_intent, VPN_USER_PERMISSION); - else { - onActivityResult(VPN_USER_PERMISSION, RESULT_OK, null); - } + Intent blocking_intent = VpnService.prepare(getApplicationContext()); // stops the VPN connection created by another application. + if (blocking_intent != null) + startActivityForResult(blocking_intent, VPN_USER_PERMISSION); + else { + onActivityResult(VPN_USER_PERMISSION, RESULT_OK, null); + } } - protected void onActivityResult(int requestCode, int resultCode, Intent data){ - if(requestCode == VPN_USER_PERMISSION) { - if(resultCode == RESULT_OK) { - Intent void_vpn_service = new Intent(getApplicationContext(), VoidVpnService.class); - void_vpn_service.setAction(Constants.START_BLOCKING_VPN_PROFILE); - startService(void_vpn_service); - } - } - finish(); + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == VPN_USER_PERMISSION) { + if (resultCode == RESULT_OK) { + Intent void_vpn_service = new Intent(getApplicationContext(), VoidVpnService.class); + void_vpn_service.setAction(Constants.START_BLOCKING_VPN_PROFILE); + startService(void_vpn_service); + } + } + finish(); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index e864288a..ac3d5d77 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -6,7 +6,7 @@ import android.os.*; import java.io.*; -public class VoidVpnService extends VpnService { +public class VoidVpnService extends VpnService { static final String TAG = VoidVpnService.class.getSimpleName(); static ParcelFileDescriptor fd; @@ -15,28 +15,28 @@ public class VoidVpnService extends VpnService { @Override public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent != null ? intent.getAction() : ""; - if (action == Constants.START_BLOCKING_VPN_PROFILE) { - thread = new Thread(new Runnable() { - public void run() { - Builder builder = new Builder(); - builder.setSession("Blocking until running"); - builder.addAddress("10.42.0.8",16); - builder.addRoute("0.0.0.0", 1); - builder.addRoute("192.168.1.0", 24); - builder.addDnsServer("10.42.0.1"); - try { - fd = builder.establish(); + String action = intent != null ? intent.getAction() : ""; + if (action == Constants.START_BLOCKING_VPN_PROFILE) { + thread = new Thread(new Runnable() { + public void run() { + Builder builder = new Builder(); + builder.setSession("Blocking until running"); + builder.addAddress("10.42.0.8", 16); + builder.addRoute("0.0.0.0", 1); + builder.addRoute("192.168.1.0", 24); + builder.addDnsServer("10.42.0.1"); + try { + fd = builder.establish(); - } catch (Exception e) { - e.printStackTrace(); - } - android.util.Log.d(TAG, "VoidVpnService set up: fd = " + fd.toString()); - } - }); - thread.run(); - } - return 0; + } catch (Exception e) { + e.printStackTrace(); + } + android.util.Log.d(TAG, "VoidVpnService set up: fd = " + fd.toString()); + } + }); + thread.run(); + } + return 0; } @Override @@ -46,14 +46,14 @@ public class VoidVpnService extends VpnService { } public static void stop() { - if(thread != null) + if (thread != null) thread.interrupt(); closeFd(); } private static void closeFd() { try { - if(fd != null) { + if (fd != null) { android.util.Log.d(TAG, "VoidVpnService closing fd = " + fd.toString()); fd.close(); } 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 27971c3d..025d049d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -33,32 +33,32 @@ public class VpnCertificateValidator { } public boolean isValid() { - if(!certificate.isEmpty()) { - X509Certificate certificate_x509 = ConfigHelper.parseX509CertificateFromString(certificate); - return isValid(certificate_x509); - } else return true; + if (!certificate.isEmpty()) { + X509Certificate certificate_x509 = ConfigHelper.parseX509CertificateFromString(certificate); + return isValid(certificate_x509); + } else return true; } - + private boolean isValid(X509Certificate certificate) { - Calendar offset_date = calculateOffsetCertificateValidity(certificate); - try { - Log.d(TAG, "offset_date = " + offset_date.getTime().toString()); - certificate.checkValidity(offset_date.getTime()); - return true; - } catch(CertificateExpiredException e) { - return false; - } catch(CertificateNotYetValidException e) { - return false; - } + Calendar offset_date = calculateOffsetCertificateValidity(certificate); + try { + Log.d(TAG, "offset_date = " + offset_date.getTime().toString()); + certificate.checkValidity(offset_date.getTime()); + return true; + } catch (CertificateExpiredException e) { + return false; + } catch (CertificateNotYetValidException e) { + return false; + } } private Calendar calculateOffsetCertificateValidity(X509Certificate certificate) { - Log.d(TAG, "certificate not after = " + certificate.getNotAfter()); - long preventive_time = Math.abs(certificate.getNotBefore().getTime() - certificate.getNotAfter().getTime())/2; - long current_date_millis = Calendar.getInstance().getTimeInMillis(); - - Calendar limit_date = Calendar.getInstance(); - limit_date.setTimeInMillis(current_date_millis + preventive_time); - return limit_date; + Log.d(TAG, "certificate not after = " + certificate.getNotAfter()); + long preventive_time = Math.abs(certificate.getNotBefore().getTime() - certificate.getNotAfter().getTime()) / 2; + long current_date_millis = Calendar.getInstance().getTimeInMillis(); + + Calendar limit_date = Calendar.getInstance(); + limit_date.setTimeInMillis(current_date_millis + preventive_time); + return limit_date; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 8b1bc793..0b9c5832 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -17,7 +17,9 @@ package se.leap.bitmaskclient.eip; import org.json.*; + import java.util.*; + import se.leap.bitmaskclient.*; public class VpnConfigGenerator { @@ -50,7 +52,7 @@ public class VpnConfigGenerator { String common_options = ""; try { Iterator keys = general_configuration.keys(); - while ( keys.hasNext() ){ + while (keys.hasNext()) { String key = keys.next().toString(); common_options += key + " "; @@ -83,11 +85,11 @@ public class VpnConfigGenerator { String ip_address = gateway.getString(ip_address_keyword); JSONObject capabilities = gateway.getJSONObject(capabilities_keyword); JSONArray ports = capabilities.getJSONArray(ports_keyword); - for (int i=0; i"; return ca + new_line + key + new_line + openvpn_cert; - } catch(JSONException e) { + } catch (JSONException e) { e.printStackTrace(); return ""; } diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java index 2b35080b..19ba1ba8 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -28,6 +28,7 @@ import java.net.*; import java.util.*; import butterknife.*; + import org.jetbrains.annotations.*; import org.json.*; @@ -41,26 +42,29 @@ import se.leap.bitmaskclient.eip.Constants; /** * Activity that builds and shows the list of known available providers. - * + *

* It also allows the user to enter custom providers with a button. - * - * @author parmegv * + * @author parmegv */ public class ConfigurationWizard extends Activity -implements NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { + implements NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { - @InjectView(R.id.progressbar_configuration_wizard) ProgressBar mProgressBar; - @InjectView(R.id.progressbar_description) TextView progressbar_description; + @InjectView(R.id.progressbar_configuration_wizard) + ProgressBar mProgressBar; + @InjectView(R.id.progressbar_description) + TextView progressbar_description; - @InjectView(R.id.provider_list) ListView provider_list_view; - @Inject ProviderListAdapter adapter; + @InjectView(R.id.provider_list) + ListView provider_list_view; + @Inject + ProviderListAdapter adapter; private ProviderManager provider_manager; private Intent mConfigState = new Intent(); private Provider selected_provider; - + final public static String TAG = ConfigurationWizard.class.getSimpleName(); final protected static String PROVIDER_SET = "PROVIDER SET"; @@ -69,7 +73,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download final private static String PROGRESSBAR_TEXT = TAG + "PROGRESSBAR_TEXT"; final private static String PROGRESSBAR_NUMBER = TAG + "PROGRESSBAR_NUMBER"; - + public ProviderAPIResultReceiver providerAPI_result_receiver; private ProviderAPIBroadcastReceiver_Update providerAPI_broadcast_receiver_update; @@ -87,32 +91,32 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download adapter = new ProviderListAdapter(getLayoutInflater(), providerRendererBuilder, provider_manager); provider_list_view.setAdapter(adapter); } - + @Override protected void onSaveInstanceState(@NotNull Bundle outState) { - if(mProgressBar != null) + if (mProgressBar != null) outState.putInt(PROGRESSBAR_NUMBER, mProgressBar.getProgress()); - if(progressbar_description != null) + if (progressbar_description != null) outState.putString(PROGRESSBAR_TEXT, progressbar_description.getText().toString()); - if(selected_provider != null) + if (selected_provider != null) outState.putParcelable(Provider.KEY, selected_provider); super.onSaveInstanceState(outState); } - + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); - fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); + preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); + fragment_manager = new FragmentManagerEnhanced(getFragmentManager()); provider_manager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null)); - setUpInitialUI(); + setUpInitialUI(); - initProviderList(); - - if (savedInstanceState != null) - restoreState(savedInstanceState); - setUpProviderAPIResultReceiver(); + initProviderList(); + + if (savedInstanceState != null) + restoreState(savedInstanceState); + setUpProviderAPIResultReceiver(); } private void restoreState(Bundle savedInstanceState) { @@ -120,7 +124,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download provider_name = savedInstanceState.getString(Provider.NAME, ""); selected_provider = savedInstanceState.getParcelable(Provider.KEY); - if(fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { + if (fragment_manager.findFragmentByTag(ProviderDetailFragment.TAG) == null && setting_up_provider) { if (selected_provider != null) onItemSelectedUi(selected_provider); if (progress > 0) @@ -131,7 +135,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download @Override protected void onPostResume() { super.onPostResume(); - if(!progressbar_text.isEmpty() && !provider_name.isEmpty() && progress != -1) { + if (!progressbar_text.isEmpty() && !provider_name.isEmpty() && progress != -1) { progressbar_description.setText(progressbar_text); mProgressBar.setProgress(progress); @@ -142,92 +146,91 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download } private void setUpInitialUI() { - setContentView(R.layout.configuration_wizard_activity); + setContentView(R.layout.configuration_wizard_activity); ButterKnife.inject(this); - - hideProgressBar(); + + hideProgressBar(); } - - private void hideProgressBar() { - mProgressBar.setVisibility(ProgressBar.INVISIBLE); - - progressbar_description.setVisibility(TextView.INVISIBLE); + + private void hideProgressBar() { + mProgressBar.setVisibility(ProgressBar.INVISIBLE); + + progressbar_description.setVisibility(TextView.INVISIBLE); } @Override protected void onDestroy() { - super.onDestroy(); - if(providerAPI_broadcast_receiver_update != null) - unregisterReceiver(providerAPI_broadcast_receiver_update); + super.onDestroy(); + if (providerAPI_broadcast_receiver_update != null) + unregisterReceiver(providerAPI_broadcast_receiver_update); } private void setUpProviderAPIResultReceiver() { providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); - providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); - - IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_PROGRESSBAR); - update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); + providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); + + IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_PROGRESSBAR); + update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); + registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); } - + @Override public void onReceiveResult(int resultCode, Bundle resultData) { - if(resultCode == ProviderAPI.PROVIDER_OK) { - mConfigState.setAction(PROVIDER_SET); - try { + if (resultCode == ProviderAPI.PROVIDER_OK) { + mConfigState.setAction(PROVIDER_SET); + try { String provider_json_string = preferences.getString(Provider.KEY, ""); - if(!provider_json_string.isEmpty()) - selected_provider.define(new JSONObject(provider_json_string)); + if (!provider_json_string.isEmpty()) + selected_provider.define(new JSONObject(provider_json_string)); } catch (JSONException e) { e.printStackTrace(); } - - if (preferences.getBoolean(Constants.ALLOWED_ANON, false)){ - mConfigState.putExtra(SERVICES_RETRIEVED, true); - - downloadVpnCertificate(); - } else { - mProgressBar.incrementProgressBy(1); - hideProgressBar(); - - setResult(RESULT_OK); - - showProviderDetails(); - } - } else if(resultCode == ProviderAPI.PROVIDER_NOK) { - hideProgressBar(); - preferences.edit().remove(Provider.KEY).apply(); - setting_up_provider = false; - - setResult(RESULT_CANCELED, mConfigState); - - String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); - showDownloadFailedDialog(reason_to_fail); - } - else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { - mProgressBar.incrementProgressBy(1); - hideProgressBar(); - - showProviderDetails(); - - setResult(RESULT_OK); - } else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { - hideProgressBar(); - - setResult(RESULT_CANCELED, mConfigState); - } else if(resultCode == AboutActivity.VIEWED) { - // Do nothing, right now - // I need this for CW to wait for the About activity to end before going back to Dashboard. - } + + if (preferences.getBoolean(Constants.ALLOWED_ANON, false)) { + mConfigState.putExtra(SERVICES_RETRIEVED, true); + + downloadVpnCertificate(); + } else { + mProgressBar.incrementProgressBy(1); + hideProgressBar(); + + setResult(RESULT_OK); + + showProviderDetails(); + } + } else if (resultCode == ProviderAPI.PROVIDER_NOK) { + hideProgressBar(); + preferences.edit().remove(Provider.KEY).apply(); + setting_up_provider = false; + + setResult(RESULT_CANCELED, mConfigState); + + String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); + showDownloadFailedDialog(reason_to_fail); + } else if (resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { + mProgressBar.incrementProgressBy(1); + hideProgressBar(); + + showProviderDetails(); + + setResult(RESULT_OK); + } else if (resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_CERTIFICATE) { + hideProgressBar(); + + setResult(RESULT_CANCELED, mConfigState); + } else if (resultCode == AboutActivity.VIEWED) { + // Do nothing, right now + // I need this for CW to wait for the About activity to end before going back to Dashboard. + } } @OnItemClick(R.id.provider_list) void onItemSelected(int position) { - //TODO Code 2 pane view + //TODO Code 2 pane view selected_provider = adapter.getItem(position); onItemSelectedUi(selected_provider); - onItemSelectedLogic(selected_provider); + onItemSelectedLogic(selected_provider); } private void onItemSelectedLogic(Provider selected_provider) { @@ -236,227 +239,231 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download private void onItemSelectedUi(Provider provider) { startProgressBar(); - adapter.hideAllBut(adapter.indexOf(provider)); + adapter.hideAllBut(adapter.indexOf(provider)); } - + @Override public void onBackPressed() { - if(setting_up_provider) { - stopSettingUpProvider(); - } else { - askDashboardToQuitApp(); - super.onBackPressed(); - } - } - + if (setting_up_provider) { + stopSettingUpProvider(); + } else { + askDashboardToQuitApp(); + super.onBackPressed(); + } + } + private void stopSettingUpProvider() { - ProviderAPI.stop(); - mProgressBar.setVisibility(ProgressBar.GONE); - mProgressBar.setProgress(0); - progressbar_description.setVisibility(TextView.GONE); + ProviderAPI.stop(); + mProgressBar.setVisibility(ProgressBar.GONE); + mProgressBar.setProgress(0); + progressbar_description.setVisibility(TextView.GONE); - cancelSettingUpProvider(); + cancelSettingUpProvider(); } - + public void cancelSettingUpProvider() { - adapter.showAllProviders(); - setting_up_provider = false; - preferences.edit().remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); + adapter.showAllProviders(); + setting_up_provider = false; + preferences.edit().remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).apply(); } - + private void askDashboardToQuitApp() { - Intent ask_quit = new Intent(); - ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); - setResult(RESULT_CANCELED, ask_quit); + Intent ask_quit = new Intent(); + ask_quit.putExtra(Dashboard.ACTION_QUIT, Dashboard.ACTION_QUIT); + setResult(RESULT_CANCELED, ask_quit); } - + private void startProgressBar() { mProgressBar.setVisibility(ProgressBar.VISIBLE); progressbar_description.setVisibility(TextView.VISIBLE); mProgressBar.setProgress(0); mProgressBar.setMax(3); - int measured_height = listItemHeight(); - mProgressBar.setTranslationY(measured_height); - progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); + int measured_height = listItemHeight(); + mProgressBar.setTranslationY(measured_height); + progressbar_description.setTranslationY(measured_height + mProgressBar.getHeight()); } - + private int listItemHeight() { View listItem = adapter.getView(0, null, provider_list_view); listItem.setLayoutParams(new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); WindowManager wm = (WindowManager) getApplicationContext() - .getSystemService(Context.WINDOW_SERVICE); + .getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int screenWidth = display.getWidth(); // deprecated int listViewWidth = screenWidth - 10 - 10; int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, - View.MeasureSpec.AT_MOST); + View.MeasureSpec.AT_MOST); listItem.measure(widthSpec, 0); return listItem.getMeasuredHeight(); } - + /** * Asks ProviderAPI to download an anonymous (anon) VPN certificate. */ private void downloadVpnCertificate() { - Intent provider_API_command = new Intent(this, ProviderAPI.class); + Intent provider_API_command = new Intent(this, ProviderAPI.class); - provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - startService(provider_API_command); + startService(provider_API_command); } - + /** * Open the new provider dialog */ public void addAndSelectNewProvider() { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); new NewProviderDialog().show(fragment_transaction, NewProviderDialog.TAG); } - + /** * Open the new provider dialog with data */ public void addAndSelectNewProvider(String main_url) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); - - DialogFragment newFragment = new NewProviderDialog(); - Bundle data = new Bundle(); - data.putString(Provider.MAIN_URL, main_url); - newFragment.setArguments(data); - newFragment.show(fragment_transaction, NewProviderDialog.TAG); - } - + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(NewProviderDialog.TAG); + + DialogFragment newFragment = new NewProviderDialog(); + Bundle data = new Bundle(); + data.putString(Provider.MAIN_URL, main_url); + newFragment.setArguments(data); + newFragment.show(fragment_transaction, NewProviderDialog.TAG); + } + /** - * Once selected a provider, this fragment offers the user to log in, - * use it anonymously (if possible) + * Once selected a provider, this fragment offers the user to log in, + * use it anonymously (if possible) * or cancel his/her election pressing the back button. - * @param reason_to_fail + * + * @param reason_to_fail */ public void showDownloadFailedDialog(String reason_to_fail) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); - - DialogFragment newFragment = DownloadFailedDialog.newInstance(reason_to_fail); - newFragment.show(fragment_transaction, DownloadFailedDialog.TAG); + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(DownloadFailedDialog.TAG); + + DialogFragment newFragment = DownloadFailedDialog.newInstance(reason_to_fail); + newFragment.show(fragment_transaction, DownloadFailedDialog.TAG); } - + /** - * Once selected a provider, this fragment offers the user to log in, - * use it anonymously (if possible) + * Once selected a provider, this fragment offers the user to log in, + * use it anonymously (if possible) * or cancel his/her election pressing the back button. + * * @param view */ private void showProviderDetails() { - if(setting_up_provider) { - FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(ProviderDetailFragment.TAG); + if (setting_up_provider) { + FragmentTransaction fragment_transaction = fragment_manager.removePreviousFragment(ProviderDetailFragment.TAG); - DialogFragment newFragment = ProviderDetailFragment.newInstance(); - newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); - setting_up_provider = false; - } + DialogFragment newFragment = ProviderDetailFragment.newInstance(); + newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); + setting_up_provider = false; + } } public void showAndSelectProvider(String provider_main_url) { - try { - selected_provider = new Provider(new URL((provider_main_url))); - adapter.add(selected_provider); - adapter.saveProviders(); + try { + selected_provider = new Provider(new URL((provider_main_url))); + adapter.add(selected_provider); + adapter.saveProviders(); autoSelectProvider(selected_provider); - } catch (MalformedURLException e) { - e.printStackTrace(); - } + } catch (MalformedURLException e) { + e.printStackTrace(); + } } - + private void autoSelectProvider(Provider provider) { - selected_provider = provider; + selected_provider = provider; onItemSelectedUi(selected_provider); - onItemSelectedLogic(selected_provider); + onItemSelectedLogic(selected_provider); } - + /** * Asks ProviderAPI to download a new provider.json file + * * @param provider_name * @param provider_main_url */ public void setUpProvider(URL provider_main_url) { - Intent provider_API_command = new Intent(this, ProviderAPI.class); - Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, provider_main_url.toString()); + Intent provider_API_command = new Intent(this, ProviderAPI.class); + Bundle parameters = new Bundle(); + parameters.putString(Provider.MAIN_URL, provider_main_url.toString()); - provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); - provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); + provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - startService(provider_API_command); - setting_up_provider = true; + startService(provider_API_command); + setting_up_provider = true; } public void retrySetUpProvider() { - cancelSettingUpProvider(); - if(!ProviderAPI.caCertDownloaded()) { - addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl()); - } else { - Intent provider_API_command = new Intent(this, ProviderAPI.class); + cancelSettingUpProvider(); + if (!ProviderAPI.caCertDownloaded()) { + addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl()); + } else { + Intent provider_API_command = new Intent(this, ProviderAPI.class); - provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - startService(provider_API_command); - } + startService(provider_API_command); + } } + @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.configuration_wizard_activity, menu); - return true; + getMenuInflater().inflate(R.menu.configuration_wizard_activity, menu); + return true; } - + @Override - public boolean onOptionsItemSelected(MenuItem item){ - switch (item.getItemId()){ - case R.id.about_leap: - startActivityForResult(new Intent(this, AboutActivity.class), 0); - return true; - case R.id.new_provider: - addAndSelectNewProvider(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.about_leap: + startActivityForResult(new Intent(this, AboutActivity.class), 0); + return true; + case R.id.new_provider: + addAndSelectNewProvider(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + public void showAllProviders() { - adapter.showAllProviders(); + adapter.showAllProviders(); } @Override public void login() { - Intent ask_login = new Intent(); - ask_login.putExtra(Provider.KEY, selected_provider); - ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); - setResult(RESULT_OK, ask_login); - setting_up_provider = false; - finish(); + Intent ask_login = new Intent(); + ask_login.putExtra(Provider.KEY, selected_provider); + ask_login.putExtra(SessionDialog.TAG, SessionDialog.TAG); + setResult(RESULT_OK, ask_login); + setting_up_provider = false; + finish(); } @Override public void use_anonymously() { - Intent pass_provider = new Intent(); - pass_provider.putExtra(Provider.KEY, selected_provider); - setResult(RESULT_OK, pass_provider); - setting_up_provider = false; - finish(); + Intent pass_provider = new Intent(); + pass_provider.putExtra(Provider.KEY, selected_provider); + setResult(RESULT_OK, pass_provider); + setting_up_provider = false; + finish(); } public class ProviderAPIBroadcastReceiver_Update extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); - mProgressBar.setProgress(update); - } + @Override + public void onReceive(Context context, Intent intent) { + int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); + mProgressBar.setProgress(update); + } } } diff --git a/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java b/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java index 96437eb2..1823d3e3 100644 --- a/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java +++ b/app/src/release/java/se/leap/bitmaskclient/NewProviderDialog.java @@ -18,6 +18,7 @@ package se.leap.bitmaskclient; import butterknife.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; + import android.app.*; import android.content.*; import android.os.*; @@ -28,7 +29,6 @@ import android.widget.*; * Implements the new custom provider dialog. * * @author parmegv - * */ public class NewProviderDialog extends DialogFragment { @@ -61,7 +61,7 @@ public class NewProviderDialog extends DialogFragment { View view = inflater.inflate(R.layout.new_provider_dialog, null); ButterKnife.inject(this, view); Bundle arguments = getArguments(); - if(arguments != null) { + if (arguments != null) { url_input_field.setText(arguments.getString(Provider.MAIN_URL, "")); } @@ -83,24 +83,26 @@ public class NewProviderDialog extends DialogFragment { private void saveProvider() { String entered_url = url_input_field.getText().toString().trim(); - if(!entered_url.startsWith("https://")) { - if (entered_url.startsWith("http://")){ + if (!entered_url.startsWith("https://")) { + if (entered_url.startsWith("http://")) { entered_url = entered_url.substring("http://".length()); } entered_url = "https://".concat(entered_url); } - if(validURL(entered_url)) { + if (validURL(entered_url)) { interface_with_ConfigurationWizard.showAndSelectProvider(entered_url); Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); } else { url_input_field.setText(""); - Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show();; + Toast.makeText(getActivity().getApplicationContext(), R.string.not_valid_url_entered, Toast.LENGTH_LONG).show(); + ; } } /** * Checks if the entered url is valid or not. + * * @param entered_url * @return true if it's not empty nor contains only the protocol. */ diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index 7f0ce001..e4b95567 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 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 @@ -20,6 +20,7 @@ import android.app.*; import android.content.*; import android.os.*; import android.util.*; + import java.io.*; import java.math.*; import java.net.*; @@ -27,308 +28,310 @@ import java.security.*; import java.security.cert.*; import java.security.interfaces.*; import java.util.*; + import javax.net.ssl.*; + import org.apache.http.client.*; import org.json.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import se.leap.bitmaskclient.eip.*; + /** * Implements HTTP api methods used to manage communications with the provider server. - * + *

* It's an IntentService because it downloads data from the Internet, so it operates in the background. - * + * * @author parmegv * @author MeanderingCode - * */ public class ProviderAPI extends IntentService { - + final public static String TAG = ProviderAPI.class.getSimpleName(), - SET_UP_PROVIDER = "setUpProvider", - DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON", - SIGN_UP = "srpRegister", - LOG_IN = "srpAuth", - LOG_OUT = "logOut", - DOWNLOAD_CERTIFICATE = "downloadUserAuthedCertificate", - PARAMETERS = "parameters", - RESULT_KEY = "result", - RECEIVER_KEY = "receiver", - ERRORS = "errors", - UPDATE_PROGRESSBAR = "update_progressbar", - CURRENT_PROGRESS = "current_progress", - DOWNLOAD_EIP_SERVICE = TAG + ".DOWNLOAD_EIP_SERVICE" - ; + SET_UP_PROVIDER = "setUpProvider", + DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON", + SIGN_UP = "srpRegister", + LOG_IN = "srpAuth", + LOG_OUT = "logOut", + DOWNLOAD_CERTIFICATE = "downloadUserAuthedCertificate", + PARAMETERS = "parameters", + RESULT_KEY = "result", + RECEIVER_KEY = "receiver", + ERRORS = "errors", + UPDATE_PROGRESSBAR = "update_progressbar", + CURRENT_PROGRESS = "current_progress", + DOWNLOAD_EIP_SERVICE = TAG + ".DOWNLOAD_EIP_SERVICE"; final public static int - SUCCESSFUL_LOGIN = 3, - FAILED_LOGIN = 4, - SUCCESSFUL_SIGNUP = 5, - FAILED_SIGNUP = 6, - SUCCESSFUL_LOGOUT = 7, - LOGOUT_FAILED = 8, - CORRECTLY_DOWNLOADED_CERTIFICATE = 9, - INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, - PROVIDER_OK = 11, - PROVIDER_NOK = 12, + SUCCESSFUL_LOGIN = 3, + FAILED_LOGIN = 4, + SUCCESSFUL_SIGNUP = 5, + FAILED_SIGNUP = 6, + SUCCESSFUL_LOGOUT = 7, + LOGOUT_FAILED = 8, + CORRECTLY_DOWNLOADED_CERTIFICATE = 9, + INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, + PROVIDER_OK = 11, + PROVIDER_NOK = 12, CORRECTLY_DOWNLOADED_EIP_SERVICE = 13, - INCORRECTLY_DOWNLOADED_EIP_SERVICE= 14 - ; - - private static boolean - CA_CERT_DOWNLOADED = false, - PROVIDER_JSON_DOWNLOADED = false, - EIP_SERVICE_JSON_DOWNLOADED = false - ; - + INCORRECTLY_DOWNLOADED_EIP_SERVICE = 14; + + private static boolean + CA_CERT_DOWNLOADED = false, + PROVIDER_JSON_DOWNLOADED = false, + EIP_SERVICE_JSON_DOWNLOADED = false; + private static String last_provider_main_url; private static boolean go_ahead = true; private static SharedPreferences preferences; private static String provider_api_url; - + public static void stop() { - go_ahead = false; + go_ahead = false; + } + + public ProviderAPI() { + super("ProviderAPI"); + Log.v("ClassName", "Provider API"); } - public ProviderAPI() { - super("ProviderAPI"); - Log.v("ClassName", "Provider API"); - } - @Override public void onCreate() { - super.onCreate(); - - preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); - CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); - } - - public static String lastProviderMainUrl() { - return last_provider_main_url; - } - - private String formatErrorMessage(final int toast_string_id) { - return "{ \"" + ERRORS + "\" : \""+getResources().getString(toast_string_id)+"\" }"; - } - - @Override - protected void onHandleIntent(Intent command) { - final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); - String action = command.getAction(); - Bundle parameters = command.getBundleExtra(PARAMETERS); - - if(provider_api_url == null && preferences.contains(Provider.KEY)) { - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); - go_ahead = true; - } catch (JSONException e) { - go_ahead = false; - } - } - - if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { - Bundle result = setUpProvider(parameters); - if(go_ahead) { - if(result.getBoolean(RESULT_KEY)) { - receiver.send(PROVIDER_OK, result); - } else { - receiver.send(PROVIDER_NOK, result); - } - } - } else if (action.equalsIgnoreCase(SIGN_UP)) { + super.onCreate(); + + preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); + CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)); + } + + public static String lastProviderMainUrl() { + return last_provider_main_url; + } + + private String formatErrorMessage(final int toast_string_id) { + return "{ \"" + ERRORS + "\" : \"" + getResources().getString(toast_string_id) + "\" }"; + } + + @Override + protected void onHandleIntent(Intent command) { + final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); + String action = command.getAction(); + Bundle parameters = command.getBundleExtra(PARAMETERS); + + if (provider_api_url == null && preferences.contains(Provider.KEY)) { + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); + go_ahead = true; + } catch (JSONException e) { + go_ahead = false; + } + } + + if (action.equalsIgnoreCase(SET_UP_PROVIDER)) { + Bundle result = setUpProvider(parameters); + if (go_ahead) { + if (result.getBoolean(RESULT_KEY)) { + receiver.send(PROVIDER_OK, result); + } else { + receiver.send(PROVIDER_NOK, result); + } + } + } else if (action.equalsIgnoreCase(SIGN_UP)) { UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.SIGNING_UP); - Bundle result = tryToRegister(parameters); - if(result.getBoolean(RESULT_KEY)) { - receiver.send(SUCCESSFUL_SIGNUP, result); - } else { - receiver.send(FAILED_SIGNUP, result); - } - } else if (action.equalsIgnoreCase(LOG_IN)) { + Bundle result = tryToRegister(parameters); + if (result.getBoolean(RESULT_KEY)) { + receiver.send(SUCCESSFUL_SIGNUP, result); + } else { + receiver.send(FAILED_SIGNUP, result); + } + } else if (action.equalsIgnoreCase(LOG_IN)) { UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_IN); - Bundle result = tryToAuthenticate(parameters); - if(result.getBoolean(RESULT_KEY)) { + Bundle result = tryToAuthenticate(parameters); + if (result.getBoolean(RESULT_KEY)) { receiver.send(SUCCESSFUL_LOGIN, result); UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_IN); - } else { + } else { receiver.send(FAILED_LOGIN, result); UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.NOT_LOGGED_IN); - } - } else if (action.equalsIgnoreCase(LOG_OUT)) { + } + } else if (action.equalsIgnoreCase(LOG_OUT)) { UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGING_OUT); - if(logOut()) { - receiver.send(SUCCESSFUL_LOGOUT, Bundle.EMPTY); - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_OUT); - } else { - receiver.send(LOGOUT_FAILED, Bundle.EMPTY); - UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.DIDNT_LOG_OUT); - } - } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { - if(updateVpnCertificate()) { - receiver.send(CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); - } else { - receiver.send(INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); - } - } else if(action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { + if (logOut()) { + receiver.send(SUCCESSFUL_LOGOUT, Bundle.EMPTY); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.LOGGED_OUT); + } else { + receiver.send(LOGOUT_FAILED, Bundle.EMPTY); + UserSessionStatus.updateStatus(UserSessionStatus.SessionStatus.DIDNT_LOG_OUT); + } + } else if (action.equalsIgnoreCase(DOWNLOAD_CERTIFICATE)) { + if (updateVpnCertificate()) { + receiver.send(CORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); + } else { + receiver.send(INCORRECTLY_DOWNLOADED_CERTIFICATE, Bundle.EMPTY); + } + } else if (action.equalsIgnoreCase(DOWNLOAD_EIP_SERVICE)) { Bundle result = getAndSetEipServiceJson(); - if(result.getBoolean(RESULT_KEY)) { + if (result.getBoolean(RESULT_KEY)) { receiver.send(CORRECTLY_DOWNLOADED_EIP_SERVICE, result); } else { receiver.send(INCORRECTLY_DOWNLOADED_EIP_SERVICE, result); } } - } + } private Bundle tryToRegister(Bundle task) { - Bundle result = new Bundle(); - int progress = 0; - - String username = User.userName(); - String password = task.getString(SessionDialog.PASSWORD); - - if(validUserLoginData(username, password)) { - result = register(username, password); - broadcastProgress(progress++); - } else { - if(!wellFormedPassword(password)) { - result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); - } - if(!validUsername(username)) { - result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); - } - } - - return result; - } - - private Bundle register(String username, String password) { - LeapSRPSession client = new LeapSRPSession(username, password); - byte[] salt = client.calculateNewSalt(); - - BigInteger password_verifier = client.calculateV(username, password, salt); - - JSONObject api_result = sendNewUserDataToSRPServer(provider_api_url, username, new BigInteger(1, salt).toString(16), password_verifier.toString(16)); - - Bundle result = new Bundle(); - if(api_result.has(ERRORS)) - result = authFailedNotification(api_result, username); - else { - result.putString(SessionDialog.USERNAME, username); - result.putString(SessionDialog.PASSWORD, password); - result.putBoolean(RESULT_KEY, true); - } - - return result; - } - - /** - * Starts the authentication process using SRP protocol. - * - * @param task containing: username, password and api url. - * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if authentication was successful. - */ - private Bundle tryToAuthenticate(Bundle task) { - Bundle result = new Bundle(); - int progress = 0; - - String username = User.userName(); - String password = task.getString(SessionDialog.PASSWORD); - if(validUserLoginData(username, password)) { - result = authenticate(username, password); - broadcastProgress(progress++); - } else { - if(!wellFormedPassword(password)) { - result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialog.USERNAME, username); - result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); - } - if(!validUsername(username)) { - result.putBoolean(RESULT_KEY, false); - result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); - } - } - - return result; - } + Bundle result = new Bundle(); + int progress = 0; + + String username = User.userName(); + String password = task.getString(SessionDialog.PASSWORD); + + if (validUserLoginData(username, password)) { + result = register(username, password); + broadcastProgress(progress++); + } else { + if (!wellFormedPassword(password)) { + result.putBoolean(RESULT_KEY, false); + result.putString(SessionDialog.USERNAME, username); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); + } + if (!validUsername(username)) { + result.putBoolean(RESULT_KEY, false); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); + } + } + + return result; + } + + private Bundle register(String username, String password) { + LeapSRPSession client = new LeapSRPSession(username, password); + byte[] salt = client.calculateNewSalt(); + + BigInteger password_verifier = client.calculateV(username, password, salt); + + JSONObject api_result = sendNewUserDataToSRPServer(provider_api_url, username, new BigInteger(1, salt).toString(16), password_verifier.toString(16)); + + Bundle result = new Bundle(); + if (api_result.has(ERRORS)) + result = authFailedNotification(api_result, username); + else { + result.putString(SessionDialog.USERNAME, username); + result.putString(SessionDialog.PASSWORD, password); + result.putBoolean(RESULT_KEY, true); + } + + return result; + } + + /** + * Starts the authentication process using SRP protocol. + * + * @param task containing: username, password and api url. + * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if authentication was successful. + */ + private Bundle tryToAuthenticate(Bundle task) { + Bundle result = new Bundle(); + int progress = 0; + + String username = User.userName(); + String password = task.getString(SessionDialog.PASSWORD); + if (validUserLoginData(username, password)) { + result = authenticate(username, password); + broadcastProgress(progress++); + } else { + if (!wellFormedPassword(password)) { + result.putBoolean(RESULT_KEY, false); + result.putString(SessionDialog.USERNAME, username); + result.putBoolean(SessionDialog.ERRORS.PASSWORD_INVALID_LENGTH.toString(), true); + } + if (!validUsername(username)) { + result.putBoolean(RESULT_KEY, false); + result.putBoolean(SessionDialog.ERRORS.USERNAME_MISSING.toString(), true); + } + } + + return result; + } private Bundle authenticate(String username, String password) { - Bundle result = new Bundle(); - - LeapSRPSession client = new LeapSRPSession(username, password); - byte[] A = client.exponential(); - - JSONObject step_result = sendAToSRPServer(provider_api_url, username, new BigInteger(1, A).toString(16)); - try { - String salt = step_result.getString(LeapSRPSession.SALT); - byte[] Bbytes = new BigInteger(step_result.getString("B"), 16).toByteArray(); - byte[] M1 = client.response(new BigInteger(salt, 16).toByteArray(), Bbytes); - if(M1 != null) { - step_result = sendM1ToSRPServer(provider_api_url, username, M1); - setTokenIfAvailable(step_result); - byte[] M2 = new BigInteger(step_result.getString(LeapSRPSession.M2), 16).toByteArray(); - if(client.verify(M2)) { - result.putBoolean(RESULT_KEY, true); - } else { - authFailedNotification(step_result, username); - } - } else { - result.putBoolean(RESULT_KEY, false); - result.putString(SessionDialog.USERNAME, username); - result.putString(getResources().getString(R.string.user_message), getResources().getString(R.string.error_srp_math_error_user_message)); - } - } catch (JSONException e) { - result = authFailedNotification(step_result, username); - e.printStackTrace(); - } - - return result; + Bundle result = new Bundle(); + + LeapSRPSession client = new LeapSRPSession(username, password); + byte[] A = client.exponential(); + + JSONObject step_result = sendAToSRPServer(provider_api_url, username, new BigInteger(1, A).toString(16)); + try { + String salt = step_result.getString(LeapSRPSession.SALT); + byte[] Bbytes = new BigInteger(step_result.getString("B"), 16).toByteArray(); + byte[] M1 = client.response(new BigInteger(salt, 16).toByteArray(), Bbytes); + if (M1 != null) { + step_result = sendM1ToSRPServer(provider_api_url, username, M1); + setTokenIfAvailable(step_result); + byte[] M2 = new BigInteger(step_result.getString(LeapSRPSession.M2), 16).toByteArray(); + if (client.verify(M2)) { + result.putBoolean(RESULT_KEY, true); + } else { + authFailedNotification(step_result, username); + } + } else { + result.putBoolean(RESULT_KEY, false); + result.putString(SessionDialog.USERNAME, username); + result.putString(getResources().getString(R.string.user_message), getResources().getString(R.string.error_srp_math_error_user_message)); + } + } catch (JSONException e) { + result = authFailedNotification(step_result, username); + e.printStackTrace(); + } + + return result; } private boolean setTokenIfAvailable(JSONObject authentication_step_result) { - try { - LeapSRPSession.setToken(authentication_step_result.getString(LeapSRPSession.TOKEN)); - CookieHandler.setDefault(null); // we don't need cookies anymore - } catch(JSONException e) { // - return false; - } - return true; - } - + try { + LeapSRPSession.setToken(authentication_step_result.getString(LeapSRPSession.TOKEN)); + CookieHandler.setDefault(null); // we don't need cookies anymore + } catch (JSONException e) { // + return false; + } + return true; + } + private Bundle authFailedNotification(JSONObject result, String username) { - Bundle user_notification_bundle = new Bundle(); - try{ - JSONObject error_message = result.getJSONObject(ERRORS); - String error_type = error_message.keys().next().toString(); - String message = error_message.get(error_type).toString(); - user_notification_bundle.putString(getResources().getString(R.string.user_message), message); - } catch(JSONException e) {} - - if(!username.isEmpty()) - user_notification_bundle.putString(SessionDialog.USERNAME, username); - user_notification_bundle.putBoolean(RESULT_KEY, false); - - return user_notification_bundle; - } - - /** - * Sets up an intent with the progress value passed as a parameter - * and sends it as a broadcast. - * @param progress - */ - private void broadcastProgress(int progress) { - Intent intentUpdate = new Intent(); - intentUpdate.setAction(UPDATE_PROGRESSBAR); - intentUpdate.addCategory(Intent.CATEGORY_DEFAULT); - intentUpdate.putExtra(CURRENT_PROGRESS, progress); - sendBroadcast(intentUpdate); - } + Bundle user_notification_bundle = new Bundle(); + try { + JSONObject error_message = result.getJSONObject(ERRORS); + String error_type = error_message.keys().next().toString(); + String message = error_message.get(error_type).toString(); + user_notification_bundle.putString(getResources().getString(R.string.user_message), message); + } catch (JSONException e) { + } + + if (!username.isEmpty()) + user_notification_bundle.putString(SessionDialog.USERNAME, username); + user_notification_bundle.putBoolean(RESULT_KEY, false); + + return user_notification_bundle; + } + + /** + * Sets up an intent with the progress value passed as a parameter + * and sends it as a broadcast. + * + * @param progress + */ + private void broadcastProgress(int progress) { + Intent intentUpdate = new Intent(); + intentUpdate.setAction(UPDATE_PROGRESSBAR); + intentUpdate.addCategory(Intent.CATEGORY_DEFAULT); + intentUpdate.putExtra(CURRENT_PROGRESS, progress); + sendBroadcast(intentUpdate); + } /** * Validates parameters entered by the user to log in + * * @param username * @param password * @return true if both parameters are present and the entered password length is greater or equal to eight (8). @@ -341,491 +344,500 @@ public class ProviderAPI extends IntentService { return username != null && !username.isEmpty(); } - /** - * Validates a password - * @param password - * @return true if the entered password length is greater or equal to eight (8). - */ - private boolean wellFormedPassword(String password) { - return password != null && password.length() >= 8; - } - - /** - * Sends an HTTP POST request to the authentication server with the SRP Parameter A. - * @param server_url - * @param username - * @param clientA First SRP parameter sent - * @return response from authentication server - */ + /** + * Validates a password + * + * @param password + * @return true if the entered password length is greater or equal to eight (8). + */ + private boolean wellFormedPassword(String password) { + return password != null && password.length() >= 8; + } + + /** + * Sends an HTTP POST request to the authentication server with the SRP Parameter A. + * + * @param server_url + * @param username + * @param clientA First SRP parameter sent + * @return response from authentication server + */ private JSONObject sendAToSRPServer(String server_url, String username, String clientA) { - Map parameters = new HashMap(); - parameters.put("login", username); - parameters.put("A", clientA); - return sendToServer(server_url + "/sessions.json", "POST", parameters); - } - - /** - * Sends an HTTP PUT request to the authentication server with the SRP Parameter M1 (or simply M). - * @param server_url - * @param username - * @param m1 Second SRP parameter sent - * @return response from authentication server - */ + Map parameters = new HashMap(); + parameters.put("login", username); + parameters.put("A", clientA); + return sendToServer(server_url + "/sessions.json", "POST", parameters); + } + + /** + * Sends an HTTP PUT request to the authentication server with the SRP Parameter M1 (or simply M). + * + * @param server_url + * @param username + * @param m1 Second SRP parameter sent + * @return response from authentication server + */ private JSONObject sendM1ToSRPServer(String server_url, String username, byte[] m1) { - Map parameters = new HashMap(); - parameters.put("client_auth", new BigInteger(1, ConfigHelper.trim(m1)).toString(16)); - return sendToServer(server_url + "/sessions/" + username +".json", "PUT", parameters); - } - - /** - * Sends an HTTP POST request to the api server to register a new user. - * @param server_url - * @param username - * @param salt - * @param password_verifier - * @return response from authentication server - */ + Map parameters = new HashMap(); + parameters.put("client_auth", new BigInteger(1, ConfigHelper.trim(m1)).toString(16)); + return sendToServer(server_url + "/sessions/" + username + ".json", "PUT", parameters); + } + + /** + * Sends an HTTP POST request to the api server to register a new user. + * + * @param server_url + * @param username + * @param salt + * @param password_verifier + * @return response from authentication server + */ private JSONObject sendNewUserDataToSRPServer(String server_url, String username, String salt, String password_verifier) { - Map parameters = new HashMap(); - parameters.put("user[login]", username); - parameters.put("user[password_salt]", salt); - parameters.put("user[password_verifier]", password_verifier); - Log.d(TAG, server_url); - Log.d(TAG, parameters.toString()); - return sendToServer(server_url + "/users.json", "POST", parameters); - } - - /** - * Executes an HTTP request expecting a JSON response. - * @param url - * @param request_method - * @param parameters - * @return response from authentication server - */ - private JSONObject sendToServer(String url, String request_method, Map parameters) { - JSONObject json_response; - HttpsURLConnection urlConnection = null; - try { - InputStream is = null; - urlConnection = (HttpsURLConnection)new URL(url).openConnection(); - urlConnection.setRequestMethod(request_method); - urlConnection.setChunkedStreamingMode(0); - urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - - DataOutputStream writer = new DataOutputStream(urlConnection.getOutputStream()); - writer.writeBytes(formatHttpParameters(parameters)); - writer.close(); - - is = urlConnection.getInputStream(); - String plain_response = new Scanner(is).useDelimiter("\\A").next(); - json_response = new JSONObject(plain_response); - } catch (ClientProtocolException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (IOException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (JSONException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (KeyManagementException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (KeyStoreException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } catch (CertificateException e) { - json_response = getErrorMessage(urlConnection); - e.printStackTrace(); - } - - return json_response; - } + Map parameters = new HashMap(); + parameters.put("user[login]", username); + parameters.put("user[password_salt]", salt); + parameters.put("user[password_verifier]", password_verifier); + Log.d(TAG, server_url); + Log.d(TAG, parameters.toString()); + return sendToServer(server_url + "/users.json", "POST", parameters); + } + + /** + * Executes an HTTP request expecting a JSON response. + * + * @param url + * @param request_method + * @param parameters + * @return response from authentication server + */ + private JSONObject sendToServer(String url, String request_method, Map parameters) { + JSONObject json_response; + HttpsURLConnection urlConnection = null; + try { + InputStream is = null; + urlConnection = (HttpsURLConnection) new URL(url).openConnection(); + urlConnection.setRequestMethod(request_method); + urlConnection.setChunkedStreamingMode(0); + urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); + + DataOutputStream writer = new DataOutputStream(urlConnection.getOutputStream()); + writer.writeBytes(formatHttpParameters(parameters)); + writer.close(); + + is = urlConnection.getInputStream(); + String plain_response = new Scanner(is).useDelimiter("\\A").next(); + json_response = new JSONObject(plain_response); + } catch (ClientProtocolException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (IOException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (JSONException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (KeyManagementException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (KeyStoreException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } catch (CertificateException e) { + json_response = getErrorMessage(urlConnection); + e.printStackTrace(); + } + + return json_response; + } private JSONObject getErrorMessage(HttpsURLConnection urlConnection) { - JSONObject error_message = new JSONObject(); - if(urlConnection != null) { - InputStream error_stream = urlConnection.getErrorStream(); - if(error_stream != null) { - String error_response = new Scanner(error_stream).useDelimiter("\\A").next(); - Log.d("Error", error_response); - try { - error_message = new JSONObject(error_response); - } catch (JSONException e) { - Log.d(TAG, e.getMessage()); - e.printStackTrace(); - } - urlConnection.disconnect(); - } - } - return error_message; - } - - private String formatHttpParameters(Map parameters) throws UnsupportedEncodingException { - StringBuilder result = new StringBuilder(); - boolean first = true; - - Iterator parameter_iterator = parameters.keySet().iterator(); - while(parameter_iterator.hasNext()) { - if(first) - first = false; - else - result.append("&&"); - - String key = parameter_iterator.next(); - String value = parameters.get(key); - - result.append(URLEncoder.encode(key, "UTF-8")); - result.append("="); - result.append(URLEncoder.encode(value, "UTF-8")); - } - - return result.toString(); - } - - /** - * Downloads a provider.json from a given URL, adding a new provider using the given name. - * @param task containing a boolean meaning if the provider is custom or not, another boolean meaning if the user completely trusts this provider, the provider name and its provider.json url. - * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful. - */ + JSONObject error_message = new JSONObject(); + if (urlConnection != null) { + InputStream error_stream = urlConnection.getErrorStream(); + if (error_stream != null) { + String error_response = new Scanner(error_stream).useDelimiter("\\A").next(); + Log.d("Error", error_response); + try { + error_message = new JSONObject(error_response); + } catch (JSONException e) { + Log.d(TAG, e.getMessage()); + e.printStackTrace(); + } + urlConnection.disconnect(); + } + } + return error_message; + } + + private String formatHttpParameters(Map parameters) throws UnsupportedEncodingException { + StringBuilder result = new StringBuilder(); + boolean first = true; + + Iterator parameter_iterator = parameters.keySet().iterator(); + while (parameter_iterator.hasNext()) { + if (first) + first = false; + else + result.append("&&"); + + String key = parameter_iterator.next(); + String value = parameters.get(key); + + result.append(URLEncoder.encode(key, "UTF-8")); + result.append("="); + result.append(URLEncoder.encode(value, "UTF-8")); + } + + return result.toString(); + } + + /** + * Downloads a provider.json from a given URL, adding a new provider using the given name. + * + * @param task containing a boolean meaning if the provider is custom or not, another boolean meaning if the user completely trusts this provider, the provider name and its provider.json url. + * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful. + */ private Bundle setUpProvider(Bundle task) { - int progress = 0; - Bundle current_download = new Bundle(); - - if(task != null && task.containsKey(Provider.MAIN_URL)) { - last_provider_main_url = task.getString(Provider.MAIN_URL); - CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; - go_ahead = true; - } - - if(!PROVIDER_JSON_DOWNLOADED) - current_download = getAndSetProviderJson(last_provider_main_url); - if(PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { - broadcastProgress(progress++); - PROVIDER_JSON_DOWNLOADED = true; - - if(!CA_CERT_DOWNLOADED) - current_download = downloadCACert(); - if(CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { - broadcastProgress(progress++); - CA_CERT_DOWNLOADED = true; - current_download = getAndSetEipServiceJson(); - if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { - broadcastProgress(progress++); - EIP_SERVICE_JSON_DOWNLOADED = true; - } - } - } - - return current_download; - } - - private Bundle downloadCACert() { - Bundle result = new Bundle(); - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - String ca_cert_url = provider_json.getString(Provider.CA_CERT_URI); - String cert_string = downloadWithCommercialCA(ca_cert_url); - result.putBoolean(RESULT_KEY, true); - - if(validCertificate(cert_string) && go_ahead) { - preferences.edit().putString(Provider.CA_CERT, cert_string).commit(); - result.putBoolean(RESULT_KEY, true); - } else { - String reason_to_fail = pickErrorMessage(cert_string); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - } catch (JSONException e) { - String reason_to_fail = formatErrorMessage(R.string.malformed_url); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - - return result; - } - - - public static boolean caCertDownloaded() { - return CA_CERT_DOWNLOADED; - } + int progress = 0; + Bundle current_download = new Bundle(); + + if (task != null && task.containsKey(Provider.MAIN_URL)) { + last_provider_main_url = task.getString(Provider.MAIN_URL); + CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; + go_ahead = true; + } + + if (!PROVIDER_JSON_DOWNLOADED) + current_download = getAndSetProviderJson(last_provider_main_url); + if (PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { + broadcastProgress(progress++); + PROVIDER_JSON_DOWNLOADED = true; + + if (!CA_CERT_DOWNLOADED) + current_download = downloadCACert(); + if (CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { + broadcastProgress(progress++); + CA_CERT_DOWNLOADED = true; + current_download = getAndSetEipServiceJson(); + if (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { + broadcastProgress(progress++); + EIP_SERVICE_JSON_DOWNLOADED = true; + } + } + } + + return current_download; + } + + private Bundle downloadCACert() { + Bundle result = new Bundle(); + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + String ca_cert_url = provider_json.getString(Provider.CA_CERT_URI); + String cert_string = downloadWithCommercialCA(ca_cert_url); + result.putBoolean(RESULT_KEY, true); + + if (validCertificate(cert_string) && go_ahead) { + preferences.edit().putString(Provider.CA_CERT, cert_string).commit(); + result.putBoolean(RESULT_KEY, true); + } else { + String reason_to_fail = pickErrorMessage(cert_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + } catch (JSONException e) { + String reason_to_fail = formatErrorMessage(R.string.malformed_url); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + + return result; + } + + + public static boolean caCertDownloaded() { + return CA_CERT_DOWNLOADED; + } private boolean validCertificate(String cert_string) { - boolean result = false; - if(!ConfigHelper.checkErroneousDownload(cert_string)) { - X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(cert_string); - try { - if(certificate != null) { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - String fingerprint = provider_json.getString(Provider.CA_CERT_FINGERPRINT); - String encoding = fingerprint.split(":")[0]; - String expected_fingerprint = fingerprint.split(":")[1]; - String real_fingerprint = base64toHex(Base64.encodeToString( - MessageDigest.getInstance(encoding).digest(certificate.getEncoded()), - Base64.DEFAULT)); - - result = real_fingerprint.trim().equalsIgnoreCase(expected_fingerprint.trim()); - } - else - result = false; - } catch (JSONException e) { - result = false; - } catch (NoSuchAlgorithmException e) { - result = false; - } catch (CertificateEncodingException e) { - result = false; - } - } - - return result; + boolean result = false; + if (!ConfigHelper.checkErroneousDownload(cert_string)) { + X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(cert_string); + try { + if (certificate != null) { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + String fingerprint = provider_json.getString(Provider.CA_CERT_FINGERPRINT); + String encoding = fingerprint.split(":")[0]; + String expected_fingerprint = fingerprint.split(":")[1]; + String real_fingerprint = base64toHex(Base64.encodeToString( + MessageDigest.getInstance(encoding).digest(certificate.getEncoded()), + Base64.DEFAULT)); + + result = real_fingerprint.trim().equalsIgnoreCase(expected_fingerprint.trim()); + } else + result = false; + } catch (JSONException e) { + result = false; + } catch (NoSuchAlgorithmException e) { + result = false; + } catch (CertificateEncodingException e) { + result = false; + } + } + + return result; } private String base64toHex(String base64_input) { - byte[] byteArray = Base64.decode(base64_input, Base64.DEFAULT); - int readBytes = byteArray.length; - StringBuffer hexData = new StringBuffer(); - int onebyte; - for (int i=0; i < readBytes; i++) { - onebyte = ((0x000000ff & byteArray[i]) | 0xffffff00); - hexData.append(Integer.toHexString(onebyte).substring(6)); - } - return hexData.toString(); - } - - private Bundle getAndSetProviderJson(String provider_main_url) { - Bundle result = new Bundle(); - - if(go_ahead) { - String provider_dot_json_string = downloadWithCommercialCA(provider_main_url + "/provider.json"); - - try { - JSONObject provider_json = new JSONObject(provider_dot_json_string); - provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); - String name = provider_json.getString(Provider.NAME); - //TODO setProviderName(name); - - preferences.edit().putString(Provider.KEY, provider_json.toString()).commit(); - preferences.edit().putBoolean(Constants.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_ANON)).commit(); - preferences.edit().putBoolean(Constants.ALLOWED_REGISTERED, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_REGISTERED)).commit(); - - result.putBoolean(RESULT_KEY, true); - } catch (JSONException e) { - //TODO Error message should be contained in that provider_dot_json_string - String reason_to_fail = pickErrorMessage(provider_dot_json_string); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - } - return result; - } - - private Bundle getAndSetEipServiceJson() { - Bundle result = new Bundle(); - String eip_service_json_string = ""; - if(go_ahead) { - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; - eip_service_json_string = downloadWithProviderCA(eip_service_url); - JSONObject eip_service_json = new JSONObject(eip_service_json_string); - eip_service_json.getInt(Provider.API_RETURN_SERIAL); - - preferences.edit().putString(Constants.KEY, eip_service_json.toString()).commit(); - - result.putBoolean(RESULT_KEY, true); - } catch (JSONException e) { - String reason_to_fail = pickErrorMessage(eip_service_json_string); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - } - return result; - } - - /** - * Interprets the error message as a JSON object and extract the "errors" keyword pair. - * If the error message is not a JSON object, then it is returned untouched. - * @param string_json_error_message - * @return final error message - */ - private String pickErrorMessage(String string_json_error_message) { - String error_message = ""; - try { - JSONObject json_error_message = new JSONObject(string_json_error_message); - error_message = json_error_message.getString(ERRORS); - } catch (JSONException e) { - // TODO Auto-generated catch block - error_message = string_json_error_message; - } - - return error_message; - } - - /** - * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. - * - * @param string_url - * @return - */ - private String downloadWithCommercialCA(String string_url) { - - String json_file_content = ""; - - URL provider_url = null; - int seconds_of_timeout = 1; - try { - provider_url = new URL(string_url); - URLConnection url_connection = provider_url.openConnection(); - url_connection.setConnectTimeout(seconds_of_timeout*1000); - if(!LeapSRPSession.getToken().isEmpty()) - url_connection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token = " + LeapSRPSession.getToken()); - json_file_content = new Scanner(url_connection.getInputStream()).useDelimiter("\\A").next(); - } catch (MalformedURLException e) { - json_file_content = formatErrorMessage(R.string.malformed_url); - } catch(SocketTimeoutException e) { - e.printStackTrace(); - json_file_content = formatErrorMessage(R.string.server_unreachable_message); - } catch (SSLHandshakeException e) { - if(provider_url != null) { - json_file_content = downloadWithProviderCA(string_url); - } else { - json_file_content = formatErrorMessage(R.string.certificate_error); - } - } catch(ConnectException e) { - json_file_content = formatErrorMessage(R.string.service_is_down_error); - } catch (FileNotFoundException e) { - json_file_content = formatErrorMessage(R.string.malformed_url); - } catch (Exception e) { - e.printStackTrace(); - if(provider_url != null) { - json_file_content = downloadWithProviderCA(string_url); - } - } - - return json_file_content; - } - - /** - * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. - * @param url_string as a string - * @return an empty string if it fails, the url content if not. - */ - private String downloadWithProviderCA(String url_string) { - String json_file_content = ""; - - try { - URL url = new URL(url_string); - // Tell the URLConnection to use a SocketFactory from our SSLContext - HttpsURLConnection urlConnection = - (HttpsURLConnection)url.openConnection(); - urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - if(!LeapSRPSession.getToken().isEmpty()) - urlConnection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token=" + LeapSRPSession.getToken()); - json_file_content = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (UnknownHostException e) { - e.printStackTrace(); - json_file_content = formatErrorMessage(R.string.server_unreachable_message); - } catch (IOException e) { - // The downloaded certificate doesn't validate our https connection. - json_file_content = formatErrorMessage(R.string.certificate_error); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchElementException e) { - e.printStackTrace(); - json_file_content = formatErrorMessage(R.string.server_unreachable_message); - } - return json_file_content; - } - - private javax.net.ssl.SSLSocketFactory getProviderSSLSocketFactory() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException { - String provider_cert_string = preferences.getString(Provider.CA_CERT,""); - - java.security.cert.Certificate provider_certificate = ConfigHelper.parseX509CertificateFromString(provider_cert_string); - - // Create a KeyStore containing our trusted CAs - String keyStoreType = KeyStore.getDefaultType(); - KeyStore keyStore = KeyStore.getInstance(keyStoreType); - keyStore.load(null, null); - keyStore.setCertificateEntry("provider_ca_certificate", provider_certificate); - - // Create a TrustManager that trusts the CAs in our KeyStore - String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); - TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); - tmf.init(keyStore); - - // Create an SSLContext that uses our TrustManager - SSLContext context = SSLContext.getInstance("TLS"); - context.init(null, tmf.getTrustManagers(), null); - - return context.getSocketFactory(); - } - - /** - * Downloads the string that's in the url with any certificate. - */ - private String downloadWithoutCA(String url_string) { - String string = ""; - try { - - HostnameVerifier hostnameVerifier = new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; - } - }; - - class DefaultTrustManager implements X509TrustManager { - - @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} - - @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - } - - SSLContext context = SSLContext.getInstance("TLS"); - context.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); - - URL url = new URL(url_string); - HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection(); - urlConnection.setSSLSocketFactory(context.getSocketFactory()); - urlConnection.setHostnameVerifier(hostnameVerifier); - string = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); - System.out.println("String ignoring certificate = " + string); - } catch (FileNotFoundException e) { - e.printStackTrace(); - string = formatErrorMessage(R.string.malformed_url); - } catch (IOException e) { - // The downloaded certificate doesn't validate our https connection. - e.printStackTrace(); - string = formatErrorMessage(R.string.certificate_error); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return string; - } + byte[] byteArray = Base64.decode(base64_input, Base64.DEFAULT); + int readBytes = byteArray.length; + StringBuffer hexData = new StringBuffer(); + int onebyte; + for (int i = 0; i < readBytes; i++) { + onebyte = ((0x000000ff & byteArray[i]) | 0xffffff00); + hexData.append(Integer.toHexString(onebyte).substring(6)); + } + return hexData.toString(); + } + + private Bundle getAndSetProviderJson(String provider_main_url) { + Bundle result = new Bundle(); + + if (go_ahead) { + String provider_dot_json_string = downloadWithCommercialCA(provider_main_url + "/provider.json"); + + try { + JSONObject provider_json = new JSONObject(provider_dot_json_string); + provider_api_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION); + String name = provider_json.getString(Provider.NAME); + //TODO setProviderName(name); + + preferences.edit().putString(Provider.KEY, provider_json.toString()).commit(); + preferences.edit().putBoolean(Constants.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_ANON)).commit(); + preferences.edit().putBoolean(Constants.ALLOWED_REGISTERED, provider_json.getJSONObject(Provider.SERVICE).getBoolean(Constants.ALLOWED_REGISTERED)).commit(); + + result.putBoolean(RESULT_KEY, true); + } catch (JSONException e) { + //TODO Error message should be contained in that provider_dot_json_string + String reason_to_fail = pickErrorMessage(provider_dot_json_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + } + return result; + } + + private Bundle getAndSetEipServiceJson() { + Bundle result = new Bundle(); + String eip_service_json_string = ""; + if (go_ahead) { + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; + eip_service_json_string = downloadWithProviderCA(eip_service_url); + JSONObject eip_service_json = new JSONObject(eip_service_json_string); + eip_service_json.getInt(Provider.API_RETURN_SERIAL); + + preferences.edit().putString(Constants.KEY, eip_service_json.toString()).commit(); + + result.putBoolean(RESULT_KEY, true); + } catch (JSONException e) { + String reason_to_fail = pickErrorMessage(eip_service_json_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + } + } + return result; + } + + /** + * Interprets the error message as a JSON object and extract the "errors" keyword pair. + * If the error message is not a JSON object, then it is returned untouched. + * + * @param string_json_error_message + * @return final error message + */ + private String pickErrorMessage(String string_json_error_message) { + String error_message = ""; + try { + JSONObject json_error_message = new JSONObject(string_json_error_message); + error_message = json_error_message.getString(ERRORS); + } catch (JSONException e) { + // TODO Auto-generated catch block + error_message = string_json_error_message; + } + + return error_message; + } + + /** + * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. + * + * @param string_url + * @return + */ + private String downloadWithCommercialCA(String string_url) { + + String json_file_content = ""; + + URL provider_url = null; + int seconds_of_timeout = 1; + try { + provider_url = new URL(string_url); + URLConnection url_connection = provider_url.openConnection(); + url_connection.setConnectTimeout(seconds_of_timeout * 1000); + if (!LeapSRPSession.getToken().isEmpty()) + url_connection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token = " + LeapSRPSession.getToken()); + json_file_content = new Scanner(url_connection.getInputStream()).useDelimiter("\\A").next(); + } catch (MalformedURLException e) { + json_file_content = formatErrorMessage(R.string.malformed_url); + } catch (SocketTimeoutException e) { + e.printStackTrace(); + json_file_content = formatErrorMessage(R.string.server_unreachable_message); + } catch (SSLHandshakeException e) { + if (provider_url != null) { + json_file_content = downloadWithProviderCA(string_url); + } else { + json_file_content = formatErrorMessage(R.string.certificate_error); + } + } catch (ConnectException e) { + json_file_content = formatErrorMessage(R.string.service_is_down_error); + } catch (FileNotFoundException e) { + json_file_content = formatErrorMessage(R.string.malformed_url); + } catch (Exception e) { + e.printStackTrace(); + if (provider_url != null) { + json_file_content = downloadWithProviderCA(string_url); + } + } + + return json_file_content; + } + + /** + * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. + * + * @param url_string as a string + * @return an empty string if it fails, the url content if not. + */ + private String downloadWithProviderCA(String url_string) { + String json_file_content = ""; + + try { + URL url = new URL(url_string); + // Tell the URLConnection to use a SocketFactory from our SSLContext + HttpsURLConnection urlConnection = + (HttpsURLConnection) url.openConnection(); + urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); + if (!LeapSRPSession.getToken().isEmpty()) + urlConnection.addRequestProperty(LeapSRPSession.AUTHORIZATION_HEADER, "Token token=" + LeapSRPSession.getToken()); + json_file_content = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnknownHostException e) { + e.printStackTrace(); + json_file_content = formatErrorMessage(R.string.server_unreachable_message); + } catch (IOException e) { + // The downloaded certificate doesn't validate our https connection. + json_file_content = formatErrorMessage(R.string.certificate_error); + } catch (KeyStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyManagementException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchElementException e) { + e.printStackTrace(); + json_file_content = formatErrorMessage(R.string.server_unreachable_message); + } + return json_file_content; + } + + private javax.net.ssl.SSLSocketFactory getProviderSSLSocketFactory() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException { + String provider_cert_string = preferences.getString(Provider.CA_CERT, ""); + + java.security.cert.Certificate provider_certificate = ConfigHelper.parseX509CertificateFromString(provider_cert_string); + + // Create a KeyStore containing our trusted CAs + String keyStoreType = KeyStore.getDefaultType(); + KeyStore keyStore = KeyStore.getInstance(keyStoreType); + keyStore.load(null, null); + keyStore.setCertificateEntry("provider_ca_certificate", provider_certificate); + + // Create a TrustManager that trusts the CAs in our KeyStore + String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); + tmf.init(keyStore); + + // Create an SSLContext that uses our TrustManager + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, tmf.getTrustManagers(), null); + + return context.getSocketFactory(); + } + + /** + * Downloads the string that's in the url with any certificate. + */ + private String downloadWithoutCA(String url_string) { + String string = ""; + try { + + HostnameVerifier hostnameVerifier = new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + + class DefaultTrustManager implements X509TrustManager { + + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + } + + SSLContext context = SSLContext.getInstance("TLS"); + context.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom()); + + URL url = new URL(url_string); + HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); + urlConnection.setSSLSocketFactory(context.getSocketFactory()); + urlConnection.setHostnameVerifier(hostnameVerifier); + string = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next(); + System.out.println("String ignoring certificate = " + string); + } catch (FileNotFoundException e) { + e.printStackTrace(); + string = formatErrorMessage(R.string.malformed_url); + } catch (IOException e) { + // The downloaded certificate doesn't validate our https connection. + e.printStackTrace(); + string = formatErrorMessage(R.string.certificate_error); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyManagementException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return string; + } private boolean logOut() { String delete_url = provider_api_url + "/logout"; @@ -833,113 +845,112 @@ public class ProviderAPI extends IntentService { HttpsURLConnection urlConnection = null; int responseCode = 0; int progress = 0; - try { - - urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection(); - urlConnection.setRequestMethod("DELETE"); - urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - - responseCode = urlConnection.getResponseCode(); - broadcastProgress(progress++); - LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IndexOutOfBoundsException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IOException e) { - // TODO Auto-generated catch block - try { - if(urlConnection != null) { - responseCode = urlConnection.getResponseCode(); - if(responseCode == 401) { - broadcastProgress(progress++); - LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); - return true; - } - } - } catch (IOException e1) { - e1.printStackTrace(); - } - - e.printStackTrace(); - return false; - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return true; - } - - /** - * Downloads a new OpenVPN certificate, attaching authenticated cookie for authenticated certificate. - * - * @return true if certificate was downloaded correctly, false if provider.json is not present in SharedPreferences, or if the certificate url could not be parsed as a URI, or if there was an SSL error. - */ - private boolean updateVpnCertificate() { - try { - JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); - - String provider_main_url = provider_json.getString(Provider.API_URL); - URL new_cert_string_url = new URL(provider_main_url + "/" + provider_json.getString(Provider.API_VERSION) + "/" + Constants.CERTIFICATE); - - String cert_string = downloadWithProviderCA(new_cert_string_url.toString()); - - if(cert_string.isEmpty() || ConfigHelper.checkErroneousDownload(cert_string)) - return false; - else - return loadCertificate(cert_string); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } - } + try { + + urlConnection = (HttpsURLConnection) new URL(delete_url).openConnection(); + urlConnection.setRequestMethod("DELETE"); + urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); + + responseCode = urlConnection.getResponseCode(); + broadcastProgress(progress++); + LeapSRPSession.setToken(""); + Log.d(TAG, Integer.toString(responseCode)); + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IndexOutOfBoundsException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (IOException e) { + // TODO Auto-generated catch block + try { + if (urlConnection != null) { + responseCode = urlConnection.getResponseCode(); + if (responseCode == 401) { + broadcastProgress(progress++); + LeapSRPSession.setToken(""); + Log.d(TAG, Integer.toString(responseCode)); + return true; + } + } + } catch (IOException e1) { + e1.printStackTrace(); + } + + e.printStackTrace(); + return false; + } catch (KeyManagementException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (KeyStoreException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return true; + } + + /** + * Downloads a new OpenVPN certificate, attaching authenticated cookie for authenticated certificate. + * + * @return true if certificate was downloaded correctly, false if provider.json is not present in SharedPreferences, or if the certificate url could not be parsed as a URI, or if there was an SSL error. + */ + private boolean updateVpnCertificate() { + try { + JSONObject provider_json = new JSONObject(preferences.getString(Provider.KEY, "")); + + String provider_main_url = provider_json.getString(Provider.API_URL); + URL new_cert_string_url = new URL(provider_main_url + "/" + provider_json.getString(Provider.API_VERSION) + "/" + Constants.CERTIFICATE); + + String cert_string = downloadWithProviderCA(new_cert_string_url.toString()); + + if (cert_string.isEmpty() || ConfigHelper.checkErroneousDownload(cert_string)) + return false; + else + return loadCertificate(cert_string); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + } private boolean loadCertificate(String cert_string) { - try { - // API returns concatenated cert & key. Split them for OpenVPN options - String certificateString = null, keyString = null; - String[] certAndKey = cert_string.split("(?<=-\n)"); - for (int i=0; i < certAndKey.length-1; i++){ - if ( certAndKey[i].contains("KEY") ) { - keyString = certAndKey[i++] + certAndKey[i]; - } - else if ( certAndKey[i].contains("CERTIFICATE") ) { - certificateString = certAndKey[i++] + certAndKey[i]; - } - } - - RSAPrivateKey key = ConfigHelper.parseRsaKeyFromString(keyString); - keyString = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT); - preferences.edit().putString(Constants.PRIVATE_KEY, "-----BEGIN RSA PRIVATE KEY-----\n"+keyString+"-----END RSA PRIVATE KEY-----").commit(); - - X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(certificateString); - certificateString = Base64.encodeToString(certificate.getEncoded(), Base64.DEFAULT); - preferences.edit().putString(Constants.CERTIFICATE, "-----BEGIN CERTIFICATE-----\n"+certificateString+"-----END CERTIFICATE-----").commit(); - return true; - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } + try { + // API returns concatenated cert & key. Split them for OpenVPN options + String certificateString = null, keyString = null; + String[] certAndKey = cert_string.split("(?<=-\n)"); + for (int i = 0; i < certAndKey.length - 1; i++) { + if (certAndKey[i].contains("KEY")) { + keyString = certAndKey[i++] + certAndKey[i]; + } else if (certAndKey[i].contains("CERTIFICATE")) { + certificateString = certAndKey[i++] + certAndKey[i]; + } + } + + RSAPrivateKey key = ConfigHelper.parseRsaKeyFromString(keyString); + keyString = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT); + preferences.edit().putString(Constants.PRIVATE_KEY, "-----BEGIN RSA PRIVATE KEY-----\n" + keyString + "-----END RSA PRIVATE KEY-----").commit(); + + X509Certificate certificate = ConfigHelper.parseX509CertificateFromString(certificateString); + certificateString = Base64.encodeToString(certificate.getEncoded(), Base64.DEFAULT); + preferences.edit().putString(Constants.CERTIFICATE, "-----BEGIN CERTIFICATE-----\n" + certificateString + "-----END CERTIFICATE-----").commit(); + return true; + } catch (CertificateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } } } diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java b/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java index 0f978290..c3ba0a42 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java @@ -14,96 +14,115 @@ import android.widget.*; public class ProviderDetailFragment extends DialogFragment { final public static String TAG = "providerDetailFragment"; - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - try { - - LayoutInflater inflater = getActivity().getLayoutInflater(); - View provider_detail_view = inflater.inflate(R.layout.provider_detail_fragment, null); - - JSONObject provider_json = new JSONObject(getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, getActivity().MODE_PRIVATE).getString(Provider.KEY, "")); - - final TextView domain = (TextView)provider_detail_view.findViewById(R.id.provider_detail_domain); - domain.setText(provider_json.getString(Provider.DOMAIN)); - final TextView name = (TextView)provider_detail_view.findViewById(R.id.provider_detail_name); - name.setText(provider_json.getJSONObject(Provider.NAME).getString("en")); - final TextView description = (TextView)provider_detail_view.findViewById(R.id.provider_detail_description); - description.setText(provider_json.getJSONObject(Provider.DESCRIPTION).getString("en")); - - builder.setView(provider_detail_view); - builder.setTitle(R.string.provider_details_fragment_title); - - if(anon_allowed(provider_json)) { - builder.setPositiveButton(R.string.use_anonymously_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - interface_with_configuration_wizard.use_anonymously(); - } - }); - } - - if(registration_allowed(provider_json)) { - builder.setNegativeButton(R.string.signup_or_login_button, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - interface_with_configuration_wizard.login(); - } - }); - } - - return builder.create(); - } catch (JSONException e) { - return null; - } - } - - private boolean anon_allowed(JSONObject provider_json) { - try { - JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - return service_description.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); - } catch (JSONException e) { - return false; - } - } - - private boolean registration_allowed(JSONObject provider_json) { - try { - JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); - return service_description.has(Provider.ALLOW_REGISTRATION) && service_description.getBoolean(Provider.ALLOW_REGISTRATION); - } catch (JSONException e) { - return false; - } - } - - @Override - public void onCancel(DialogInterface dialog) { - super.onCancel(dialog); - SharedPreferences.Editor editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); - editor.remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); - interface_with_configuration_wizard.showAllProviders(); - } - - public static DialogFragment newInstance() { - ProviderDetailFragment provider_detail_fragment = new ProviderDetailFragment(); - return provider_detail_fragment; - } - - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - interface_with_configuration_wizard = (ProviderDetailFragmentInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement LogInDialogListener"); + + rride + pub ic + + Dialog onCreateDialog(Bundle savedInstanceState) { + Al tDialog.Builder builder = new AlertDialog.Builder(getActivity()); + tr { + + L utInflater inflater = getActivity().getLayoutInflater(); + V provider_detail_view = inflater.inflate(R.layout.provider_detail_fragment, null); + + + ect provider_json = new JSONObject(getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, getActivity().MODE_PRIVATE).getString(Provider.KEY, "")); + + fin + View domain = (TextView) provider_d + etail_view.findViewById(R.id.provider_detail_domain); + domain.Text(provider_json.getString(Provider.DOMAIN)); + final T View name = (TextView) provider_d + etail_view.findViewById(R.id.provider_detail_name); + name.se xt (provider_json.getJSONObject(Provider.NAME).getString("en")); + final T View description = (TextView) provider_d + etail_view.findViewById(R.id.provider_detail_description); + descrip n.setText(provider_json.getJSONObject(Provider.DESCRIPTION).getString("en")); + + builde + ew(provider_detail_view); + builder.se tle (R.string.provider_details_fragment_title); + + if (anon_a + pr ovider_json)){ + builder.setP iveButton + (R.string.use_anonymously_button, new DialogInterface.OnClickListener() { + public void ick(DialogInterface dialog, int id) { + interface_ onfiguration_wizard.use_anonymously(); + } + }); + regi ati al lowed (provider_json)){ + builder.setN iveButton + (R.string.signup_or_login_button, new DialogInterface.OnClickListener() { + public void ick(DialogInterface dialog, int id) { + interface_ onfiguration_wizard.login(); + } + }); + urn lde reate(); + }catch(JSONE eption e){ + return null; + + } + + private oo ea + non_allowed(JSONObject provider_json) { + try { + JSONOb ct ser e_description = provider_json.getJSONObject(Provider.SERVICE); + return service + scription.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); + } catch (JSONEx ption e){ + return false; + + } + + private ol an + gistration_allowed(JSONObject provider_json) { + try { + JSONObj t serv _description = provider_json.getJSONObject(Provider.SERVICE); + return service_ + cription.has(Provider.ALLOW_REGISTRATION) && service_description.getBoolean(Provider.ALLOW_REGISTRATION); + } catch (JSONExc tion e){ + return false; + } + + @Override pu li + oid onCanc l(DialogInterface dialog) { + super.onCancel(di og); + SharedPreferences ditor + editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); + editor.remove(Pro der.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); + interface_with_co iguration_wizard.showAllProviders(); + } + + public static ial gFragment newInstance() { + ProviderDetailFra ent provider_detail_fragment = new ProviderDetailFragment(); + return provider_d ail_fragment; + } + + @Override + ublic void onAttach (Activity activity){ + super.onAttach(activity); + try { + interface_with_conf guration_wizard = (ProviderDetailFragmentInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement LogInDialogListener"); + } + } + + public interface P + iderDetailFragmentInterface { + public void login() + + public void use_ano + + mously(); + + public void showAll + + oviders(); + } + + ProviderDetailFr gm + Interface interface_with_configuration_wizard; } - } - - public interface ProviderDetailFragmentInterface { - public void login(); - public void use_anonymously(); - public void showAllProviders(); - } - - ProviderDetailFragmentInterface interface_with_configuration_wizard; -} diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java b/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java index 64e5be07..b604ce09 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java @@ -1,7 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * - * This program is free software: you can redistribute it and/or modify + * * 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. @@ -14,64 +13,110 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +ackage se.leap.bitmaskclient; -import java.io.*; -import java.util.*; -import java.net.*; + import java.io.*; + import java.util.*; + import java.net.*; /** * Models the provider list shown in the ConfigurationWizard. - * - * @author parmegv * + * @author parmegv */ public class ProviderListContent { - public static List ITEMS = new ArrayList(); - - public static Map ITEM_MAP = new HashMap(); - - /** - * Adds a new provider item to the end of the items map, and to the items list. - * @param item - */ - public static void addItem(ProviderItem item) { - ITEMS.add(item); - ITEM_MAP.put(String.valueOf(ITEMS.size()), item); - } - public static void removeItem(ProviderItem item) { - ITEMS.remove(item); - ITEM_MAP.remove(item); - } - - /** - * A provider item. - */ - public static class ProviderItem { - final public static String CUSTOM = "custom"; - private String provider_main_url; - private String name; - - /** - * @param name of the provider - * @param provider_main_url used to download provider.json file of the provider - */ - public ProviderItem(String name, String provider_main_url) { - this.name = name; - this.provider_main_url = provider_main_url; - } - - public String name() { return name; } - - public String providerMainUrl() { return provider_main_url; } - - public String domain() { - try { - return new URL(provider_main_url).getHost(); - } catch (MalformedURLException e) { - return provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); - } - } - } -} + p blic + static List ITEMS = new ArrayList(); + + p blic + static Map ITEM_MAP = new HashMap(); + + / * + * + Adds a + new + provider item + to the + end of + the items + map, + and to + the items + list. + * + * + @param + item + / + p blic + + static void addItem(ProviderItem item) { + EMS.add(item); + EM_MAP.put(String.valueOf(ITEMS.size()), item); + } + + p + blic + + static void removeItem(ProviderItem item) { + EMS.remove(item); + EM_MAP.remove(item); + } + + / * + * + A provider + item. + / + p blic + + static class ProviderItem { + nal + public static String CUSTOM = "custom"; + ivate String + provider_main_url; + ivate String + name; + + * + * + @param + name of + the provider + * + @param + provider_main_url used + to download + provider.json file + of the + provider + / + + blic ProviderItem(String name, String provider_main_url) { + is.name = name; + is.provider_main_url = provider_main_url; + + + ic String name() { + ret + urn name; + } + + + p + String providerMainUrl () { + retur + n provider_main_url; + } + + + pub + tring domain () { + try { + retu ew URL(provider_main_url).getHost(); + } cat(MalformedURLException e) { + retu rovider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); + } + } + } -- cgit v1.2.3 From e54c7a5407da82feb6dc6fb0de5e2d9f3756b651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 19:29:55 +0100 Subject: Remove all Logs. In the near future, I should implement an error feedback mechanism (ideally using ics-openvpn's log view), and avoid the e.printStackTrace() and Log.d("","") without losing information. --- .../java/se/leap/bitmaskclient/ProviderAPI.java | 9 +- .../main/java/se/leap/bitmaskclient/Dashboard.java | 1 - .../java/se/leap/bitmaskclient/EipFragment.java | 7 -- .../java/se/leap/bitmaskclient/OnBootReceiver.java | 3 - .../java/se/leap/bitmaskclient/eip/EipStatus.java | 3 - .../java/se/leap/bitmaskclient/eip/Gateway.java | 4 - .../se/leap/bitmaskclient/eip/GatewaysManager.java | 4 +- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 5 +- .../bitmaskclient/eip/VpnCertificateValidator.java | 4 - .../java/se/leap/bitmaskclient/ProviderAPI.java | 9 +- .../se/leap/bitmaskclient/ProviderListContent.java | 135 +++++++-------------- 11 files changed, 51 insertions(+), 133 deletions(-) diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index 50d09c55..0b24e1c9 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -93,8 +93,7 @@ public class ProviderAPI extends IntentService { } public ProviderAPI() { - super("ProviderAPI"); - Log.v("ClassName", "Provider API"); + super(TAG); } @Override @@ -403,8 +402,6 @@ public class ProviderAPI extends IntentService { parameters.put("user[login]", username); parameters.put("user[password_salt]", salt); parameters.put("user[password_verifier]", password_verifier); - Log.d(TAG, server_url); - Log.d(TAG, parameters.toString()); return sendToServer(server_url + "/users.json", "POST", parameters); } @@ -465,11 +462,9 @@ public class ProviderAPI extends IntentService { InputStream error_stream = urlConnection.getErrorStream(); if (error_stream != null) { String error_response = new Scanner(error_stream).useDelimiter("\\A").next(); - Log.d("Error", error_response); try { error_message = new JSONObject(error_response); } catch (JSONException e) { - Log.d(TAG, e.getMessage()); e.printStackTrace(); } urlConnection.disconnect(); @@ -870,7 +865,6 @@ public class ProviderAPI extends IntentService { responseCode = urlConnection.getResponseCode(); broadcastProgress(progress++); LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -887,7 +881,6 @@ public class ProviderAPI extends IntentService { if (responseCode == 401) { broadcastProgress(progress++); LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); return true; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java index 2e9aa74c..ee64a1b3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java +++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java @@ -421,7 +421,6 @@ public class Dashboard extends Activity implements SessionDialog.SessionDialogIn @Override public void onReceiveResult(int resultCode, Bundle resultData) { - Log.d(TAG, "onReceiveResult"); if (resultCode == ProviderAPI.SUCCESSFUL_SIGNUP) { String username = resultData.getString(SessionDialog.USERNAME); String password = resultData.getString(SessionDialog.PASSWORD); diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 3ad658f5..02eedd77 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -76,8 +76,6 @@ public class EipFragment extends Fragment implements Observer { if (eip_status.isConnecting()) eip_switch.setVisibility(View.VISIBLE); - Log.d(TAG, "onCreateView, eip_switch is checked? " + eip_switch.isChecked()); - Bundle arguments = getArguments(); if (arguments != null && arguments.containsKey(START_ON_BOOT) && arguments.getBoolean(START_ON_BOOT)) startEipFromScratch(); @@ -149,8 +147,6 @@ public class EipFragment extends Fragment implements Observer { private boolean canLogInToStartEIP() { boolean isAllowedRegistered = Dashboard.preferences.getBoolean(Constants.ALLOWED_REGISTERED, false); boolean isLoggedIn = !LeapSRPSession.getToken().isEmpty(); - Log.d(TAG, "Allow registered? " + isAllowedRegistered); - Log.d(TAG, "Is logged in? " + isLoggedIn); return isAllowedRegistered && !isLoggedIn && !eip_status.isConnecting() && !eip_status.isConnected(); } @@ -283,7 +279,6 @@ public class EipFragment extends Fragment implements Observer { private void setConnectedUI() { hideProgressBar(); - Log.d(TAG, "setConnectedUi? " + eip_status.isConnected()); adjustSwitch(); is_starting_to_connect = false; status_message.setText(dashboard.getString(R.string.eip_state_connected)); @@ -302,12 +297,10 @@ public class EipFragment extends Fragment implements Observer { private void adjustSwitch() { if (eip_status.isConnected() || eip_status.isConnecting() || is_starting_to_connect) { - Log.d(TAG, "adjustSwitch, isConnected || isConnecting, is checked"); if (!eip_switch.isChecked()) { eip_switch.setChecked(true); } } else { - Log.d(TAG, "adjustSwitch, !isConnected && !isConnecting? " + eip_status.toString()); if (eip_switch.isChecked()) { eip_switch.setChecked(false); diff --git a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java index 073d727f..9171e816 100644 --- a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java +++ b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java @@ -1,7 +1,6 @@ package se.leap.bitmaskclient; import android.content.*; -import android.util.*; import se.leap.bitmaskclient.eip.*; @@ -15,8 +14,6 @@ public class OnBootReceiver extends BroadcastReceiver { preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE); boolean provider_configured = !preferences.getString(Provider.KEY, "").isEmpty(); boolean start_on_boot = preferences.getBoolean(Dashboard.START_ON_BOOT, false); - Log.d("OnBootReceiver", "Provider configured " + String.valueOf(provider_configured)); - Log.d("OnBootReceiver", "Start on boot " + String.valueOf(start_on_boot)); if (provider_configured && start_on_boot) { Intent dashboard_intent = new Intent(context, Dashboard.class); dashboard_intent.setAction(Constants.ACTION_START_EIP); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index e636c693..ad68f96e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -16,7 +16,6 @@ */ package se.leap.bitmaskclient.eip; -import android.util.*; import android.content.*; import java.util.*; @@ -51,7 +50,6 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { setConnectedOrDisconnected(); } else if (isConnecting()) setConnecting(); - Log.d(TAG, "update state with level " + level); } private void updateStatus(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { @@ -93,7 +91,6 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { } public void setConnectedOrDisconnected() { - Log.d(TAG, "setConnectedOrDisconnected()"); wants_to_disconnect = false; current_status.setChanged(); current_status.notifyObservers(); 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 b2c7af8c..ff7d011e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -16,8 +16,6 @@ */ package se.leap.bitmaskclient.eip; -import android.util.*; - import com.google.gson.*; import org.json.*; @@ -106,12 +104,10 @@ public class Gateway { return cp.convertProfile(); } catch (ConfigParser.ConfigParseError e) { // FIXME We didn't get a VpnProfile! Error handling! and log level - Log.v(TAG, "Error creating VPNProfile"); e.printStackTrace(); return null; } catch (IOException e) { // FIXME We didn't get a VpnProfile! Error handling! and log level - Log.v(TAG, "Error creating VPNProfile"); e.printStackTrace(); return null; } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index b41fcfd4..1c64328e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -17,7 +17,6 @@ package se.leap.bitmaskclient.eip; import android.content.*; -import android.util.*; import com.google.gson.*; import com.google.gson.reflect.*; @@ -77,8 +76,7 @@ public class GatewaysManager { for (Gateway gateway : gateways_list) addGateway(gateway); this.gateways.addAll(gateways_list); - } else - Log.d("GatewaysManager", "No gateways added"); + } } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index ac3d5d77..dac92fe2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -31,7 +31,6 @@ public class VoidVpnService extends VpnService { } catch (Exception e) { e.printStackTrace(); } - android.util.Log.d(TAG, "VoidVpnService set up: fd = " + fd.toString()); } }); thread.run(); @@ -53,10 +52,8 @@ public class VoidVpnService extends VpnService { private static void closeFd() { try { - if (fd != null) { - android.util.Log.d(TAG, "VoidVpnService closing fd = " + fd.toString()); + if (fd != null) fd.close(); - } } catch (IOException e) { e.printStackTrace(); } 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 025d049d..8fce6a37 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -16,8 +16,6 @@ */ package se.leap.bitmaskclient.eip; -import android.util.*; - import java.security.cert.*; import java.util.*; @@ -42,7 +40,6 @@ public class VpnCertificateValidator { private boolean isValid(X509Certificate certificate) { Calendar offset_date = calculateOffsetCertificateValidity(certificate); try { - Log.d(TAG, "offset_date = " + offset_date.getTime().toString()); certificate.checkValidity(offset_date.getTime()); return true; } catch (CertificateExpiredException e) { @@ -53,7 +50,6 @@ public class VpnCertificateValidator { } private Calendar calculateOffsetCertificateValidity(X509Certificate certificate) { - Log.d(TAG, "certificate not after = " + certificate.getNotAfter()); long preventive_time = Math.abs(certificate.getNotBefore().getTime() - certificate.getNotAfter().getTime()) / 2; long current_date_millis = Calendar.getInstance().getTimeInMillis(); diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java index e4b95567..7cf80a4f 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java @@ -92,8 +92,7 @@ public class ProviderAPI extends IntentService { } public ProviderAPI() { - super("ProviderAPI"); - Log.v("ClassName", "Provider API"); + super(TAG); } @Override @@ -397,8 +396,6 @@ public class ProviderAPI extends IntentService { parameters.put("user[login]", username); parameters.put("user[password_salt]", salt); parameters.put("user[password_verifier]", password_verifier); - Log.d(TAG, server_url); - Log.d(TAG, parameters.toString()); return sendToServer(server_url + "/users.json", "POST", parameters); } @@ -459,11 +456,9 @@ public class ProviderAPI extends IntentService { InputStream error_stream = urlConnection.getErrorStream(); if (error_stream != null) { String error_response = new Scanner(error_stream).useDelimiter("\\A").next(); - Log.d("Error", error_response); try { error_message = new JSONObject(error_response); } catch (JSONException e) { - Log.d(TAG, e.getMessage()); e.printStackTrace(); } urlConnection.disconnect(); @@ -854,7 +849,6 @@ public class ProviderAPI extends IntentService { responseCode = urlConnection.getResponseCode(); broadcastProgress(progress++); LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -871,7 +865,6 @@ public class ProviderAPI extends IntentService { if (responseCode == 401) { broadcastProgress(progress++); LeapSRPSession.setToken(""); - Log.d(TAG, Integer.toString(responseCode)); return true; } } diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java b/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java index b604ce09..235ae5ab 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderListContent.java @@ -1,6 +1,7 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * * This program is free software: you can redistribute it and/or modify + * + * 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. @@ -13,11 +14,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -ackage se.leap.bitmaskclient; +package se.leap.bitmaskclient; - import java.io.*; - import java.util.*; - import java.net.*; +import java.util.*; +import java.net.*; /** * Models the provider list shown in the ConfigurationWizard. @@ -26,97 +26,56 @@ ackage se.leap.bitmaskclient; */ public class ProviderListContent { - p blic - static List ITEMS = new ArrayList(); - - p blic - static Map ITEM_MAP = new HashMap(); + public static List ITEMS = new ArrayList(); - / * - * - Adds a - new - provider item - to the - end of - the items - map, - and to - the items - list. - * - * - @param - item - / - p blic + public static Map ITEM_MAP = new HashMap(); - static void addItem(ProviderItem item) { - EMS.add(item); - EM_MAP.put(String.valueOf(ITEMS.size()), item); + /** + * Adds a new provider item to the end of the items map, and to the items list. + * + * @param item + */ + public static void addItem(ProviderItem item) { + ITEMS.add(item); + ITEM_MAP.put(String.valueOf(ITEMS.size()), item); } - p - blic - - static void removeItem(ProviderItem item) { - EMS.remove(item); - EM_MAP.remove(item); + public static void removeItem(ProviderItem item) { + ITEMS.remove(item); + ITEM_MAP.remove(item); } - / * - * - A provider - item. - / - p blic - - static class ProviderItem { - nal - public static String CUSTOM = "custom"; - ivate String - provider_main_url; - ivate String - name; - - * - * - @param - name of - the provider - * - @param - provider_main_url used - to download - provider.json file - of the - provider - / - - blic ProviderItem(String name, String provider_main_url) { - is.name = name; - is.provider_main_url = provider_main_url; - - - ic String name() { - ret - urn name; - } - + /** + * A provider item. + */ + public static class ProviderItem { + final public static String CUSTOM = "custom"; + private String provider_main_url; + private String name; + + /** + * @param name of the provider + * @param provider_main_url used to download provider.json file of the provider + */ + public ProviderItem(String name, String provider_main_url) { + this.name = name; + this.provider_main_url = provider_main_url; + } - p - String providerMainUrl () { - retur - n provider_main_url; - } + public String name() { + return name; + } + public String providerMainUrl() { + return provider_main_url; + } - pub - tring domain () { - try { - retu ew URL(provider_main_url).getHost(); - } cat(MalformedURLException e) { - retu rovider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); - } + public String domain() { + try { + return new URL(provider_main_url).getHost(); + } catch (MalformedURLException e) { + return provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); } } + } +} -- cgit v1.2.3 From 9e5d0f1a2399787b1a57349e0e3b5ad596fcbf8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 19:35:07 +0100 Subject: ProviderDetailFragment release was bad formated --- .../leap/bitmaskclient/ProviderDetailFragment.java | 2 +- .../leap/bitmaskclient/ProviderDetailFragment.java | 185 ++++++++++----------- 2 files changed, 85 insertions(+), 102 deletions(-) diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java index 5a7362d2..bd21f91e 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderDetailFragment.java @@ -2,7 +2,7 @@ package se.leap.bitmaskclient; import org.json.*; -import se.leap.bitmaskclient.eip.Constants; +import se.leap.bitmaskclient.eip.*; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import android.app.*; diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java b/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java index c3ba0a42..24d048a6 100644 --- a/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java +++ b/app/src/release/java/se/leap/bitmaskclient/ProviderDetailFragment.java @@ -15,114 +15,97 @@ public class ProviderDetailFragment extends DialogFragment { final public static String TAG = "providerDetailFragment"; - rride - pub ic - - Dialog onCreateDialog(Bundle savedInstanceState) { - Al tDialog.Builder builder = new AlertDialog.Builder(getActivity()); - tr { - - L utInflater inflater = getActivity().getLayoutInflater(); - V provider_detail_view = inflater.inflate(R.layout.provider_detail_fragment, null); - - - ect provider_json = new JSONObject(getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, getActivity().MODE_PRIVATE).getString(Provider.KEY, "")); - - fin - View domain = (TextView) provider_d - etail_view.findViewById(R.id.provider_detail_domain); - domain.Text(provider_json.getString(Provider.DOMAIN)); - final T View name = (TextView) provider_d - etail_view.findViewById(R.id.provider_detail_name); - name.se xt (provider_json.getJSONObject(Provider.NAME).getString("en")); - final T View description = (TextView) provider_d - etail_view.findViewById(R.id.provider_detail_description); - descrip n.setText(provider_json.getJSONObject(Provider.DESCRIPTION).getString("en")); - - builde - ew(provider_detail_view); - builder.se tle (R.string.provider_details_fragment_title); - - if (anon_a - pr ovider_json)){ - builder.setP iveButton - (R.string.use_anonymously_button, new DialogInterface.OnClickListener() { - public void ick(DialogInterface dialog, int id) { - interface_ onfiguration_wizard.use_anonymously(); + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + try { + + LayoutInflater inflater = getActivity().getLayoutInflater(); + View provider_detail_view = inflater.inflate(R.layout.provider_detail_fragment, null); + + JSONObject provider_json = new JSONObject(getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, getActivity().MODE_PRIVATE).getString(Provider.KEY, "")); + + final TextView domain = (TextView) provider_detail_view.findViewById(R.id.provider_detail_domain); + domain.setText(provider_json.getString(Provider.DOMAIN)); + final TextView name = (TextView) provider_detail_view.findViewById(R.id.provider_detail_name); + name.setText(provider_json.getJSONObject(Provider.NAME).getString("en")); + final TextView description = (TextView) provider_detail_view.findViewById(R.id.provider_detail_description); + description.setText(provider_json.getJSONObject(Provider.DESCRIPTION).getString("en")); + + builder.setView(provider_detail_view); + builder.setTitle(R.string.provider_details_fragment_title); + + if (anon_allowed(provider_json)) { + builder.setPositiveButton(R.string.use_anonymously_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + interface_with_configuration_wizard.use_anonymously(); } }); - regi ati al lowed (provider_json)){ - builder.setN iveButton - (R.string.signup_or_login_button, new DialogInterface.OnClickListener() { - public void ick(DialogInterface dialog, int id) { - interface_ onfiguration_wizard.login(); - } - }); - urn lde reate(); - }catch(JSONE eption e){ - return null; - - } - - private oo ea - non_allowed(JSONObject provider_json) { - try { - JSONOb ct ser e_description = provider_json.getJSONObject(Provider.SERVICE); - return service - scription.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); - } catch (JSONEx ption e){ - return false; - - } - - private ol an - gistration_allowed(JSONObject provider_json) { - try { - JSONObj t serv _description = provider_json.getJSONObject(Provider.SERVICE); - return service_ - cription.has(Provider.ALLOW_REGISTRATION) && service_description.getBoolean(Provider.ALLOW_REGISTRATION); - } catch (JSONExc tion e){ - return false; - } - - @Override pu li - oid onCanc l(DialogInterface dialog) { - super.onCancel(di og); - SharedPreferences ditor - editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); - editor.remove(Pro der.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); - interface_with_co iguration_wizard.showAllProviders(); - } - - public static ial gFragment newInstance() { - ProviderDetailFra ent provider_detail_fragment = new ProviderDetailFragment(); - return provider_d ail_fragment; } - @Override - ublic void onAttach (Activity activity){ - super.onAttach(activity); - try { - interface_with_conf guration_wizard = (ProviderDetailFragmentInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement LogInDialogListener"); - } + if (registration_allowed(provider_json)) { + builder.setNegativeButton(R.string.signup_or_login_button, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + interface_with_configuration_wizard.login(); + } + }); } - public interface P - iderDetailFragmentInterface { - public void login() - - public void use_ano + return builder.create(); + } catch (JSONException e) { + return null; + } + } + + private boolean anon_allowed(JSONObject provider_json) { + try { + JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); + return service_description.has(Constants.ALLOWED_ANON) && service_description.getBoolean(Constants.ALLOWED_ANON); + } catch (JSONException e) { + return false; + } + } + + private boolean registration_allowed(JSONObject provider_json) { + try { + JSONObject service_description = provider_json.getJSONObject(Provider.SERVICE); + return service_description.has(Provider.ALLOW_REGISTRATION) && service_description.getBoolean(Provider.ALLOW_REGISTRATION); + } catch (JSONException e) { + return false; + } + } + + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + SharedPreferences.Editor editor = getActivity().getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE).edit(); + editor.remove(Provider.KEY).remove(Constants.ALLOWED_ANON).remove(Constants.KEY).commit(); + interface_with_configuration_wizard.showAllProviders(); + } + + public static DialogFragment newInstance() { + ProviderDetailFragment provider_detail_fragment = new ProviderDetailFragment(); + return provider_detail_fragment; + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + interface_with_configuration_wizard = (ProviderDetailFragmentInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement LogInDialogListener"); + } + } - mously(); + public interface ProviderDetailFragmentInterface { + public void login(); - public void showAll + public void use_anonymously(); - oviders(); - } + public void showAllProviders(); + } - ProviderDetailFr gm - Interface interface_with_configuration_wizard; - } + ProviderDetailFragmentInterface interface_with_configuration_wizard; +} -- cgit v1.2.3 From a8b4c47e44ea7f2eb6f0c032af344fb04b255694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 19:50:26 +0100 Subject: Conjugate the verb to be in user session status. --- .../java/se/leap/bitmaskclient/UserSessionStatus.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java index 369df1a3..e43c8a25 100644 --- a/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/UserSessionStatus.java @@ -68,7 +68,20 @@ public class UserSessionStatus extends Observable { @Override public String toString() { - return User.userName() + " is " + String username = User.userName(); + + return username + " " + conjugateToBe(username) + " " + session_status.toString().toLowerCase().replaceAll("_", " "); } + + private String conjugateToBe(String subject) { + String conjugation = ""; + if(subject.equalsIgnoreCase("I")) + conjugation = "am"; + else if(subject.equalsIgnoreCase("you") || subject.equalsIgnoreCase("we")|| subject.equalsIgnoreCase("they")) + conjugation = "are"; + else conjugation = "is"; + + return conjugation; + } } -- cgit v1.2.3 From 5734bbe4e7e509b81c6670c8a40225997593ddef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 27 Mar 2015 13:40:12 +0100 Subject: Just coded, not tested --- .../test/testConfigurationWizard.java | 72 +++++++++++++++++++++- .../test/testDashboardIntegration.java | 2 +- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java index 7a19409b..c01e21c6 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java @@ -1,6 +1,14 @@ package se.leap.bitmaskclient.test; +import android.app.Instrumentation; +import android.content.Intent; +import android.graphics.drawable.Drawable; import android.test.*; +import android.view.ActionProvider; +import android.view.ContextMenu; +import android.view.MenuItem; +import android.view.SubMenu; +import android.view.View; import android.widget.*; import com.robotium.solo.*; @@ -97,14 +105,74 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2 Date: Fri, 27 Mar 2015 13:43:36 +0100 Subject: 0.9.3 RC3 --- CHANGELOG | 3 +++ app/src/main/AndroidManifest.xml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 12fdeb6e..98eebcdb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,9 @@ Bugs: - Don't try to log out if nothing is logged in. - Give better user feedback to some problems - Switching orientation doesn't hide any useful message +- Blocking VPN is more consistently removed if an error happens +- Typo: you is not logged in -> you *are* not logged in +- Cleaner log messages to logcat Features: - Show the user status progress and info in its own place diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 07315bdd..6d947bc3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,8 +17,8 @@ + android:versionCode="119" + android:versionName="0.9.3RC3" > -- cgit v1.2.3 From 76396cda955c3681db9aa86224f465e3fd2ebf4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 30 Mar 2015 17:53:50 +0200 Subject: testEveryProvider() coded and working. Calyx fails. --- app/build.gradle | 2 +- .../test/testConfigurationWizard.java | 25 ++------ .../test/testDashboardIntegration.java | 68 ++++++++++++++++++++-- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4d3c5b9c..4441cafa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,7 +39,7 @@ android { } dependencies { - androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1' + androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.3.1' compile 'com.jakewharton:butterknife:6.0.0+' provided 'com.squareup.dagger:dagger-compiler:1.2.2+' compile 'com.github.pedrovgs:renderers:1.3+' diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java index c01e21c6..efb843f7 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java @@ -40,7 +40,7 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2 Date: Mon, 30 Mar 2015 18:16:13 +0200 Subject: Cleaning moved methods. --- .../test/testConfigurationWizard.java | 51 ---------------------- 1 file changed, 51 deletions(-) diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java index efb843f7..1fa4cf2f 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java @@ -1,14 +1,6 @@ package se.leap.bitmaskclient.test; -import android.app.Instrumentation; -import android.content.Intent; -import android.graphics.drawable.Drawable; import android.test.*; -import android.view.ActionProvider; -import android.view.ContextMenu; -import android.view.MenuItem; -import android.view.SubMenu; -import android.view.View; import android.widget.*; import com.robotium.solo.*; @@ -119,47 +111,4 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2 Date: Mon, 30 Mar 2015 18:37:39 +0200 Subject: Wait for the vpn certificate download to start vpn in test --- .../se/leap/bitmaskclient/test/testDashboardIntegration.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java index bcabc46c..d2fb9901 100644 --- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java +++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java @@ -162,26 +162,28 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2 Date: Fri, 10 Apr 2015 11:18:02 +0200 Subject: 0.9.3 --- app/src/main/AndroidManifest.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6d947bc3..cc31de43 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,8 +17,8 @@ + android:versionCode="120" + android:versionName="0.9.3" > -- cgit v1.2.3