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.android.databinding.library.baseAdapters.BR;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the configuration for a WireGuard peer (a [Peer] block).
|
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public class Peer extends BaseObservable implements Parcelable {
|
|
|
|
public static final Creator<Peer> CREATOR = new Creator<Peer>() {
|
2017-08-24 06:47:44 +02:00
|
|
|
@Override
|
|
|
|
public Peer createFromParcel(final Parcel in) {
|
|
|
|
return new Peer(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Peer[] newArray(final int size) {
|
|
|
|
return new Peer[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
private String allowedIPs;
|
|
|
|
private String endpoint;
|
|
|
|
private String persistentKeepalive;
|
2017-08-24 09:00:53 +02:00
|
|
|
private String preSharedKey;
|
2017-07-30 00:30:33 +02:00
|
|
|
private String publicKey;
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public Peer() {
|
|
|
|
// Do nothing.
|
2017-08-20 01:50:35 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private Peer(final Parcel in) {
|
2017-08-24 06:47:44 +02:00
|
|
|
allowedIPs = in.readString();
|
|
|
|
endpoint = in.readString();
|
|
|
|
persistentKeepalive = in.readString();
|
2017-08-24 09:00:53 +02:00
|
|
|
preSharedKey = in.readString();
|
2017-08-24 06:47:44 +02:00
|
|
|
publicKey = in.readString();
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public static Peer newInstance() {
|
|
|
|
return new Peer();
|
2017-08-13 14:24:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 06:47:44 +02:00
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
@Bindable
|
|
|
|
public String getAllowedIPs() {
|
|
|
|
return allowedIPs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getEndpoint() {
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getPersistentKeepalive() {
|
|
|
|
return persistentKeepalive;
|
|
|
|
}
|
|
|
|
|
2017-08-24 09:00:53 +02:00
|
|
|
@Bindable
|
|
|
|
public String getPreSharedKey() {
|
|
|
|
return preSharedKey;
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
@Bindable
|
|
|
|
public String getPublicKey() {
|
|
|
|
return publicKey;
|
|
|
|
}
|
|
|
|
|
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.ALLOWED_IPS)
|
2018-01-01 09:06:37 +01:00
|
|
|
setAllowedIPs(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.ENDPOINT)
|
2018-01-01 09:06:37 +01:00
|
|
|
setEndpoint(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.PERSISTENT_KEEPALIVE)
|
2018-01-01 09:06:37 +01:00
|
|
|
setPersistentKeepalive(key.parse(line));
|
2017-11-26 23:45:41 +01:00
|
|
|
else if (key == Attribute.PRESHARED_KEY)
|
2018-01-01 09:06:37 +01:00
|
|
|
setPreSharedKey(key.parse(line));
|
2017-07-30 00:30:33 +02:00
|
|
|
else if (key == Attribute.PUBLIC_KEY)
|
2018-01-01 09:06:37 +01:00
|
|
|
setPublicKey(key.parse(line));
|
2017-08-13 14:24:03 +02:00
|
|
|
else
|
|
|
|
throw new IllegalArgumentException(line);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setAllowedIPs(String allowedIPs) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (allowedIPs != null && allowedIPs.isEmpty())
|
|
|
|
allowedIPs = null;
|
2017-07-30 00:30:33 +02:00
|
|
|
this.allowedIPs = allowedIPs;
|
|
|
|
notifyPropertyChanged(BR.allowedIPs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEndpoint(String endpoint) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (endpoint != null && endpoint.isEmpty())
|
|
|
|
endpoint = null;
|
2017-07-30 00:30:33 +02:00
|
|
|
this.endpoint = endpoint;
|
|
|
|
notifyPropertyChanged(BR.endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPersistentKeepalive(String persistentKeepalive) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (persistentKeepalive != null && persistentKeepalive.isEmpty())
|
|
|
|
persistentKeepalive = null;
|
2017-07-30 00:30:33 +02:00
|
|
|
this.persistentKeepalive = persistentKeepalive;
|
|
|
|
notifyPropertyChanged(BR.persistentKeepalive);
|
|
|
|
}
|
|
|
|
|
2017-08-24 09:00:53 +02:00
|
|
|
public void setPreSharedKey(String preSharedKey) {
|
|
|
|
if (preSharedKey != null && preSharedKey.isEmpty())
|
|
|
|
preSharedKey = null;
|
|
|
|
this.preSharedKey = preSharedKey;
|
|
|
|
notifyPropertyChanged(BR.preSharedKey);
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
public void setPublicKey(String publicKey) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (publicKey != null && publicKey.isEmpty())
|
|
|
|
publicKey = null;
|
2017-07-30 00:30:33 +02:00
|
|
|
this.publicKey = publicKey;
|
|
|
|
notifyPropertyChanged(BR.publicKey);
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:51:32 +01:00
|
|
|
@Override
|
2017-07-30 00:30:33 +02:00
|
|
|
public String toString() {
|
|
|
|
final StringBuilder sb = new StringBuilder().append("[Peer]\n");
|
|
|
|
if (allowedIPs != null)
|
|
|
|
sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPs));
|
|
|
|
if (endpoint != null)
|
|
|
|
sb.append(Attribute.ENDPOINT.composeWith(endpoint));
|
|
|
|
if (persistentKeepalive != null)
|
|
|
|
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
|
2017-08-24 09:00:53 +02:00
|
|
|
if (preSharedKey != null)
|
2017-11-26 23:45:41 +01:00
|
|
|
sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey));
|
2017-07-30 00:30:33 +02:00
|
|
|
if (publicKey != null)
|
|
|
|
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2017-08-24 06:47:44 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToParcel(final Parcel dest, final int flags) {
|
|
|
|
dest.writeString(allowedIPs);
|
|
|
|
dest.writeString(endpoint);
|
|
|
|
dest.writeString(persistentKeepalive);
|
2017-08-24 09:00:53 +02:00
|
|
|
dest.writeString(preSharedKey);
|
2017-08-24 06:47:44 +02:00
|
|
|
dest.writeString(publicKey);
|
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|