summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2021-10-06 12:47:01 +0200
committerArne Schwabe <arne@rfc2549.org>2021-10-06 12:47:01 +0200
commitaaab1021aa5c6151dfeb9920fd48ebbc52076363 (patch)
tree68f9754557f6456e3a2c3a51891c3be78a8d993c
parent858371c328bc6c60dec4b3b0ea7d7573d78d3e63 (diff)
Allow setting a default profile via App Restrictions
-rw-r--r--main/src/main/java/de/blinkt/openvpn/api/AppRestrictions.java49
-rw-r--r--main/src/main/res/values/untranslatable.xml1
-rw-r--r--main/src/main/res/xml/app_restrictions.xml7
3 files changed, 44 insertions, 13 deletions
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<String> 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<VpnProfile> 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 @@
<string name="apprest_vpnlist">List of VPN configurations</string>
<string name="apprest_vpnconf">VPN configuration</string>
<string name="apprest_ver">Version of the managed configuration schema (Currently always 1)</string>
+ <string name="apprest_defprof">UUID of the profile that should be selected as default profile in the app</string>
<string name="privacy_policy">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.</string>
<string name="eol_notice_header">End of service</string>
<string name="eol_notice">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.&lt;p&gt; However, the author reserves the right to suspend development or stop publishing the OpenVPN app or updates to it at any time.</string>
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 @@
-->
</restriction>
</restriction>
+ <restriction
+
+ android:key="defaultprofile"
+ android:title="@string/apprest_defprof"
+ android:restrictionType="string"
+ android:defaultValue=""
+ />
</restrictions> \ No newline at end of file