diff options
Diffstat (limited to 'app/src')
5 files changed, 117 insertions, 107 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 956cd67e..c4f4dbc9 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -37,6 +37,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.TextView; import java.util.Observable; import java.util.Observer; @@ -251,7 +252,7 @@ public class EipFragment extends Fragment implements Observer { } private void handleSwitchOff() { - if (isOpenVpnRunningWithoutNetwork() || eipStatus.isConnecting()) { + if (eipStatus.isConnecting()) { askPendingStartCancellation(); } else if (eipStatus.isConnected()) { askToStopEIP(); diff --git a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java index ee754b32..b4be55a4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/MainActivity.java @@ -2,11 +2,15 @@ package se.leap.bitmaskclient; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; @@ -22,9 +26,14 @@ import org.json.JSONObject; import java.util.Observable; import java.util.Observer; +import de.blinkt.openvpn.core.IOpenVPNServiceInternal; +import de.blinkt.openvpn.core.OpenVPNService; +import de.blinkt.openvpn.core.ProfileManager; +import de.blinkt.openvpn.core.VpnStatus; import se.leap.bitmaskclient.drawer.NavigationDrawerFragment; import se.leap.bitmaskclient.eip.EipCommand; import se.leap.bitmaskclient.eip.EipStatus; +import se.leap.bitmaskclient.eip.VoidVpnService; import se.leap.bitmaskclient.fragments.LogFragment; import static android.content.Intent.CATEGORY_DEFAULT; @@ -34,7 +43,9 @@ import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START; import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP; +import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN; import static se.leap.bitmaskclient.Constants.EIP_REQUEST; +import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_CONFIGURE_LEAP; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN; @@ -51,15 +62,31 @@ import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed; import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements Observer { public final static String TAG = MainActivity.class.getSimpleName(); private Provider provider = new Provider(); private SharedPreferences preferences; + private EipStatus eipStatus; private NavigationDrawerFragment navigationDrawerFragment; private MainActivityBroadcastReceiver mainActivityBroadcastReceiver; + private IOpenVPNServiceInternal mService; + private ServiceConnection openVpnConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName className, + IBinder service) { + mService = IOpenVPNServiceInternal.Stub.asInterface(service); + } + + @Override + public void onServiceDisconnected(ComponentName arg0) { + mService = null; + } + + }; + public final static String ACTION_SHOW_VPN_FRAGMENT = "action_show_vpn_fragment"; public final static String ACTION_SHOW_LOG_FRAGMENT = "action_show_log_fragment"; @@ -87,12 +114,14 @@ public class MainActivity extends AppCompatActivity { R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); + eipStatus = EipStatus.getInstance(); handleIntentAction(getIntent()); } @Override protected void onResume() { super.onResume(); + bindOpenVpnService(); } @Override @@ -176,6 +205,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onPause() { super.onPause(); + unbindService(openVpnConnection); } @Override @@ -185,6 +215,14 @@ public class MainActivity extends AppCompatActivity { super.onDestroy(); } + + @Override + public void update(Observable observable, Object data) { + if (observable instanceof EipStatus) { + eipStatus = (EipStatus) observable; + } + } + private void setUpBroadcastReceiver() { IntentFilter updateIntentFilter = new IntentFilter(BROADCAST_EIP_EVENT); updateIntentFilter.addAction(BROADCAST_PROVIDER_API_EVENT); @@ -245,6 +283,7 @@ public class MainActivity extends AppCompatActivity { case EIP_ACTION_STOP: switch (resultCode) { case RESULT_OK: + stop(); break; case RESULT_CANCELED: break; @@ -304,6 +343,39 @@ public class MainActivity extends AppCompatActivity { } + + private void stop() { + preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply(); + if (eipStatus.isBlockingVpnEstablished()) { + stopBlockingVpn(); + } + disconnect(); + } + + private void stopBlockingVpn() { + Log.d(TAG, "stop VoidVpn!"); + Intent stopVoidVpnIntent = new Intent(this, VoidVpnService.class); + stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN); + startService(stopVoidVpnIntent); + } + + private void disconnect() { + ProfileManager.setConntectedVpnProfileDisconnected(this); + if (mService != null) { + try { + mService.stopVPN(false); + } catch (RemoteException e) { + VpnStatus.logException(e); + } + } + } + + private void bindOpenVpnService() { + Intent intent = new Intent(this, OpenVPNService.class); + intent.setAction(OpenVPNService.START_SERVICE); + bindService(intent, openVpnConnection, Context.BIND_AUTO_CREATE); + } + private void askUserToLogIn(String userMessage) { Intent intent = new Intent(this, LoginActivity.class); intent.putExtra(PROVIDER_KEY, provider); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java index 0c83c261..5cf180d3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -17,13 +17,9 @@ package se.leap.bitmaskclient.eip; import android.app.IntentService; -import android.content.ComponentName; import android.content.Intent; -import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.Bundle; -import android.os.IBinder; -import android.os.RemoteException; import android.os.ResultReceiver; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -32,20 +28,13 @@ import org.json.JSONException; import org.json.JSONObject; import java.lang.ref.WeakReference; -import java.util.Observable; -import java.util.Observer; import de.blinkt.openvpn.LaunchVPN; -import de.blinkt.openvpn.core.IOpenVPNServiceInternal; -import de.blinkt.openvpn.core.OpenVPNService; -import de.blinkt.openvpn.core.ProfileManager; -import de.blinkt.openvpn.core.VpnStatus; import se.leap.bitmaskclient.OnBootReceiver; import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_OK; import static android.content.Intent.CATEGORY_DEFAULT; -import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_NONETWORK; import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE; import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY; @@ -54,7 +43,6 @@ import static se.leap.bitmaskclient.Constants.EIP_ACTION_IS_RUNNING; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_ALWAYS_ON_VPN; import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP; -import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN; import static se.leap.bitmaskclient.Constants.EIP_EARLY_ROUTES; import static se.leap.bitmaskclient.Constants.EIP_RECEIVER; import static se.leap.bitmaskclient.Constants.EIP_REQUEST; @@ -75,7 +63,7 @@ import static se.leap.bitmaskclient.R.string.vpn_certificate_is_invalid; * @author Sean Leonard <meanderingcode@aetherislands.net> * @author Parménides GV <parmegv@sdf.org> */ -public final class EIP extends IntentService implements Observer{ +public final class EIP extends IntentService { public final static String TAG = EIP.class.getSimpleName(), SERVICE_API_PATH = "config/eip-service.json", @@ -85,27 +73,6 @@ public final class EIP extends IntentService implements Observer{ private WeakReference<ResultReceiver> mReceiverRef = new WeakReference<>(null); private SharedPreferences preferences; - private EipStatus eipStatus; - private IOpenVPNServiceInternal mService; - private boolean stopEipIfConnectionIsEstablished = false; - - private ServiceConnection openVpnConnection = new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName className, - IBinder service) { - mService = IOpenVPNServiceInternal.Stub.asInterface(service); - if (stopEipIfConnectionIsEstablished) { - stopEipIfConnectionIsEstablished = false; - stopEIP(); - } - } - - @Override - public void onServiceDisconnected(ComponentName arg0) { - mService = null; - } - }; - public EIP() { super(TAG); } @@ -113,31 +80,10 @@ public final class EIP extends IntentService implements Observer{ @Override public void onCreate() { super.onCreate(); - eipStatus = EipStatus.getInstance(); - eipStatus.addObserver(this); - Intent intent = new Intent(this, OpenVPNService.class); - intent.setAction(OpenVPNService.START_SERVICE); - bindOpenVpnService(); preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); } @Override - public void onDestroy() { - super.onDestroy(); - unbindService(openVpnConnection); - eipStatus.deleteObserver(this); - eipStatus = null; - mService = null; - } - - @Override - public void update(Observable observable, Object data) { - if (observable instanceof EipStatus) { - eipStatus = (EipStatus) observable; - } - } - - @Override protected void onHandleIntent(Intent intent) { String action = intent.getAction(); if (intent.getParcelableExtra(EIP_RECEIVER) != null) { @@ -237,33 +183,13 @@ public final class EIP extends IntentService implements Observer{ } private void stopEIP() { - if (mService == null) { - stopEipIfConnectionIsEstablished = true; - return; - } - + // TODO stop eip from here if possible... + EipStatus eipStatus = EipStatus.getInstance(); int resultCode = RESULT_CANCELED; - if (isOpenVpnRunningWithoutNetwork() || eipStatus.isConnected() || eipStatus.isConnecting()) { - preferences.edit().putBoolean(EIP_RESTART_ON_BOOT, false).apply(); - if (eipStatus.isBlockingVpnEstablished()) { - stopBlockingVpn(); - } - ProfileManager.setConntectedVpnProfileDisconnected(this); - try { - mService.stopVPN(false); - } catch (RemoteException e) { - VpnStatus.logException(e); - } + if (eipStatus.isConnected() || eipStatus.isConnecting()) resultCode = RESULT_OK; - } - tellToReceiverOrBroadcast(EIP_ACTION_STOP, resultCode); - } - private void stopBlockingVpn() { - Log.d(TAG, "stop VoidVpn!"); - Intent stopVoidVpnIntent = new Intent(this, VoidVpnService.class); - stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN); - startService(stopVoidVpnIntent); + tellToReceiverOrBroadcast(EIP_ACTION_STOP, resultCode); } /** @@ -272,6 +198,7 @@ public final class EIP extends IntentService implements Observer{ * request if it's not connected, <code>Activity.RESULT_OK</code> otherwise. */ private void isRunning() { + EipStatus eipStatus = EipStatus.getInstance(); int resultCode = (eipStatus.isConnected()) ? RESULT_OK : RESULT_CANCELED; @@ -351,22 +278,4 @@ public final class EIP extends IntentService implements Observer{ e.printStackTrace(); } } - - private boolean isOpenVpnRunningWithoutNetwork() { - boolean isRunning = false; - try { - isRunning = eipStatus.getLevel() == LEVEL_NONETWORK && - mService.isVpnRunning(); - } catch (Exception e) { - //eat me - e.printStackTrace(); - } - return isRunning; - } - - private void bindOpenVpnService() { - Intent intent = new Intent(this, OpenVPNService.class); - intent.setAction(OpenVPNService.START_SERVICE); - bindService(intent, openVpnConnection, BIND_AUTO_CREATE); - } } diff --git a/app/src/main/res/layout-xlarge/eip_service_fragment.xml b/app/src/main/res/layout-xlarge/eip_service_fragment.xml index 2fe046b8..c09bda2d 100644 --- a/app/src/main/res/layout-xlarge/eip_service_fragment.xml +++ b/app/src/main/res/layout-xlarge/eip_service_fragment.xml @@ -58,7 +58,11 @@ android:id="@+id/eipLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginTop="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" android:text="@string/eip_service_label" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" @@ -98,7 +102,11 @@ android:id="@+id/routed_text" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginTop="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" android:text="@string/vpn_securely_routed" android:gravity="center" android:visibility="visible" @@ -110,7 +118,10 @@ android:id="@+id/vpn_route" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" android:gravity="center" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" diff --git a/app/src/main/res/layout/eip_service_fragment.xml b/app/src/main/res/layout/eip_service_fragment.xml index 347f09d2..30b94de9 100644 --- a/app/src/main/res/layout/eip_service_fragment.xml +++ b/app/src/main/res/layout/eip_service_fragment.xml @@ -56,7 +56,11 @@ android:id="@+id/eipLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginTop="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" android:text="@string/eip_service_label" android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" @@ -81,18 +85,28 @@ android:id="@+id/vpn_main_button" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginBottom="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginTop="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" app:layout_constraintBottom_toBottomOf="@+id/background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" android:text="@string/vpn.button.turn.on" - style="@style/BitmaskButtonBlack" /> + style="@style/BitmaskButtonBlack" + /> <android.support.v7.widget.AppCompatTextView android:id="@+id/routed_text" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginTop="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" android:text="@string/vpn_securely_routed" android:gravity="center" android:visibility="visible" @@ -104,7 +118,10 @@ android:id="@+id/vpn_route" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:padding="@dimen/stdpadding" + android:layout_marginEnd="@dimen/stdpadding" + android:layout_marginStart="@dimen/stdpadding" + android:layout_marginLeft="@dimen/stdpadding" + android:layout_marginRight="@dimen/stdpadding" android:gravity="center" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" |