diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2019-04-13 20:37:03 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-03-17 22:27:45 +0100 |
commit | ab4e2b29e61e913ce63a4494a3b3cfe069a3bb12 (patch) | |
tree | f05fa201e151b272de6dd2356e0ff5da47c746ed /ui | |
parent | 553ec49d11eb7ad727b0409982a2285e86f4917d (diff) |
Implement get and set tunnel config actions
Add three tunnel actions:
com.wireguard.android.action.GET_TUNNEL_CONFIG
com.wireguard.android.action.SET_TUNNEL_CONFIG
com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG
Diffstat (limited to 'ui')
-rw-r--r-- | ui/src/main/AndroidManifest.xml | 3 | ||||
-rw-r--r-- | ui/src/main/java/com/wireguard/android/model/TunnelManager.java | 86 |
2 files changed, 69 insertions, 20 deletions
diff --git a/ui/src/main/AndroidManifest.xml b/ui/src/main/AndroidManifest.xml index 5e993ae2..99a8f623 100644 --- a/ui/src/main/AndroidManifest.xml +++ b/ui/src/main/AndroidManifest.xml @@ -70,6 +70,9 @@ <action android:name="com.wireguard.android.action.REFRESH_TUNNEL_STATES" /> <action android:name="com.wireguard.android.action.SET_TUNNEL_UP" /> <action android:name="com.wireguard.android.action.SET_TUNNEL_DOWN" /> + <action android:name="com.wireguard.android.action.GET_TUNNEL_CONFIG" /> + <action android:name="com.wireguard.android.action.SET_TUNNEL_CONFIG" /> + <action android:name="com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG" /> </intent-filter> </receiver> diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelManager.java b/ui/src/main/java/com/wireguard/android/model/TunnelManager.java index e370f8de..e1806c2f 100644 --- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -8,6 +8,10 @@ package com.wireguard.android.model; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.util.Log; +import androidx.annotation.Nullable; +import androidx.databinding.BaseObservable; +import androidx.databinding.Bindable; import com.wireguard.android.Application; import com.wireguard.android.BR; @@ -19,9 +23,13 @@ import com.wireguard.android.configStore.ConfigStore; import com.wireguard.android.util.ExceptionLoggers; import com.wireguard.android.util.ObservableSortedKeyedArrayList; import com.wireguard.android.util.ObservableSortedKeyedList; +import com.wireguard.config.BadConfigException; import com.wireguard.config.Config; import com.wireguard.util.NonNullForAll; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -42,6 +50,7 @@ import java9.util.stream.StreamSupport; @NonNullForAll public final class TunnelManager extends BaseObservable { + private static final String TAG = "WireGuard/" + TunnelManager.class.getSimpleName(); private static final Comparator<String> COMPARATOR = Comparators.<String>thenComparing( String.CASE_INSENSITIVE_ORDER, Comparators.naturalOrder()); private static final String KEY_LAST_USED_TUNNEL = "last_used_tunnel"; @@ -215,6 +224,15 @@ public final class TunnelManager extends BaseObservable { }).thenApply(tunnel::onConfigChanged); } + CompletionStage<Config> setTunnelUserspaceConfig(final Tunnel tunnel, final Config config) { + Log.println(Log.INFO, TAG, "Called setTunnelUserspaceConfig " + config); + return Application.getAsyncWorker().supplyAsync(() -> { + Log.println(Log.INFO, TAG, "Async setTunnelUserspaceConfig " + config); + final Config appliedConfig = Application.getBackend().applyUserspaceConfig(tunnel, config); + return appliedConfig; + }); + } + CompletionStage<String> setTunnelName(final ObservableTunnel tunnel, final String name) { if (Tunnel.isNameInvalid(name)) return CompletableFuture.failedFuture(new IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name))); @@ -263,6 +281,7 @@ public final class TunnelManager extends BaseObservable { public static final class IntentReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, @Nullable final Intent intent) { + Log.println(Log.INFO, TAG, "Broadcast received " + intent); final TunnelManager manager = Application.getTunnelManager(); if (intent == null) return; @@ -275,29 +294,56 @@ public final class TunnelManager extends BaseObservable { return; } - /* We disable the below, for now, as the security model of allowing this - * might take a bit more consideration. - */ - if (true) - return; - - final State state; - if ("com.wireguard.android.action.SET_TUNNEL_UP".equals(action)) - state = State.UP; - else if ("com.wireguard.android.action.SET_TUNNEL_DOWN".equals(action)) - state = State.DOWN; - else - return; - final String tunnelName = intent.getStringExtra("tunnel"); if (tunnelName == null) return; - manager.getTunnels().thenAccept(tunnels -> { - final ObservableTunnel tunnel = tunnels.get(tunnelName); - if (tunnel == null) - return; - manager.setTunnelState(tunnel, state); - }); + + if ("com.wireguard.android.action.SET_TUNNEL_UP".equals(action) || + "com.wireguard.android.action.SET_TUNNEL_DOWN".equals(action)) { + final State state; + if ("com.wireguard.android.action.SET_TUNNEL_UP".equals(action)) + state = State.UP; + else /* "com.wireguard.android.action.SET_TUNNEL_DOWN".equals(action) */ + state = State.DOWN; + + manager.getTunnels().thenAccept(tunnels -> { + final ObservableTunnel tunnel = tunnels.get(tunnelName); + if (tunnel == null) + return; + manager.setTunnelState(tunnel, state); + }); + } else if ("com.wireguard.android.action.GET_TUNNEL_CONFIG".equals(action) || + "com.wireguard.android.action.SET_TUNNEL_CONFIG".equals(action) || + "com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG".equals(action)) { + final ActionCode code; + if ("com.wireguard.android.action.SET_TUNNEL_CONFIG".equals(action)) + code = ActionCode.SET_CONFIG; + else if ("com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG".equals(action)) + code = ActionCode.SET_USERSPACE_CONFIG; + else /* "com.wireguard.android.action.GET_TUNNEL_CONFIG".equals(action) */ + code = ActionCode.GET_CONFIG; + manager.getTunnels().thenAccept(tunnels -> { + final ObservableTunnel tunnel = tunnels.get(tunnelName); + if (tunnel == null) + return; + + final String config = intent.getStringExtra("config"); + try { + if (code == ActionCode.SET_USERSPACE_CONFIG) { + Log.println(Log.INFO, TAG, "Call setTunnelUserspaceConfig " + config); + manager.setTunnelUserspaceConfig(tunnel, Config.parse(new BufferedReader(new StringReader(config)))); + } else { + Log.println(Log.INFO, TAG, "Call setTunnelConfig " + config); + manager.setTunnelConfig(tunnel, Config.parse(new BufferedReader(new StringReader(config)))); + } + } catch (IOException | BadConfigException e) { + Log.println(Log.ERROR, TAG, Log.getStackTraceString(e)); + } + }); + } else + return; } } + + private enum ActionCode { GET_CONFIG, SET_CONFIG, SET_USERSPACE_CONFIG }; } |