Backend: Add a function to enumerate running tunnels
Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
parent
2543f28274
commit
1c2239ae91
@ -5,6 +5,8 @@ import com.wireguard.android.model.Tunnel.State;
|
|||||||
import com.wireguard.android.model.Tunnel.Statistics;
|
import com.wireguard.android.model.Tunnel.Statistics;
|
||||||
import com.wireguard.config.Config;
|
import com.wireguard.config.Config;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import java9.util.concurrent.CompletionStage;
|
import java9.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,6 +27,14 @@ public interface Backend {
|
|||||||
*/
|
*/
|
||||||
CompletionStage<Config> applyConfig(Tunnel tunnel, Config config);
|
CompletionStage<Config> applyConfig(Tunnel tunnel, Config config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enumerate the names of currently-running tunnels.
|
||||||
|
*
|
||||||
|
* @return A future completed when the set of running tunnel names is available. This future
|
||||||
|
* will always be completed on the main thread.
|
||||||
|
*/
|
||||||
|
CompletionStage<Set<String>> enumerate();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the actual state of a tunnel, asynchronously.
|
* Get the actual state of a tunnel, asynchronously.
|
||||||
*
|
*
|
||||||
|
@ -12,12 +12,15 @@ import com.wireguard.config.Config;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import java9.util.concurrent.CompletableFuture;
|
import java9.util.concurrent.CompletableFuture;
|
||||||
import java9.util.concurrent.CompletionStage;
|
import java9.util.concurrent.CompletionStage;
|
||||||
|
import java9.util.stream.Collectors;
|
||||||
|
import java9.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by samuel on 12/19/17.
|
* Created by samuel on 12/19/17.
|
||||||
@ -53,26 +56,24 @@ public final class WgQuickBackend implements Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletionStage<State> getState(final Tunnel tunnel) {
|
public CompletionStage<Set<String>> enumerate() {
|
||||||
Log.v(TAG, "Requested state for tunnel " + tunnel.getName());
|
|
||||||
return asyncWorker.supplyAsync(() -> {
|
return asyncWorker.supplyAsync(() -> {
|
||||||
final List<String> output = new LinkedList<>();
|
final List<String> output = new LinkedList<>();
|
||||||
final State state;
|
// Don't throw an exception here or nothing will show up in the UI.
|
||||||
if (rootShell.run(output, "wg show interfaces") != 0) {
|
if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
|
||||||
state = State.UNKNOWN;
|
return Collections.emptySet();
|
||||||
} else if (output.isEmpty()) {
|
// wg puts all interface names on the same line. Split them into separate elements.
|
||||||
// There are no running interfaces.
|
return Stream.of(output.get(0).split(" "))
|
||||||
state = State.DOWN;
|
.collect(Collectors.toUnmodifiableSet());
|
||||||
} else {
|
|
||||||
// wg puts all interface names on the same line. Split them into separate elements.
|
|
||||||
final String[] names = output.get(0).split(" ");
|
|
||||||
state = Arrays.asList(names).contains(tunnel.getName()) ? State.UP : State.DOWN;
|
|
||||||
}
|
|
||||||
Log.v(TAG, "Got state " + state + " for tunnel " + tunnel.getName());
|
|
||||||
return state;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletionStage<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);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletionStage<Statistics> getStatistics(final Tunnel tunnel) {
|
public CompletionStage<Statistics> getStatistics(final Tunnel tunnel) {
|
||||||
return CompletableFuture.completedFuture(new Statistics());
|
return CompletableFuture.completedFuture(new Statistics());
|
||||||
|
Loading…
Reference in New Issue
Block a user