diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-04-17 05:20:17 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-04-17 05:27:05 +0200 |
commit | b5360871e8d510e5f0cd1a5c94e87fa71ef6a54a (patch) | |
tree | e554470778ede2cba445992e86b0c9d8082e92f9 /app/src/main/java/com/wireguard/config | |
parent | e40c579b0e5c209f34fce0adc06e521985555594 (diff) |
Remove sloppy java with enterprise java horrors
Since the amount of mind numbing boiler plate has been increased, this
must be the proper way to do things.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/config')
-rw-r--r-- | app/src/main/java/com/wireguard/config/Attribute.java | 19 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Interface.java | 97 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Peer.java | 43 |
3 files changed, 109 insertions, 50 deletions
diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java index ee7fea33..50dbc38d 100644 --- a/app/src/main/java/com/wireguard/config/Attribute.java +++ b/app/src/main/java/com/wireguard/config/Attribute.java @@ -1,5 +1,7 @@ package com.wireguard.config; +import android.text.TextUtils; + import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -43,10 +45,22 @@ enum Attribute { return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]); } + public static String listToString(final String[] list) { + return TextUtils.join(", ", list); + } + + public static String[] stringToList(final String string) { + return string.trim().split("\\s*,\\s*"); + } + public String composeWith(final Object value) { return String.format("%s = %s%n", token, value); } + public String composeWith(final String[] value) { + return String.format("%s = %s%n", token, listToString(value)); + } + public String getToken() { return token; } @@ -55,4 +69,9 @@ enum Attribute { final Matcher matcher = pattern.matcher(line); return matcher.matches() ? matcher.group(1) : null; } + + public String[] parseList(final CharSequence line) { + final Matcher matcher = pattern.matcher(line); + return matcher.matches() ? stringToList(matcher.group(1)) : null; + } } diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index b13f78a0..faf70cf6 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -26,20 +26,21 @@ public class Interface extends BaseObservable implements Parcelable { } }; - private String address; - private String dns; + private String[] addressList; + private String[] dnsList; private Keypair keypair; private String listenPort; private String mtu; private String privateKey; public Interface() { - // Do nothing. + addressList = new String[0]; + dnsList = new String[0]; } private Interface(final Parcel in) { - address = in.readString(); - dns = in.readString(); + addressList = in.createStringArray(); + dnsList = in.createStringArray(); listenPort = in.readString(); mtu = in.readString(); setPrivateKey(in.readString()); @@ -58,13 +59,23 @@ public class Interface extends BaseObservable implements Parcelable { } @Bindable - public String getAddress() { - return address; + public String getAddressString() { + return Attribute.listToString(addressList); } @Bindable - public String getDns() { - return dns; + public String[] getAddresses() { + return addressList; + } + + @Bindable + public String getDnsString() { + return Attribute.listToString(dnsList); + } + + @Bindable + public String[] getDnses() { + return dnsList; } @Bindable @@ -90,9 +101,9 @@ public class Interface extends BaseObservable implements Parcelable { public void parse(final String line) { final Attribute key = Attribute.match(line); if (key == Attribute.ADDRESS) - addAddress(key.parse(line)); + addAddresses(key.parseList(line)); else if (key == Attribute.DNS) - addDns(key.parse(line)); + addDnses(key.parseList(line)); else if (key == Attribute.LISTEN_PORT) setListenPort(key.parse(line)); else if (key == Attribute.MTU) @@ -103,32 +114,44 @@ public class Interface extends BaseObservable implements Parcelable { throw new IllegalArgumentException(line); } - public void addAddress(String address) { - if (address != null && address.isEmpty()) - address = null; - setAddress((this.address == null) ? address - : this.address + ", " + address); + 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)); } - public void setAddress(String address) { - if (address != null && address.isEmpty()) - address = null; - this.address = address; - notifyPropertyChanged(BR.address); + 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); } - public void addDns(String dns) { - if (dns != null && dns.isEmpty()) - dns = null; - setDns((this.dns == null) ? dns - : this.dns + ", " + dns); + public void setDnses(String[] dnses) { + if (dnses == null) + dnses = new String[0]; + this.dnsList = dnses; + notifyPropertyChanged(BR.dnses); } - public void setDns(String dns) { - if (dns != null && dns.isEmpty()) - dns = null; - this.dns = dns; - notifyPropertyChanged(BR.dns); + public void setDnsString(String dnsString) { + setDnses(Attribute.stringToList(dnsString)); } public void setListenPort(String listenPort) { @@ -165,10 +188,10 @@ public class Interface extends BaseObservable implements Parcelable { @Override public String toString() { final StringBuilder sb = new StringBuilder().append("[Interface]\n"); - if (address != null) - sb.append(Attribute.ADDRESS.composeWith(address)); - if (dns != null) - sb.append(Attribute.DNS.composeWith(dns)); + if (addressList != null && addressList.length > 0) + sb.append(Attribute.ADDRESS.composeWith(addressList)); + if (dnsList != null && dnsList.length > 0) + sb.append(Attribute.DNS.composeWith(dnsList)); if (listenPort != null) sb.append(Attribute.LISTEN_PORT.composeWith(listenPort)); if (mtu != null) @@ -180,8 +203,8 @@ public class Interface extends BaseObservable implements Parcelable { @Override public void writeToParcel(final Parcel dest, final int flags) { - dest.writeString(address); - dest.writeString(dns); + dest.writeStringArray(addressList); + dest.writeStringArray(dnsList); dest.writeString(listenPort); dest.writeString(mtu); dest.writeString(privateKey); diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index ac576d3e..954a220a 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -24,18 +24,18 @@ public class Peer extends BaseObservable implements Parcelable { } }; - private String allowedIPs; + private String[] allowedIPList; private String endpoint; private String persistentKeepalive; private String preSharedKey; private String publicKey; public Peer() { - // Do nothing. + allowedIPList = new String[0]; } private Peer(final Parcel in) { - allowedIPs = in.readString(); + allowedIPList = in.createStringArray(); endpoint = in.readString(); persistentKeepalive = in.readString(); preSharedKey = in.readString(); @@ -51,9 +51,13 @@ public class Peer extends BaseObservable implements Parcelable { return 0; } + + @Bindable + public String getAllowedIPsString() { return Attribute.listToString(allowedIPList); } + @Bindable - public String getAllowedIPs() { - return allowedIPs; + public String[] getAllowedIPs() { + return allowedIPList; } @Bindable @@ -79,7 +83,7 @@ public class Peer extends BaseObservable implements Parcelable { public void parse(final String line) { final Attribute key = Attribute.match(line); if (key == Attribute.ALLOWED_IPS) - setAllowedIPs(key.parse(line)); + addAllowedIPs(key.parseList(line)); else if (key == Attribute.ENDPOINT) setEndpoint(key.parse(line)); else if (key == Attribute.PERSISTENT_KEEPALIVE) @@ -92,13 +96,26 @@ public class Peer extends BaseObservable implements Parcelable { throw new IllegalArgumentException(line); } - public void setAllowedIPs(String allowedIPs) { - if (allowedIPs != null && allowedIPs.isEmpty()) - allowedIPs = null; - this.allowedIPs = allowedIPs; + public void addAllowedIPs(String[] allowedIPs) { + if (allowedIPs == null || allowedIPs.length == 0) + return; + String[] both = new String[allowedIPs.length + this.allowedIPList.length]; + System.arraycopy(this.allowedIPList, 0, both, 0, this.allowedIPList.length); + System.arraycopy(allowedIPs, 0, both, this.allowedIPList.length, allowedIPs.length); + setAllowedIPs(both); + } + + public void setAllowedIPs(String[] allowedIPs) { + if (allowedIPs == null) + allowedIPs = new String[0]; + this.allowedIPList = allowedIPs; notifyPropertyChanged(BR.allowedIPs); } + public void setAllowedIPsString(String allowedIPsString) { + setAllowedIPs(Attribute.stringToList(allowedIPsString)); + } + public void setEndpoint(String endpoint) { if (endpoint != null && endpoint.isEmpty()) endpoint = null; @@ -130,8 +147,8 @@ public class Peer extends BaseObservable implements Parcelable { @Override public String toString() { final StringBuilder sb = new StringBuilder().append("[Peer]\n"); - if (allowedIPs != null) - sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPs)); + if (allowedIPList != null) + sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPList)); if (endpoint != null) sb.append(Attribute.ENDPOINT.composeWith(endpoint)); if (persistentKeepalive != null) @@ -145,7 +162,7 @@ public class Peer extends BaseObservable implements Parcelable { @Override public void writeToParcel(final Parcel dest, final int flags) { - dest.writeString(allowedIPs); + dest.writeStringArray(allowedIPList); dest.writeString(endpoint); dest.writeString(persistentKeepalive); dest.writeString(preSharedKey); |