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-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.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-04-30 05:00:51 +02:00
|
|
|
public class Interface {
|
|
|
|
public static class Observable extends BaseObservable implements Parcelable {
|
2018-04-28 04:45:17 +02:00
|
|
|
private String addresses;
|
|
|
|
private String dnses;
|
|
|
|
private String publicKey;
|
|
|
|
private String privateKey;
|
|
|
|
private String listenPort;
|
|
|
|
private String mtu;
|
|
|
|
|
|
|
|
public Observable(Interface parent) {
|
2018-04-30 05:00:51 +02:00
|
|
|
if (parent != null)
|
|
|
|
loadData(parent);
|
2018-04-28 04:45:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-04-30 05:00:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Observable(final Parcel in) {
|
|
|
|
addresses = in.readString();
|
|
|
|
dnses = in.readString();
|
|
|
|
publicKey = in.readString();
|
|
|
|
privateKey = in.readString();
|
|
|
|
listenPort = in.readString();
|
|
|
|
mtu = in.readString();
|
|
|
|
}
|
2018-04-28 04:45:17 +02:00
|
|
|
}
|
|
|
|
|
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-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-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-17 05:20:17 +02:00
|
|
|
return Attribute.listToString(addressList);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:31 +02:00
|
|
|
public IPCidr[] getAddresses() {
|
|
|
|
return addressList.toArray(new IPCidr[addressList.size()]);
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private List<String> getDnsStrings() {
|
2018-04-18 05:28:31 +02:00
|
|
|
List<String> strings = new LinkedList<>();
|
|
|
|
for (final InetAddress addr : dnsList)
|
|
|
|
strings.add(addr.getHostAddress());
|
|
|
|
return strings;
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private 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
|
|
|
}
|
|
|
|
|
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-04-18 05:28:31 +02:00
|
|
|
public int getListenPort() {
|
2017-07-30 00:30:33 +02:00
|
|
|
return listenPort;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
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-28 04:45:17 +02:00
|
|
|
private 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-03-05 13:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setAddressString(final String addressString) {
|
2018-04-18 05:28:31 +02:00
|
|
|
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-28 04:45:17 +02:00
|
|
|
private void addDnses(String[] dnses) {
|
2018-04-18 05:28:31 +02:00
|
|
|
if (dnses != null && dnses.length > 0) {
|
|
|
|
for (final String dns : dnses) {
|
2018-04-27 18:29:14 +02:00
|
|
|
this.dnsList.add(Attribute.parseIPString(dns));
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-05 13:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private 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-28 04:45:17 +02:00
|
|
|
private void setListenPort(int listenPort) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.listenPort = listenPort;
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setListenPortString(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-28 04:45:17 +02:00
|
|
|
private void setMtu(int mtu) {
|
2017-07-30 00:30:33 +02:00
|
|
|
this.mtu = mtu;
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void setMtuString(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-04-28 04:45:17 +02:00
|
|
|
private void setPrivateKey(String privateKey) {
|
2017-08-17 10:30:06 +02:00
|
|
|
if (privateKey != null && privateKey.isEmpty())
|
|
|
|
privateKey = null;
|
2018-04-28 04:45:17 +02:00
|
|
|
if (privateKey == null)
|
2017-08-09 09:51:54 +02:00
|
|
|
keypair = null;
|
2018-04-28 04:45:17 +02:00
|
|
|
else
|
|
|
|
keypair = 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()));
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|