summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java21
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/MainActivity.java77
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EIP.java305
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java110
4 files changed, 323 insertions, 190 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java
index aaff9ebc..03f8a881 100644
--- a/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/ConfigHelper.java
@@ -21,19 +21,16 @@ import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
-import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.spongycastle.util.encoders.Base64;
-import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
@@ -51,13 +48,10 @@ import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.Set;
-import static android.R.attr.name;
import static se.leap.bitmaskclient.Constants.ALWAYS_ON_SHOW_DIALOG;
import static se.leap.bitmaskclient.Constants.DEFAULT_SHARED_PREFS_BATTERY_SAVER;
import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION;
@@ -442,4 +436,19 @@ public class ConfigHelper {
SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
return preferences.getBoolean(ALWAYS_ON_SHOW_DIALOG, true);
}
+
+ public static JSONObject getEipDefinitionFromPreferences(SharedPreferences preferences) {
+ JSONObject result = new JSONObject();
+ try {
+ String eipDefinitionString = preferences.getString(PROVIDER_EIP_DEFINITION, "");
+ if (!eipDefinitionString.isEmpty()) {
+ result = new JSONObject(eipDefinitionString);
+ }
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return result;
+ }
+
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
index 19294618..0956446f 100644
--- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
+++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java
@@ -18,15 +18,11 @@ package se.leap.bitmaskclient;
import android.content.BroadcastReceiver;
-import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
-import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
-import android.os.IBinder;
-import android.os.RemoteException;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
@@ -39,17 +35,8 @@ import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
-import java.util.Observable;
-import java.util.Observer;
-
-import de.blinkt.openvpn.core.IOpenVPNServiceInternal;
-import de.blinkt.openvpn.core.OpenVPNService;
-import de.blinkt.openvpn.core.ProfileManager;
-import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.drawer.NavigationDrawerFragment;
import se.leap.bitmaskclient.eip.EipCommand;
-import se.leap.bitmaskclient.eip.EipStatus;
-import se.leap.bitmaskclient.eip.VoidVpnService;
import se.leap.bitmaskclient.fragments.LogFragment;
import static android.content.Intent.CATEGORY_DEFAULT;
@@ -59,9 +46,7 @@ import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP;
-import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
-import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT;
import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP;
import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN;
@@ -78,31 +63,15 @@ import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed;
import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message;
-public class MainActivity extends AppCompatActivity implements Observer {
+public class MainActivity extends AppCompatActivity {
public final static String TAG = MainActivity.class.getSimpleName();
private Provider provider = new Provider();
private SharedPreferences preferences;
- private EipStatus eipStatus;
private NavigationDrawerFragment navigationDrawerFragment;
private MainActivityBroadcastReceiver mainActivityBroadcastReceiver;
- private IOpenVPNServiceInternal mService;
- private ServiceConnection openVpnConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName className,
- IBinder service) {
- mService = IOpenVPNServiceInternal.Stub.asInterface(service);
- }
-
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- mService = null;
- }
-
- };
-
public final static String ACTION_SHOW_VPN_FRAGMENT = "action_show_vpn_fragment";
public final static String ACTION_SHOW_LOG_FRAGMENT = "action_show_log_fragment";
@@ -130,14 +99,12 @@ public class MainActivity extends AppCompatActivity implements Observer {
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
- eipStatus = EipStatus.getInstance();
handleIntentAction(getIntent());
}
@Override
protected void onResume() {
super.onResume();
- bindOpenVpnService();
}
@Override
@@ -221,7 +188,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
@Override
protected void onPause() {
super.onPause();
- unbindService(openVpnConnection);
}
@Override
@@ -231,14 +197,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
super.onDestroy();
}
-
- @Override
- public void update(Observable observable, Object data) {
- if (observable instanceof EipStatus) {
- eipStatus = (EipStatus) observable;
- }
- }
-
private void setUpBroadcastReceiver() {
IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_EIP_EVENT);
updateIntentFilter.addAction(BROADCAST_PROVIDER_API_EVENT);
@@ -299,7 +257,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
case EIP_ACTION_STOP:
switch (resultCode) {
case RESULT_OK:
- stop();
break;
case RESULT_CANCELED:
break;
@@ -360,38 +317,6 @@ public class MainActivity extends AppCompatActivity implements Observer {
}
- private void stop() {
- preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply();
- if (eipStatus.isBlockingVpnEstablished()) {
- stopBlockingVpn();
- }
- disconnect();
- }
-
- private void stopBlockingVpn() {
- Log.d(TAG, "stop VoidVpn!");
- Intent stopVoidVpnIntent = new Intent(this, VoidVpnService.class);
- stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN);
- startService(stopVoidVpnIntent);
- }
-
- private void disconnect() {
- ProfileManager.setConntectedVpnProfileDisconnected(this);
- if (mService != null) {
- try {
- mService.stopVPN(false);
- } catch (RemoteException e) {
- VpnStatus.logException(e);
- }
- }
- }
-
- private void bindOpenVpnService() {
- Intent intent = new Intent(this, OpenVPNService.class);
- intent.setAction(OpenVPNService.START_SERVICE);
- bindService(intent, openVpnConnection, Context.BIND_AUTO_CREATE);
- }
-
private void askUserToLogIn(String userMessage) {
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra(PROVIDER_KEY, provider);
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 665e0ebd..bdad7fa1 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
@@ -16,11 +16,18 @@
*/
package se.leap.bitmaskclient.eip;
-import android.app.IntentService;
+import android.app.Service;
+import android.content.ComponentName;
+import android.content.Context;
import android.content.Intent;
+import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
+import android.os.IBinder;
+import android.os.RemoteException;
import android.os.ResultReceiver;
+import android.support.annotation.Nullable;
+import android.support.annotation.StringRes;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
@@ -28,8 +35,16 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.lang.ref.WeakReference;
+import java.util.Observable;
+import java.util.Observer;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
import de.blinkt.openvpn.LaunchVPN;
+import de.blinkt.openvpn.core.IOpenVPNServiceInternal;
+import de.blinkt.openvpn.core.OpenVPNService;
+import de.blinkt.openvpn.core.ProfileManager;
+import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.OnBootReceiver;
import static android.app.Activity.RESULT_CANCELED;
@@ -43,11 +58,11 @@ import static se.leap.bitmaskclient.Constants.EIP_ACTION_IS_RUNNING;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_ALWAYS_ON_VPN;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP;
+import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN;
import static se.leap.bitmaskclient.Constants.EIP_EARLY_ROUTES;
import static se.leap.bitmaskclient.Constants.EIP_RECEIVER;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT;
-import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION;
import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.ERROR_INVALID_VPN_CERTIFICATE;
@@ -56,14 +71,14 @@ import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid;
/**
* EIP is the abstract base class for interacting with and managing the Encrypted
* Internet Proxy connection. Connections are started, stopped, and queried through
- * this IntentService.
+ * this Service.
* 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 <meanderingcode@aetherislands.net>
* @author Parménides GV <parmegv@sdf.org>
*/
-public final class EIP extends IntentService {
+public final class EIP extends Service implements Observer {
public final static String TAG = EIP.class.getSimpleName(),
SERVICE_API_PATH = "config/eip-service.json",
@@ -72,46 +87,76 @@ public final class EIP extends IntentService {
private WeakReference<ResultReceiver> mReceiverRef = new WeakReference<>(null);
private SharedPreferences preferences;
+ private AtomicInteger processCounter;
+ private EipStatus eipStatus;
+
+ private AtomicBoolean disconnectOnConnect = new AtomicBoolean(false);
+
+ private IOpenVPNServiceInternal mService;
+ private ServiceConnection openVpnConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName className, IBinder service) {
+ mService = IOpenVPNServiceInternal.Stub.asInterface(service);
+ if (disconnectOnConnect.get()) {
+ disconnect();
+ disconnectOnConnect.set(false);
+ }
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName arg0) {
+ mService = null;
+ }
+ };
- public EIP() {
- super(TAG);
+ @Nullable
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
}
@Override
public void onCreate() {
super.onCreate();
+ bindOpenVpnService();
+ eipStatus = EipStatus.getInstance();
+ eipStatus.addObserver(this);
+ processCounter = new AtomicInteger(0);
preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
}
@Override
- protected void onHandleIntent(Intent intent) {
- String action = intent.getAction();
- if (intent.getParcelableExtra(EIP_RECEIVER) != null) {
- mReceiverRef = new WeakReference<>((ResultReceiver) intent.getParcelableExtra(EIP_RECEIVER));
- }
+ public void onDestroy() {
+ super.onDestroy();
+ eipStatus.deleteObserver(this);
+ unbindService(openVpnConnection);
+ }
- if (action == null) {
- return;
+ /**
+ * update eipStatus whenever it changes
+ */
+ @Override
+ public void update(Observable observable, Object data) {
+ if (observable instanceof EipStatus) {
+ eipStatus = (EipStatus) observable;
}
+ }
- switch (action) {
- case EIP_ACTION_START:
- boolean earlyRoutes = intent.getBooleanExtra(EIP_EARLY_ROUTES, true);
- startEIP(earlyRoutes);
- break;
- case EIP_ACTION_START_ALWAYS_ON_VPN:
- startEIPAlwaysOnVpn();
- break;
- case EIP_ACTION_STOP:
- stopEIP();
- break;
- case EIP_ACTION_IS_RUNNING:
- isRunning();
- break;
- case EIP_ACTION_CHECK_CERT_VALIDITY:
- checkVPNCertificateValidity();
- break;
+ /**
+ * register new process with processCounter and start it
+ * @param intent the intent that started this EIP call
+ */
+ @Override
+ public int onStartCommand(final Intent intent, int flags, int startId) {
+ processCounter.incrementAndGet();
+ final String action = intent.getAction();
+ if (intent.getParcelableExtra(EIP_RECEIVER) != null) {
+ mReceiverRef = new WeakReference<>((ResultReceiver) intent.getParcelableExtra(EIP_RECEIVER));
+ }
+ if (action != null) {
+ new EipThread(this, action, intent).start();
}
+ return START_STICKY;
}
/**
@@ -125,9 +170,8 @@ public final class EIP extends IntentService {
}
Bundle result = new Bundle();
-
if (!preferences.getBoolean(EIP_RESTART_ON_BOOT, false)){
- preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, true).commit();
+ preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, true).apply();
}
GatewaysManager gatewaysManager = gatewaysFromPreferences();
@@ -173,6 +217,10 @@ public final class EIP extends IntentService {
startActivity(voidVpnLauncher);
}
+ /**
+ * starts the VPN and connects to the given gateway
+ * @param gateway to connect to
+ */
private void launchActiveGateway(Gateway gateway) {
Intent intent = new Intent(this, LaunchVPN.class);
intent.setAction(Intent.ACTION_MAIN);
@@ -182,14 +230,18 @@ public final class EIP extends IntentService {
startActivity(intent);
}
+ /**
+ * Stop VPN
+ * First checks if the OpenVpnConnection is open then
+ * terminates EIP if currently connected or connecting
+ */
private void stopEIP() {
- // TODO stop eip from here if possible...
- // TODO then refactor EipFragment.handleSwitchOff
- EipStatus eipStatus = EipStatus.getInstance();
+ // if the OpenVpnConnection doesn't exist remember to disconnect after connecting
int resultCode = RESULT_CANCELED;
- if (eipStatus.isConnected() || eipStatus.isConnecting())
+ if (eipStatus.isConnected() || eipStatus.isConnecting()) {
+ stop();
resultCode = RESULT_OK;
-
+ }
tellToReceiverOrBroadcast(EIP_ACTION_STOP, resultCode);
}
@@ -206,29 +258,20 @@ public final class EIP extends IntentService {
tellToReceiverOrBroadcast(EIP_ACTION_IS_RUNNING, resultCode);
}
- private JSONObject eipDefinitionFromPreferences() {
- JSONObject result = new JSONObject();
- try {
- String eipDefinitionString = preferences.getString(PROVIDER_EIP_DEFINITION, "");
- if (!eipDefinitionString.isEmpty()) {
- result = new JSONObject(eipDefinitionString);
- }
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
-
+ /**
+ * read eipServiceJson from preferences and parse Gateways
+ * @return GatewaysManager
+ */
private GatewaysManager gatewaysFromPreferences() {
GatewaysManager gatewaysManager = new GatewaysManager(this, preferences);
- //TODO: THIS IS A QUICK FIX - it deletes all profiles in ProfileManager, thus it's possible
- // to add all gateways from prefs without duplicates, but this should be refactored.
- gatewaysManager.clearGatewaysAndProfiles();
- gatewaysManager.fromEipServiceJson(eipDefinitionFromPreferences());
+ gatewaysManager.configureFromPreferences();
return gatewaysManager;
}
+ /**
+ * read VPN certificate from preferences and check it
+ * broadcast result
+ */
private void checkVPNCertificateValidity() {
int resultCode = isVPNCertificateValid() ?
RESULT_OK :
@@ -236,11 +279,22 @@ public final class EIP extends IntentService {
tellToReceiverOrBroadcast(EIP_ACTION_CHECK_CERT_VALIDITY, resultCode);
}
+ /**
+ * read VPN certificate from preferences and check it
+ * @return true if VPN certificate is valid false otherwise
+ */
private boolean isVPNCertificateValid() {
VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(PROVIDER_VPN_CERTIFICATE, ""));
return validator.isValid();
}
+ /**
+ * send resultCode and resultData to receiver or
+ * broadcast the result if no receiver is defined
+ * @param action the action that has been performed
+ * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
+ * @param resultData other data to broadcast or return to receiver
+ */
private void tellToReceiverOrBroadcast(String action, int resultCode, Bundle resultData) {
resultData.putString(EIP_REQUEST, action);
if (mReceiverRef.get() != null) {
@@ -250,10 +304,21 @@ public final class EIP extends IntentService {
}
}
+ /**
+ * send resultCode and resultData to receiver or
+ * broadcast the result if no receiver is defined
+ * @param action the action that has been performed
+ * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
+ */
private void tellToReceiverOrBroadcast(String action, int resultCode) {
tellToReceiverOrBroadcast(action, resultCode, new Bundle());
}
+ /**
+ * broadcast resul
+ * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
+ * @param resultData other data to broadcast or return to receiver
+ */
private void broadcastEvent(int resultCode , Bundle resultData) {
Intent intentUpdate = new Intent(BROADCAST_EIP_EVENT);
intentUpdate.addCategory(CATEGORY_DEFAULT);
@@ -263,20 +328,132 @@ public final class EIP extends IntentService {
LocalBroadcastManager.getInstance(this).sendBroadcast(intentUpdate);
}
- Bundle setErrorResult(Bundle result, int errorMessageId, String errorId) {
+
+ /**
+ * helper function to add error to result bundle
+ * @param result - result of an action
+ * @param errorMessageId - id of string resource describing the error
+ * @param errorId - MainActivityErrorDialog DownloadError id
+ */
+ void setErrorResult(Bundle result, @StringRes int errorMessageId, String errorId) {
JSONObject errorJson = new JSONObject();
- addErrorMessageToJson(errorJson, getResources().getString(errorMessageId), errorId);
+ try {
+ errorJson.put(ERRORS, getResources().getString(errorMessageId));
+ errorJson.put(ERROR_ID, errorId);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
result.putString(ERRORS, errorJson.toString());
result.putBoolean(BROADCAST_RESULT_KEY, false);
- return result;
}
- private void addErrorMessageToJson(JSONObject jsonObject, String errorMessage, String errorId) {
- try {
- jsonObject.put(ERRORS, errorMessage);
- jsonObject.put(ERROR_ID, errorId);
- } catch (JSONException e) {
- e.printStackTrace();
+
+ /**
+ * disable startup on restart
+ * then stop VPN
+ */
+ private void stop() {
+ preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply();
+ if (eipStatus.isBlockingVpnEstablished()) {
+ stopBlockingVpn();
+ }
+ disconnect();
+ }
+
+ /**
+ * stop void vpn from blocking internet
+ */
+ private void stopBlockingVpn() {
+ Log.d(TAG, "stop VoidVpn!");
+ Intent stopVoidVpnIntent = new Intent(this, VoidVpnService.class);
+ stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN);
+ startService(stopVoidVpnIntent);
+ }
+
+
+ /**
+ * check if OpenVPNConnection is connected
+ * then terminate VPN connection
+ */
+ private void disconnect() {
+ // if OpenVPNConnection is not connected remember to disconnect
+ // after connection is established
+ if (mService == null) {
+ disconnectOnConnect.set(true);
+ return;
+ }
+
+ ProfileManager.setConntectedVpnProfileDisconnected(this);
+ if (mService != null) {
+ try {
+ mService.stopVPN(false);
+ } catch (RemoteException e) {
+ VpnStatus.logException(e);
+ }
+ }
+ }
+
+ /**
+ * bind OpenVPNService to openVPNConnection
+ */
+ private void bindOpenVpnService() {
+ Intent intent = new Intent(this, OpenVPNService.class);
+ intent.setAction(OpenVPNService.START_SERVICE);
+ bindService(intent, openVpnConnection, Context.BIND_AUTO_CREATE);
+ }
+
+ /**
+ * Thread to handle the requests to this service
+ */
+ class EipThread extends Thread {
+ EipThread(EIP eipReference, String action, Intent startIntent) {
+ super(new EipRunnable(eipReference, action, startIntent));
+ }
+ }
+
+ /**
+ * Runnable to be used in the EipThread class
+ */
+ class EipRunnable implements Runnable {
+ private String action;
+ private EIP eipReference;
+ private Intent intent;
+
+ EipRunnable(EIP eipReference, String action, Intent startIntent){
+ super();
+ this.action = action;
+ this.eipReference = eipReference;
+ this.intent = startIntent;
+ }
+
+ /**
+ * select correct function to call based upon transmitted action
+ * after completing check if no other thread is running
+ * if no other thread is running terminate the EIP service
+ */
+ @Override
+ public void run() {
+ switch (action) {
+ case EIP_ACTION_START:
+ boolean earlyRoutes = intent.getBooleanExtra(EIP_EARLY_ROUTES, true);
+ startEIP(earlyRoutes);
+ break;
+ case EIP_ACTION_START_ALWAYS_ON_VPN:
+ startEIPAlwaysOnVpn();
+ break;
+ case EIP_ACTION_STOP:
+ stopEIP();
+ break;
+ case EIP_ACTION_IS_RUNNING:
+ isRunning();
+ break;
+ case EIP_ACTION_CHECK_CERT_VALIDITY:
+ checkVPNCertificateValidity();
+ break;
+ }
+ if (processCounter.decrementAndGet() == 0) {
+ eipReference.stopSelf();
+ }
}
}
}
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 1bdb53ab..a04ede08 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
@@ -20,7 +20,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
-import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
@@ -36,6 +35,7 @@ import java.util.List;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.Connection;
import de.blinkt.openvpn.core.ProfileManager;
+import se.leap.bitmaskclient.ConfigHelper;
import se.leap.bitmaskclient.Provider;
import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY;
@@ -49,52 +49,49 @@ public class GatewaysManager {
private Context context;
private SharedPreferences preferences;
private List<Gateway> gateways = new ArrayList<>();
- private ProfileManager profile_manager;
- private Type list_type = new TypeToken<ArrayList<Gateway>>() {
- }.getType();
+ private ProfileManager profileManager;
+ private Type listType = new TypeToken<ArrayList<Gateway>>() {}.getType();
- public GatewaysManager() {
- }
-
- public GatewaysManager(Context context, SharedPreferences preferences) {
+ GatewaysManager(Context context, SharedPreferences preferences) {
this.context = context;
this.preferences = preferences;
- profile_manager = ProfileManager.getInstance(context);
+ profileManager = ProfileManager.getInstance(context);
}
+ /**
+ * select closest Gateway
+ * @return the closest Gateway
+ */
public Gateway select() {
- GatewaySelector gateway_selector = new GatewaySelector(gateways);
- return gateway_selector.select();
+ GatewaySelector gatewaySelector = new GatewaySelector(gateways);
+ return gatewaySelector.select();
}
+ /**
+ * check if there are no gateways defined
+ * @return true if no gateways defined else false
+ */
public boolean isEmpty() {
return gateways.isEmpty();
}
+ /**
+ * @return number of gateways defined in the GatewaysManager
+ */
public int size() {
return gateways.size();
}
- public void addFromString(String gateways) {
- List<Gateway> gateways_list = new ArrayList<>();
- try {
- gateways_list = new Gson().fromJson(gateways, list_type);
- } catch (JsonSyntaxException e) {
- gateways_list.add(new Gson().fromJson(gateways, Gateway.class));
- }
-
- if (gateways_list != null) {
- for (Gateway gateway : gateways_list)
- addGateway(gateway);
- }
- }
-
@Override
public String toString() {
- return new Gson().toJson(gateways, list_type);
+ return new Gson().toJson(gateways, listType);
}
- public void fromEipServiceJson(JSONObject eipDefinition) {
+ /**
+ * parse gateways from eipDefinition
+ * @param eipDefinition eipServiceJson
+ */
+ void fromEipServiceJson(JSONObject eipDefinition) {
try {
JSONArray gatewaysDefined = eipDefinition.getJSONArray("gateways");
for (int i = 0; i < gatewaysDefined.length(); i++) {
@@ -113,6 +110,11 @@ public class GatewaysManager {
}
}
+ /**
+ * check if a gateway is an OpenVpn gateway
+ * @param gateway to check
+ * @return true if gateway is an OpenVpn gateway otherwise false
+ */
private boolean isOpenVpnGateway(JSONObject gateway) {
try {
String transport = gateway.getJSONObject("capabilities").getJSONArray("transport").toString();
@@ -137,7 +139,7 @@ public class GatewaysManager {
private boolean containsProfileWithSecrets(VpnProfile profile) {
boolean result = false;
- Collection<VpnProfile> profiles = profile_manager.getProfiles();
+ Collection<VpnProfile> profiles = profileManager.getProfiles();
for (VpnProfile aux : profiles) {
result = result || sameConnections(profile.mConnections, aux.mConnections)
&& profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename)
@@ -146,11 +148,11 @@ public class GatewaysManager {
return result;
}
- protected void clearGatewaysAndProfiles() {
+ void clearGatewaysAndProfiles() {
gateways.clear();
- ArrayList<VpnProfile> profiles = new ArrayList<>(profile_manager.getProfiles());
+ ArrayList<VpnProfile> profiles = new ArrayList<>(profileManager.getProfiles());
for (VpnProfile profile : profiles) {
- profile_manager.removeProfile(context, profile);
+ profileManager.removeProfile(context, profile);
}
}
@@ -159,42 +161,62 @@ public class GatewaysManager {
gateways.add(gateway);
VpnProfile profile = gateway.getProfile();
- profile_manager.addProfile(profile);
+ profileManager.addProfile(profile);
}
private void removeDuplicatedGateway(Gateway gateway) {
Iterator<Gateway> it = gateways.iterator();
- List<Gateway> gateways_to_remove = new ArrayList<>();
+ List<Gateway> gatewaysToRemove = new ArrayList<>();
while (it.hasNext()) {
Gateway aux = it.next();
if (sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) {
- gateways_to_remove.add(aux);
+ gatewaysToRemove.add(aux);
}
}
- gateways.removeAll(gateways_to_remove);
+ gateways.removeAll(gatewaysToRemove);
removeDuplicatedProfiles(gateway.getProfile());
}
private void removeDuplicatedProfiles(VpnProfile original) {
- Collection<VpnProfile> profiles = profile_manager.getProfiles();
- List<VpnProfile> remove_list = new ArrayList<>();
+ Collection<VpnProfile> profiles = profileManager.getProfiles();
+ List<VpnProfile> removeList = new ArrayList<>();
for (VpnProfile aux : profiles) {
- if (sameConnections(original.mConnections, aux.mConnections))
- remove_list.add(aux);
+ if (sameConnections(original.mConnections, aux.mConnections)) {
+ removeList.add(aux);
+ }
+ }
+ for (VpnProfile profile : removeList) {
+ profileManager.removeProfile(context, profile);
}
- for (VpnProfile profile : remove_list)
- profile_manager.removeProfile(context, profile);
}
+ /**
+ * check if all connections in c1 are also in c2
+ * @param c1 array of connections
+ * @param c2 array of connections
+ * @return true if all connections of c1 exist in c2 and vice versa
+ */
private boolean sameConnections(Connection[] c1, Connection[] c2) {
- int same_connections = 0;
+ int sameConnections = 0;
for (Connection c1_aux : c1) {
for (Connection c2_aux : c2)
if (c2_aux.mServerName.equals(c1_aux.mServerName)) {
- same_connections++;
+ sameConnections++;
break;
}
}
- return c1.length == c2.length && c1.length == same_connections;
+ return c1.length == c2.length && c1.length == sameConnections;
+ }
+
+ /**
+ * read EipServiceJson from preferences and set gateways
+ */
+ void configureFromPreferences() {
+ //TODO: THIS IS A QUICK FIX - it deletes all profiles in ProfileManager, thus it's possible
+ // to add all gateways from prefs without duplicates, but this should be refactored.
+ clearGatewaysAndProfiles();
+ fromEipServiceJson(
+ ConfigHelper.getEipDefinitionFromPreferences(preferences)
+ );
}
}