From d62526fde6566b6a51ccc2a3f2095ea6d6584fd6 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 9 Mar 2020 10:01:54 -0600 Subject: WgQuickBackend: by default use single-tunnel mode like GoBackend, but add option Note that this currently doesn't play well with people activating wg-quick tunnels from outside the app. Those tunnels won't be deactivated. But presumably that's desired behavior anyway, considering people are mucking around at the command line. Signed-off-by: Jason A. Donenfeld --- .../main/java/com/wireguard/android/Application.java | 20 ++++++++++++++++++-- .../wireguard/android/activity/SettingsActivity.java | 3 ++- ui/src/main/res/values/strings.xml | 3 +++ ui/src/main/res/xml/preferences.xml | 6 ++++++ 4 files changed, 29 insertions(+), 3 deletions(-) (limited to 'ui/src') diff --git a/ui/src/main/java/com/wireguard/android/Application.java b/ui/src/main/java/com/wireguard/android/Application.java index 2ebeb69d..655c6b21 100644 --- a/ui/src/main/java/com/wireguard/android/Application.java +++ b/ui/src/main/java/com/wireguard/android/Application.java @@ -35,7 +35,7 @@ import java.util.Locale; import java9.util.concurrent.CompletableFuture; -public class Application extends android.app.Application { +public class Application extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener { private static final String TAG = "WireGuard/" + Application.class.getSimpleName(); public static final String USER_AGENT; @@ -86,7 +86,9 @@ public class Application extends android.app.Application { try { if (!didStartRootShell) app.rootShell.start(); - backend = new WgQuickBackend(app.getApplicationContext(), app.rootShell, app.toolsInstaller); + WgQuickBackend wgQuickBackend = new WgQuickBackend(app.getApplicationContext(), app.rootShell, app.toolsInstaller); + wgQuickBackend.setMultipleTunnels(app.sharedPreferences.getBoolean("multiple_tunnels", false)); + backend = wgQuickBackend; } catch (final Exception ignored) { } } @@ -167,5 +169,19 @@ public class Application extends android.app.Application { tunnelManager.onCreate(); asyncWorker.supplyAsync(Application::getBackend).thenAccept(futureBackend::complete); + + sharedPreferences.registerOnSharedPreferenceChangeListener(this); + } + + @Override + public void onTerminate() { + sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); + super.onTerminate(); + } + + @Override + public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) { + if ("multiple_tunnels".equals(key) && backend != null && backend instanceof WgQuickBackend) + ((WgQuickBackend)backend).setMultipleTunnels(sharedPreferences.getBoolean(key, false)); } } diff --git a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java index f545c371..b597a9b1 100644 --- a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java +++ b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java @@ -99,7 +99,8 @@ public class SettingsActivity extends ThemeChangeAwareActivity { final Preference wgQuickOnlyPrefs[] = { getPreferenceManager().findPreference("tools_installer"), - getPreferenceManager().findPreference("restore_on_boot") + getPreferenceManager().findPreference("restore_on_boot"), + getPreferenceManager().findPreference("multiple_tunnels") }; for (final Preference pref : wgQuickOnlyPrefs) pref.setVisible(false); diff --git a/ui/src/main/res/values/strings.xml b/ui/src/main/res/values/strings.xml index 45964eec..5df74017 100644 --- a/ui/src/main/res/values/strings.xml +++ b/ui/src/main/res/values/strings.xml @@ -104,6 +104,9 @@ Downloading and installing… Something went wrong. Please try again MTU + Allow multiple simultaneous tunnels + Multiple tunnels may be turned on simultaneously + Turning on one tunnel will turn off others Name Trying to bring up a tunnel with no config No configurations found diff --git a/ui/src/main/res/xml/preferences.xml b/ui/src/main/res/xml/preferences.xml index 9c09ae89..6899edce 100644 --- a/ui/src/main/res/xml/preferences.xml +++ b/ui/src/main/res/xml/preferences.xml @@ -8,6 +8,12 @@ android:title="@string/restore_on_boot_title" /> +