summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/eip')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/Constants.java7
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EIP.java151
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java111
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java114
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java52
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java73
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java44
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java70
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java54
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java157
10 files changed, 416 insertions, 417 deletions
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..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,17 +17,14 @@
package se.leap.bitmaskclient.eip;
/**
- *
* Constants for intent passing, shared preferences
*
* @author Parménides GV <parmegv@sdf.org>
- *
*/
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";
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..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
@@ -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.*;
@@ -34,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 <meanderingcode@aetherislands.net>
* @author Parménides GV <parmegv@sdf.org>
*/
@@ -44,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);
}
/**
@@ -109,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 <code>Activity.RESULT_CANCELED</code> to the ResultReceiver that made the
* request if it's not connected, <code>Activity.RESULT_OK</code> 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);
}
/**
@@ -150,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();
}
@@ -188,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 4ac3bd6a..ad68f96e 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
@@ -16,11 +16,11 @@
*/
package se.leap.bitmaskclient.eip;
-import android.util.Log;
+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();
@@ -33,106 +33,123 @@ 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) {
- current_status = getInstance();
- current_status.setState(state);
- current_status.setLogMessage(logmessage);
- current_status.setLocalizedResId(localizedResId);
- current_status.setLevel(level);
- current_status.setChanged();
- if(isConnected() || isDisconnected())
- setConnectedOrDisconnected();
- else if(isConnecting())
- setConnecting();
- Log.d(TAG, "update state with level " + level);
- current_status.notifyObservers();
+ updateStatus(state, logmessage, localizedResId, level);
+ if (isConnected() || isDisconnected()) {
+ setConnectedOrDisconnected();
+ } else if (isConnecting())
+ setConnecting();
+ }
+
+ 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() {
- 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();
+ 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) {
+ boolean result = false;
+ String[] error_keywords = {"error", "ERROR", "fatal", "FATAL"};
+
+ 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]))
+ result = true;
+ }
+ return result;
}
@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 0d8a2f7b..ff7d011e 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
@@ -16,32 +16,25 @@
*/
package se.leap.bitmaskclient.eip;
-import android.app.Activity;
-import android.content.SharedPreferences;
-import android.util.Log;
-
-import com.google.gson.Gson;
+import com.google.gson.*;
-import org.json.JSONException;
-import org.json.JSONObject;
+import org.json.*;
-import java.io.IOException;
-import java.io.StringReader;
+import java.io.*;
-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.
* Each instance contains a VpnProfile for OpenVPN specific data and member
* variables describing capabilities and location (name)
- *
+ *
* @author Sean Leonard <meanderingcode@aetherislands.net>
* @author Parménides GV <parmegv@sdf.org>
*/
public class Gateway {
-
+
public final static String TAG = Gateway.class.getSimpleName();
private JSONObject general_configuration;
@@ -51,86 +44,85 @@ 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
+ e.printStackTrace();
+ return null;
+ } catch (IOException e) {
+ // FIXME We didn't get a VpnProfile! Error handling! and log level
+ 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 39ae7ca6..90c8f890 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
@@ -1,46 +1,42 @@
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<Gateway> gateways;
public GatewaySelector(List<Gateway> gateways) {
- this.gateways = gateways;
+ this.gateways = gateways;
}
public Gateway select() {
- return closestGateway();
+ return closestGateway();
}
-
+
private Gateway closestGateway() {
- TreeMap<Integer, Set<Gateway>> offsets = calculateOffsets();
- return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next();
+ TreeMap<Integer, Set<Gateway>> offsets = calculateOffsets();
+ return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next();
}
-
+
private TreeMap<Integer, Set<Gateway>> calculateOffsets() {
- TreeMap<Integer, Set<Gateway>> offsets = new TreeMap<Integer, Set<Gateway>>();
- int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
- for(Gateway gateway : gateways) {
- int dist = timezoneDistance(localOffset, gateway.getTimezone());
- Set<Gateway> set = (offsets.get(dist) != null) ?
- offsets.get(dist) : new HashSet<Gateway>();
- set.add(gateway);
- offsets.put(dist, set);
- }
- return offsets;
+ TreeMap<Integer, Set<Gateway>> offsets = new TreeMap<Integer, Set<Gateway>>();
+ int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
+ for (Gateway gateway : gateways) {
+ int dist = timezoneDistance(localOffset, gateway.getTimezone());
+ Set<Gateway> set = (offsets.get(dist) != null) ?
+ offsets.get(dist) : new HashSet<Gateway>();
+ 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 401cd701..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.Log;
import com.google.gson.*;
import com.google.gson.reflect.*;
@@ -40,15 +39,18 @@ public class GatewaysManager {
private SharedPreferences preferences;
private List<Gateway> gateways = new ArrayList<>();
private ProfileManager profile_manager;
- private Type list_type = new TypeToken<ArrayList<Gateway>>() {}.getType();
+ private Type list_type = new TypeToken<ArrayList<Gateway>>() {
+ }.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,16 +68,15 @@ public class GatewaysManager {
List<Gateway> gateways_list = new ArrayList<Gateway>();
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");
+ }
}
@Override
@@ -85,21 +86,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 +128,10 @@ public class GatewaysManager {
boolean result = false;
Collection<VpnProfile> 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 +150,9 @@ public class GatewaysManager {
private void removeDuplicatedGateway(Gateway gateway) {
Iterator<Gateway> it = gateways.iterator();
List<Gateway> 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 +161,21 @@ public class GatewaysManager {
}
private void removeDuplicatedProfiles(VpnProfile original) {
- Collection<VpnProfile> profiles = profile_manager.getProfiles();
+ Collection<VpnProfile> profiles = profile_manager.getProfiles();
List<VpnProfile> 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 d79d8003..5c9263b3 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 {
@@ -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 0c773208..dac92fe2 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java
@@ -1,53 +1,61 @@
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 {
+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() {
- 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) {
+ 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();
+ }
+ }
+ });
+ 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 {
- fd.close();
- return true;
- } catch (IOException | NullPointerException e) {
- android.util.Log.d(TAG, "VoidVpnService didn't stop");
+ if (fd != null)
+ fd.close();
+ } catch (IOException e) {
e.printStackTrace();
- return false;
}
}
}
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..8fce6a37 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
@@ -16,14 +16,10 @@
*/
package se.leap.bitmaskclient.eip;
-import android.util.Log;
-
-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();
@@ -35,32 +31,30 @@ 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 {
+ 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;
+ 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 6f260f55..0b9c5832 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
@@ -16,16 +16,11 @@
*/
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 org.json.*;
-import java.util.Iterator;
+import java.util.*;
-import se.leap.bitmaskclient.Provider;
+import se.leap.bitmaskclient.*;
public class VpnConfigGenerator {
@@ -37,79 +32,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; i<ports.length(); i++) {
- String port_specific_remotes = "";
- int port = ports.getInt(i);
- JSONArray protocols = capabilities.getJSONArray(protocol_keyword);
- for ( int j=0; j<protocols.length(); j++ ) {
- String protocol = protocols.optString(j);
- String new_remote = remote_keyword + " " + ip_address + " " + port + " " + protocol + new_line;
-
- port_specific_remotes = protocol.equalsIgnoreCase(udp) ?
- port_specific_remotes.replaceFirst(remote_keyword, new_remote + new_line + remote_keyword) :
- new_remote;
- }
- remotes += port_specific_remotes;
- }
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- return remotes;
+ 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; i < ports.length(); i++) {
+ String port_specific_remotes = "";
+ int port = ports.getInt(i);
+ JSONArray protocols = capabilities.getJSONArray(protocol_keyword);
+ for (int j = 0; j < protocols.length(); j++) {
+ String protocol = protocols.optString(j);
+ String new_remote = remote_keyword + " " + ip_address + " " + port + " " + protocol + new_line;
+
+ port_specific_remotes = protocol.equalsIgnoreCase(udp) ?
+ port_specific_remotes.replaceFirst(remote_keyword, new_remote + new_line + remote_keyword) :
+ new_remote;
+ }
+ remotes += port_specific_remotes;
+ }
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return remotes;
}
private String secretsConfiguration() {
@@ -136,18 +131,18 @@ public class VpnConfigGenerator {
+ "</cert>";
return ca + new_line + key + new_line + openvpn_cert;
- } catch(JSONException e) {
+ } catch (JSONException e) {
e.printStackTrace();
return "";
}
}
private String androidCustomizations() {
- return
- "remote-cert-tls server"
- + new_line
- + "persist-tun"
- + new_line
- + "auth-retry nointeract";
+ return
+ "remote-cert-tls server"
+ + new_line
+ + "persist-tun"
+ + new_line
+ + "auth-retry nointeract";
}
}