2018-05-02 17:29:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Samuel Holland <samuel@sholland.org>
|
2018-05-02 21:40:01 +02:00
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-05-02 17:29:58 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.backend;
|
|
|
|
|
2018-04-23 16:37:42 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.content.Context;
|
2018-04-23 16:37:42 +02:00
|
|
|
import android.content.Intent;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
2018-04-23 16:37:42 +02:00
|
|
|
import com.wireguard.android.Application;
|
2018-01-01 09:06:37 +01:00
|
|
|
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.
|
2018-01-18 11:10:13 +01:00
|
|
|
setStateInternal(tunnel, tunnel.getConfig(), State.DOWN);
|
2018-01-09 17:25:06 +01:00
|
|
|
try {
|
2018-01-18 11:10:13 +01:00
|
|
|
setStateInternal(tunnel, config, State.UP);
|
2018-01-09 17:25:06 +01:00
|
|
|
} catch (final Exception e) {
|
|
|
|
// The new configuration didn't work, so try to go back to the old one.
|
2018-01-18 11:10:13 +01:00
|
|
|
setStateInternal(tunnel, tunnel.getConfig(), State.UP);
|
2018-01-09 17:25:06 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 07:24:56 +01:00
|
|
|
return config;
|
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-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-18 11:10:13 +01:00
|
|
|
setStateInternal(tunnel, tunnel.getConfig(), state);
|
|
|
|
return getState(tunnel);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setStateInternal(final Tunnel tunnel, final Config config, final State state)
|
|
|
|
throws Exception {
|
|
|
|
final File tempFile = new File(localTemporaryDir, tunnel.getName() + ".conf");
|
2018-04-26 19:41:35 +02:00
|
|
|
try (FileOutputStream stream = new FileOutputStream(tempFile, false)) {
|
|
|
|
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
|
2018-01-18 11:10:13 +01:00
|
|
|
}
|
2018-05-05 12:56:52 +02:00
|
|
|
final String command = String.format("wg-quick %s '%s'",
|
|
|
|
state.toString().toLowerCase(), tempFile.getAbsolutePath());
|
2018-04-26 19:41:35 +02:00
|
|
|
final int result = rootShell.run(null, command);
|
2018-04-30 18:37:52 +02:00
|
|
|
// noinspection ResultOfMethodCallIgnored
|
2018-04-26 19:41:35 +02:00
|
|
|
tempFile.delete();
|
2018-01-08 19:46:51 +01:00
|
|
|
if (result != 0)
|
2018-01-18 11:10:13 +01:00
|
|
|
throw new Exception("Unable to configure tunnel (wg-quick returned " + result + ')');
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
2018-04-23 16:37:42 +02:00
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public static final class WgQuickChangeReceiver extends BroadcastReceiver {
|
2018-04-23 16:37:42 +02:00
|
|
|
@Override
|
2018-04-30 18:37:52 +02:00
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
2018-04-23 16:37:42 +02:00
|
|
|
Log.d(TAG, "Refreshing tunnel states");
|
|
|
|
Application.getComponent().getTunnelManager().refreshTunnelStates();
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|