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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-17 05:20:17 +02:00
|
|
|
private String[] addressList;
|
|
|
|
private String[] dnsList;
|
2017-08-01 08:12:59 +02:00
|
|
|
private Keypair keypair;
|
2018-01-01 09:06:37 +01:00
|
|
|
private String listenPort;
|
2017-07-30 00:30:33 +02:00
|
|
|
private String 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-17 05:20:17 +02:00
|
|
|
addressList = new String[0];
|
|
|
|
dnsList = new String[0];
|
2017-08-24 06:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private Interface(final Parcel in) {
|
2018-04-17 05:20:17 +02:00
|
|
|
addressList = in.createStringArray();
|
|
|
|
dnsList = in.createStringArray();
|
2017-08-24 06:47:44 +02:00
|
|
|
listenPort = in.readString();
|
|
|
|
mtu = in.readString();
|
|
|
|
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() {
|
|
|
|
return Attribute.listToString(addressList);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
2018-04-17 05:20:17 +02:00
|
|
|
public String[] getAddresses() {
|
|
|
|
return addressList;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getDnsString() {
|
|
|
|
return Attribute.listToString(dnsList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String[] getDnses() {
|
|
|
|
return dnsList;
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getListenPort() {
|
|
|
|
return listenPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getMtu() {
|
|
|
|
return mtu;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
2017-08-24 06:43:58 +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-01-01 09:06:37 +01:00
|
|
|
setListenPort(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.MTU)
|
2018-01-01 09:06:37 +01:00
|
|
|
setMtu(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-17 05:20:17 +02:00
|
|
|
public void addAddresses(String[] addresses) {
|
|
|
|
if (addresses == null || addresses.length == 0)
|
|
|
|
return;
|
|
|
|
String[] both = new String[addresses.length + this.addressList.length];
|
|
|
|
System.arraycopy(this.addressList, 0, both, 0, this.addressList.length);
|
|
|
|
System.arraycopy(addresses, 0, both, this.addressList.length, addresses.length);
|
|
|
|
setAddresses(both);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAddresses(String[] addresses) {
|
|
|
|
if (addresses == null)
|
|
|
|
addresses = new String[0];
|
|
|
|
this.addressList = addresses;
|
|
|
|
notifyPropertyChanged(BR.addresses);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAddressString(String addressString) {
|
|
|
|
setAddresses(Attribute.stringToList(addressString));
|
2018-03-05 13:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
public void addDnses(String[] dnses) {
|
|
|
|
if (dnses == null || dnses.length == 0)
|
|
|
|
return;
|
|
|
|
String[] both = new String[dnses.length + this.dnsList.length];
|
|
|
|
System.arraycopy(this.dnsList, 0, both, 0, this.dnsList.length);
|
|
|
|
System.arraycopy(dnses, 0, both, this.dnsList.length, dnses.length);
|
|
|
|
setDnses(both);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
public void setDnses(String[] dnses) {
|
|
|
|
if (dnses == null)
|
|
|
|
dnses = new String[0];
|
|
|
|
this.dnsList = dnses;
|
|
|
|
notifyPropertyChanged(BR.dnses);
|
2018-03-05 13:47:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
public void setDnsString(String dnsString) {
|
|
|
|
setDnses(Attribute.stringToList(dnsString));
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setListenPort(String listenPort) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (listenPort != null && listenPort.isEmpty())
|
|
|
|
listenPort = null;
|
2017-07-30 00:30:33 +02:00
|
|
|
this.listenPort = listenPort;
|
|
|
|
notifyPropertyChanged(BR.listenPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMtu(String mtu) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (mtu != null && mtu.isEmpty())
|
|
|
|
mtu = null;
|
2017-07-30 00:30:33 +02:00
|
|
|
this.mtu = mtu;
|
|
|
|
notifyPropertyChanged(BR.mtu);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
} catch (final IllegalArgumentException ignored) {
|
|
|
|
keypair = null;
|
|
|
|
}
|
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-17 05:20:17 +02:00
|
|
|
if (addressList != null && addressList.length > 0)
|
|
|
|
sb.append(Attribute.ADDRESS.composeWith(addressList));
|
|
|
|
if (dnsList != null && dnsList.length > 0)
|
|
|
|
sb.append(Attribute.DNS.composeWith(dnsList));
|
2017-07-30 00:30:33 +02:00
|
|
|
if (listenPort != null)
|
|
|
|
sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
|
|
|
|
if (mtu != null)
|
|
|
|
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-17 05:20:17 +02:00
|
|
|
dest.writeStringArray(addressList);
|
|
|
|
dest.writeStringArray(dnsList);
|
2017-08-24 06:47:44 +02:00
|
|
|
dest.writeString(listenPort);
|
|
|
|
dest.writeString(mtu);
|
|
|
|
dest.writeString(privateKey);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|