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-02-07 19:19:20 +01:00
|
|
|
package com.wireguard.android.backend;
|
|
|
|
|
2018-05-27 18:57:52 +02:00
|
|
|
import android.app.PendingIntent;
|
2018-02-07 19:19:20 +01:00
|
|
|
import android.content.Context;
|
2018-02-18 22:16:26 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
2018-02-07 19:19:20 +01:00
|
|
|
import android.support.v4.util.ArraySet;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2018-04-25 18:26:42 +02:00
|
|
|
import com.wireguard.android.Application;
|
2018-05-27 18:57:52 +02:00
|
|
|
import com.wireguard.android.activity.MainActivity;
|
2018-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.android.model.Tunnel;
|
|
|
|
import com.wireguard.android.model.Tunnel.State;
|
|
|
|
import com.wireguard.android.model.Tunnel.Statistics;
|
2018-05-27 18:57:52 +02:00
|
|
|
import com.wireguard.android.util.ExceptionLoggers;
|
2018-05-29 05:07:50 +02:00
|
|
|
import com.wireguard.android.util.SharedLibraryLoader;
|
2018-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.config.Config;
|
2018-04-18 05:28:31 +02:00
|
|
|
import com.wireguard.config.IPCidr;
|
2018-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.config.Interface;
|
|
|
|
import com.wireguard.config.Peer;
|
|
|
|
import com.wireguard.crypto.KeyEncoding;
|
|
|
|
|
2018-03-01 09:10:06 +01:00
|
|
|
import java.net.InetAddress;
|
2018-02-07 19:19:20 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Formatter;
|
|
|
|
import java.util.Set;
|
2018-04-15 04:03:42 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
|
|
import java9.util.concurrent.CompletableFuture;
|
2018-02-07 19:19:20 +01:00
|
|
|
|
|
|
|
public final class GoBackend implements Backend {
|
|
|
|
private static final String TAG = "WireGuard/" + GoBackend.class.getSimpleName();
|
2018-04-30 18:39:12 +02:00
|
|
|
private static CompletableFuture<VpnService> vpnService = new CompletableFuture<>();
|
2018-02-07 19:19:20 +01:00
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
private final Context context;
|
2018-02-07 19:19:20 +01:00
|
|
|
private Tunnel currentTunnel;
|
2018-02-18 22:16:26 +01:00
|
|
|
private int currentTunnelHandle = -1;
|
2018-02-07 19:19:20 +01:00
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public GoBackend(final Context context) {
|
2018-05-29 05:07:50 +02:00
|
|
|
SharedLibraryLoader.loadSharedLibrary(context, "wg-go");
|
2018-02-07 19:19:20 +01:00
|
|
|
this.context = context;
|
2018-04-15 04:03:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
private static native int wgGetSocketV4(int handle);
|
|
|
|
|
|
|
|
private static native int wgGetSocketV6(int handle);
|
|
|
|
|
|
|
|
private static native void wgTurnOff(int handle);
|
|
|
|
|
2018-04-18 16:44:05 +02:00
|
|
|
private static native int wgTurnOn(String ifName, int tunFd, String settings);
|
2018-02-07 19:19:20 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception {
|
|
|
|
if (tunnel.getState() == State.UP) {
|
|
|
|
// Restart the tunnel to apply the new config.
|
|
|
|
setStateInternal(tunnel, tunnel.getConfig(), State.DOWN);
|
|
|
|
try {
|
|
|
|
setStateInternal(tunnel, config, State.UP);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
// The new configuration didn't work, so try to go back to the old one.
|
|
|
|
setStateInternal(tunnel, tunnel.getConfig(), State.UP);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<String> enumerate() {
|
|
|
|
if (currentTunnel != null) {
|
|
|
|
final Set<String> runningTunnels = new ArraySet<>();
|
|
|
|
runningTunnels.add(currentTunnel.getName());
|
|
|
|
return runningTunnels;
|
|
|
|
}
|
|
|
|
return Collections.emptySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public State getState(final Tunnel tunnel) {
|
|
|
|
return currentTunnel == tunnel ? State.UP : State.DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Statistics getStatistics(final Tunnel tunnel) {
|
|
|
|
return new Statistics();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public State setState(final Tunnel tunnel, State state) throws Exception {
|
|
|
|
final State originalState = getState(tunnel);
|
|
|
|
if (state == State.TOGGLE)
|
|
|
|
state = originalState == State.UP ? State.DOWN : State.UP;
|
|
|
|
if (state == originalState)
|
|
|
|
return originalState;
|
|
|
|
if (state == State.UP && currentTunnel != null)
|
|
|
|
throw new IllegalStateException("Only one userspace tunnel can run at a time");
|
|
|
|
Log.d(TAG, "Changing tunnel " + tunnel.getName() + " to state " + state);
|
|
|
|
setStateInternal(tunnel, tunnel.getConfig(), state);
|
|
|
|
return getState(tunnel);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setStateInternal(final Tunnel tunnel, final Config config, final State state)
|
|
|
|
throws Exception {
|
2018-02-18 22:16:26 +01:00
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
if (state == State.UP) {
|
2018-02-18 22:16:26 +01:00
|
|
|
Log.i(TAG, "Bringing tunnel up");
|
|
|
|
|
|
|
|
if (VpnService.prepare(context) != null)
|
|
|
|
throw new Exception("VPN service not authorized by user");
|
2018-02-07 19:19:20 +01:00
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
final VpnService service;
|
2018-04-15 04:03:42 +02:00
|
|
|
if (!vpnService.isDone())
|
|
|
|
startVpnService();
|
|
|
|
|
|
|
|
try {
|
|
|
|
service = vpnService.get(2, TimeUnit.SECONDS);
|
2018-04-30 18:37:52 +02:00
|
|
|
} catch (final TimeoutException e) {
|
|
|
|
throw new Exception("Unable to start Android VPN service", e);
|
2018-04-15 04:03:42 +02:00
|
|
|
}
|
2018-02-18 22:16:26 +01:00
|
|
|
|
|
|
|
if (currentTunnelHandle != -1) {
|
|
|
|
Log.w(TAG, "Tunnel already up");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build config
|
2018-02-07 19:19:20 +01:00
|
|
|
final Interface iface = config.getInterface();
|
2018-04-30 18:37:52 +02:00
|
|
|
final String goConfig;
|
|
|
|
try (Formatter fmt = new Formatter(new StringBuilder())) {
|
|
|
|
fmt.format("replace_peers=true\n");
|
|
|
|
if (iface.getPrivateKey() != null)
|
|
|
|
fmt.format("private_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(iface.getPrivateKey())));
|
|
|
|
if (iface.getListenPort() != 0)
|
|
|
|
fmt.format("listen_port=%d\n", config.getInterface().getListenPort());
|
|
|
|
for (final Peer peer : config.getPeers()) {
|
|
|
|
if (peer.getPublicKey() != null)
|
|
|
|
fmt.format("public_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPublicKey())));
|
|
|
|
if (peer.getPreSharedKey() != null)
|
|
|
|
fmt.format("preshared_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPreSharedKey())));
|
|
|
|
if (peer.getEndpoint() != null)
|
|
|
|
fmt.format("endpoint=%s\n", peer.getResolvedEndpointString());
|
|
|
|
if (peer.getPersistentKeepalive() != 0)
|
|
|
|
fmt.format("persistent_keepalive_interval=%d\n", peer.getPersistentKeepalive());
|
|
|
|
for (final IPCidr addr : peer.getAllowedIPs()) {
|
|
|
|
fmt.format("allowed_ip=%s\n", addr.toString());
|
|
|
|
}
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
2018-04-30 18:37:52 +02:00
|
|
|
goConfig = fmt.toString();
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
2018-02-18 22:16:26 +01:00
|
|
|
|
|
|
|
// Create the vpn tunnel with android API
|
2018-04-30 18:37:52 +02:00
|
|
|
final VpnService.Builder builder = service.getBuilder();
|
2018-02-18 22:16:26 +01:00
|
|
|
builder.setSession(tunnel.getName());
|
2018-03-05 11:18:43 +01:00
|
|
|
|
2018-05-27 18:57:52 +02:00
|
|
|
final Intent configureIntent = new Intent(context, MainActivity.class);
|
|
|
|
configureIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
builder.setConfigureIntent(PendingIntent.getActivity(context, 0, configureIntent, 0));
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
for (final IPCidr addr : config.getInterface().getAddresses())
|
|
|
|
builder.addAddress(addr.getAddress(), addr.getCidr());
|
2018-03-05 11:18:43 +01:00
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
for (final InetAddress addr : config.getInterface().getDnses())
|
|
|
|
builder.addDnsServer(addr.getHostAddress());
|
2018-02-18 22:16:26 +01:00
|
|
|
|
|
|
|
for (final Peer peer : config.getPeers()) {
|
2018-04-18 05:28:31 +02:00
|
|
|
for (final IPCidr addr : peer.getAllowedIPs())
|
|
|
|
builder.addRoute(addr.getAddress(), addr.getCidr());
|
2018-02-18 22:16:26 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
int mtu = config.getInterface().getMtu();
|
|
|
|
if (mtu == 0)
|
2018-04-17 05:59:23 +02:00
|
|
|
mtu = 1280;
|
|
|
|
builder.setMtu(mtu);
|
|
|
|
|
2018-02-18 22:16:26 +01:00
|
|
|
builder.setBlocking(true);
|
2018-04-30 18:37:52 +02:00
|
|
|
final ParcelFileDescriptor tun = builder.establish();
|
2018-02-18 22:16:26 +01:00
|
|
|
if (tun == null)
|
|
|
|
throw new Exception("Unable to create tun device");
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig);
|
2018-02-18 22:16:26 +01:00
|
|
|
if (currentTunnelHandle < 0)
|
2018-04-30 18:37:52 +02:00
|
|
|
throw new Exception("Unable to turn tunnel on (wgTurnOn return " + currentTunnelHandle + ')');
|
2018-02-18 22:16:26 +01:00
|
|
|
|
|
|
|
currentTunnel = tunnel;
|
|
|
|
|
2018-04-15 04:03:42 +02:00
|
|
|
service.protect(wgGetSocketV4(currentTunnelHandle));
|
|
|
|
service.protect(wgGetSocketV6(currentTunnelHandle));
|
2018-02-07 19:19:20 +01:00
|
|
|
} else {
|
2018-02-18 22:16:26 +01:00
|
|
|
Log.i(TAG, "Bringing tunnel down");
|
|
|
|
|
|
|
|
if (currentTunnelHandle == -1) {
|
|
|
|
Log.w(TAG, "Tunnel already down");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wgTurnOff(currentTunnelHandle);
|
2018-02-07 19:19:20 +01:00
|
|
|
currentTunnel = null;
|
2018-02-18 22:16:26 +01:00
|
|
|
currentTunnelHandle = -1;
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 18:39:12 +02:00
|
|
|
|
|
|
|
private void startVpnService() {
|
2018-05-27 18:57:52 +02:00
|
|
|
Log.d(TAG, "Requesting to start VpnService");
|
2018-04-30 18:39:12 +02:00
|
|
|
context.startService(new Intent(context, VpnService.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class VpnService extends android.net.VpnService {
|
|
|
|
public Builder getBuilder() {
|
|
|
|
return new Builder();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
vpnService.complete(this);
|
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
for (final Tunnel tunnel : Application.getComponent().getTunnelManager().getTunnels()) {
|
|
|
|
if (tunnel != null && tunnel.getState() != State.DOWN)
|
|
|
|
tunnel.setState(State.DOWN);
|
|
|
|
}
|
|
|
|
vpnService = vpnService.newIncompleteFuture();
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
2018-05-27 18:57:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
vpnService.complete(this);
|
|
|
|
if (intent == null || intent.getComponent() == null || !intent.getComponent().getPackageName().equals(getPackageName())) {
|
|
|
|
Log.d(TAG, "Service started by Always-on VPN feature");
|
|
|
|
Application.getComponent().getTunnelManager().restoreState(true).whenComplete(ExceptionLoggers.D);
|
|
|
|
}
|
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
|
}
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|