2018-02-07 19:19:20 +01:00
|
|
|
package com.wireguard.android.backend;
|
|
|
|
|
|
|
|
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-03-02 13:08:25 +01:00
|
|
|
import android.util.Pair;
|
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;
|
|
|
|
import com.wireguard.config.Config;
|
|
|
|
import com.wireguard.config.Interface;
|
|
|
|
import com.wireguard.config.Peer;
|
|
|
|
import com.wireguard.crypto.KeyEncoding;
|
|
|
|
|
2018-03-02 10:34:40 +01:00
|
|
|
import java.net.Inet4Address;
|
2018-03-01 09:10:06 +01:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
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();
|
|
|
|
|
|
|
|
static {
|
|
|
|
System.loadLibrary("wg-go");
|
|
|
|
}
|
|
|
|
|
|
|
|
private Tunnel currentTunnel;
|
2018-02-18 22:16:26 +01:00
|
|
|
private int currentTunnelHandle = -1;
|
2018-02-07 19:19:20 +01:00
|
|
|
|
2018-02-18 22:16:26 +01:00
|
|
|
private Context context;
|
|
|
|
|
|
|
|
public GoBackend(Context context) {
|
2018-02-07 19:19:20 +01:00
|
|
|
this.context = context;
|
2018-04-15 04:03:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void startVpnService() {
|
2018-02-18 22:16:26 +01:00
|
|
|
context.startService(new Intent(context, VpnService.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class VpnService extends android.net.VpnService {
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2018-04-15 04:03:42 +02:00
|
|
|
vpnService.complete(this);
|
2018-02-18 22:16:26 +01:00
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2018-04-15 04:03:42 +02:00
|
|
|
vpnService = vpnService.newIncompleteFuture();
|
2018-02-18 22:16:26 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder getBuilder() {
|
|
|
|
return new Builder();
|
|
|
|
}
|
2018-02-07 19:19:20 +01:00
|
|
|
}
|
|
|
|
|
2018-04-15 04:03:42 +02:00
|
|
|
private static CompletableFuture<VpnService> vpnService = new CompletableFuture<>();
|
2018-02-18 22:16:26 +01: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-17 05:59:23 +02:00
|
|
|
private static native int wgTurnOn(String ifName, int tunFd, int mtu, 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);
|
|
|
|
}
|
|
|
|
|
2018-03-02 10:34:40 +01:00
|
|
|
private String parseEndpoint(String string) throws Exception {
|
|
|
|
String[] part;
|
2018-04-17 05:20:17 +02:00
|
|
|
if (string.charAt(0) == '[') {
|
2018-03-02 10:34:40 +01:00
|
|
|
int end = string.indexOf(']');
|
2018-04-17 05:20:17 +02:00
|
|
|
if (end == -1 || end >= string.length() - 2 || string.charAt(end + 1) != ':')
|
2018-03-02 10:34:40 +01:00
|
|
|
throw new Exception("Invalid endpoint " + string);
|
|
|
|
|
|
|
|
part = new String[2];
|
|
|
|
part[0] = string.substring(1, end);
|
|
|
|
part[1] = string.substring(end + 2);
|
2018-04-17 05:20:17 +02:00
|
|
|
} else {
|
2018-03-02 10:34:40 +01:00
|
|
|
part = string.split(":", 2);
|
|
|
|
}
|
|
|
|
|
2018-03-01 09:10:06 +01:00
|
|
|
if (part.length != 2 || part[0].isEmpty() || part[1].isEmpty())
|
2018-03-02 10:34:40 +01:00
|
|
|
throw new Exception("Invalid endpoint " + string);
|
|
|
|
|
2018-03-01 09:10:06 +01:00
|
|
|
InetAddress address = InetAddress.getByName(part[0]);
|
2018-04-17 05:20:17 +02:00
|
|
|
int port = -1;
|
|
|
|
try {
|
|
|
|
port = Integer.parseInt(part[1], 10);
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
if (port < 1)
|
|
|
|
throw new Exception("Invalid endpoint port " + part[1]);
|
2018-03-01 09:10:06 +01:00
|
|
|
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
|
2018-03-02 10:34:40 +01:00
|
|
|
if (socketAddress.getAddress() instanceof Inet4Address)
|
|
|
|
return socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort();
|
|
|
|
else
|
|
|
|
return "[" + socketAddress.getAddress().getHostAddress() + "]:" + socketAddress.getPort();
|
2018-03-01 09:10:06 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
private Pair<String, Integer> parseAddressWithCidr(String in) {
|
|
|
|
int cidr = -1;
|
|
|
|
String addr = in;
|
|
|
|
int slash = in.lastIndexOf('/');
|
|
|
|
if (slash != -1 && slash < in.length() - 1) {
|
2018-03-02 13:08:25 +01:00
|
|
|
try {
|
2018-04-17 05:20:17 +02:00
|
|
|
cidr = Integer.parseInt(in.substring(slash + 1), 10);
|
|
|
|
addr = in.substring(0, slash);
|
2018-03-02 13:08:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
boolean isV6 = addr.indexOf(':') != -1;
|
|
|
|
if (isV6 && (cidr > 128 || cidr < 0))
|
|
|
|
cidr = 128;
|
|
|
|
else if (!isV6 && (cidr > 32 || cidr < 0))
|
|
|
|
cidr = 32;
|
|
|
|
return new Pair<>(addr, cidr);
|
2018-03-02 13:08:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 19:19:20 +01:00
|
|
|
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-15 04:03:42 +02:00
|
|
|
VpnService service;
|
|
|
|
if (!vpnService.isDone())
|
|
|
|
startVpnService();
|
|
|
|
|
|
|
|
try {
|
|
|
|
service = vpnService.get(2, TimeUnit.SECONDS);
|
|
|
|
} catch (TimeoutException e) {
|
|
|
|
throw new Exception("Unable to start Android VPN service");
|
|
|
|
}
|
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
|
|
|
Formatter fmt = new Formatter(new StringBuilder());
|
|
|
|
final Interface iface = config.getInterface();
|
|
|
|
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() != null)
|
|
|
|
fmt.format("listen_port=%d\n", Integer.parseInt(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())));
|
2018-04-17 05:20:17 +02:00
|
|
|
if (peer.getEndpoint() != null)
|
2018-03-02 10:34:40 +01:00
|
|
|
fmt.format("endpoint=%s\n", parseEndpoint(peer.getEndpoint()));
|
2018-02-07 19:19:20 +01:00
|
|
|
if (peer.getPersistentKeepalive() != null)
|
2018-04-17 05:20:17 +02:00
|
|
|
fmt.format("persistent_keepalive_interval=%d\n", Integer.parseInt(peer.getPersistentKeepalive(), 10));
|
|
|
|
for (final String addr : peer.getAllowedIPs()) {
|
|
|
|
final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
|
|
|
|
fmt.format("allowed_ip=%s\n", addressCidr.first + "/" + addressCidr.second);
|
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-15 04:03:42 +02:00
|
|
|
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-04-17 05:20:17 +02:00
|
|
|
for (final String addr : config.getInterface().getAddresses()) {
|
|
|
|
final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
|
|
|
|
final InetAddress address = InetAddress.getByName(addressCidr.first);
|
|
|
|
builder.addAddress(address.getHostAddress(), addressCidr.second);
|
2018-03-05 11:18:43 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
for (final String addr : config.getInterface().getDnses()) {
|
|
|
|
final InetAddress address = InetAddress.getByName(addr);
|
|
|
|
builder.addDnsServer(address.getHostAddress());
|
2018-03-05 11:18:43 +01:00
|
|
|
}
|
2018-02-18 22:16:26 +01:00
|
|
|
|
|
|
|
for (final Peer peer : config.getPeers()) {
|
2018-04-17 05:20:17 +02:00
|
|
|
for (final String addr : peer.getAllowedIPs()) {
|
|
|
|
final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
|
|
|
|
builder.addRoute(addressCidr.first, addressCidr.second);
|
2018-02-18 22:16:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 05:59:23 +02:00
|
|
|
int mtu = -1;
|
|
|
|
try {
|
|
|
|
mtu = Integer.parseInt(config.getInterface().getMtu(), 10);
|
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
if (mtu < 0)
|
|
|
|
mtu = 1280;
|
|
|
|
builder.setMtu(mtu);
|
|
|
|
|
2018-02-18 22:16:26 +01:00
|
|
|
builder.setBlocking(true);
|
|
|
|
ParcelFileDescriptor tun = builder.establish();
|
|
|
|
if (tun == null)
|
|
|
|
throw new Exception("Unable to create tun device");
|
|
|
|
|
2018-04-17 05:59:23 +02:00
|
|
|
currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), mtu, fmt.toString());
|
2018-02-18 22:16:26 +01:00
|
|
|
if (currentTunnelHandle < 0)
|
|
|
|
throw new Exception("Unable to turn tunnel on (wgTurnOn return " + currentTunnelHandle + ")");
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|