2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
|
|
|
import android.databinding.BaseObservable;
|
|
|
|
import android.databinding.Bindable;
|
|
|
|
import android.databinding.Observable;
|
|
|
|
|
|
|
|
import com.android.databinding.library.baseAdapters.BR;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the configuration for a WireGuard peer (a [Peer] block).
|
|
|
|
*/
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
|
2017-07-30 00:30:33 +02:00
|
|
|
private String allowedIPs;
|
|
|
|
private String endpoint;
|
|
|
|
private String persistentKeepalive;
|
|
|
|
private String publicKey;
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
@Override
|
|
|
|
public Peer copy() {
|
|
|
|
final Peer copy = new Peer();
|
|
|
|
copy.copyFrom(this);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void copyFrom(final Peer source) {
|
|
|
|
allowedIPs = source.allowedIPs;
|
|
|
|
endpoint = source.endpoint;
|
|
|
|
persistentKeepalive = source.persistentKeepalive;
|
|
|
|
publicKey = source.publicKey;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bindable
|
|
|
|
public String getPublicKey() {
|
|
|
|
return publicKey;
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
public void parseFrom(final String line) {
|
2017-07-30 00:30:33 +02:00
|
|
|
final Attribute key = Attribute.match(line);
|
|
|
|
if (key == Attribute.ALLOWED_IPS)
|
|
|
|
allowedIPs = key.parseFrom(line);
|
|
|
|
else if (key == Attribute.ENDPOINT)
|
|
|
|
endpoint = key.parseFrom(line);
|
|
|
|
else if (key == Attribute.PERSISTENT_KEEPALIVE)
|
|
|
|
persistentKeepalive = key.parseFrom(line);
|
|
|
|
else if (key == Attribute.PUBLIC_KEY)
|
|
|
|
publicKey = key.parseFrom(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
if (publicKey != null)
|
|
|
|
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|