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;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
import com.wireguard.android.BR;
|
2017-08-08 18:22:41 +02:00
|
|
|
import com.wireguard.crypto.KeyEncoding;
|
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;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
/**
|
|
|
|
* Represents the configuration for a WireGuard interface (an [Interface] block).
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public class Interface extends BaseObservable implements Parcelable {
|
|
|
|
public static final Creator<Interface> CREATOR = new Creator<Interface>() {
|
2017-08-24 06:47:44 +02:00
|
|
|
@Override
|
|
|
|
public Interface createFromParcel(final Parcel in) {
|
|
|
|
return new Interface(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Interface[] newArray(final int size) {
|
|
|
|
return new Interface[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
private List<IPCidr> addressList;
|
|
|
|
private List<InetAddress> dnsList;
|
2017-08-01 08:12:59 +02:00
|
|
|
private Keypair keypair;
|
2018-04-18 05:28:31 +02:00
|
|
|
private int listenPort;
|
|
|
|
private int mtu;
|
2017-08-17 10:30:06 +02:00
|
|
|
private String privateKey;
|
2017-08-01 08:12:59 +02:00
|
|
|
|
2017-08-24 06:47:44 +02:00
|
|
|
public Interface() {
|
2018-04-18 05:28:31 +02:00
|
|
|
addressList = new LinkedList<>();
|
|
|
|
dnsList = new LinkedList<>();
|
2017-08-24 06:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private Interface(final Parcel in) {
|
2018-04-18 05:28:31 +02:00
|
|
|
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();
|
2017-08-24 06:47:44 +02:00
|
|
|
setPrivateKey(in.readString());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-01 08:12:59 +02:00
|
|
|
public void generateKeypair() {
|
|
|
|
keypair = new Keypair();
|
2017-08-17 10:30:06 +02:00
|
|
|
privateKey = keypair.getPrivateKey();
|
2017-08-01 08:12:59 +02:00
|
|
|
notifyPropertyChanged(BR.privateKey);
|
|
|
|
notifyPropertyChanged(BR.publicKey);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
|
|
|
|
@Bindable
|
2018-04-17 05:20:17 +02:00
|
|
|
public String getAddressString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (addressList.isEmpty())
|
|
|
|
return null;
|
2018-04-17 05:20:17 +02:00
|
|
|
return Attribute.listToString(addressList);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
2018-04-18 05:28:31 +02:00
|
|
|
public IPCidr[] getAddresses() {
|
|
|
|
return addressList.toArray(new IPCidr[addressList.size()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getDnsStrings() {
|
|
|
|
List<String> strings = new LinkedList<>();
|
|
|
|
for (final InetAddress addr : dnsList)
|
|
|
|
strings.add(addr.getHostAddress());
|
|
|
|
return strings;
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getDnsString() {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (dnsList.isEmpty())
|
|
|
|
return null;
|
|
|
|
return Attribute.listToString(getDnsStrings());
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
2018-04-18 05:28:31 +02:00
|
|
|
public int getListenPort() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return listenPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
2018-04-18 05:28:31 +02:00
|
|
|
public String getListenPortString() {
|
|
|
|
if (listenPort == 0)
|
|
|
|
return null;
|
|
|
|
return new Integer(listenPort).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public int getMtu() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return mtu;
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
@Bindable
|
|
|
|
public String getMtuString() {
|
|
|
|
if (mtu == 0)
|
|
|
|
return null;
|
|
|
|
return new Integer(mtu).toString();
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
@Bindable
|
|
|
|
public String getPrivateKey() {
|
2017-08-17 10:30:06 +02:00
|
|
|
return privateKey;
|
2017-08-01 08:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getPublicKey() {
|
|
|
|
return keypair != null ? keypair.getPublicKey() : null;
|
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);
|
|
|
|
if (key == Attribute.ADDRESS)
|
2018-04-17 05:20:17 +02:00
|
|
|
addAddresses(key.parseList(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.DNS)
|
2018-04-17 05:20:17 +02:00
|
|
|
addDnses(key.parseList(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.LISTEN_PORT)
|
2018-04-18 05:28:31 +02:00
|
|
|
setListenPortString(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.MTU)
|
2018-04-18 05:28:31 +02:00
|
|
|
setMtuString(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.PRIVATE_KEY)
|
2018-01-01 09:06:37 +01:00
|
|
|
setPrivateKey(key.parse(line));
|
2017-08-13 14:24:03 +02:00
|
|
|
else
|
|
|
|
throw new IllegalArgumentException(line);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:31:10 +02:00
|
|
|
public void addAddresses(String[] addresses) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (addresses != null && addresses.length > 0) {
|
|
|
|
for (final String addr : addresses) {
|
|
|
|
if (addr.isEmpty())
|
2018-04-24 03:31:10 +02:00
|
|
|
throw new IllegalArgumentException("Address is empty");
|
2018-04-18 05:28:31 +02:00
|
|
|
this.addressList.add(new IPCidr(addr));
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
notifyPropertyChanged(BR.addresses);
|
2018-04-18 05:28:31 +02:00
|
|
|
notifyPropertyChanged(BR.addressString);
|
2018-04-17 05:20:17 +02:00
|
|
|
|
2018-03-05 13:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public void setAddressString(final String addressString) {
|
|
|
|
this.addressList.clear();
|
2018-04-24 03:31:10 +02:00
|
|
|
addAddresses(Attribute.stringToList(addressString));
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 03:31:10 +02:00
|
|
|
public void addDnses(String[] dnses) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (dnses != null && dnses.length > 0) {
|
|
|
|
for (final String dns : dnses) {
|
|
|
|
if (dns.isEmpty())
|
2018-04-24 03:31:10 +02:00
|
|
|
throw new IllegalArgumentException("DNS is empty");
|
|
|
|
try {
|
|
|
|
this.dnsList.add(InetAddress.getByName(dns));
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
notifyPropertyChanged(BR.dnses);
|
2018-04-18 05:28:31 +02:00
|
|
|
notifyPropertyChanged(BR.dnsString);
|
|
|
|
|
2018-03-05 13:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public void setDnsString(final String dnsString) {
|
2018-04-24 03:31:10 +02:00
|
|
|
this.dnsList.clear();
|
|
|
|
addDnses(Attribute.stringToList(dnsString));
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public void setListenPort(int listenPort) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.listenPort = listenPort;
|
|
|
|
notifyPropertyChanged(BR.listenPort);
|
2018-04-18 05:28:31 +02:00
|
|
|
notifyPropertyChanged(BR.listenPortString);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setListenPortString(final String port) {
|
|
|
|
if (port != null && !port.isEmpty())
|
|
|
|
setListenPort(Integer.parseInt(port, 10));
|
|
|
|
else
|
|
|
|
setListenPort(0);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public void setMtu(int mtu) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.mtu = mtu;
|
|
|
|
notifyPropertyChanged(BR.mtu);
|
2018-04-18 05:28:31 +02:00
|
|
|
notifyPropertyChanged(BR.mtuString);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMtuString(final String mtu) {
|
|
|
|
if (mtu != null && !mtu.isEmpty())
|
|
|
|
setMtu(Integer.parseInt(mtu, 10));
|
|
|
|
else
|
|
|
|
setMtu(0);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2017-08-17 10:30:06 +02:00
|
|
|
public void setPrivateKey(String privateKey) {
|
|
|
|
if (privateKey != null && privateKey.isEmpty())
|
|
|
|
privateKey = null;
|
|
|
|
this.privateKey = privateKey;
|
|
|
|
if (privateKey != null && privateKey.length() == KeyEncoding.KEY_LENGTH_BASE64) {
|
|
|
|
try {
|
|
|
|
keypair = new Keypair(privateKey);
|
2018-04-24 03:31:10 +02:00
|
|
|
} catch (final IllegalArgumentException e) {
|
2017-08-17 10:30:06 +02:00
|
|
|
keypair = null;
|
2018-04-24 03:31:10 +02:00
|
|
|
throw e;
|
2017-08-17 10:30:06 +02:00
|
|
|
}
|
2017-08-09 09:51:54 +02:00
|
|
|
} else {
|
|
|
|
keypair = null;
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
notifyPropertyChanged(BR.privateKey);
|
2017-08-01 08:12:59 +02:00
|
|
|
notifyPropertyChanged(BR.publicKey);
|
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()));
|
|
|
|
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));
|
2017-08-17 10:30:06 +02:00
|
|
|
if (privateKey != null)
|
|
|
|
sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey));
|
2017-07-30 00:30:33 +02:00
|
|
|
return sb.toString();
|
|
|
|
}
|
2017-08-24 06:47:44 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(final Parcel dest, final int flags) {
|
2018-04-18 05:28:31 +02:00
|
|
|
dest.writeTypedList(addressList);
|
|
|
|
dest.writeInt(dnsList.size());
|
|
|
|
for (final InetAddress addr : dnsList)
|
|
|
|
dest.writeByteArray(addr.getAddress());
|
|
|
|
dest.writeInt(listenPort);
|
|
|
|
dest.writeInt(mtu);
|
2017-08-24 06:47:44 +02:00
|
|
|
dest.writeString(privateKey);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|