summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java')
-rw-r--r--app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java114
1 files changed, 86 insertions, 28 deletions
diff --git a/app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java b/app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java
index 4f9c219b..f776fc2e 100644
--- a/app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java
+++ b/app/src/main/java/de/blinkt/openvpn/core/ProfileManager.java
@@ -9,7 +9,6 @@ import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
-import android.preference.PreferenceManager;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -17,7 +16,9 @@ import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Locale;
import java.util.Set;
+import java.util.UUID;
import de.blinkt.openvpn.VpnProfile;
@@ -25,6 +26,7 @@ public class ProfileManager {
private static final String PREFS_NAME = "VPNList";
private static final String LAST_CONNECTED_PROFILE = "lastConnectedProfile";
+ private static final String TEMPORARY_PROFILE_FILENAME = "temporary-vpn-profile";
private static ProfileManager instance;
private static VpnProfile mLastConnectedVpn = null;
@@ -59,30 +61,31 @@ public class ProfileManager {
}
public static void setConntectedVpnProfileDisconnected(Context c) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
+ SharedPreferences prefs = Preferences.getDefaultSharedPreferences(c);
Editor prefsedit = prefs.edit();
prefsedit.putString(LAST_CONNECTED_PROFILE, null);
prefsedit.apply();
}
- public static void setConnectedVpnProfile(Context c, VpnProfile connectedrofile) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
+ /**
+ * Sets the profile that is connected (to connect if the service restarts)
+ */
+ public static void setConnectedVpnProfile(Context c, VpnProfile connectedProfile) {
+ SharedPreferences prefs = Preferences.getDefaultSharedPreferences(c);
Editor prefsedit = prefs.edit();
- prefsedit.putString(LAST_CONNECTED_PROFILE, connectedrofile.getUUIDString());
+ prefsedit.putString(LAST_CONNECTED_PROFILE, connectedProfile.getUUIDString());
prefsedit.apply();
- mLastConnectedVpn = connectedrofile;
+ mLastConnectedVpn = connectedProfile;
}
- public static VpnProfile getLastConnectedProfile(Context c, boolean onBoot) {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
-
- boolean useStartOnBoot = prefs.getBoolean("restartvpnonboot", false);
-
- if (onBoot && !useStartOnBoot)
- return null;
+ /**
+ * Returns the profile that was last connected (to connect if the service restarts)
+ */
+ public static VpnProfile getLastConnectedProfile(Context c) {
+ SharedPreferences prefs = Preferences.getDefaultSharedPreferences(c);
String lastConnectedProfile = prefs.getString(LAST_CONNECTED_PROFILE, null);
if (lastConnectedProfile != null)
@@ -106,7 +109,7 @@ public class ProfileManager {
}
public void saveProfileList(Context context) {
- SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
+ SharedPreferences sharedprefs = Preferences.getSharedPreferencesMulti(PREFS_NAME, context);
Editor editor = sharedprefs.edit();
editor.putStringSet("vpnlist", profiles.keySet());
@@ -124,24 +127,35 @@ public class ProfileManager {
}
- public static void setTemporaryProfile(VpnProfile tmp) {
+ public static void setTemporaryProfile(Context c, VpnProfile tmp) {
ProfileManager.tmpprofile = tmp;
+ saveProfile(c, tmp, true, true);
}
- public static boolean isTempProfile()
- {
- return mLastConnectedVpn == tmpprofile;
+ public static boolean isTempProfile() {
+ return mLastConnectedVpn != null && mLastConnectedVpn == tmpprofile;
}
-
public void saveProfile(Context context, VpnProfile profile) {
- ObjectOutputStream vpnfile;
+ saveProfile(context, profile, true, false);
+ }
+
+ private static void saveProfile(Context context, VpnProfile profile, boolean updateVersion, boolean isTemporary) {
+
+ if (updateVersion)
+ profile.mVersion += 1;
+ ObjectOutputStream vpnFile;
+
+ String filename = profile.getUUID().toString() + ".vp";
+ if (isTemporary)
+ filename = TEMPORARY_PROFILE_FILENAME + ".vp";
+
try {
- vpnfile = new ObjectOutputStream(context.openFileOutput((profile.getUUID().toString() + ".vp"), Activity.MODE_PRIVATE));
+ vpnFile = new ObjectOutputStream(context.openFileOutput(filename, Activity.MODE_PRIVATE));
- vpnfile.writeObject(profile);
- vpnfile.flush();
- vpnfile.close();
+ vpnFile.writeObject(profile);
+ vpnFile.flush();
+ vpnFile.close();
} catch (IOException e) {
VpnStatus.logException("saving VPN profile", e);
throw new RuntimeException(e);
@@ -151,11 +165,13 @@ public class ProfileManager {
private void loadVPNList(Context context) {
profiles = new HashMap<>();
- SharedPreferences listpref = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
+ SharedPreferences listpref = Preferences.getSharedPreferencesMulti(PREFS_NAME, context);
Set<String> vlist = listpref.getStringSet("vpnlist", null);
if (vlist == null) {
vlist = new HashSet<>();
}
+ // Always try to load the temporary profile
+ vlist.add(TEMPORARY_PROFILE_FILENAME);
for (String vpnentry : vlist) {
try {
@@ -167,10 +183,15 @@ public class ProfileManager {
continue;
vp.upgradeProfile();
- profiles.put(vp.getUUID().toString(), vp);
+ if (vpnentry.equals(TEMPORARY_PROFILE_FILENAME)) {
+ tmpprofile = vp;
+ } else {
+ profiles.put(vp.getUUID().toString(), vp);
+ }
} catch (IOException | ClassNotFoundException e) {
- VpnStatus.logException("Loading VPN List", e);
+ if (!vpnentry.equals(TEMPORARY_PROFILE_FILENAME))
+ VpnStatus.logException("Loading VPN List", e);
}
}
}
@@ -187,12 +208,49 @@ public class ProfileManager {
}
public static VpnProfile get(Context context, String profileUUID) {
+ return get(context, profileUUID, 0, 10);
+ }
+
+ public static VpnProfile get(Context context, String profileUUID, int version, int tries) {
checkInstance(context);
- return get(profileUUID);
+ VpnProfile profile = get(profileUUID);
+ int tried = 0;
+ while ((profile == null || profile.mVersion < version) && (tried++ < tries)) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException ignored) {
+ }
+ instance.loadVPNList(context);
+ profile = get(profileUUID);
+ int ver = profile == null ? -1 : profile.mVersion;
+ }
+
+ if (tried > 5)
+
+ {
+ int ver = profile == null ? -1 : profile.mVersion;
+ VpnStatus.logError(String.format(Locale.US, "Used x %d tries to get current version (%d/%d) of the profile", tried, ver, version));
+ }
+ return profile;
}
public static VpnProfile getLastConnectedVpn() {
return mLastConnectedVpn;
}
+ public static VpnProfile getAlwaysOnVPN(Context context) {
+ checkInstance(context);
+ SharedPreferences prefs = Preferences.getDefaultSharedPreferences(context);
+
+ String uuid = prefs.getString("alwaysOnVpn", null);
+ return get(uuid);
+
+ }
+
+ public static void updateLRU(Context c, VpnProfile profile) {
+ profile.mLastUsed = System.currentTimeMillis();
+ // LRU does not change the profile, no need for the service to refresh
+ if (profile!=tmpprofile)
+ saveProfile(c, profile, false, false);
+ }
}