summaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
authorParménides GV <parmegv@sdf.org>2015-01-02 13:47:09 +0100
committerParménides GV <parmegv@sdf.org>2015-01-02 13:47:09 +0100
commit3229c824b3d7935388abacb9fa578c27800c5c49 (patch)
tree827403fe527c50e61299b5b42dbc39e55b65ed39 /app/src
parent44b59b984f76da62d409b585047224cb1e958016 (diff)
parent38fdfbdacf414ab85fffd833f57e5fc93b03ad4f (diff)
Merge branch 'bug/Bitmask-gets-killed-problems-when-trying-to-restart-it-#6586' into develop
Diffstat (limited to 'app/src')
-rw-r--r--app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java2
-rw-r--r--app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java13
-rw-r--r--app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java57
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Dashboard.java4
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/EipFragment.java17
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java31
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EIP.java177
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java47
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java56
-rw-r--r--app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java12
-rw-r--r--app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java119
11 files changed, 351 insertions, 184 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 229c3452..78ce1c81 100644
--- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java
+++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testConfigurationWizard.java
@@ -49,7 +49,7 @@ public class testConfigurationWizard extends ActivityInstrumentationTestCase2<Co
solo.enterText(0, "calyx.net");
solo.clickOnCheckBox(0);
solo.clickOnText(solo.getString(R.string.save));
- added_providers = added_providers+1;
+ //added_providers = added_providers+1;
assertTrue("Provider details dialog did not appear", solo.waitForFragmentByTag(ProviderDetailFragment.TAG, 60*1000));
solo.goBack();
}
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 94cb67a3..0c7db284 100644
--- a/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java
+++ b/app/src/androidTest/java/se/leap/bitmaskclient/test/testDashboardIntegration.java
@@ -9,6 +9,10 @@ import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import com.robotium.solo.Solo;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Scanner;
+
import de.blinkt.openvpn.activities.DisconnectVPN;
import se.leap.bitmaskclient.ConfigurationWizard;
import se.leap.bitmaskclient.Dashboard;
@@ -150,4 +154,13 @@ public class testDashboardIntegration extends ActivityInstrumentationTestCase2<D
"-----END CERTIFICATE-----";
}
+
+ public void testReboot() {
+ try {
+ String command = "adb shell am broadcast -a android.intent.action.BOOT_COMPLETED";
+ Runtime.getRuntime().exec(command);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java
index ce13d322..d63d641a 100644
--- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java
+++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java
@@ -232,8 +232,8 @@ public class ProviderAPI extends IntentService {
Bundle result = new Bundle();
int progress = 0;
- String username = (String) task.get(SessionDialog.USERNAME);
- String password = (String) task.get(SessionDialog.PASSWORD);
+ String username = task.getString(SessionDialog.USERNAME);
+ String password = task.getString(SessionDialog.PASSWORD);
if(validUserLoginData(username, password)) {
result = authenticate(username, password);
broadcast_progress(progress++);
@@ -243,7 +243,7 @@ public class ProviderAPI extends IntentService {
result.putString(SessionDialog.USERNAME, username);
result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true);
}
- if(username.isEmpty()) {
+ if(!validUsername(username)) {
result.putBoolean(RESULT_KEY, false);
result.putBoolean(SessionDialog.USERNAME_MISSING, true);
}
@@ -327,21 +327,25 @@ public class ProviderAPI extends IntentService {
/**
* Validates parameters entered by the user to log in
- * @param entered_username
- * @param entered_password
+ * @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 entered_username, String entered_password) {
- return !(entered_username.isEmpty()) && wellFormedPassword(entered_password);
+ 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 entered_password
+ * @param password
* @return true if the entered password length is greater or equal to eight (8).
*/
- private boolean wellFormedPassword(String entered_password) {
- return entered_password.length() >= 8;
+ private boolean wellFormedPassword(String password) {
+ return password != null && password.length() >= 8;
}
/**
@@ -826,20 +830,23 @@ public class ProviderAPI extends IntentService {
return string;
}
- /**
- * Logs out from the api url retrieved from the task.
- * @return true if there were no exceptions
- */
+ /**
+ * Logs out from the api url retrieved from the task.
+ * @return true if there were no exceptions
+ */
private boolean logOut() {
+ String delete_url = provider_api_url + "/logout";
+
+ HttpsURLConnection urlConnection = null;
+ int responseCode = 0;
+ int progress = 0;
try {
- String delete_url = provider_api_url + "/logout";
- int progress = 0;
- HttpsURLConnection urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection();
+ urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection();
urlConnection.setRequestMethod("DELETE");
urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory());
- int responseCode = urlConnection.getResponseCode();
+ responseCode = urlConnection.getResponseCode();
broadcast_progress(progress++);
LeapSRPSession.setToken("");
Log.d(TAG, Integer.toString(responseCode));
@@ -853,6 +860,20 @@ public class ProviderAPI extends IntentService {
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
+ try {
+ if(urlConnection != null) {
+ responseCode = urlConnection.getResponseCode();
+ if(responseCode == 401) {
+ broadcast_progress(progress++);
+ LeapSRPSession.setToken("");
+ Log.d(TAG, Integer.toString(responseCode));
+ return true;
+ }
+ }
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ }
+
e.printStackTrace();
return false;
} catch (KeyManagementException e) {
diff --git a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java
index 659cd22c..4e6120ab 100644
--- a/app/src/main/java/se/leap/bitmaskclient/Dashboard.java
+++ b/app/src/main/java/se/leap/bitmaskclient/Dashboard.java
@@ -108,7 +108,9 @@ 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 | JSONException e) {
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (JSONException e) {
e.printStackTrace();
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
index 27c70c43..54432033 100644
--- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
@@ -109,15 +109,10 @@ public class EipFragment extends Fragment implements Observer {
super.onSaveInstanceState(outState);
}
- protected void saveEipStatus() {
- boolean eip_is_on = false;
- Log.d(TAG, "saveEipStatus");
- if(eip_switch.isChecked()) {
- eip_is_on = true;
- }
-
- if(parent_activity != null)
- Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, eip_is_on).commit();
+ 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();
}
@OnCheckedChanged(R.id.eipSwitch)
@@ -127,7 +122,7 @@ public class EipFragment extends Fragment implements Observer {
else
handleSwitchOff();
- saveEipStatus();
+ saveStatus();
}
private void handleSwitchOn() {
@@ -192,8 +187,8 @@ public class EipFragment extends Fragment implements Observer {
if(!eip_switch.isChecked()) {
eip_switch.setChecked(true);
- saveEipStatus();
}
+ saveStatus();
eipCommand(Constants.ACTION_START_EIP);
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java
index 07ed6c8f..96b87085 100644
--- a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java
+++ b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java
@@ -3,22 +3,29 @@ 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 se.leap.bitmaskclient.eip.Constants;
public class OnBootReceiver extends BroadcastReceiver {
- // Debug: am broadcast -a android.intent.action.BOOT_COMPLETED
- @Override
- public void onReceive(Context context, Intent intent) {
- if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
- if (!context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE).getString(Provider.KEY, "").isEmpty() && context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Context.MODE_PRIVATE).getBoolean(Dashboard.START_ON_BOOT, false)) {
- 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);
- }
- }
+ 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);
+ 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);
}
+ }
}
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 3d3070c8..dcf36a82 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
@@ -25,19 +25,27 @@ import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Iterator;
import java.util.List;
+import java.util.NoSuchElementException;
import de.blinkt.openvpn.LaunchVPN;
import de.blinkt.openvpn.VpnProfile;
+import de.blinkt.openvpn.core.Connection;
import de.blinkt.openvpn.core.ProfileManager;
import se.leap.bitmaskclient.Dashboard;
import se.leap.bitmaskclient.EipFragment;
+import se.leap.bitmaskclient.Provider;
import static se.leap.bitmaskclient.eip.Constants.ACTION_CHECK_CERT_VALIDITY;
import static se.leap.bitmaskclient.eip.Constants.ACTION_IS_EIP_RUNNING;
@@ -71,7 +79,7 @@ public final class EIP extends IntentService {
private static SharedPreferences preferences;
private static JSONObject eip_definition;
- private static List<Gateway> gateways = new ArrayList<Gateway>();
+ private static List<Gateway> gateways = new ArrayList<>();
private static ProfileManager profile_manager;
private static Gateway gateway;
@@ -84,12 +92,14 @@ public final class EIP extends IntentService {
super.onCreate();
context = getApplicationContext();
- profile_manager = ProfileManager.getInstance(context);
+ preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE);
- preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE);
- refreshEipDefinition();
+ profile_manager = ProfileManager.getInstance(context);
+ eip_definition = eipDefinitionFromPreferences();
+ if(gateways.isEmpty())
+ gateways = gatewaysFromPreferences();
}
-
+
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
@@ -101,7 +111,7 @@ public final class EIP extends IntentService {
stopEIP();
else if (action.equals(ACTION_IS_EIP_RUNNING))
isRunning();
- else if (action.equals(ACTION_UPDATE_EIP_SERVICE))
+ else if (action.equals(ACTION_UPDATE_EIP_SERVICE))
updateEIPService();
else if (action.equals(ACTION_CHECK_CERT_VALIDITY))
checkCertValidity();
@@ -119,11 +129,13 @@ public final class EIP extends IntentService {
GatewaySelector gateway_selector = new GatewaySelector(gateways);
gateway = gateway_selector.select();
+ Log.d(TAG, "Connecting to " + gateway.getProfile().getUUIDString());
if(gateway != null && gateway.getProfile() != null) {
mReceiver = EipFragment.getReceiver();
launchActiveGateway();
- }
- tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK);
+ tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK);
+ } else
+ tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED);
}
/**
@@ -141,6 +153,8 @@ public final class EIP extends IntentService {
intent.setAction(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(LaunchVPN.EXTRA_NAME, gateway.getProfile().getName());
+ Log.d(TAG, gateway.getProfile().mClientCertFilename);
+ Log.d(TAG, gateway.getProfile().mClientKeyFilename);
intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true);
startActivity(intent);
}
@@ -173,46 +187,58 @@ public final class EIP extends IntentService {
* TODO Implement API call to refresh eip-service.json from the provider
*/
private void updateEIPService() {
- refreshEipDefinition();
- deleteAllVpnProfiles();
- updateGateways();
+ eip_definition = eipDefinitionFromPreferences();
+ if(eip_definition != null)
+ updateGateways();
tellToReceiver(ACTION_UPDATE_EIP_SERVICE, Activity.RESULT_OK);
}
- private void refreshEipDefinition() {
+ private JSONObject eipDefinitionFromPreferences() {
try {
String eip_definition_string = preferences.getString(KEY, "");
if(!eip_definition_string.isEmpty()) {
- eip_definition = new JSONObject(eip_definition_string);
+ return new JSONObject(eip_definition_string);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
+ return null;
}
-
- private void deleteAllVpnProfiles() {
- Collection<VpnProfile> profiles = profile_manager.getProfiles();
- profiles.removeAll(profiles);
- gateways.clear();
+
+ private List<Gateway> gatewaysFromPreferences() {
+ List<Gateway> result;
+
+ String gateways_string = preferences.getString(Gateway.TAG, "");
+ Log.d(TAG, "Recovering gateways: " + gateways_string);
+ Type type_list_gateways = new TypeToken<ArrayList<Gateway>>() {}.getType();
+ result = gateways_string.isEmpty() ?
+ new ArrayList<Gateway>()
+ : (List<Gateway>) new Gson().fromJson(gateways_string, type_list_gateways);
+ Log.d(TAG, "Gateways from preferences = " + result.size());
+ preferences.edit().remove(Gateway.TAG);
+ return result;
}
/**
* Walk the list of gateways defined in eip-service.json and parse them into
* Gateway objects.
- * TODO Store the Gateways (as Serializable) in SharedPreferences
*/
private void updateGateways(){
try {
- if(eip_definition != null) {
JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways");
for (int i = 0; i < gatewaysDefined.length(); i++) {
JSONObject gw = gatewaysDefined.getJSONObject(i);
if (isOpenVpnGateway(gw)) {
- addGateway(new Gateway(eip_definition, context, gw));
+ JSONObject secrets = secretsConfiguration();
+ Gateway aux = new Gateway(eip_definition, secrets, gw);
+ Log.d(TAG, "Possible new gateway: " + aux.getProfile().getUUIDString());
+ if(!containsProfileWithSecrets(aux.getProfile())) {
+ addGateway(aux);
+ }
}
}
- }
+ gatewaysToPreferences();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -228,9 +254,114 @@ public final class EIP extends IntentService {
}
}
+
+ private JSONObject secretsConfiguration() {
+ JSONObject result = new JSONObject();
+ try {
+ result.put(Provider.CA_CERT, preferences.getString(Provider.CA_CERT, ""));
+ result.put(Constants.PRIVATE_KEY, preferences.getString(Constants.PRIVATE_KEY, ""));
+ result.put(Constants.CERTIFICATE, preferences.getString(Constants.CERTIFICATE, ""));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
private void addGateway(Gateway gateway) {
- profile_manager.addProfile(gateway.getProfile());
+ VpnProfile profile = gateway.getProfile();
+ removeGateway(gateway);
+
+ profile_manager.addProfile(profile);
+ profile_manager.saveProfile(context, profile);
+ profile_manager.saveProfileList(context);
+
gateways.add(gateway);
+ Log.d(TAG, "Gateway added: " + gateway.getProfile().getUUIDString());
+ }
+
+ private void removeGateway(Gateway gateway) {
+ VpnProfile profile = gateway.getProfile();
+ removeDuplicatedProfile(profile);
+ removeDuplicatedGateway(profile);
+ }
+
+ private void removeDuplicatedProfile(VpnProfile original) {
+ if(containsProfile(original)) {
+ VpnProfile remove = duplicatedProfile(original);
+ profile_manager.removeProfile(context, remove);
+ Log.d(TAG, "Removing profile " + remove.getUUIDString());
+ }if(containsProfile(original)) removeDuplicatedProfile(original);
+ }
+
+ private boolean containsProfile(VpnProfile profile) {
+ Collection<VpnProfile> profiles = profile_manager.getProfiles();
+ for(VpnProfile aux : profiles) {
+ if (sameConnections(profile.mConnections, aux.mConnections)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean containsProfileWithSecrets(VpnProfile profile) {
+ boolean result = false;
+
+ if(containsProfile(profile)) {
+ Collection<VpnProfile> profiles = profile_manager.getProfiles();
+ for(VpnProfile aux : profiles) {
+ result = result == false ?
+ sameConnections(profile.mConnections, aux.mConnections)
+ && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename)
+ && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename)
+ : true;
+ }
+ }
+ return result;
+ }
+
+ private VpnProfile duplicatedProfile(VpnProfile profile) {
+ VpnProfile duplicated = null;
+ Collection<VpnProfile> profiles = profile_manager.getProfiles();
+ for(VpnProfile aux : profiles) {
+ if (sameConnections(profile.mConnections, aux.mConnections)) {
+ duplicated = aux;
+ }
+ }
+ if(duplicated != null) return duplicated;
+ else throw new NoSuchElementException(profile.getName());
+ }
+
+ private boolean sameConnections(Connection[] c1, Connection[] c2) {
+ int same_connections = 0;
+ for(Connection c1_aux : c1) {
+ for(Connection c2_aux : c2)
+ if(c2_aux.mServerName.equals(c1_aux.mServerName)) {
+ same_connections++;
+ break;
+ }
+ }
+ return c1.length == c2.length && c1.length == same_connections;
+
+ }
+
+ private void removeDuplicatedGateway(VpnProfile profile) {
+ Iterator<Gateway> it = gateways.iterator();
+ List<Gateway> gateways_to_remove = new ArrayList<>();
+ while(it.hasNext()) {
+ Gateway aux = it.next();
+ if(sameConnections(aux.getProfile().mConnections, profile.mConnections)) {
+ gateways_to_remove.add(aux);
+ Log.d(TAG, "Removing gateway " + aux.getProfile().getUUIDString());
+ }
+ }
+ gateways.removeAll(gateways_to_remove);
+ }
+
+ private void gatewaysToPreferences() {
+ Type type_list_gateways = new TypeToken<List<Gateway>>() {}.getType();
+ String gateways_string = new Gson().toJson(gateways, type_list_gateways);
+ Log.d(TAG, "Saving gateways: " + gateways_string);
+ preferences.edit().putString(Gateway.TAG, gateways_string).apply();
}
private void checkCertValidity() {
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
index 3ee9443c..daf7d4a7 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
@@ -17,7 +17,6 @@
package se.leap.bitmaskclient.eip;
import android.app.Activity;
-import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
@@ -26,12 +25,9 @@ import org.json.JSONObject;
import java.io.IOException;
import java.io.StringReader;
-import java.util.Collection;
-import java.util.Iterator;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.ConfigParser;
-import de.blinkt.openvpn.core.ProfileManager;
import se.leap.bitmaskclient.Dashboard;
/**
@@ -44,48 +40,30 @@ import se.leap.bitmaskclient.Dashboard;
*/
public class Gateway {
- private String TAG = Gateway.class.getSimpleName();
-
+ public static String TAG = Gateway.class.getSimpleName();
+
+ private JSONObject general_configuration;
+ private JSONObject secrets;
+ private JSONObject gateway;
+
private String mName;
private int timezone;
- private JSONObject general_configuration;
- private Context context;
private VpnProfile mVpnProfile;
- private JSONObject mGateway;
-
/**
* Build a gateway object from a JSON OpenVPN gateway definition in eip-service.json
* and create a VpnProfile belonging to it.
- *
- * @param gateway The JSON OpenVPN gateway definition to parse
*/
- protected Gateway(JSONObject eip_definition, Context context, JSONObject gateway){
+ protected Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway){
+
+ this.gateway = gateway;
+ this.secrets = secrets;
- mGateway = gateway;
-
- this.context = context;
general_configuration = getGeneralConfiguration(eip_definition);
timezone = getTimezone(eip_definition);
mName = locationAsName(eip_definition);
- // Currently deletes VpnProfile for host, if there already is one, and builds new
- ProfileManager vpl = ProfileManager.getInstance(context);
- Collection<VpnProfile> profiles = vpl.getProfiles();
- for (Iterator<VpnProfile> it = profiles.iterator(); it.hasNext(); ){
- VpnProfile p = it.next();
-
- if ( p.mName.equalsIgnoreCase( mName ) ) {
- it.remove();
- vpl.removeProfile(context, p);
- }
- }
-
mVpnProfile = createVPNProfile();
mVpnProfile.mName = mName;
-
- vpl.addProfile(mVpnProfile);
- vpl.saveProfile(context, mVpnProfile);
- vpl.saveProfileList(context);
}
private JSONObject getGeneralConfiguration(JSONObject eip_definition) {
@@ -110,7 +88,7 @@ public class Gateway {
try {
JSONObject locations = eip_definition.getJSONObject("locations");
- return locations.getJSONObject(mGateway.getString("location"));
+ return locations.getJSONObject(gateway.getString("location"));
} catch (JSONException e) {
return new JSONObject();
}
@@ -123,8 +101,7 @@ public class Gateway {
try {
ConfigParser cp = new ConfigParser();
- SharedPreferences preferences = context.getSharedPreferences(Dashboard.SHARED_PREFERENCES, Activity.MODE_PRIVATE);
- VpnConfigGenerator vpn_configuration_generator = new VpnConfigGenerator(preferences, general_configuration, mGateway);
+ VpnConfigGenerator vpn_configuration_generator = new VpnConfigGenerator(general_configuration, secrets, gateway);
String configuration = vpn_configuration_generator.generate();
cp.parseConfig(new StringReader(configuration));
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 a320bee5..6f260f55 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java
@@ -31,15 +31,15 @@ public class VpnConfigGenerator {
private JSONObject general_configuration;
private JSONObject gateway;
-
- private static SharedPreferences preferences;
+ private JSONObject secrets;
+
public final static String TAG = VpnConfigGenerator.class.getSimpleName();
private final String new_line = System.getProperty("line.separator"); // Platform new line
- public VpnConfigGenerator(SharedPreferences preferences, JSONObject general_configuration, JSONObject gateway) {
+ public VpnConfigGenerator(JSONObject general_configuration, JSONObject secrets, JSONObject gateway) {
this.general_configuration = general_configuration;
this.gateway = gateway;
- VpnConfigGenerator.preferences = preferences;
+ this.secrets = secrets;
}
public String generate() {
@@ -113,29 +113,33 @@ public class VpnConfigGenerator {
}
private String secretsConfiguration() {
-
- String ca =
- "<ca>"
- + new_line
- + preferences.getString(Provider.CA_CERT, "")
- + new_line
- + "</ca>";
-
- String key =
- "<key>"
- + new_line
- + preferences.getString(Constants.PRIVATE_KEY, "")
- + new_line
- + "</key>";
-
- String openvpn_cert =
- "<cert>"
- + new_line
- + preferences.getString(Constants.CERTIFICATE, "")
- + new_line
- + "</cert>";
+ try {
+ String ca =
+ "<ca>"
+ + new_line
+ + secrets.getString(Provider.CA_CERT)
+ + new_line
+ + "</ca>";
+
+ String key =
+ "<key>"
+ + new_line
+ + secrets.getString(Constants.PRIVATE_KEY)
+ + new_line
+ + "</key>";
+
+ String openvpn_cert =
+ "<cert>"
+ + new_line
+ + secrets.getString(Constants.CERTIFICATE)
+ + new_line
+ + "</cert>";
- return ca + new_line + key + new_line + openvpn_cert;
+ return ca + new_line + key + new_line + openvpn_cert;
+ } catch(JSONException e) {
+ e.printStackTrace();
+ return "";
+ }
}
private String androidCustomizations() {
diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java
index 3d9bb7b1..af8a5d46 100644
--- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java
+++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java
@@ -58,16 +58,16 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
@Inject ProviderListAdapter adapter;
private ProviderManager provider_manager;
- private Intent mConfigState = new Intent();
+ private Intent mConfigState = new Intent();
private Provider selected_provider;
final public static String TAG = ConfigurationWizard.class.getSimpleName();
- final public static String TYPE_OF_CERTIFICATE = "type_of_certificate";
- final public static String ANON_CERTIFICATE = "anon_certificate";
- final public static String AUTHED_CERTIFICATE = "authed_certificate";
+ final public static String TYPE_OF_CERTIFICATE = "type_of_certificate";
+ final public static String ANON_CERTIFICATE = "anon_certificate";
+ final public static String AUTHED_CERTIFICATE = "authed_certificate";
- final protected static String PROVIDER_SET = "PROVIDER SET";
- final protected static String SERVICES_RETRIEVED = "SERVICES RETRIEVED";
+ final protected static String PROVIDER_SET = "PROVIDER SET";
+ final protected static String SERVICES_RETRIEVED = "SERVICES RETRIEVED";
final protected static String ASSETS_URL_FOLDER = "urls";
final private static String PROGRESSBAR_TEXT = TAG + "PROGRESSBAR_TEXT";
diff --git a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java
index 8b8cabdf..d019ca59 100644
--- a/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java
+++ b/app/src/release/java/se/leap/bitmaskclient/ProviderAPI.java
@@ -239,7 +239,7 @@ public class ProviderAPI extends IntentService {
result.putString(SessionDialog.USERNAME, username);
result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true);
}
- if(username.isEmpty()) {
+ if(!validUsername(username)) {
result.putBoolean(RESULT_KEY, false);
result.putBoolean(SessionDialog.USERNAME_MISSING, true);
}
@@ -320,23 +320,27 @@ public class ProviderAPI extends IntentService {
sendBroadcast(intentUpdate);
}
- /**
- * Validates parameters entered by the user to log in
- * @param entered_username
- * @param entered_password
- * @return true if both parameters are present and the entered password length is greater or equal to eight (8).
- */
- private boolean validUserLoginData(String entered_username, String entered_password) {
- return !(entered_username.isEmpty()) && wellFormedPassword(entered_password);
- }
+ /**
+ * 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 entered_password
+ * @param password
* @return true if the entered password length is greater or equal to eight (8).
*/
- private boolean wellFormedPassword(String entered_password) {
- return entered_password.length() >= 8;
+ private boolean wellFormedPassword(String password) {
+ return password != null && password.length() >= 8;
}
/**
@@ -815,51 +819,64 @@ public class ProviderAPI extends IntentService {
}
return string;
}
-
- /**
- * Logs out from the api url retrieved from the task.
- * @return true if there were no exceptions
- */
- private boolean logOut() {
- try {
- String delete_url = provider_api_url + "/logout";
- int progress = 0;
- HttpsURLConnection urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection();
- urlConnection.setRequestMethod("DELETE");
- urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory());
+ private boolean logOut() {
+ String delete_url = provider_api_url + "/logout";
- int responseCode = urlConnection.getResponseCode();
+ 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();
+ broadcast_progress(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) {
broadcast_progress(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
- 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;
+ }
}
- 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.