diff options
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/eip')
6 files changed, 92 insertions, 23 deletions
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 971d973f..0e6d9b95 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -53,7 +53,9 @@ import se.leap.bitmaskclient.OnBootReceiver; 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; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY; @@ -63,6 +65,7 @@ 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_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; @@ -150,10 +153,12 @@ public final class EIP extends JobIntentService implements Observer { if (intent.getParcelableExtra(EIP_RECEIVER) != null) { mResultRef = new WeakReference<>((ResultReceiver) intent.getParcelableExtra(EIP_RECEIVER)); } + int nClosestGateway; switch (action) { case EIP_ACTION_START: boolean earlyRoutes = intent.getBooleanExtra(EIP_EARLY_ROUTES, true); - startEIP(earlyRoutes); + nClosestGateway = intent.getIntExtra(EIP_N_CLOSEST_GATEWAY, 0); + startEIP(earlyRoutes, nClosestGateway); break; case EIP_ACTION_START_ALWAYS_ON_VPN: startEIPAlwaysOnVpn(); @@ -170,13 +175,17 @@ public final class EIP extends JobIntentService implements Observer { } } + /** * 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. + * @param earlyRoutes if true, a void vpn gets set up + * @param nClosestGateway the gateway that is the n nearest one to the users place */ @SuppressLint("ApplySharedPref") - private void startEIP(boolean earlyRoutes) { + private void startEIP(boolean earlyRoutes, int nClosestGateway) { + Log.d(TAG, "start EIP with early routes: " + earlyRoutes + " and nClosest Gateway: " + nClosestGateway); if (!eipStatus.isBlockingVpnEstablished() && earlyRoutes) { earlyRoutes(); } @@ -186,16 +195,17 @@ public final class EIP extends JobIntentService implements Observer { preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, true).commit(); } - GatewaysManager gatewaysManager = gatewaysFromPreferences(); if (!isVPNCertificateValid()) { setErrorResult(result, vpn_certificate_is_invalid, ERROR_INVALID_VPN_CERTIFICATE.toString()); - tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED); + tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED, result); return; } - Gateway gateway = gatewaysManager.select(); + GatewaysManager gatewaysManager = gatewaysFromPreferences(); + Gateway gateway = gatewaysManager.select(nClosestGateway); + if (gateway != null && gateway.getProfile() != null) { - launchActiveGateway(gateway); + launchActiveGateway(gateway, nClosestGateway); tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_OK); } else tellToReceiverOrBroadcast(EIP_ACTION_START, RESULT_CANCELED); @@ -209,11 +219,11 @@ public final class EIP extends JobIntentService implements Observer { Log.d(TAG, "startEIPAlwaysOnVpn vpn"); GatewaysManager gatewaysManager = gatewaysFromPreferences(); - Gateway gateway = gatewaysManager.select(); + Gateway gateway = gatewaysManager.select(0); if (gateway != null && gateway.getProfile() != null) { - Log.d(TAG, "startEIPAlwaysOnVpn eip launch avtive gateway vpn"); - launchActiveGateway(gateway); + Log.d(TAG, "startEIPAlwaysOnVpn eip launch closest gateway."); + launchActiveGateway(gateway, 0); } else { Log.d(TAG, "startEIPAlwaysOnVpn no active profile available!"); } @@ -234,13 +244,21 @@ public final class EIP extends JobIntentService implements Observer { * * @param gateway to connect to */ - private void launchActiveGateway(Gateway gateway) { - Intent intent = new Intent(this, LaunchVPN.class); - intent.setAction(Intent.ACTION_MAIN); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); - intent.putExtra(LaunchVPN.EXTRA_TEMP_VPN_PROFILE, gateway.getProfile()); - startActivity(intent); + private void launchActiveGateway(@NonNull Gateway gateway, int nClosestGateway) { + /*Intent gatewaySetupWatcherIntent = new Intent(BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT); + gatewaySetupWatcherIntent.putExtra(EIP_REQUEST, ) + gatewaySetupWatcherIntent.putExtra(LaunchVPN.EXTRA_TEMP_VPN_PROFILE, gateway.getProfile());*/ + + + Intent intent = new Intent(BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT); //new Intent(this, LaunchVPN.class); + //intent.setAction(Intent.ACTION_MAIN); + //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + //intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); + intent.putExtra(EXTRA_TEMP_VPN_PROFILE, gateway.getProfile()); + intent.putExtra(Gateway.KEY_N_CLOSEST_GATEWAY, nClosestGateway); + //startActivity(intent); + LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + } /** @@ -277,6 +295,14 @@ public final class EIP extends JobIntentService implements Observer { } /** + * Updates the eip.json. It containes information about the vpn service of a provider such as + * available gateways, supported protocols and open ports. + */ + private void updateEipJson() { + + } + + /** * read VPN certificate from preferences and check it * broadcast result */ diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java index 19735483..2bca5c9b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipCommand.java @@ -10,10 +10,13 @@ import android.support.annotation.VisibleForTesting; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import se.leap.bitmaskclient.Provider; + import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY; 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_EARLY_ROUTES; +import static se.leap.bitmaskclient.Constants.EIP_N_CLOSEST_GATEWAY; import static se.leap.bitmaskclient.Constants.EIP_RECEIVER; /** @@ -22,7 +25,7 @@ import static se.leap.bitmaskclient.Constants.EIP_RECEIVER; public class EipCommand { - public static void execute(@NotNull Context context, @NotNull String action) { + private static void execute(@NotNull Context context, @NotNull String action) { execute(context, action, null, null); } @@ -33,7 +36,7 @@ public class EipCommand { * filter for the EIP class * @param resultReceiver The resultreceiver to reply to */ - public static void execute(@NotNull Context context, @NotNull String action, @Nullable ResultReceiver resultReceiver, @Nullable Intent vpnIntent) { + private static void execute(@NotNull Context context, @NotNull String action, @Nullable ResultReceiver resultReceiver, @Nullable Intent vpnIntent) { // TODO validate "action"...how do we get the list of intent-filters for a class via Android API? if (vpnIntent == null) { vpnIntent = new Intent(); @@ -48,6 +51,14 @@ public class EipCommand { public static void startVPN(@NonNull Context context, boolean earlyRoutes) { Intent baseIntent = new Intent(); baseIntent.putExtra(EIP_EARLY_ROUTES, earlyRoutes); + baseIntent.putExtra(EIP_N_CLOSEST_GATEWAY, 0); + execute(context, EIP_ACTION_START, null, baseIntent); + } + + public static void startVPN(@NonNull Context context, boolean earlyRoutes, int nClosestGateway) { + Intent baseIntent = new Intent(); + baseIntent.putExtra(EIP_EARLY_ROUTES, earlyRoutes); + baseIntent.putExtra(EIP_N_CLOSEST_GATEWAY, nClosestGateway); execute(context, EIP_ACTION_START, null, baseIntent); } 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 df252500..861f5fd3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java @@ -19,6 +19,7 @@ package se.leap.bitmaskclient.eip; import android.content.Context; import android.os.AsyncTask; import android.support.annotation.VisibleForTesting; +import android.util.Log; import java.util.Observable; @@ -26,6 +27,7 @@ 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. @@ -76,7 +78,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { currentStatus.setLocalizedResId(localizedResId); currentStatus.setLevel(level); currentStatus.setEipLevel(level); - if (tmp != currentStatus.getLevel()) { + if (tmp != currentStatus.getLevel() || "RECONNECTING".equals(state)) { currentStatus.setChanged(); currentStatus.notifyObservers(); } @@ -86,6 +88,10 @@ public class EipStatus extends Observable implements VpnStatus.StateListener { public void setConnectedVPN(String uuid) { } + public boolean isReconnecting() { + Log.d(TAG, "eip currentVPNStatus : " + currentStatus.getState() ); + return "RECONNECTING".equals(currentStatus.getState()); + } private void setEipLevel(ConnectionStatus level) { switch (level) { 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 6cccdcd2..317a91bd 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -38,6 +38,7 @@ import de.blinkt.openvpn.core.ConfigParser; public class Gateway { public final static String TAG = Gateway.class.getSimpleName(); + public final static String KEY_N_CLOSEST_GATEWAY = "N_CLOSEST_GATEWAY"; private JSONObject generalConfiguration; private JSONObject secrets; 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 90c8f890..078e3fd5 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java @@ -1,20 +1,42 @@ package se.leap.bitmaskclient.eip; +import android.util.Log; + import java.util.*; public class GatewaySelector { + private final static String TAG = GatewaySelector.class.getSimpleName(); List<Gateway> gateways; + TreeMap<Integer, Set<Gateway>> offsets; public GatewaySelector(List<Gateway> gateways) { this.gateways = gateways; + this.offsets = calculateOffsets(); + } public Gateway select() { return closestGateway(); } + public Gateway select(int nClosest) throws IndexOutOfBoundsException{ + int i = 0; + for (Map.Entry<Integer,Set<Gateway>> entrySet : offsets.entrySet()) { + Iterator<Gateway> iterator = entrySet.getValue().iterator(); + while (iterator.hasNext()) { + Gateway gateway = iterator.next(); + if (i == nClosest) { + return gateway; + } + i = i+1; + } + } + + Log.e(TAG, "There are less than " + nClosest + " Gateways available."); + return null; + } + private Gateway closestGateway() { - TreeMap<Integer, Set<Gateway>> offsets = calculateOffsets(); return offsets.isEmpty() ? null : offsets.firstEntry().getValue().iterator().next(); } 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 5b4db5af..003cef7d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -18,6 +18,7 @@ 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; @@ -46,6 +47,8 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; */ public class GatewaysManager { + private static final String TAG = GatewaysManager.class.getSimpleName(); + private Context context; private SharedPreferences preferences; private List<Gateway> gateways = new ArrayList<>(); @@ -60,11 +63,11 @@ public class GatewaysManager { /** * select closest Gateway - * @return the closest Gateway + * @return the n closest Gateway */ - public Gateway select() { + public Gateway select(int nClosest) { GatewaySelector gatewaySelector = new GatewaySelector(gateways); - return gatewaySelector.select(); + return gatewaySelector.select(nClosest); } /** |