From aaab1021aa5c6151dfeb9920fd48ebbc52076363 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 6 Oct 2021 12:47:01 +0200 Subject: Allow setting a default profile via App Restrictions --- .../de/blinkt/openvpn/api/AppRestrictions.java | 49 ++++++++++++++++------ main/src/main/res/values/untranslatable.xml | 1 + main/src/main/res/xml/app_restrictions.xml | 7 ++++ 3 files changed, 44 insertions(+), 13 deletions(-) (limited to 'main/src') diff --git a/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java b/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java index 02b54add..175ecb70 100644 --- a/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java +++ b/main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java @@ -10,9 +10,12 @@ import android.content.*; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; +import android.text.TextUtils; + import de.blinkt.openvpn.VpnProfile; import de.blinkt.openvpn.core.ConfigParser; import de.blinkt.openvpn.core.Connection; +import de.blinkt.openvpn.core.Preferences; import de.blinkt.openvpn.core.ProfileManager; import de.blinkt.openvpn.core.VpnStatus; @@ -99,6 +102,9 @@ public class AppRestrictions { Set provisionedUuids = new HashSet<>(); + String defaultprofile = restrictions.getString("defaultprofile", null); + boolean defaultprofileProvisioned = false; + ProfileManager pm = ProfileManager.getInstance(c); for (Parcelable profile : profileList) { if (!(profile instanceof Bundle)) { @@ -116,6 +122,9 @@ public class AppRestrictions { continue; } + if (uuid.equals(defaultprofile)) + defaultprofileProvisioned = true; + String ovpnHash = hashConfig(ovpn); provisionedUuids.add(uuid.toLowerCase(Locale.ENGLISH)); @@ -135,36 +144,51 @@ public class AppRestrictions { Vector profilesToRemove = new Vector<>(); // get List of all managed profiles - for (VpnProfile vp: pm.getProfiles()) - { + for (VpnProfile vp : pm.getProfiles()) { if (PROFILE_CREATOR.equals(vp.mProfileCreator)) { if (!provisionedUuids.contains(vp.getUUIDString())) profilesToRemove.add(vp); } } - for (VpnProfile vp: profilesToRemove) { + for (VpnProfile vp : profilesToRemove) { VpnStatus.logInfo("Remove with uuid: %s and name: %s since it is no longer in the list of managed profiles"); pm.removeProfile(c, vp); } + if (!TextUtils.isEmpty(defaultprofile)) { + if (!defaultprofileProvisioned) { + VpnStatus.logError("App restrictions: Setting a default profile UUID without providing a profile with that UUID"); + } else { + SharedPreferences prefs = Preferences.getDefaultSharedPreferences(c); + String uuid = prefs.getString("alwaysOnVpn", null); + if (!defaultprofile.equals(uuid)) + { + SharedPreferences.Editor editor = prefs.edit(); + editor.putString("alwaysOnVpn", defaultprofile); + editor.apply(); + } + } + } } private String prepare(String config) { String newLine = System.getProperty("line.separator"); - if (!config.contains(newLine)&& !config.contains(" ")) { + if (!config.contains(newLine) && !config.contains(" ")) { try { byte[] decoded = android.util.Base64.decode(config.getBytes(), android.util.Base64.DEFAULT); - config = new String(decoded); - return config; - } catch(IllegalArgumentException e) { - + config = new String(decoded); + return config; + } catch (IllegalArgumentException e) { + } } return config; - }; - + } + + ; + private void addProfile(Context c, String config, String uuid, String name, VpnProfile vpnProfile) { - config = prepare(config); + config = prepare(config); ConfigParser cp = new ConfigParser(); try { cp.parseConfig(new StringReader(config)); @@ -204,8 +228,7 @@ public class AppRestrictions { applyRestrictions(c); } - public void pauseCheckRestrictions(Context c) - { + public void pauseCheckRestrictions(Context c) { removeChangesListener(c); } } diff --git a/main/src/main/res/values/untranslatable.xml b/main/src/main/res/values/untranslatable.xml index c42c2e92..bb2b643d 100644 --- a/main/src/main/res/values/untranslatable.xml +++ b/main/src/main/res/values/untranslatable.xml @@ -74,6 +74,7 @@ List of VPN configurations VPN configuration Version of the managed configuration schema (Currently always 1) + UUID of the profile that should be selected as default profile in the app The app OpenVPN for Android does not communicate to any server other than the OpenVPN servers provided in configuration files. The author himself does not collect any data and no therefore also no data is saved. For the privacy policy for the OpenVPN server/VPN service you are using (or other services related to the project like GitHub), please refer to their respective privacy policy. End of service The program and its components are under open-source licenses that allow you to use this app forever according to terms of the open-source licenses for details.<p> However, the author reserves the right to suspend development or stop publishing the OpenVPN app or updates to it at any time. diff --git a/main/src/main/res/xml/app_restrictions.xml b/main/src/main/res/xml/app_restrictions.xml index 38e1dcf3..bd053388 100644 --- a/main/src/main/res/xml/app_restrictions.xml +++ b/main/src/main/res/xml/app_restrictions.xml @@ -61,4 +61,11 @@ --> + \ No newline at end of file -- cgit v1.2.3