Make TunnelManager the point of asynchronicity

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland
2018-01-07 00:24:56 -06:00
parent 5a2f692d73
commit be8b6017d5
6 changed files with 125 additions and 154 deletions
@@ -6,7 +6,6 @@ import android.util.Log;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.model.Tunnel.Statistics;
import com.wireguard.android.util.AsyncWorker;
import com.wireguard.android.util.RootShell;
import com.wireguard.config.Config;
@@ -17,25 +16,20 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java9.util.concurrent.CompletableFuture;
import java9.util.concurrent.CompletionStage;
import java9.util.stream.Collectors;
import java9.util.stream.Stream;
/**
* Created by samuel on 12/19/17.
* WireGuard backend that uses {@code wg-quick} to implement tunnel configuration.
*/
public final class WgQuickBackend implements Backend {
private static final String TAG = WgQuickBackend.class.getSimpleName();
private final AsyncWorker asyncWorker;
private final Context context;
private final RootShell rootShell;
public WgQuickBackend(final AsyncWorker asyncWorker, final Context context,
final RootShell rootShell) {
this.asyncWorker = asyncWorker;
public WgQuickBackend(final Context context, final RootShell rootShell) {
this.context = context;
this.rootShell = rootShell;
}
@@ -49,47 +43,47 @@ public final class WgQuickBackend implements Backend {
}
@Override
public CompletionStage<Config> applyConfig(final Tunnel tunnel, final Config config) {
public Config applyConfig(final Tunnel tunnel, final Config config) {
if (tunnel.getState() == State.UP)
return CompletableFuture.failedFuture(new UnsupportedOperationException("stub"));
return CompletableFuture.completedFuture(config);
throw new UnsupportedOperationException("Not implemented");
return config;
}
@Override
public CompletionStage<Set<String>> enumerate() {
return asyncWorker.supplyAsync(() -> {
final List<String> output = new LinkedList<>();
// Don't throw an exception here or nothing will show up in the UI.
if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
return Collections.emptySet();
// wg puts all interface names on the same line. Split them into separate elements.
return Stream.of(output.get(0).split(" "))
.collect(Collectors.toUnmodifiableSet());
});
public Set<String> enumerate() {
final List<String> output = new LinkedList<>();
// Don't throw an exception here or nothing will show up in the UI.
if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
return Collections.emptySet();
// wg puts all interface names on the same line. Split them into separate elements.
return Stream.of(output.get(0).split(" ")).collect(Collectors.toUnmodifiableSet());
}
@Override
public CompletionStage<State> getState(final Tunnel tunnel) {
public State getState(final Tunnel tunnel) {
Log.v(TAG, "Requested state for tunnel " + tunnel.getName());
return enumerate().thenApply(set -> set.contains(tunnel.getName()) ? State.UP : State.DOWN);
return enumerate().contains(tunnel.getName()) ? State.UP : State.DOWN;
}
@Override
public CompletionStage<Statistics> getStatistics(final Tunnel tunnel) {
return CompletableFuture.completedFuture(new Statistics());
public Statistics getStatistics(final Tunnel tunnel) {
return new Statistics();
}
@Override
public CompletionStage<State> setState(final Tunnel tunnel, final State state) {
public State setState(final Tunnel tunnel, final State state) throws IOException {
Log.v(TAG, "Requested state change to " + state + " for tunnel " + tunnel.getName());
return tunnel.getStateAsync().thenCompose(currentState -> asyncWorker.supplyAsync(() -> {
final String stateName = resolveState(currentState, state).name().toLowerCase();
final File file = new File(context.getFilesDir(), tunnel.getName() + ".conf");
final String path = file.getAbsolutePath();
final State originalState = getState(tunnel);
final State resolvedState = resolveState(originalState, state);
if (resolvedState == State.UP) {
// FIXME: Assumes file layout from FileConfigStore. Use a temporary file.
if (rootShell.run(null, String.format("wg-quick %s '%s'", stateName, path)) != 0)
final File file = new File(context.getFilesDir(), tunnel.getName() + ".conf");
if (rootShell.run(null, String.format("wg-quick up '%s'", file.getAbsolutePath())) != 0)
throw new IOException("wg-quick failed");
return tunnel;
})).thenCompose(this::getState);
} else {
if (rootShell.run(null, String.format("wg-quick down '%s'", tunnel.getName())) != 0)
throw new IOException("wg-quick failed");
}
return getState(tunnel);
}
}