aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2017-08-23 18:04:45 -0500
committerSamuel Holland <samuel@sholland.org>2017-08-23 18:04:45 -0500
commitd1a5c1a72e5181d4e6c3dfff7b9d0552f4500587 (patch)
tree8192894596e8151192834a38faf41481fad4fbb9
parentConfig/Interface: Allow copyFrom() to work on null (diff)
downloadwireguard-android-d1a5c1a72e5181d4e6c3dfff7b9d0552f4500587.tar.xz
wireguard-android-d1a5c1a72e5181d4e6c3dfff7b9d0552f4500587.zip
VpnService: Fix some edge cases in ConfigUpdater
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--app/src/main/java/com/wireguard/android/VpnService.java37
1 files changed, 19 insertions, 18 deletions
diff --git a/app/src/main/java/com/wireguard/android/VpnService.java b/app/src/main/java/com/wireguard/android/VpnService.java
index 19040c56..ad8eb608 100644
--- a/app/src/main/java/com/wireguard/android/VpnService.java
+++ b/app/src/main/java/com/wireguard/android/VpnService.java
@@ -353,27 +353,27 @@ public class VpnService extends Service
}
private class ConfigUpdater extends AsyncTask<Void, Void, Boolean> {
- private Config newConfig;
+ private Config knownConfig;
+ private final Config newConfig;
private final String newName;
- private final Config oldConfig;
private final String oldName;
private final Boolean shouldConnect;
- private ConfigUpdater(final Config oldConfig, final Config newConfig,
+ private ConfigUpdater(final Config knownConfig, final Config newConfig,
final Boolean shouldConnect) {
- this.newConfig = newConfig;
- this.oldConfig = oldConfig;
- this.shouldConnect = shouldConnect;
+ this.knownConfig = knownConfig;
+ this.newConfig = newConfig.copy();
newName = newConfig.getName();
// When adding a config, "old file" and "new file" are the same thing.
- oldName = oldConfig != null ? oldConfig.getName() : newName;
+ oldName = knownConfig != null ? knownConfig.getName() : newName;
+ this.shouldConnect = shouldConnect;
if (isAddOrRename() && configurations.containsKey(newName))
throw new IllegalStateException("Config " + newName + " already exists");
}
@Override
protected Boolean doInBackground(final Void... voids) {
- Log.i(TAG, (oldConfig == null ? "Adding" : "Updating") + " config " + newName);
+ Log.i(TAG, (knownConfig == null ? "Adding" : "Updating") + " config " + newName);
final File newFile = new File(getFilesDir(), newName + ".conf");
final File oldFile = new File(getFilesDir(), oldName + ".conf");
if (isAddOrRename() && newFile.exists()) {
@@ -396,28 +396,29 @@ public class VpnService extends Service
}
private boolean isAddOrRename() {
- return oldConfig == null || !newName.equals(oldName);
+ return knownConfig == null || !newName.equals(oldName);
}
private boolean isRename() {
- return oldConfig != null && !newName.equals(oldName);
+ return knownConfig != null && !newName.equals(oldName);
}
@Override
protected void onPostExecute(final Boolean result) {
if (!result)
return;
- if (oldConfig != null) {
+ if (knownConfig != null)
configurations.remove(oldName);
- oldConfig.copyFrom(newConfig);
- newConfig = oldConfig;
- }
- newConfig.setIsEnabled(false);
- configurations.put(newName, newConfig);
- if (isRename() && oldName.equals(primaryName))
+ if (knownConfig == null)
+ knownConfig = new Config();
+ knownConfig.copyFrom(newConfig);
+ knownConfig.setIsEnabled(false);
+ knownConfig.setIsPrimary(oldName != null && oldName.equals(primaryName));
+ configurations.put(newName, knownConfig);
+ if (isRename() && oldName != null && oldName.equals(primaryName))
preferences.edit().putString(KEY_PRIMARY_CONFIG, newName).apply();
if (shouldConnect)
- new ConfigEnabler(newConfig).execute();
+ new ConfigEnabler(knownConfig).execute();
}
}
}