2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.backend;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
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.RootShell;
|
2018-01-09 06:26:39 +01:00
|
|
|
import com.wireguard.android.util.ToolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
import com.wireguard.config.Config;
|
|
|
|
|
|
|
|
import java.io.File;
|
2018-01-09 17:25:06 +01:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2018-01-07 08:45:27 +01:00
|
|
|
import java.util.ArrayList;
|
2018-01-08 09:19:13 +01:00
|
|
|
import java.util.Collections;
|
2018-01-01 09:06:37 +01:00
|
|
|
import java.util.List;
|
2018-01-06 12:07:17 +01:00
|
|
|
import java.util.Set;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-01-06 12:07:17 +01:00
|
|
|
import java9.util.stream.Collectors;
|
|
|
|
import java9.util.stream.Stream;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-07 07:24:56 +01:00
|
|
|
* WireGuard backend that uses {@code wg-quick} to implement tunnel configuration.
|
2018-01-01 09:06:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
public final class WgQuickBackend implements Backend {
|
2018-01-08 20:29:51 +01:00
|
|
|
private static final String TAG = "WireGuard/" + WgQuickBackend.class.getSimpleName();
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-01-09 17:25:06 +01:00
|
|
|
private final File localTemporaryDir;
|
2018-01-01 09:06:37 +01:00
|
|
|
private final RootShell rootShell;
|
2018-01-09 06:26:39 +01:00
|
|
|
private final ToolsInstaller toolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-01-09 06:26:39 +01:00
|
|
|
public WgQuickBackend(final Context context, final RootShell rootShell,
|
|
|
|
final ToolsInstaller toolsInstaller) {
|
2018-01-09 17:25:06 +01:00
|
|
|
localTemporaryDir = new File(context.getCacheDir(), "tmp");
|
2018-01-01 09:06:37 +01:00
|
|
|
this.rootShell = rootShell;
|
2018-01-09 06:26:39 +01:00
|
|
|
this.toolsInstaller = toolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-09 17:25:06 +01:00
|
|
|
public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception {
|
|
|
|
if (tunnel.getState() == State.UP) {
|
|
|
|
// Restart the tunnel to apply the new config.
|
|
|
|
setState(tunnel, State.DOWN);
|
|
|
|
try {
|
|
|
|
bringUpTunnel(tunnel, config);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
// The new configuration didn't work, so try to go back to the old one.
|
|
|
|
bringUpTunnel(tunnel, tunnel.getConfig());
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 07:24:56 +01:00
|
|
|
return config;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 17:25:06 +01:00
|
|
|
private int bringUpTunnel(final Tunnel tunnel, final Config config) throws Exception {
|
|
|
|
final File tempFile = new File(localTemporaryDir, tunnel.getName() + ".conf");
|
|
|
|
try (FileOutputStream stream = new FileOutputStream(tempFile, false)) {
|
|
|
|
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
|
}
|
|
|
|
final int result = rootShell.run(null, "wg-quick up '" + tempFile.getAbsolutePath() + '\'');
|
|
|
|
if (!tempFile.delete())
|
|
|
|
Log.w(TAG, "Couldn't delete temporary file after bringing up " + tunnel.getName());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@Override
|
2018-01-07 07:24:56 +01:00
|
|
|
public Set<String> enumerate() {
|
2018-01-07 08:45:27 +01:00
|
|
|
final List<String> output = new ArrayList<>();
|
2018-01-07 07:24:56 +01:00
|
|
|
// Don't throw an exception here or nothing will show up in the UI.
|
2018-01-08 19:46:51 +01:00
|
|
|
try {
|
2018-01-09 06:26:39 +01:00
|
|
|
toolsInstaller.ensureToolsAvailable();
|
2018-01-08 19:46:51 +01:00
|
|
|
if (rootShell.run(output, "wg show interfaces") != 0 || output.isEmpty())
|
|
|
|
return Collections.emptySet();
|
2018-01-09 16:18:03 +01:00
|
|
|
} catch (final Exception e) {
|
|
|
|
Log.w(TAG, "Unable to enumerate running tunnels", e);
|
2018-01-07 07:24:56 +01:00
|
|
|
return Collections.emptySet();
|
2018-01-08 19:46:51 +01:00
|
|
|
}
|
2018-01-07 07:24:56 +01:00
|
|
|
// wg puts all interface names on the same line. Split them into separate elements.
|
|
|
|
return Stream.of(output.get(0).split(" ")).collect(Collectors.toUnmodifiableSet());
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 12:07:17 +01:00
|
|
|
@Override
|
2018-01-07 07:24:56 +01:00
|
|
|
public State getState(final Tunnel tunnel) {
|
|
|
|
return enumerate().contains(tunnel.getName()) ? State.UP : State.DOWN;
|
2018-01-06 12:07:17 +01:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@Override
|
2018-01-07 07:24:56 +01:00
|
|
|
public Statistics getStatistics(final Tunnel tunnel) {
|
|
|
|
return new Statistics();
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-01-09 06:14:07 +01:00
|
|
|
public State setState(final Tunnel tunnel, State state) throws Exception {
|
2018-01-07 07:24:56 +01:00
|
|
|
final State originalState = getState(tunnel);
|
2018-01-09 06:14:07 +01:00
|
|
|
if (state == State.TOGGLE)
|
|
|
|
state = originalState == State.UP ? State.DOWN : State.UP;
|
|
|
|
if (state == originalState)
|
2018-01-08 11:15:15 +01:00
|
|
|
return originalState;
|
2018-01-10 03:16:42 +01:00
|
|
|
if (state == State.UP && !new File("/sys/module/wireguard").exists())
|
|
|
|
throw new ModuleNotLoadedException("WireGuard module not loaded");
|
2018-01-09 16:37:49 +01:00
|
|
|
Log.d(TAG, "Changing tunnel " + tunnel.getName() + " to state " + state);
|
2018-01-09 06:26:39 +01:00
|
|
|
toolsInstaller.ensureToolsAvailable();
|
2018-01-08 11:15:15 +01:00
|
|
|
final int result;
|
2018-01-10 03:16:42 +01:00
|
|
|
if (state == State.UP)
|
2018-01-09 17:25:06 +01:00
|
|
|
result = bringUpTunnel(tunnel, tunnel.getConfig());
|
2018-01-10 03:16:42 +01:00
|
|
|
else
|
2018-01-09 17:25:06 +01:00
|
|
|
result = rootShell.run(null, "wg-quick down '" + tunnel.getName() + '\'');
|
2018-01-08 19:46:51 +01:00
|
|
|
if (result != 0)
|
2018-01-10 03:16:42 +01:00
|
|
|
throw new Exception("Unable to configure tunnel");
|
2018-01-07 07:24:56 +01:00
|
|
|
return getState(tunnel);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
2018-01-10 03:16:42 +01:00
|
|
|
|
|
|
|
public static class ModuleNotLoadedException extends Exception {
|
|
|
|
public ModuleNotLoadedException(final String message, final Throwable cause) {
|
|
|
|
super(message, cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ModuleNotLoadedException(final String message) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|