From 65a09aa799e525d3bc60f5f4f489a3d70c6a8554 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 6 Jul 2019 17:37:48 +0200 Subject: get rid of ics-openvpn's ProfileManager, reduces boilerplate and dead code --- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 5 +- .../java/se/leap/bitmaskclient/eip/EipStatus.java | 5 +- .../java/se/leap/bitmaskclient/eip/Gateway.java | 14 ++++ .../se/leap/bitmaskclient/eip/GatewaysManager.java | 80 +--------------------- 4 files changed, 21 insertions(+), 83 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/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 a0c96267..a5434871 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -46,7 +46,6 @@ import java.util.concurrent.LinkedBlockingQueue; import de.blinkt.openvpn.core.ConnectionStatus; 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 se.leap.bitmaskclient.R; @@ -54,7 +53,6 @@ import se.leap.bitmaskclient.R; import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_OK; import static android.content.Intent.CATEGORY_DEFAULT; -import static de.blinkt.openvpn.LaunchVPN.EXTRA_TEMP_VPN_PROFILE; import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT; import static se.leap.bitmaskclient.Constants.BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE; @@ -70,6 +68,7 @@ import static se.leap.bitmaskclient.Constants.EIP_N_CLOSEST_GATEWAY; 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_PROFILE; 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; @@ -243,7 +242,7 @@ public final class EIP extends JobIntentService implements Observer { */ private void launchActiveGateway(@NonNull Gateway gateway, int nClosestGateway) { Intent intent = new Intent(BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT); - intent.putExtra(EXTRA_TEMP_VPN_PROFILE, gateway.getProfile()); + intent.putExtra(PROVIDER_PROFILE, gateway.getProfile()); intent.putExtra(Gateway.KEY_N_CLOSEST_GATEWAY, nClosestGateway); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 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 fc07c521..64904816 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -25,9 +25,7 @@ import java.util.Observable; import de.blinkt.openvpn.core.ConnectionStatus; import de.blinkt.openvpn.core.LogItem; -import de.blinkt.openvpn.core.ProfileManager; import de.blinkt.openvpn.core.VpnStatus; -import se.leap.bitmaskclient.Provider; /** * EipStatus is a Singleton that represents a reduced set of a vpn's ConnectionStatus. @@ -37,6 +35,7 @@ import se.leap.bitmaskclient.Provider; public class EipStatus extends Observable implements VpnStatus.StateListener { public static String TAG = EipStatus.class.getSimpleName(); private static EipStatus currentStatus; + public enum EipLevel { CONNECTING, DISCONNECTING, @@ -99,7 +98,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { currentEipLevel = EipLevel.CONNECTED; break; case LEVEL_VPNPAUSED: - if (ProfileManager.getLastConnectedVpn() != null && ProfileManager.getLastConnectedVpn().mPersistTun) { + if (VpnStatus.getLastConnectedVpnProfile() != null && VpnStatus.getLastConnectedVpnProfile().mPersistTun) { //if persistTun is enabled, treat EipLevel as connecting as it *shouldn't* allow passing traffic in the clear... currentEipLevel = EipLevel.CONNECTING; } else { 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 317a91bd..55ade1ae 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -132,4 +132,18 @@ public class Gateway { public String toString() { return new Gson().toJson(this, Gateway.class); } + + @Override + public boolean equals(Object obj) { + return obj instanceof Gateway && + (this.mVpnProfile != null && + ((Gateway) obj).mVpnProfile != null && + this.mVpnProfile.mConnections != null && + ((Gateway) obj).mVpnProfile != null && + this.mVpnProfile.mConnections.length > 0 && + ((Gateway) obj).mVpnProfile.mConnections.length > 0 && + this.mVpnProfile.mConnections[0].mServerName != null && + this.mVpnProfile.mConnections[0].mServerName.equals(((Gateway) obj).mVpnProfile.mConnections[0].mServerName)) || + this.mVpnProfile == null && ((Gateway) obj).mVpnProfile == null; + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java index 003cef7d..060843fd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers + * Copyright (c) 2013 - 2019 LEAP Encryption Access Project and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,7 +18,6 @@ package se.leap.bitmaskclient.eip; import android.content.Context; import android.content.SharedPreferences; -import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; @@ -29,13 +28,8 @@ import org.json.JSONObject; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; import java.util.List; -import de.blinkt.openvpn.VpnProfile; -import de.blinkt.openvpn.core.Connection; -import de.blinkt.openvpn.core.ProfileManager; import se.leap.bitmaskclient.Provider; import se.leap.bitmaskclient.utils.PreferenceHelper; @@ -52,13 +46,11 @@ public class GatewaysManager { private Context context; private SharedPreferences preferences; private List gateways = new ArrayList<>(); - private ProfileManager profileManager; private Type listType = new TypeToken>() {}.getType(); GatewaysManager(Context context, SharedPreferences preferences) { this.context = context; this.preferences = preferences; - profileManager = ProfileManager.getInstance(context); } /** @@ -102,7 +94,7 @@ public class GatewaysManager { if (isOpenVpnGateway(gw)) { JSONObject secrets = secretsConfiguration(); Gateway aux = new Gateway(eipDefinition, secrets, gw); - if (!containsProfileWithSecrets(aux.getProfile())) { + if (!gateways.contains(aux)) { addGateway(aux); } } @@ -139,85 +131,19 @@ public class GatewaysManager { return result; } - private boolean containsProfileWithSecrets(VpnProfile profile) { - boolean result = false; - Collection profiles = profileManager.getProfiles(); - for (VpnProfile aux : profiles) { - result = result || sameConnections(profile.mConnections, aux.mConnections) - && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename) - && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename); - } - return result; - } - - void clearGatewaysAndProfiles() { + void clearGateways() { gateways.clear(); - ArrayList profiles = new ArrayList<>(profileManager.getProfiles()); - for (VpnProfile profile : profiles) { - profileManager.removeProfile(context, profile); - } } private void addGateway(Gateway gateway) { - removeDuplicatedGateway(gateway); gateways.add(gateway); - - VpnProfile profile = gateway.getProfile(); - profileManager.addProfile(profile); - } - - private void removeDuplicatedGateway(Gateway gateway) { - Iterator it = gateways.iterator(); - List gatewaysToRemove = new ArrayList<>(); - while (it.hasNext()) { - Gateway aux = it.next(); - if (sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) { - gatewaysToRemove.add(aux); - } - } - gateways.removeAll(gatewaysToRemove); - removeDuplicatedProfiles(gateway.getProfile()); - } - - private void removeDuplicatedProfiles(VpnProfile original) { - Collection profiles = profileManager.getProfiles(); - List removeList = new ArrayList<>(); - for (VpnProfile aux : profiles) { - if (sameConnections(original.mConnections, aux.mConnections)) { - removeList.add(aux); - } - } - for (VpnProfile profile : removeList) { - profileManager.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 sameConnections = 0; - for (Connection c1_aux : c1) { - for (Connection c2_aux : c2) - if (c2_aux.mServerName.equals(c1_aux.mServerName)) { - sameConnections++; - break; - } - } - 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( PreferenceHelper.getEipDefinitionFromPreferences(preferences) ); -- cgit v1.2.3