diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-04-30 05:00:51 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-04-30 05:29:54 +0200 |
commit | 73b0c4ea8116c4b1088dcd84cb2f4d7181baf7ab (patch) | |
tree | 225036077f4ef272531cf4e87e423c603181b449 /app/src/main/java/com/wireguard/config | |
parent | 622f41f11f92005e2dd3791fd13b0ace294958d5 (diff) |
TunnelEditorFragment: rewrite and simplify
This should remove some null pointer dereferences and overall make the
thing more robust.
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/Config.java | 79 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/IPCidr.java | 42 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Interface.java | 88 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Peer.java | 82 |
4 files changed, 120 insertions, 171 deletions
diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java index b38d2a13..6330ff5e 100644 --- a/app/src/main/java/com/wireguard/config/Config.java +++ b/app/src/main/java/com/wireguard/config/Config.java @@ -21,35 +21,24 @@ import java.util.List; * Represents a wg-quick configuration file, its name, and its connection state. */ -public class Config implements Parcelable { - public static final Creator<Config> CREATOR = new Creator<Config>() { - @Override - public Config createFromParcel(final Parcel in) { - return new Config(in); - } - - @Override - public Config[] newArray(final int size) { - return new Config[size]; - } - }; - - public static class Observable extends BaseObservable { +public class Config { + public static class Observable extends BaseObservable implements Parcelable { private String name; private Interface.Observable observableInterface; private ObservableList<Peer.Observable> observablePeers; - public Observable(Config parent, String name) { this.name = name; loadData(parent); } public void loadData(Config parent) { - this.observableInterface = new Interface.Observable(parent.interfaceSection); + this.observableInterface = new Interface.Observable(parent == null ? null : parent.interfaceSection); this.observablePeers = new ObservableArrayList<>(); - for (Peer peer : parent.getPeers()) - this.observablePeers.add(new Peer.Observable(peer)); + if (parent != null) { + for (Peer peer : parent.getPeers()) + this.observablePeers.add(new Peer.Observable(peer)); + } } public void commitData(Config parent) { @@ -66,7 +55,7 @@ public class Config implements Parcelable { @Bindable public String getName() { - return name; + return name == null ? "" : name; } public void setName(String name) { @@ -83,20 +72,43 @@ public class Config implements Parcelable { public ObservableList<Peer.Observable> getPeers() { return observablePeers; } - } - private final Interface interfaceSection; - private List<Peer> peers = new ArrayList<>(); - public Config() { - interfaceSection = new Interface(); - } + public static final Creator<Observable> CREATOR = new Creator<Observable>() { + @Override + public Observable createFromParcel(final Parcel in) { + return new Observable(in); + } - private Config(final Parcel in) { - interfaceSection = in.readParcelable(Interface.class.getClassLoader()); - in.readTypedList(peers, Peer.CREATOR); + @Override + public Observable[] newArray(final int size) { + return new Observable[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(final Parcel dest, final int flags) { + dest.writeString(name); + dest.writeParcelable(observableInterface, flags); + dest.writeTypedList(observablePeers); + } + + private Observable(final Parcel in) { + name = in.readString(); + observableInterface = in.readParcelable(Interface.Observable.class.getClassLoader()); + observablePeers = new ObservableArrayList<>(); + in.readTypedList(observablePeers, Peer.Observable.CREATOR); + } } + private final Interface interfaceSection = new Interface(); + private List<Peer> peers = new ArrayList<>(); + public static Config from(final InputStream stream) throws IOException { return from(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))); } @@ -130,11 +142,6 @@ public class Config implements Parcelable { return config; } - @Override - public int describeContents() { - return 0; - } - public Interface getInterface() { return interfaceSection; } @@ -150,10 +157,4 @@ public class Config implements Parcelable { sb.append('\n').append(peer); return sb.toString(); } - - @Override - public void writeToParcel(final Parcel dest, final int flags) { - dest.writeParcelable(interfaceSection, flags); - dest.writeTypedList(peers); - } } diff --git a/app/src/main/java/com/wireguard/config/IPCidr.java b/app/src/main/java/com/wireguard/config/IPCidr.java index 569fd410..41d43cd6 100644 --- a/app/src/main/java/com/wireguard/config/IPCidr.java +++ b/app/src/main/java/com/wireguard/config/IPCidr.java @@ -1,35 +1,15 @@ package com.wireguard.config; -import android.os.Parcel; -import android.os.Parcelable; - import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.util.Locale; -public class IPCidr implements Parcelable { +public class IPCidr { private InetAddress address; private int cidr; - - public static final Parcelable.Creator<IPCidr> CREATOR = new Parcelable.Creator<IPCidr>() { - @Override - public IPCidr createFromParcel(final Parcel in) { - return new IPCidr(in); - } - - @Override - public IPCidr[] newArray(final int size) { - return new IPCidr[size]; - } - }; - - IPCidr(String in) { - parse(in); - } - - private void parse(String in) { + public IPCidr(String in) { cidr = -1; int slash = in.lastIndexOf('/'); if (slash != -1 && slash < in.length() - 1) { @@ -58,22 +38,4 @@ public class IPCidr implements Parcelable { public String toString() { return String.format(Locale.getDefault(), "%s/%d", address.getHostAddress(), cidr); } - - @Override - public void writeToParcel(final Parcel dest, final int flags) { - dest.writeString(this.toString()); - } - - @Override - public int describeContents() { - return 0; - } - - private IPCidr(final Parcel in) { - try { - parse(in.readString()); - } catch (Exception ignored) { - } - } - } diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index 5b125f51..758b528d 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -6,7 +6,6 @@ import android.os.Parcel; import android.os.Parcelable; import com.wireguard.android.BR; -import com.wireguard.crypto.KeyEncoding; import com.wireguard.crypto.Keypair; import java.net.InetAddress; @@ -17,20 +16,8 @@ import java.util.List; * Represents the configuration for a WireGuard interface (an [Interface] block). */ -public class Interface implements Parcelable { - public static final Creator<Interface> CREATOR = new Creator<Interface>() { - @Override - public Interface createFromParcel(final Parcel in) { - return new Interface(in); - } - - @Override - public Interface[] newArray(final int size) { - return new Interface[size]; - } - }; - - public static class Observable extends BaseObservable { +public class Interface { + public static class Observable extends BaseObservable implements Parcelable { private String addresses; private String dnses; private String publicKey; @@ -39,7 +26,8 @@ public class Interface implements Parcelable { private String mtu; public Observable(Interface parent) { - loadData(parent); + if (parent != null) + loadData(parent); } public void loadData(Interface parent) { @@ -131,6 +119,43 @@ public class Interface implements Parcelable { this.mtu = mtu; notifyPropertyChanged(BR.mtu); } + + + public static final Creator<Observable> CREATOR = new Creator<Observable>() { + @Override + public Observable createFromParcel(final Parcel in) { + return new Observable(in); + } + + @Override + public Observable[] newArray(final int size) { + return new Observable[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(final Parcel dest, final int flags) { + dest.writeString(addresses); + dest.writeString(dnses); + dest.writeString(publicKey); + dest.writeString(privateKey); + dest.writeString(listenPort); + dest.writeString(mtu); + } + + private Observable(final Parcel in) { + addresses = in.readString(); + dnses = in.readString(); + publicKey = in.readString(); + privateKey = in.readString(); + listenPort = in.readString(); + mtu = in.readString(); + } } private List<IPCidr> addressList; @@ -144,26 +169,6 @@ public class Interface implements Parcelable { dnsList = new LinkedList<>(); } - private Interface(final Parcel in) { - addressList = in.createTypedArrayList(IPCidr.CREATOR); - int dnsItems = in.readInt(); - dnsList = new LinkedList<>(); - for (int i = 0; i < dnsItems; ++i) { - try { - dnsList.add(InetAddress.getByAddress(in.createByteArray())); - } catch (Exception ignored) { - } - } - listenPort = in.readInt(); - mtu = in.readInt(); - setPrivateKey(in.readString()); - } - - @Override - public int describeContents() { - return 0; - } - private String getAddressString() { if (addressList.isEmpty()) return null; @@ -313,15 +318,4 @@ public class Interface implements Parcelable { sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey())); return sb.toString(); } - - @Override - public void writeToParcel(final Parcel dest, final int flags) { - dest.writeTypedList(addressList); - dest.writeInt(dnsList.size()); - for (final InetAddress addr : dnsList) - dest.writeByteArray(addr.getAddress()); - dest.writeInt(listenPort); - dest.writeInt(mtu); - dest.writeString(keypair == null ? "" : keypair.getPrivateKey()); - } } diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index 28495f46..327365b3 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -21,27 +21,14 @@ import java.util.Locale; * Represents the configuration for a WireGuard peer (a [Peer] block). */ -public class Peer implements Parcelable { - public static final Creator<Peer> CREATOR = new Creator<Peer>() { - @Override - public Peer createFromParcel(final Parcel in) { - return new Peer(in); - } - - @Override - public Peer[] newArray(final int size) { - return new Peer[size]; - } - }; - - public static class Observable extends BaseObservable { +public class Peer { + public static class Observable extends BaseObservable implements Parcelable { private String allowedIPs; private String endpoint; private String persistentKeepalive; private String preSharedKey; private String publicKey; - public Observable(Peer parent) { loadData(parent); } @@ -118,6 +105,41 @@ public class Peer implements Parcelable { this.publicKey = publicKey; notifyPropertyChanged(BR.publicKey); } + + + public static final Creator<Observable> CREATOR = new Creator<Observable>() { + @Override + public Observable createFromParcel(final Parcel in) { + return new Observable(in); + } + + @Override + public Observable[] newArray(final int size) { + return new Observable[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(final Parcel dest, final int flags) { + dest.writeString(allowedIPs); + dest.writeString(endpoint); + dest.writeString(persistentKeepalive); + dest.writeString(preSharedKey); + dest.writeString(publicKey); + } + + private Observable(final Parcel in) { + allowedIPs = in.readString(); + endpoint = in.readString(); + persistentKeepalive = in.readString(); + preSharedKey = in.readString(); + publicKey = in.readString(); + } } private List<IPCidr> allowedIPsList; @@ -130,26 +152,6 @@ public class Peer implements Parcelable { allowedIPsList = new LinkedList<>(); } - private Peer(final Parcel in) { - allowedIPsList = in.createTypedArrayList(IPCidr.CREATOR); - String host = in.readString(); - int port = in.readInt(); - if (host != null && !host.isEmpty() && port > 0) - endpoint = InetSocketAddress.createUnresolved(host, port); - persistentKeepalive = in.readInt(); - preSharedKey = in.readString(); - if (preSharedKey != null && preSharedKey.isEmpty()) - preSharedKey = null; - publicKey = in.readString(); - if (publicKey != null && publicKey.isEmpty()) - publicKey = null; - } - - @Override - public int describeContents() { - return 0; - } - private String getAllowedIPsString() { if (allowedIPsList.isEmpty()) @@ -299,14 +301,4 @@ public class Peer implements Parcelable { sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey)); return sb.toString(); } - - @Override - public void writeToParcel(final Parcel dest, final int flags) { - dest.writeTypedList(allowedIPsList); - dest.writeString(endpoint == null ? null : endpoint.getHostString()); - dest.writeInt(endpoint == null ? 0 : endpoint.getPort()); - dest.writeInt(persistentKeepalive); - dest.writeString(preSharedKey); - dest.writeString(publicKey); - } } |