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-07-06 04:09:48 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 17:29:58 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
|
|
|
import android.databinding.BaseObservable;
|
|
|
|
import android.databinding.Bindable;
|
2017-08-24 06:47:44 +02:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2018-07-13 02:10:35 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
import com.wireguard.android.BR;
|
2017-08-24 06:47:44 +02:00
|
|
|
import com.wireguard.crypto.Keypair;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
import java.net.InetAddress;
|
2018-04-30 18:37:52 +02:00
|
|
|
import java.util.ArrayList;
|
2018-07-04 23:47:55 +02:00
|
|
|
import java.util.Arrays;
|
2018-04-18 05:28:31 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
/**
|
|
|
|
* Represents the configuration for a WireGuard interface (an [Interface] block).
|
|
|
|
*/
|
|
|
|
|
2018-04-30 05:00:51 +02:00
|
|
|
public class Interface {
|
2018-06-19 07:43:49 +02:00
|
|
|
private final List<InetNetwork> addressList;
|
2018-04-30 18:37:52 +02:00
|
|
|
private final List<InetAddress> dnsList;
|
2018-07-04 23:47:55 +02:00
|
|
|
private final List<String> excludedApplications;
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable private Keypair keypair;
|
2018-04-18 05:28:31 +02:00
|
|
|
private int listenPort;
|
|
|
|
private int mtu;
|
2018-04-30 18:37:52 +02:00
|
|
|
|
2017-08-24 06:47:44 +02:00
|
|
|
public Interface() {
|
2018-04-30 18:37:52 +02:00
|
|
|
addressList = new ArrayList<>();
|
|
|
|
dnsList = new ArrayList<>();
|
2018-07-04 23:47:55 +02:00
|
|
|
excludedApplications = new ArrayList<>();
|
2017-08-24 06:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void addAddresses(@Nullable final String[] addresses) {
|
2018-04-30 18:39:12 +02:00
|
|
|
if (addresses != null && addresses.length > 0) {
|
|
|
|
for (final String addr : addresses) {
|
|
|
|
if (addr.isEmpty())
|
|
|
|
throw new IllegalArgumentException("Address is empty");
|
2018-06-19 07:43:49 +02:00
|
|
|
addressList.add(new InetNetwork(addr));
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void addDnses(@Nullable final String[] dnses) {
|
2018-04-30 18:39:12 +02:00
|
|
|
if (dnses != null && dnses.length > 0) {
|
|
|
|
for (final String dns : dnses) {
|
2018-06-19 07:43:49 +02:00
|
|
|
dnsList.add(InetAddresses.parse(dns));
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void addExcludedApplications(@Nullable final String[] applications) {
|
2018-07-04 23:47:55 +02:00
|
|
|
if (applications != null && applications.length > 0) {
|
|
|
|
excludedApplications.addAll(Arrays.asList(applications));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-28 04:45:17 +02:00
|
|
|
private String getAddressString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (addressList.isEmpty())
|
|
|
|
return null;
|
2018-04-30 18:37:52 +02:00
|
|
|
return Attribute.iterableToString(addressList);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 07:43:49 +02:00
|
|
|
public InetNetwork[] getAddresses() {
|
|
|
|
return addressList.toArray(new InetNetwork[addressList.size()]);
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
private String getDnsString() {
|
|
|
|
if (dnsList.isEmpty())
|
|
|
|
return null;
|
2018-04-30 18:37:52 +02:00
|
|
|
return Attribute.iterableToString(getDnsStrings());
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private List<String> getDnsStrings() {
|
2018-04-30 18:37:52 +02:00
|
|
|
final List<String> strings = new ArrayList<>();
|
2018-04-18 05:28:31 +02:00
|
|
|
for (final InetAddress addr : dnsList)
|
|
|
|
strings.add(addr.getHostAddress());
|
|
|
|
return strings;
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public InetAddress[] getDnses() {
|
|
|
|
return dnsList.toArray(new InetAddress[dnsList.size()]);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-07-04 23:47:55 +02:00
|
|
|
private String getExcludedApplicationsString() {
|
|
|
|
if (excludedApplications.isEmpty())
|
|
|
|
return null;
|
|
|
|
return Attribute.iterableToString(excludedApplications);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String[] getExcludedApplications() {
|
|
|
|
return excludedApplications.toArray(new String[excludedApplications.size()]);
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public int getListenPort() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return listenPort;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-28 04:45:17 +02:00
|
|
|
private String getListenPortString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (listenPort == 0)
|
|
|
|
return null;
|
2018-04-27 18:33:39 +02:00
|
|
|
return Integer.valueOf(listenPort).toString();
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getMtu() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return mtu;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-28 04:45:17 +02:00
|
|
|
private String getMtuString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (mtu == 0)
|
|
|
|
return null;
|
2018-04-27 18:33:39 +02:00
|
|
|
return Integer.toString(mtu);
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2017-07-30 00:30:33 +02:00
|
|
|
public String getPrivateKey() {
|
2018-04-28 04:45:17 +02:00
|
|
|
if (keypair == null)
|
|
|
|
return null;
|
|
|
|
return keypair.getPrivateKey();
|
2017-08-01 08:12:59 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2017-08-01 08:12:59 +02:00
|
|
|
public String getPublicKey() {
|
2018-04-28 04:45:17 +02:00
|
|
|
if (keypair == null)
|
|
|
|
return null;
|
|
|
|
return keypair.getPublicKey();
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:31:10 +02:00
|
|
|
public void parse(final String line) {
|
2017-07-30 00:30:33 +02:00
|
|
|
final Attribute key = Attribute.match(line);
|
2018-04-30 18:37:52 +02:00
|
|
|
switch (key) {
|
|
|
|
case ADDRESS:
|
|
|
|
addAddresses(key.parseList(line));
|
|
|
|
break;
|
|
|
|
case DNS:
|
|
|
|
addDnses(key.parseList(line));
|
|
|
|
break;
|
2018-07-04 23:47:55 +02:00
|
|
|
case EXCLUDED_APPLICATIONS:
|
|
|
|
addExcludedApplications(key.parseList(line));
|
|
|
|
break;
|
2018-04-30 18:37:52 +02:00
|
|
|
case LISTEN_PORT:
|
|
|
|
setListenPortString(key.parse(line));
|
|
|
|
break;
|
|
|
|
case MTU:
|
|
|
|
setMtuString(key.parse(line));
|
|
|
|
break;
|
|
|
|
case PRIVATE_KEY:
|
|
|
|
setPrivateKey(key.parse(line));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException(line);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void setAddressString(@Nullable final String addressString) {
|
2018-04-30 18:37:52 +02:00
|
|
|
addressList.clear();
|
2018-04-24 03:31:10 +02:00
|
|
|
addAddresses(Attribute.stringToList(addressString));
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void setDnsString(@Nullable final String dnsString) {
|
2018-04-30 18:37:52 +02:00
|
|
|
dnsList.clear();
|
2018-04-24 03:31:10 +02:00
|
|
|
addDnses(Attribute.stringToList(dnsString));
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void setExcludedApplicationsString(@Nullable final String applicationsString) {
|
2018-07-04 23:47:55 +02:00
|
|
|
excludedApplications.clear();
|
|
|
|
addExcludedApplications(Attribute.stringToList(applicationsString));
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
private void setListenPort(final int listenPort) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.listenPort = listenPort;
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void setListenPortString(@Nullable final String port) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (port != null && !port.isEmpty())
|
|
|
|
setListenPort(Integer.parseInt(port, 10));
|
|
|
|
else
|
|
|
|
setListenPort(0);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
private void setMtu(final int mtu) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.mtu = mtu;
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void setMtuString(@Nullable final String mtu) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (mtu != null && !mtu.isEmpty())
|
|
|
|
setMtu(Integer.parseInt(mtu, 10));
|
|
|
|
else
|
|
|
|
setMtu(0);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
private void setPrivateKey(@Nullable String privateKey) {
|
2017-08-17 10:30:06 +02:00
|
|
|
if (privateKey != null && privateKey.isEmpty())
|
|
|
|
privateKey = null;
|
2018-04-30 18:37:52 +02:00
|
|
|
keypair = privateKey == null ? null : new Keypair(privateKey);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
final StringBuilder sb = new StringBuilder().append("[Interface]\n");
|
2018-04-18 05:28:31 +02:00
|
|
|
if (!addressList.isEmpty())
|
2018-04-17 05:20:17 +02:00
|
|
|
sb.append(Attribute.ADDRESS.composeWith(addressList));
|
2018-04-18 05:28:31 +02:00
|
|
|
if (!dnsList.isEmpty())
|
|
|
|
sb.append(Attribute.DNS.composeWith(getDnsStrings()));
|
2018-07-04 23:47:55 +02:00
|
|
|
if (!excludedApplications.isEmpty())
|
|
|
|
sb.append(Attribute.EXCLUDED_APPLICATIONS.composeWith(excludedApplications));
|
2018-04-18 05:28:31 +02:00
|
|
|
if (listenPort != 0)
|
2017-07-30 00:30:33 +02:00
|
|
|
sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
|
2018-04-18 05:28:31 +02:00
|
|
|
if (mtu != 0)
|
2017-07-30 00:30:33 +02:00
|
|
|
sb.append(Attribute.MTU.composeWith(mtu));
|
2018-04-28 04:45:17 +02:00
|
|
|
if (keypair != null)
|
|
|
|
sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey()));
|
2017-07-30 00:30:33 +02:00
|
|
|
return sb.toString();
|
|
|
|
}
|
2018-04-30 18:39:12 +02:00
|
|
|
|
|
|
|
public static class Observable extends BaseObservable implements Parcelable {
|
|
|
|
public static final Creator<Observable> CREATOR = new Creator<Observable>() {
|
|
|
|
@Override
|
|
|
|
public Observable createFromParcel(final Parcel in) {
|
|
|
|
return new Observable(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Observable[] newArray(final int size) {
|
|
|
|
return new Observable[size];
|
|
|
|
}
|
|
|
|
};
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable private String addresses;
|
|
|
|
@Nullable private String dnses;
|
|
|
|
@Nullable private String excludedApplications;
|
|
|
|
@Nullable private String listenPort;
|
|
|
|
@Nullable private String mtu;
|
|
|
|
@Nullable private String privateKey;
|
|
|
|
@Nullable private String publicKey;
|
|
|
|
|
|
|
|
public Observable(@Nullable final Interface parent) {
|
2018-04-30 18:39:12 +02:00
|
|
|
if (parent != null)
|
|
|
|
loadData(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Observable(final Parcel in) {
|
|
|
|
addresses = in.readString();
|
|
|
|
dnses = in.readString();
|
|
|
|
publicKey = in.readString();
|
|
|
|
privateKey = in.readString();
|
|
|
|
listenPort = in.readString();
|
|
|
|
mtu = in.readString();
|
2018-07-04 23:47:55 +02:00
|
|
|
excludedApplications = in.readString();
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public void commitData(final Interface parent) {
|
|
|
|
parent.setAddressString(addresses);
|
|
|
|
parent.setDnsString(dnses);
|
2018-07-04 23:47:55 +02:00
|
|
|
parent.setExcludedApplicationsString(excludedApplications);
|
2018-04-30 18:37:52 +02:00
|
|
|
parent.setPrivateKey(privateKey);
|
|
|
|
parent.setListenPortString(listenPort);
|
|
|
|
parent.setMtuString(mtu);
|
2018-04-30 18:39:12 +02:00
|
|
|
loadData(parent);
|
|
|
|
notifyChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void generateKeypair() {
|
2018-04-30 18:37:52 +02:00
|
|
|
final Keypair keypair = new Keypair();
|
2018-04-30 18:39:12 +02:00
|
|
|
privateKey = keypair.getPrivateKey();
|
|
|
|
publicKey = keypair.getPublicKey();
|
|
|
|
notifyPropertyChanged(BR.privateKey);
|
|
|
|
notifyPropertyChanged(BR.publicKey);
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
@Bindable
|
|
|
|
public String getAddresses() {
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
@Bindable
|
|
|
|
public String getDnses() {
|
|
|
|
return dnses;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-07-04 23:47:55 +02:00
|
|
|
@Bindable
|
|
|
|
public String getExcludedApplications() {
|
|
|
|
return excludedApplications;
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:34:50 +02:00
|
|
|
@Bindable
|
|
|
|
public int getExcludedApplicationsCount() {
|
|
|
|
return Attribute.stringToList(excludedApplications).length;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
@Bindable
|
|
|
|
public String getListenPort() {
|
|
|
|
return listenPort;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
@Bindable
|
|
|
|
public String getMtu() {
|
|
|
|
return mtu;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
@Bindable
|
|
|
|
public String getPrivateKey() {
|
|
|
|
return privateKey;
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@Nullable
|
2018-04-30 18:39:12 +02:00
|
|
|
@Bindable
|
|
|
|
public String getPublicKey() {
|
|
|
|
return publicKey;
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
protected void loadData(final Interface parent) {
|
|
|
|
addresses = parent.getAddressString();
|
|
|
|
dnses = parent.getDnsString();
|
2018-07-04 23:47:55 +02:00
|
|
|
excludedApplications = parent.getExcludedApplicationsString();
|
2018-04-30 18:37:52 +02:00
|
|
|
publicKey = parent.getPublicKey();
|
|
|
|
privateKey = parent.getPrivateKey();
|
|
|
|
listenPort = parent.getListenPortString();
|
|
|
|
mtu = parent.getMtuString();
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public void setAddresses(final String addresses) {
|
2018-04-30 18:39:12 +02:00
|
|
|
this.addresses = addresses;
|
|
|
|
notifyPropertyChanged(BR.addresses);
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public void setDnses(final String dnses) {
|
2018-04-30 18:39:12 +02:00
|
|
|
this.dnses = dnses;
|
|
|
|
notifyPropertyChanged(BR.dnses);
|
|
|
|
}
|
|
|
|
|
2018-07-04 23:47:55 +02:00
|
|
|
public void setExcludedApplications(final String excludedApplications) {
|
|
|
|
this.excludedApplications = excludedApplications;
|
|
|
|
notifyPropertyChanged(BR.excludedApplications);
|
2018-07-05 21:34:50 +02:00
|
|
|
notifyPropertyChanged(BR.excludedApplicationsCount);
|
2018-07-04 23:47:55 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public void setListenPort(final String listenPort) {
|
2018-04-30 18:39:12 +02:00
|
|
|
this.listenPort = listenPort;
|
|
|
|
notifyPropertyChanged(BR.listenPort);
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public void setMtu(final String mtu) {
|
2018-04-30 18:39:12 +02:00
|
|
|
this.mtu = mtu;
|
|
|
|
notifyPropertyChanged(BR.mtu);
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:37:52 +02:00
|
|
|
public void setPrivateKey(final String privateKey) {
|
2018-04-30 18:39:12 +02:00
|
|
|
this.privateKey = privateKey;
|
|
|
|
|
|
|
|
try {
|
2018-04-30 18:37:52 +02:00
|
|
|
publicKey = new Keypair(privateKey).getPublicKey();
|
|
|
|
} catch (final IllegalArgumentException ignored) {
|
|
|
|
publicKey = "";
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
notifyPropertyChanged(BR.privateKey);
|
|
|
|
notifyPropertyChanged(BR.publicKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(final Parcel dest, final int flags) {
|
|
|
|
dest.writeString(addresses);
|
|
|
|
dest.writeString(dnses);
|
|
|
|
dest.writeString(publicKey);
|
|
|
|
dest.writeString(privateKey);
|
|
|
|
dest.writeString(listenPort);
|
|
|
|
dest.writeString(mtu);
|
2018-07-04 23:47:55 +02:00
|
|
|
dest.writeString(excludedApplications);
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|