From b87f082fe0e9c05cf45c56b510a2c4000133b572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 24 Mar 2015 19:07:43 +0100 Subject: Indent everything with IntelliJ --- .../java/se/leap/bitmaskclient/eip/Constants.java | 6 +- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 150 ++++++++++----------- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 95 ++++++------- .../java/se/leap/bitmaskclient/eip/Gateway.java | 100 +++++++------- .../se/leap/bitmaskclient/eip/GatewaySelector.java | 46 +++---- .../se/leap/bitmaskclient/eip/GatewaysManager.java | 69 +++++----- .../se/leap/bitmaskclient/eip/VoidVpnLauncher.java | 36 ++--- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 48 +++---- .../bitmaskclient/eip/VpnCertificateValidator.java | 46 +++---- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 10 +- 10 files changed, 306 insertions(+), 300 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/eip') diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java index f7206927..39ad7c08 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Constants.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -17,16 +17,14 @@ package se.leap.bitmaskclient.eip; /** - * * Constants for intent passing, shared preferences * * @author Parménides GV - * */ public interface Constants { public final static String TAG = Constants.class.getSimpleName(); - + public final static String ACTION_CHECK_CERT_VALIDITY = TAG + ".CHECK_CERT_VALIDITY"; public final static String ACTION_START_EIP = TAG + ".START_EIP"; public final static String ACTION_STOP_EIP = TAG + ".STOP_EIP"; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index 6335f818..beed7948 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -33,7 +33,7 @@ import static se.leap.bitmaskclient.eip.Constants.*; * this IntentService. * Contains logic for parsing eip-service.json from the provider, configuring and selecting * gateways, and controlling {@link de.blinkt.openvpn.core.OpenVPNService} connections. - * + * * @author Sean Leonard * @author Parménides GV */ @@ -43,64 +43,64 @@ public final class EIP extends IntentService { public final static String SERVICE_API_PATH = "config/eip-service.json"; public static final int DISCONNECT = 15; - + private static Context context; private static ResultReceiver mReceiver; private static SharedPreferences preferences; - + private static JSONObject eip_definition; private static GatewaysManager gateways_manager = new GatewaysManager(); private static Gateway gateway; - - public EIP(){ - super(TAG); + + public EIP() { + super(TAG); } - + @Override public void onCreate() { - super.onCreate(); - - context = getApplicationContext(); + super.onCreate(); + + context = getApplicationContext(); preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE); - eip_definition = eipDefinitionFromPreferences(); - if(gateways_manager.isEmpty()) + eip_definition = eipDefinitionFromPreferences(); + if (gateways_manager.isEmpty()) gatewaysFromPreferences(); } @Override protected void onHandleIntent(Intent intent) { - String action = intent.getAction(); - mReceiver = intent.getParcelableExtra(RECEIVER_TAG); - - if ( action.equals(ACTION_START_EIP)) - startEIP(); - else if (action.equals(ACTION_STOP_EIP)) - stopEIP(); - else if (action.equals(ACTION_IS_EIP_RUNNING)) - isRunning(); + String action = intent.getAction(); + mReceiver = intent.getParcelableExtra(RECEIVER_TAG); + + if (action.equals(ACTION_START_EIP)) + startEIP(); + else if (action.equals(ACTION_STOP_EIP)) + stopEIP(); + else if (action.equals(ACTION_IS_EIP_RUNNING)) + isRunning(); else if (action.equals(ACTION_UPDATE_EIP_SERVICE)) - updateEIPService(); - else if (action.equals(ACTION_CHECK_CERT_VALIDITY)) - checkCertValidity(); + updateEIPService(); + else if (action.equals(ACTION_CHECK_CERT_VALIDITY)) + checkCertValidity(); } - + /** * Initiates an EIP connection by selecting a gateway and preparing and sending an * Intent to {@link de.blinkt.openvpn.LaunchVPN}. * It also sets up early routes. */ private void startEIP() { - if(gateways_manager.isEmpty()) - updateEIPService(); + if (gateways_manager.isEmpty()) + updateEIPService(); earlyRoutes(); - gateway = gateways_manager.select(); - if(gateway != null && gateway.getProfile() != null) { - mReceiver = EipFragment.getReceiver(); - launchActiveGateway(); - tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK); - } else - tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED); + gateway = gateways_manager.select(); + if (gateway != null && gateway.getProfile() != null) { + mReceiver = EipFragment.getReceiver(); + launchActiveGateway(); + tellToReceiver(ACTION_START_EIP, Activity.RESULT_OK); + } else + tellToReceiver(ACTION_START_EIP, Activity.RESULT_CANCELED); } /** @@ -108,40 +108,40 @@ public final class EIP extends IntentService { * VpnService is started properly. */ private void earlyRoutes() { - Intent void_vpn_launcher = new Intent(context, VoidVpnLauncher.class); - void_vpn_launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(void_vpn_launcher); + Intent void_vpn_launcher = new Intent(context, VoidVpnLauncher.class); + void_vpn_launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(void_vpn_launcher); } - + private void launchActiveGateway() { - Intent intent = new Intent(this,LaunchVPN.class); - intent.setAction(Intent.ACTION_MAIN); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(LaunchVPN.EXTRA_NAME, gateway.getProfile().getName()); - intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); - startActivity(intent); + Intent intent = new Intent(this, LaunchVPN.class); + intent.setAction(Intent.ACTION_MAIN); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(LaunchVPN.EXTRA_NAME, gateway.getProfile().getName()); + intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); + startActivity(intent); } private void stopEIP() { - EipStatus eip_status = EipStatus.getInstance(); - int result_code = Activity.RESULT_CANCELED; - if(eip_status.isConnected() || eip_status.isConnecting()) - result_code = Activity.RESULT_OK; + EipStatus eip_status = EipStatus.getInstance(); + int result_code = Activity.RESULT_CANCELED; + if (eip_status.isConnected() || eip_status.isConnecting()) + result_code = Activity.RESULT_OK; - tellToReceiver(ACTION_STOP_EIP, result_code); + tellToReceiver(ACTION_STOP_EIP, result_code); } - + /** * Checks the last stored status notified by ics-openvpn * Sends Activity.RESULT_CANCELED to the ResultReceiver that made the * request if it's not connected, Activity.RESULT_OK otherwise. */ private void isRunning() { - EipStatus eip_status = EipStatus.getInstance(); - int resultCode = (eip_status.isConnected()) ? - Activity.RESULT_OK : - Activity.RESULT_CANCELED; - tellToReceiver(ACTION_IS_EIP_RUNNING, resultCode); + EipStatus eip_status = EipStatus.getInstance(); + int resultCode = (eip_status.isConnected()) ? + Activity.RESULT_OK : + Activity.RESULT_CANCELED; + tellToReceiver(ACTION_IS_EIP_RUNNING, resultCode); } /** @@ -149,27 +149,27 @@ public final class EIP extends IntentService { * TODO Implement API call to refresh eip-service.json from the provider */ private void updateEIPService() { - eip_definition = eipDefinitionFromPreferences(); - if(eip_definition.length() > 0) + eip_definition = eipDefinitionFromPreferences(); + if (eip_definition.length() > 0) updateGateways(); - tellToReceiver(ACTION_UPDATE_EIP_SERVICE, Activity.RESULT_OK); + tellToReceiver(ACTION_UPDATE_EIP_SERVICE, Activity.RESULT_OK); } private JSONObject eipDefinitionFromPreferences() { JSONObject result = new JSONObject(); - try { - String eip_definition_string = preferences.getString(KEY, ""); - if(!eip_definition_string.isEmpty()) { - result = new JSONObject(eip_definition_string); - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + try { + String eip_definition_string = preferences.getString(KEY, ""); + if (!eip_definition_string.isEmpty()) { + result = new JSONObject(eip_definition_string); + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return result; } - private void updateGateways(){ + private void updateGateways() { gateways_manager.fromEipServiceJson(eip_definition); gatewaysToPreferences(); } @@ -187,15 +187,15 @@ public final class EIP extends IntentService { } private void checkCertValidity() { - VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(CERTIFICATE, "")); - int resultCode = validator.isValid() ? - Activity.RESULT_OK : - Activity.RESULT_CANCELED; - tellToReceiver(ACTION_CHECK_CERT_VALIDITY, resultCode); + VpnCertificateValidator validator = new VpnCertificateValidator(preferences.getString(CERTIFICATE, "")); + int resultCode = validator.isValid() ? + Activity.RESULT_OK : + Activity.RESULT_CANCELED; + tellToReceiver(ACTION_CHECK_CERT_VALIDITY, resultCode); } private void tellToReceiver(String action, int resultCode) { - if (mReceiver != null){ + if (mReceiver != null) { Bundle resultData = new Bundle(); resultData.putString(REQUEST_TAG, action); mReceiver.send(resultCode, resultData); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java index d1ed0d27..e636c693 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -34,104 +34,105 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { private int localized_res_id; public static EipStatus getInstance() { - if(current_status == null) { - current_status = new EipStatus(); - VpnStatus.addStateListener(current_status); - } - return current_status; + if (current_status == null) { + current_status = new EipStatus(); + VpnStatus.addStateListener(current_status); + } + return current_status; } - private EipStatus() { } + private EipStatus() { + } @Override public void updateState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { - updateStatus(state, logmessage, localizedResId, level); - if(isConnected() || isDisconnected()) { - setConnectedOrDisconnected(); - } else if(isConnecting()) - setConnecting(); - Log.d(TAG, "update state with level " + level); + updateStatus(state, logmessage, localizedResId, level); + if (isConnected() || isDisconnected()) { + setConnectedOrDisconnected(); + } else if (isConnecting()) + setConnecting(); + Log.d(TAG, "update state with level " + level); } private void updateStatus(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) { - current_status = getInstance(); - current_status.setState(state); - current_status.setLogMessage(logmessage); - current_status.setLocalizedResId(localizedResId); - current_status.setLevel(level); - current_status.setChanged(); + current_status = getInstance(); + current_status.setState(state); + current_status.setLogMessage(logmessage); + current_status.setLocalizedResId(localizedResId); + current_status.setLevel(level); + current_status.setChanged(); } public boolean wantsToDisconnect() { - return wants_to_disconnect; + return wants_to_disconnect; } public boolean isConnecting() { - return - !isConnected() && - !isDisconnected() && - !isPaused(); + return + !isConnected() && + !isDisconnected() && + !isPaused(); } public boolean isConnected() { - return level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED; + return level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED; } public boolean isDisconnected() { - return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; + return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED; } public boolean isPaused() { - return level == VpnStatus.ConnectionStatus.LEVEL_VPNPAUSED; + return level == VpnStatus.ConnectionStatus.LEVEL_VPNPAUSED; } public void setConnecting() { - wants_to_disconnect = false; - current_status.setChanged(); - current_status.notifyObservers(); + wants_to_disconnect = false; + current_status.setChanged(); + current_status.notifyObservers(); } public void setConnectedOrDisconnected() { - Log.d(TAG, "setConnectedOrDisconnected()"); - wants_to_disconnect = false; - current_status.setChanged(); - current_status.notifyObservers(); + Log.d(TAG, "setConnectedOrDisconnected()"); + wants_to_disconnect = false; + current_status.setChanged(); + current_status.notifyObservers(); } public void setDisconnecting() { - wants_to_disconnect = false; + wants_to_disconnect = false; } public String getState() { - return state; + return state; } public String getLogMessage() { - return log_message; + return log_message; } public int getLocalizedResId() { - return localized_res_id; + return localized_res_id; } public VpnStatus.ConnectionStatus getLevel() { - return level; + return level; } private void setState(String state) { - this.state = state; + this.state = state; } private void setLogMessage(String log_message) { - this.log_message = log_message; + this.log_message = log_message; } private void setLocalizedResId(int localized_res_id) { - this.localized_res_id = localized_res_id; + this.localized_res_id = localized_res_id; } private void setLevel(VpnStatus.ConnectionStatus level) { - EipStatus.level = level; + EipStatus.level = level; } public boolean errorInLast(int lines, Context context) { @@ -141,9 +142,9 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { VpnStatus.LogItem[] log = VpnStatus.getlogbuffer(); String message = ""; for (int i = 1; i <= lines && log.length > i; i++) { - message = log[log.length-i].getString(context); - for(int j = 0; j < error_keywords.length; j++) - if(message.contains(error_keywords[j])) + message = log[log.length - i].getString(context); + for (int j = 0; j < error_keywords.length; j++) + if (message.contains(error_keywords[j])) result = true; } return result; @@ -151,7 +152,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { @Override public String toString() { - return "State: " + state + " Level: " + level.toString(); + return "State: " + state + " Level: " + level.toString(); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java index 10ddc0ef..b2c7af8c 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -21,6 +21,7 @@ import android.util.*; import com.google.gson.*; import org.json.*; + import java.io.*; import de.blinkt.openvpn.*; @@ -30,12 +31,12 @@ import de.blinkt.openvpn.core.*; * Gateway provides objects defining gateways and their metadata. * Each instance contains a VpnProfile for OpenVPN specific data and member * variables describing capabilities and location (name) - * + * * @author Sean Leonard * @author Parménides GV */ public class Gateway { - + public final static String TAG = Gateway.class.getSimpleName(); private JSONObject general_configuration; @@ -45,86 +46,87 @@ public class Gateway { private String mName; private int timezone; private VpnProfile mVpnProfile; + /** * Build a gateway object from a JSON OpenVPN gateway definition in eip-service.json * and create a VpnProfile belonging to it. */ - public Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway){ + public Gateway(JSONObject eip_definition, JSONObject secrets, JSONObject gateway) { - this.gateway = gateway; + this.gateway = gateway; this.secrets = secrets; - general_configuration = getGeneralConfiguration(eip_definition); - timezone = getTimezone(eip_definition); - mName = locationAsName(eip_definition); + general_configuration = getGeneralConfiguration(eip_definition); + timezone = getTimezone(eip_definition); + mName = locationAsName(eip_definition); - mVpnProfile = createVPNProfile(); - mVpnProfile.mName = mName; + mVpnProfile = createVPNProfile(); + mVpnProfile.mName = mName; } private JSONObject getGeneralConfiguration(JSONObject eip_definition) { - try { - return eip_definition.getJSONObject("openvpn_configuration"); - } catch (JSONException e) { - return new JSONObject(); - } + try { + return eip_definition.getJSONObject("openvpn_configuration"); + } catch (JSONException e) { + return new JSONObject(); + } } private int getTimezone(JSONObject eip_definition) { - JSONObject location = getLocationInfo(eip_definition); - return location.optInt("timezone"); + JSONObject location = getLocationInfo(eip_definition); + return location.optInt("timezone"); } private String locationAsName(JSONObject eip_definition) { - JSONObject location = getLocationInfo(eip_definition); - return location.optString("name"); + JSONObject location = getLocationInfo(eip_definition); + return location.optString("name"); } private JSONObject getLocationInfo(JSONObject eip_definition) { - try { - JSONObject locations = eip_definition.getJSONObject("locations"); + try { + JSONObject locations = eip_definition.getJSONObject("locations"); - return locations.getJSONObject(gateway.getString("location")); - } catch (JSONException e) { - return new JSONObject(); - } + return locations.getJSONObject(gateway.getString("location")); + } catch (JSONException e) { + return new JSONObject(); + } } - + /** * Create and attach the VpnProfile to our gateway object */ - private VpnProfile createVPNProfile(){ - try { - ConfigParser cp = new ConfigParser(); - - VpnConfigGenerator vpn_configuration_generator = new VpnConfigGenerator(general_configuration, secrets, gateway); - String configuration = vpn_configuration_generator.generate(); - - cp.parseConfig(new StringReader(configuration)); - return cp.convertProfile(); - } catch (ConfigParser.ConfigParseError e) { - // FIXME We didn't get a VpnProfile! Error handling! and log level - Log.v(TAG,"Error creating VPNProfile"); - e.printStackTrace(); - return null; - } catch (IOException e) { - // FIXME We didn't get a VpnProfile! Error handling! and log level - Log.v(TAG,"Error creating VPNProfile"); - e.printStackTrace(); - return null; - } + private VpnProfile createVPNProfile() { + try { + ConfigParser cp = new ConfigParser(); + + VpnConfigGenerator vpn_configuration_generator = new VpnConfigGenerator(general_configuration, secrets, gateway); + String configuration = vpn_configuration_generator.generate(); + + cp.parseConfig(new StringReader(configuration)); + return cp.convertProfile(); + } catch (ConfigParser.ConfigParseError e) { + // FIXME We didn't get a VpnProfile! Error handling! and log level + Log.v(TAG, "Error creating VPNProfile"); + e.printStackTrace(); + return null; + } catch (IOException e) { + // FIXME We didn't get a VpnProfile! Error handling! and log level + Log.v(TAG, "Error creating VPNProfile"); + e.printStackTrace(); + return null; + } } public String getName() { - return mName; + return mName; } public VpnProfile getProfile() { - return mVpnProfile; + return mVpnProfile; } public int getTimezone() { - return timezone; + return timezone; } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java index 11823511..90c8f890 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java @@ -6,37 +6,37 @@ public class GatewaySelector { List gateways; public GatewaySelector(List gateways) { - this.gateways = gateways; + this.gateways = gateways; } public Gateway select() { - return closestGateway(); + return closestGateway(); } - + private Gateway closestGateway() { - TreeMap> offsets = calculateOffsets(); - return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next(); + TreeMap> offsets = calculateOffsets(); + return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next(); } - + private TreeMap> calculateOffsets() { - TreeMap> offsets = new TreeMap>(); - int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000; - for(Gateway gateway : gateways) { - int dist = timezoneDistance(localOffset, gateway.getTimezone()); - Set set = (offsets.get(dist) != null) ? - offsets.get(dist) : new HashSet(); - set.add(gateway); - offsets.put(dist, set); - } - return offsets; + TreeMap> offsets = new TreeMap>(); + int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000; + for (Gateway gateway : gateways) { + int dist = timezoneDistance(localOffset, gateway.getTimezone()); + Set set = (offsets.get(dist) != null) ? + offsets.get(dist) : new HashSet(); + set.add(gateway); + offsets.put(dist, set); + } + return offsets; } - + private int timezoneDistance(int local_timezone, int remote_timezone) { - // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12 - int dist = Math.abs(local_timezone - remote_timezone); - // Farther than 12 timezones and it's shorter around the "back" - if (dist > 12) - dist = 12 - (dist -12); // Well i'll be. Absolute values make equations do funny things. - return dist; + // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12 + int dist = Math.abs(local_timezone - remote_timezone); + // Farther than 12 timezones and it's shorter around the "back" + if (dist > 12) + dist = 12 - (dist - 12); // Well i'll be. Absolute values make equations do funny things. + return dist; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index af5670f5..b41fcfd4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -40,15 +40,18 @@ public class GatewaysManager { private SharedPreferences preferences; private List gateways = new ArrayList<>(); private ProfileManager profile_manager; - private Type list_type = new TypeToken>() {}.getType(); + private Type list_type = new TypeToken>() { + }.getType(); - public GatewaysManager() {} + public GatewaysManager() { + } public GatewaysManager(Context context, SharedPreferences preferences) { this.context = context; this.preferences = preferences; profile_manager = ProfileManager.getInstance(context); } + public Gateway select() { GatewaySelector gateway_selector = new GatewaySelector(gateways); return gateway_selector.select(); @@ -66,13 +69,13 @@ public class GatewaysManager { List gateways_list = new ArrayList(); try { gateways_list = new Gson().fromJson(gateways, list_type); - } catch(JsonSyntaxException e) { + } catch (JsonSyntaxException e) { gateways_list.add(new Gson().fromJson(gateways, Gateway.class)); } - if(gateways_list != null) { + if (gateways_list != null) { for (Gateway gateway : gateways_list) - addGateway(gateway); + addGateway(gateway); this.gateways.addAll(gateways_list); } else Log.d("GatewaysManager", "No gateways added"); @@ -85,21 +88,21 @@ public class GatewaysManager { public void fromEipServiceJson(JSONObject eip_definition) { try { - JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways"); - for (int i = 0; i < gatewaysDefined.length(); i++) { - JSONObject gw = gatewaysDefined.getJSONObject(i); - if (isOpenVpnGateway(gw)) { - JSONObject secrets = secretsConfiguration(); - Gateway aux = new Gateway(eip_definition, secrets, gw); - if(!containsProfileWithSecrets(aux.getProfile())) { - addGateway(aux); - } - } - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + JSONArray gatewaysDefined = eip_definition.getJSONArray("gateways"); + for (int i = 0; i < gatewaysDefined.length(); i++) { + JSONObject gw = gatewaysDefined.getJSONObject(i); + if (isOpenVpnGateway(gw)) { + JSONObject secrets = secretsConfiguration(); + Gateway aux = new Gateway(eip_definition, secrets, gw); + if (!containsProfileWithSecrets(aux.getProfile())) { + addGateway(aux); + } + } + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } private boolean isOpenVpnGateway(JSONObject gateway) { @@ -127,10 +130,10 @@ public class GatewaysManager { boolean result = false; Collection profiles = profile_manager.getProfiles(); - for(VpnProfile aux : profiles) { + for (VpnProfile aux : profiles) { result = result || sameConnections(profile.mConnections, aux.mConnections) - && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) - && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename); + && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) + && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename); } return result; } @@ -149,9 +152,9 @@ public class GatewaysManager { private void removeDuplicatedGateway(Gateway gateway) { Iterator it = gateways.iterator(); List gateways_to_remove = new ArrayList<>(); - while(it.hasNext()) { + while (it.hasNext()) { Gateway aux = it.next(); - if(sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) { + if (sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) { gateways_to_remove.add(aux); } } @@ -160,21 +163,21 @@ public class GatewaysManager { } private void removeDuplicatedProfiles(VpnProfile original) { - Collection profiles = profile_manager.getProfiles(); + Collection profiles = profile_manager.getProfiles(); List remove_list = new ArrayList<>(); - for(VpnProfile aux : profiles) { - if (sameConnections(original.mConnections, aux.mConnections)) - remove_list.add(aux); - } + for (VpnProfile aux : profiles) { + if (sameConnections(original.mConnections, aux.mConnections)) + remove_list.add(aux); + } for (VpnProfile profile : remove_list) profile_manager.removeProfile(context, profile); } private boolean sameConnections(Connection[] c1, Connection[] c2) { int same_connections = 0; - for(Connection c1_aux : c1) { - for(Connection c2_aux : c2) - if(c2_aux.mServerName.equals(c1_aux.mServerName)) { + for (Connection c1_aux : c1) { + for (Connection c2_aux : c2) + if (c2_aux.mServerName.equals(c1_aux.mServerName)) { same_connections++; break; } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java index 39a802c6..5c9263b3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java @@ -11,27 +11,27 @@ public class VoidVpnLauncher extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setUp(); + super.onCreate(savedInstanceState); + setUp(); } - + public void setUp() { - Intent blocking_intent = VpnService.prepare(getApplicationContext()); // stops the VPN connection created by another application. - if(blocking_intent != null) - startActivityForResult(blocking_intent, VPN_USER_PERMISSION); - else { - onActivityResult(VPN_USER_PERMISSION, RESULT_OK, null); - } + Intent blocking_intent = VpnService.prepare(getApplicationContext()); // stops the VPN connection created by another application. + if (blocking_intent != null) + startActivityForResult(blocking_intent, VPN_USER_PERMISSION); + else { + onActivityResult(VPN_USER_PERMISSION, RESULT_OK, null); + } } - protected void onActivityResult(int requestCode, int resultCode, Intent data){ - if(requestCode == VPN_USER_PERMISSION) { - if(resultCode == RESULT_OK) { - Intent void_vpn_service = new Intent(getApplicationContext(), VoidVpnService.class); - void_vpn_service.setAction(Constants.START_BLOCKING_VPN_PROFILE); - startService(void_vpn_service); - } - } - finish(); + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == VPN_USER_PERMISSION) { + if (resultCode == RESULT_OK) { + Intent void_vpn_service = new Intent(getApplicationContext(), VoidVpnService.class); + void_vpn_service.setAction(Constants.START_BLOCKING_VPN_PROFILE); + startService(void_vpn_service); + } + } + finish(); } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index e864288a..ac3d5d77 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -6,7 +6,7 @@ import android.os.*; import java.io.*; -public class VoidVpnService extends VpnService { +public class VoidVpnService extends VpnService { static final String TAG = VoidVpnService.class.getSimpleName(); static ParcelFileDescriptor fd; @@ -15,28 +15,28 @@ public class VoidVpnService extends VpnService { @Override public int onStartCommand(Intent intent, int flags, int startId) { - String action = intent != null ? intent.getAction() : ""; - if (action == Constants.START_BLOCKING_VPN_PROFILE) { - thread = new Thread(new Runnable() { - public void run() { - Builder builder = new Builder(); - builder.setSession("Blocking until running"); - builder.addAddress("10.42.0.8",16); - builder.addRoute("0.0.0.0", 1); - builder.addRoute("192.168.1.0", 24); - builder.addDnsServer("10.42.0.1"); - try { - fd = builder.establish(); + String action = intent != null ? intent.getAction() : ""; + if (action == Constants.START_BLOCKING_VPN_PROFILE) { + thread = new Thread(new Runnable() { + public void run() { + Builder builder = new Builder(); + builder.setSession("Blocking until running"); + builder.addAddress("10.42.0.8", 16); + builder.addRoute("0.0.0.0", 1); + builder.addRoute("192.168.1.0", 24); + builder.addDnsServer("10.42.0.1"); + try { + fd = builder.establish(); - } catch (Exception e) { - e.printStackTrace(); - } - android.util.Log.d(TAG, "VoidVpnService set up: fd = " + fd.toString()); - } - }); - thread.run(); - } - return 0; + } catch (Exception e) { + e.printStackTrace(); + } + android.util.Log.d(TAG, "VoidVpnService set up: fd = " + fd.toString()); + } + }); + thread.run(); + } + return 0; } @Override @@ -46,14 +46,14 @@ public class VoidVpnService extends VpnService { } public static void stop() { - if(thread != null) + if (thread != null) thread.interrupt(); closeFd(); } private static void closeFd() { try { - if(fd != null) { + if (fd != null) { android.util.Log.d(TAG, "VoidVpnService closing fd = " + fd.toString()); fd.close(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java index 27971c3d..025d049d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2013 LEAP Encryption Access Project and contributers - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -33,32 +33,32 @@ public class VpnCertificateValidator { } public boolean isValid() { - if(!certificate.isEmpty()) { - X509Certificate certificate_x509 = ConfigHelper.parseX509CertificateFromString(certificate); - return isValid(certificate_x509); - } else return true; + if (!certificate.isEmpty()) { + X509Certificate certificate_x509 = ConfigHelper.parseX509CertificateFromString(certificate); + return isValid(certificate_x509); + } else return true; } - + private boolean isValid(X509Certificate certificate) { - Calendar offset_date = calculateOffsetCertificateValidity(certificate); - try { - Log.d(TAG, "offset_date = " + offset_date.getTime().toString()); - certificate.checkValidity(offset_date.getTime()); - return true; - } catch(CertificateExpiredException e) { - return false; - } catch(CertificateNotYetValidException e) { - return false; - } + Calendar offset_date = calculateOffsetCertificateValidity(certificate); + try { + Log.d(TAG, "offset_date = " + offset_date.getTime().toString()); + certificate.checkValidity(offset_date.getTime()); + return true; + } catch (CertificateExpiredException e) { + return false; + } catch (CertificateNotYetValidException e) { + return false; + } } private Calendar calculateOffsetCertificateValidity(X509Certificate certificate) { - Log.d(TAG, "certificate not after = " + certificate.getNotAfter()); - long preventive_time = Math.abs(certificate.getNotBefore().getTime() - certificate.getNotAfter().getTime())/2; - long current_date_millis = Calendar.getInstance().getTimeInMillis(); - - Calendar limit_date = Calendar.getInstance(); - limit_date.setTimeInMillis(current_date_millis + preventive_time); - return limit_date; + Log.d(TAG, "certificate not after = " + certificate.getNotAfter()); + long preventive_time = Math.abs(certificate.getNotBefore().getTime() - certificate.getNotAfter().getTime()) / 2; + long current_date_millis = Calendar.getInstance().getTimeInMillis(); + + Calendar limit_date = Calendar.getInstance(); + limit_date.setTimeInMillis(current_date_millis + preventive_time); + return limit_date; } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 8b1bc793..0b9c5832 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -17,7 +17,9 @@ package se.leap.bitmaskclient.eip; import org.json.*; + import java.util.*; + import se.leap.bitmaskclient.*; public class VpnConfigGenerator { @@ -50,7 +52,7 @@ public class VpnConfigGenerator { String common_options = ""; try { Iterator keys = general_configuration.keys(); - while ( keys.hasNext() ){ + while (keys.hasNext()) { String key = keys.next().toString(); common_options += key + " "; @@ -83,11 +85,11 @@ public class VpnConfigGenerator { String ip_address = gateway.getString(ip_address_keyword); JSONObject capabilities = gateway.getJSONObject(capabilities_keyword); JSONArray ports = capabilities.getJSONArray(ports_keyword); - for (int i=0; i"; return ca + new_line + key + new_line + openvpn_cert; - } catch(JSONException e) { + } catch (JSONException e) { e.printStackTrace(); return ""; } -- cgit v1.2.3