]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Initiate configured default profile when triggered as Always-on VPN
authorTobias Brunner <tobias@strongswan.org>
Tue, 12 Jun 2018 15:46:08 +0000 (17:46 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 3 Jul 2018 09:31:36 +0000 (11:31 +0200)
With Android 8.1 this isn't triggered after a reboot until the device
has been unlocked once (solving the issue with the key store) and traffic
may optionally be blocked by the user until the VPN is established.

There are still some issues (e.g. password prompts and fatal errors), and we
might need some workaround for older Android releases.

src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java

index cb148521b82781f67b1805f02073c02d32775e4b..53d84cf0cb957ced64dbaf4253ad2f99084c4af1 100644 (file)
@@ -75,6 +75,7 @@ import java.util.SortedSet;
 public class CharonVpnService extends VpnService implements Runnable, VpnStateService.VpnStateListener
 {
        private static final String TAG = CharonVpnService.class.getSimpleName();
+       private static final String VPN_SERVICE_ACTION = "android.net.VpnService";
        public static final String DISCONNECT_ACTION = "org.strongswan.android.CharonVpnService.DISCONNECT";
        private static final String NOTIFICATION_CHANNEL = "org.strongswan.android.CharonVpnService.VPN_STATE_NOTIFICATION";
        public static final String LOG_FILE = "charon.log";
@@ -134,14 +135,21 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
        {
                if (intent != null)
                {
-                       if (DISCONNECT_ACTION.equals(intent.getAction()))
-                       {
-                               setNextProfile(null);
+                       VpnProfile profile = null;
+
+                       if (VPN_SERVICE_ACTION.equals(intent.getAction()))
+                       {       /* triggered when Always-on VPN is activated */
+                               SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
+                               String uuid = pref.getString(Constants.PREF_DEFAULT_VPN_PROFILE, null);
+                               if (uuid == null || uuid.equals(Constants.PREF_DEFAULT_VPN_PROFILE_MRU))
+                               {
+                                       uuid = pref.getString(Constants.PREF_MRU_VPN_PROFILE, null);
+                               }
+                               profile = mDataSource.getVpnProfile(uuid);
                        }
-                       else
+                       else if (!DISCONNECT_ACTION.equals(intent.getAction()))
                        {
                                Bundle bundle = intent.getExtras();
-                               VpnProfile profile = null;
                                if (bundle != null)
                                {
                                        profile = mDataSource.getVpnProfile(bundle.getLong(VpnProfileDataSource.KEY_ID));
@@ -155,8 +163,8 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                                                        .apply();
                                        }
                                }
-                               setNextProfile(profile);
                        }
+                       setNextProfile(profile);
                }
                return START_NOT_STICKY;
        }