aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/config/Peer.java
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-04-28 04:45:17 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-04-28 04:51:43 +0200
commit63071f57b7b501c23843f502a5d2a1ac83338bc7 (patch)
tree21a9a0f4de77b67669949534c27e033d1ee896c3 /app/src/main/java/com/wireguard/config/Peer.java
parentDo not do DNS lookups for IPs (diff)
downloadwireguard-android-63071f57b7b501c23843f502a5d2a1ac83338bc7.tar.xz
wireguard-android-63071f57b7b501c23843f502a5d2a1ac83338bc7.zip
Use validation instead of two-way binding
This is insane, but it appears to be working. We essentially store things in a separate class for editing, and then commit it back at a given time. This business with onViewStateRestored in both TunnelEditorFragment and in TunnelDetailFragment is buggy and likely wrong. In general TunnelEditorFragment should probably be rewritten. The relationship with the changed name is not clear. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/config/Peer.java')
-rw-r--r--app/src/main/java/com/wireguard/config/Peer.java135
1 files changed, 103 insertions, 32 deletions
diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java
index 9895f447..5c506b36 100644
--- a/app/src/main/java/com/wireguard/config/Peer.java
+++ b/app/src/main/java/com/wireguard/config/Peer.java
@@ -6,6 +6,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import com.android.databinding.library.baseAdapters.BR;
+import com.wireguard.crypto.KeyEncoding;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
@@ -19,7 +20,7 @@ import java.util.List;
* Represents the configuration for a WireGuard peer (a [Peer] block).
*/
-public class Peer extends BaseObservable implements Parcelable {
+public class Peer implements Parcelable {
public static final Creator<Peer> CREATOR = new Creator<Peer>() {
@Override
public Peer createFromParcel(final Parcel in) {
@@ -32,6 +33,92 @@ public class Peer extends BaseObservable implements Parcelable {
}
};
+ public static class Observable extends BaseObservable {
+ private String allowedIPs;
+ private String endpoint;
+ private String persistentKeepalive;
+ private String preSharedKey;
+ private String publicKey;
+
+
+ public Observable(Peer parent) {
+ loadData(parent);
+ }
+ public static Observable newInstance() {
+ return new Observable(new Peer());
+ }
+
+ public void loadData(Peer parent) {
+ this.allowedIPs = parent.getAllowedIPsString();
+ this.endpoint = parent.getEndpointString();
+ this.persistentKeepalive = parent.getPersistentKeepaliveString();
+ this.preSharedKey = parent.getPreSharedKey();
+ this.publicKey = parent.getPublicKey();
+ }
+
+ public void commitData(Peer parent) {
+ parent.setAllowedIPsString(this.allowedIPs);
+ parent.setEndpointString(this.endpoint);
+ parent.setPersistentKeepaliveString(this.persistentKeepalive);
+ parent.setPreSharedKey(this.preSharedKey);
+ parent.setPublicKey(this.publicKey);
+ if (parent.getPublicKey() == null)
+ throw new IllegalArgumentException("Peer public key may not be empty");
+ loadData(parent);
+ notifyChange();
+ }
+
+ @Bindable
+ public String getAllowedIPs() {
+ return allowedIPs;
+ }
+
+ public void setAllowedIPs(String allowedIPs) {
+ this.allowedIPs = allowedIPs;
+ notifyPropertyChanged(BR.allowedIPs);
+ }
+
+ @Bindable
+ public String getEndpoint() {
+ return endpoint;
+ }
+
+ public void setEndpoint(String endpoint) {
+ this.endpoint = endpoint;
+ notifyPropertyChanged(BR.endpoint);
+ }
+
+ @Bindable
+ public String getPersistentKeepalive() {
+ return persistentKeepalive;
+ }
+
+ public void setPersistentKeepalive(String persistentKeepalive) {
+ this.persistentKeepalive = persistentKeepalive;
+ notifyPropertyChanged(BR.persistentKeepalive);
+ }
+
+ @Bindable
+ public String getPreSharedKey() {
+ return preSharedKey;
+ }
+
+ public void setPreSharedKey(String preSharedKey) {
+ this.preSharedKey = preSharedKey;
+ notifyPropertyChanged(BR.preSharedKey);
+ }
+
+ @Bindable
+ public String getPublicKey() {
+ return publicKey;
+ }
+
+ public void setPublicKey(String publicKey) {
+ this.publicKey = publicKey;
+ notifyPropertyChanged(BR.publicKey);
+ }
+ }
+
private List<IPCidr> allowedIPsList;
private InetSocketAddress endpoint;
private int persistentKeepalive;
@@ -57,35 +144,27 @@ public class Peer extends BaseObservable implements Parcelable {
publicKey = null;
}
- public static Peer newInstance() {
- return new Peer();
- }
-
@Override
public int describeContents() {
return 0;
}
- @Bindable
- public String getAllowedIPsString() {
+ private String getAllowedIPsString() {
if (allowedIPsList.isEmpty())
return null;
return Attribute.listToString(allowedIPsList);
}
- @Bindable
public IPCidr[] getAllowedIPs() {
return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
}
- @Bindable
public InetSocketAddress getEndpoint() {
return endpoint;
}
- @Bindable
- public String getEndpointString() {
+ private String getEndpointString() {
if (endpoint == null)
return null;
return String.format("%s:%d", endpoint.getHostString(), endpoint.getPort());
@@ -103,24 +182,20 @@ public class Peer extends BaseObservable implements Parcelable {
return String.format("%s:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
}
- @Bindable
public int getPersistentKeepalive() {
return persistentKeepalive;
}
- @Bindable
- public String getPersistentKeepaliveString() {
+ private String getPersistentKeepaliveString() {
if (persistentKeepalive == 0)
return null;
return new Integer(persistentKeepalive).toString();
}
- @Bindable
public String getPreSharedKey() {
return preSharedKey;
}
- @Bindable
public String getPublicKey() {
return publicKey;
}
@@ -141,28 +216,24 @@ public class Peer extends BaseObservable implements Parcelable {
throw new IllegalArgumentException(line);
}
- public void addAllowedIPs(String[] allowedIPs) {
+ private void addAllowedIPs(String[] allowedIPs) {
if (allowedIPs != null && allowedIPs.length > 0) {
for (final String allowedIP : allowedIPs) {
this.allowedIPsList.add(new IPCidr(allowedIP));
}
}
- notifyPropertyChanged(BR.allowedIPs);
- notifyPropertyChanged(BR.allowedIPsString);
}
- public void setAllowedIPsString(final String allowedIPsString) {
+ private void setAllowedIPsString(final String allowedIPsString) {
this.allowedIPsList.clear();
addAllowedIPs(Attribute.stringToList(allowedIPsString));
}
- public void setEndpoint(InetSocketAddress endpoint) {
+ private void setEndpoint(InetSocketAddress endpoint) {
this.endpoint = endpoint;
- notifyPropertyChanged(BR.endpoint);
- notifyPropertyChanged(BR.endpointString);
}
- public void setEndpointString(final String endpoint) {
+ private void setEndpointString(final String endpoint) {
if (endpoint != null && !endpoint.isEmpty()) {
InetSocketAddress constructedEndpoint;
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
@@ -179,31 +250,31 @@ public class Peer extends BaseObservable implements Parcelable {
setEndpoint(null);
}
- public void setPersistentKeepalive(int persistentKeepalive) {
+ private void setPersistentKeepalive(int persistentKeepalive) {
this.persistentKeepalive = persistentKeepalive;
- notifyPropertyChanged(BR.persistentKeepalive);
- notifyPropertyChanged(BR.persistentKeepaliveString);
}
- public void setPersistentKeepaliveString(String persistentKeepalive) {
+ private void setPersistentKeepaliveString(String persistentKeepalive) {
if (persistentKeepalive != null && !persistentKeepalive.isEmpty())
setPersistentKeepalive(Integer.parseInt(persistentKeepalive, 10));
else
setPersistentKeepalive(0);
}
- public void setPreSharedKey(String preSharedKey) {
+ private void setPreSharedKey(String preSharedKey) {
if (preSharedKey != null && preSharedKey.isEmpty())
preSharedKey = null;
+ if (preSharedKey != null)
+ KeyEncoding.keyFromBase64(preSharedKey);
this.preSharedKey = preSharedKey;
- notifyPropertyChanged(BR.preSharedKey);
}
- public void setPublicKey(String publicKey) {
+ private void setPublicKey(String publicKey) {
if (publicKey != null && publicKey.isEmpty())
publicKey = null;
+ if (publicKey != null)
+ KeyEncoding.keyFromBase64(publicKey);
this.publicKey = publicKey;
- notifyPropertyChanged(BR.publicKey);
}
@Override