summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/backend/GoBackend.java
blob: 28ce7a5ee2cc9975b83fdcdbd7229500cad17cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.wireguard.android.backend;

import android.content.Context;
import android.content.Intent;
import android.os.ParcelFileDescriptor;
import android.support.v4.util.ArraySet;
import android.util.Log;
import android.util.Pair;

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;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Formatter;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import java9.util.concurrent.CompletableFuture;

public final class GoBackend implements Backend {
    private static final String TAG = "WireGuard/" + GoBackend.class.getSimpleName();

    static {
        System.loadLibrary("wg-go");
    }

    private Tunnel currentTunnel;
    private int currentTunnelHandle = -1;

    private Context context;

    public GoBackend(Context context) {
        this.context = context;
    }

    public void startVpnService() {
        context.startService(new Intent(context, VpnService.class));
    }

    public static class VpnService extends android.net.VpnService {
        @Override
        public void onCreate() {
            vpnService.complete(this);
            super.onCreate();
        }

        @Override
        public void onDestroy() {
            vpnService = vpnService.newIncompleteFuture();
            super.onDestroy();
        }

        public Builder getBuilder() {
            return new Builder();
        }
    }

    private static CompletableFuture<VpnService> vpnService = new CompletableFuture<>();

    private static native int wgGetSocketV4(int handle);

    private static native int wgGetSocketV6(int handle);

    private static native void wgTurnOff(int handle);

    private static native int wgTurnOn(String ifName, int tunFd, int mtu, String settings);

    @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 String parseEndpoint(String string) throws Exception {
        String[] part;
        if (string.charAt(0) == '[') {
            int end = string.indexOf(']');
            if (end == -1 || end >= string.length() - 2 || string.charAt(end + 1) != ':')
                throw new Exception("Invalid endpoint " + string);

            part = new String[2];
            part[0] = string.substring(1, end);
            part[1] = string.substring(end + 2);
        } else {
            part = string.split(":", 2);
        }

        if (part.length != 2 || part[0].isEmpty() || part[1].isEmpty())
            throw new Exception("Invalid endpoint " + string);

        InetAddress address = InetAddress.getByName(part[0]);
        int port = -1;
        try {
            port = Integer.parseInt(part[1], 10);
        } catch (Exception e) {
        }
        if (port < 1)
            throw new Exception("Invalid endpoint port " + part[1]);
        InetSocketAddress socketAddress = new InetSocketAddress(address, port);
        if (socketAddress.getAddress() instanceof Inet4Address)
            return socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort();
        else
            return "[" + socketAddress.getAddress().getHostAddress() + "]:" + socketAddress.getPort();
    }

    private Pair<String, Integer> parseAddressWithCidr(String in) {
        int cidr = -1;
        String addr = in;
        int slash = in.lastIndexOf('/');
        if (slash != -1 && slash < in.length() - 1) {
            try {
                cidr = Integer.parseInt(in.substring(slash + 1), 10);
                addr = in.substring(0, slash);
            } catch (Exception e) {
            }
        }
        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);
    }

    private void setStateInternal(final Tunnel tunnel, final Config config, final State state)
            throws Exception {

        if (state == State.UP) {
            Log.i(TAG, "Bringing tunnel up");

            if (VpnService.prepare(context) != null)
                throw new Exception("VPN service not authorized by user");

            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");
            }

            if (currentTunnelHandle != -1) {
                Log.w(TAG, "Tunnel already up");
                return;
            }

            // Build config
            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())));
                if (peer.getEndpoint() != null)
                    fmt.format("endpoint=%s\n", parseEndpoint(peer.getEndpoint()));
                if (peer.getPersistentKeepalive() != null)
                    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);
                }
            }

            // Create the vpn tunnel with android API
            VpnService.Builder builder = service.getBuilder();
            builder.setSession(tunnel.getName());

            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);
            }

            for (final String addr : config.getInterface().getDnses()) {
                final InetAddress address = InetAddress.getByName(addr);
                builder.addDnsServer(address.getHostAddress());
            }

            for (final Peer peer : config.getPeers()) {
                for (final String addr : peer.getAllowedIPs()) {
                    final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
                    builder.addRoute(addressCidr.first, addressCidr.second);
                }
            }

            int mtu = -1;
            try {
                mtu = Integer.parseInt(config.getInterface().getMtu(), 10);
            } catch (Exception e) {
            }
            if (mtu < 0)
                mtu = 1280;
            builder.setMtu(mtu);

            builder.setBlocking(true);
            ParcelFileDescriptor tun = builder.establish();
            if (tun == null)
                throw new Exception("Unable to create tun device");

            currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), mtu, fmt.toString());
            if (currentTunnelHandle < 0)
                throw new Exception("Unable to turn tunnel on (wgTurnOn return " + currentTunnelHandle + ")");

            currentTunnel = tunnel;

            service.protect(wgGetSocketV4(currentTunnelHandle));
            service.protect(wgGetSocketV6(currentTunnelHandle));
        } else {
            Log.i(TAG, "Bringing tunnel down");

            if (currentTunnelHandle == -1) {
                Log.w(TAG, "Tunnel already down");
                return;
            }

            wgTurnOff(currentTunnelHandle);
            currentTunnel = null;
            currentTunnelHandle = -1;
        }
    }
}