aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/config/Interface.java
blob: 2ed2b4e6fc0d1ae22f6e1d7341c109bd4451b042 (plain) (blame)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package com.wireguard.config;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.os.Parcel;
import android.os.Parcelable;

import com.wireguard.android.BR;
import com.wireguard.crypto.KeyEncoding;
import com.wireguard.crypto.Keypair;

import java.net.InetAddress;
import java.util.LinkedList;
import java.util.List;

/**
 * Represents the configuration for a WireGuard interface (an [Interface] block).
 */

public class Interface implements Parcelable {
    public static final Creator<Interface> CREATOR = new Creator<Interface>() {
        @Override
        public Interface createFromParcel(final Parcel in) {
            return new Interface(in);
        }

        @Override
        public Interface[] newArray(final int size) {
            return new Interface[size];
        }
    };

    public static class Observable extends BaseObservable {
        private String addresses;
        private String dnses;
        private String publicKey;
        private String privateKey;
        private String listenPort;
        private String mtu;

        public Observable(Interface parent) {
            loadData(parent);
        }

        public void loadData(Interface parent) {
            this.addresses = parent.getAddressString();
            this.dnses = parent.getDnsString();
            this.publicKey = parent.getPublicKey();
            this.privateKey = parent.getPrivateKey();
            this.listenPort = parent.getListenPortString();
            this.mtu = parent.getMtuString();
        }

        public void commitData(Interface parent) {
            parent.setAddressString(this.addresses);
            parent.setDnsString(this.dnses);
            parent.setPrivateKey(this.privateKey);
            parent.setListenPortString(this.listenPort);
            parent.setMtuString(this.mtu);
            loadData(parent);
            notifyChange();
        }

        @Bindable
        public String getAddresses() {
            return addresses;
        }

        public void setAddresses(String addresses) {
            this.addresses = addresses;
            notifyPropertyChanged(BR.addresses);
        }

        @Bindable
        public String getDnses() {
            return dnses;
        }

        public void setDnses(String dnses) {
            this.dnses = dnses;
            notifyPropertyChanged(BR.dnses);
        }

        @Bindable
        public String getPublicKey() {
            return publicKey;
        }

        @Bindable
        public String getPrivateKey() {
            return privateKey;
        }

        public void setPrivateKey(String privateKey) {
            this.privateKey = privateKey;

            try {
                this.publicKey = new Keypair(privateKey).getPublicKey();
            } catch (IllegalArgumentException ignored) {
                this.publicKey = "";
            }

            notifyPropertyChanged(BR.privateKey);
            notifyPropertyChanged(BR.publicKey);
        }

        public void generateKeypair() {
            Keypair keypair = new Keypair();
            privateKey = keypair.getPrivateKey();
            publicKey = keypair.getPublicKey();
            notifyPropertyChanged(BR.privateKey);
            notifyPropertyChanged(BR.publicKey);
        }

        @Bindable
        public String getListenPort() {
            return listenPort;
        }

        public void setListenPort(String listenPort) {
            this.listenPort = listenPort;
            notifyPropertyChanged(BR.listenPort);
        }

        @Bindable
        public String getMtu() {
            return mtu;
        }

        public void setMtu(String mtu) {
            this.mtu = mtu;
            notifyPropertyChanged(BR.mtu);
        }
    }

    private List<IPCidr> addressList;
    private List<InetAddress> dnsList;
    private Keypair keypair;
    private int listenPort;
    private int mtu;

    public Interface() {
        addressList = new LinkedList<>();
        dnsList = new LinkedList<>();
    }

    private Interface(final Parcel in) {
        addressList = in.createTypedArrayList(IPCidr.CREATOR);
        int dnsItems = in.readInt();
        dnsList = new LinkedList<>();
        for (int i = 0; i < dnsItems; ++i) {
            try {
                dnsList.add(InetAddress.getByAddress(in.createByteArray()));
            } catch (Exception e) {
            }
        }
        listenPort = in.readInt();
        mtu = in.readInt();
        setPrivateKey(in.readString());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    private String getAddressString() {
        if (addressList.isEmpty())
            return null;
        return Attribute.listToString(addressList);
    }

    public IPCidr[] getAddresses() {
        return addressList.toArray(new IPCidr[addressList.size()]);
    }

    private List<String> getDnsStrings() {
        List<String> strings = new LinkedList<>();
        for (final InetAddress addr : dnsList)
            strings.add(addr.getHostAddress());
        return strings;
    }

    private String getDnsString() {
        if (dnsList.isEmpty())
            return null;
        return Attribute.listToString(getDnsStrings());
    }

    public InetAddress[] getDnses() {
        return dnsList.toArray(new InetAddress[dnsList.size()]);
    }

    public int getListenPort() {
        return listenPort;
    }

    private String getListenPortString() {
        if (listenPort == 0)
            return null;
        return new Integer(listenPort).toString();
    }

    public int getMtu() {
        return mtu;
    }

    private String getMtuString() {
        if (mtu == 0)
            return null;
        return new Integer(mtu).toString();
    }

    public String getPrivateKey() {
        if (keypair == null)
            return null;
        return keypair.getPrivateKey();
    }

    public String getPublicKey() {
        if (keypair == null)
            return null;
        return keypair.getPublicKey();
    }

    public void parse(final String line) {
        final Attribute key = Attribute.match(line);
        if (key == Attribute.ADDRESS)
            addAddresses(key.parseList(line));
        else if (key == Attribute.DNS)
            addDnses(key.parseList(line));
        else if (key == Attribute.LISTEN_PORT)
            setListenPortString(key.parse(line));
        else if (key == Attribute.MTU)
            setMtuString(key.parse(line));
        else if (key == Attribute.PRIVATE_KEY)
            setPrivateKey(key.parse(line));
        else
            throw new IllegalArgumentException(line);
    }

    private void addAddresses(String[] addresses) {
        if (addresses != null && addresses.length > 0) {
            for (final String addr : addresses) {
                if (addr.isEmpty())
                    throw new IllegalArgumentException("Address is empty");
                this.addressList.add(new IPCidr(addr));
            }
        }
    }

    private void setAddressString(final String addressString) {
        this.addressList.clear();
        addAddresses(Attribute.stringToList(addressString));
    }

    private void addDnses(String[] dnses) {
        if (dnses != null && dnses.length > 0) {
            for (final String dns : dnses) {
                this.dnsList.add(Attribute.parseIPString(dns));
            }
        }
    }

    private void setDnsString(final String dnsString) {
        this.dnsList.clear();
        addDnses(Attribute.stringToList(dnsString));
    }

    private void setListenPort(int listenPort) {
        this.listenPort = listenPort;
    }

    private void setListenPortString(final String port) {
        if (port != null && !port.isEmpty())
            setListenPort(Integer.parseInt(port, 10));
        else
            setListenPort(0);
    }

    private void setMtu(int mtu) {
        this.mtu = mtu;
    }

    private void setMtuString(final String mtu) {
        if (mtu != null && !mtu.isEmpty())
            setMtu(Integer.parseInt(mtu, 10));
        else
            setMtu(0);
    }

    private void setPrivateKey(String privateKey) {
        if (privateKey != null && privateKey.isEmpty())
            privateKey = null;
        if (privateKey == null)
            keypair = null;
        else
            keypair = new Keypair(privateKey);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder().append("[Interface]\n");
        if (!addressList.isEmpty())
            sb.append(Attribute.ADDRESS.composeWith(addressList));
        if (!dnsList.isEmpty())
            sb.append(Attribute.DNS.composeWith(getDnsStrings()));
        if (listenPort != 0)
            sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
        if (mtu != 0)
            sb.append(Attribute.MTU.composeWith(mtu));
        if (keypair != null)
            sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey()));
        return sb.toString();
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeTypedList(addressList);
        dest.writeInt(dnsList.size());
        for (final InetAddress addr : dnsList)
            dest.writeByteArray(addr.getAddress());
        dest.writeInt(listenPort);
        dest.writeInt(mtu);
        dest.writeString(keypair == null ? "" : keypair.getPrivateKey());
    }
}