diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-13 07:24:03 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-13 07:24:03 -0500 |
commit | 5e55d196be092f4a4dcb212cf09d7a1bdab70e00 (patch) | |
tree | eb765a1b961fefdaa7ddc3cfae9cb83a09e0c031 /app/src/main/java/com/wireguard/config | |
parent | c72d30a1af8114ef506a137e3e7274ac33d82bd1 (diff) |
Major renaming and refactoring in activity and service
Apparently "configuration" is the proper term, not "profile".
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 | 12 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Config.java (renamed from app/src/main/java/com/wireguard/config/Profile.java) | 87 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Copyable.java | 1 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Interface.java | 32 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/config/Peer.java | 29 |
5 files changed, 104 insertions, 57 deletions
diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java index 7629456a..e00ebb4c 100644 --- a/app/src/main/java/com/wireguard/config/Attribute.java +++ b/app/src/main/java/com/wireguard/config/Attribute.java @@ -24,23 +24,23 @@ enum Attribute { static { map = new HashMap<>(Attribute.values().length); - for (Attribute key : Attribute.values()) + for (final Attribute key : Attribute.values()) map.put(key.getToken(), key); } - public static Attribute match(String line) { + public static Attribute match(final String line) { return map.get(line.split("\\s|=")[0]); } private final String token; private final Pattern pattern; - Attribute(String token) { - this.pattern = Pattern.compile(token + "\\s*=\\s*(\\S.*)"); + Attribute(final String token) { + pattern = Pattern.compile(token + "\\s*=\\s*(\\S.*)"); this.token = token; } - public String composeWith(String value) { + public String composeWith(final String value) { return token + " = " + value + "\n"; } @@ -48,7 +48,7 @@ enum Attribute { return token; } - public String parseFrom(String line) { + public String parseFrom(final String line) { final Matcher matcher = pattern.matcher(line); if (matcher.matches()) return matcher.group(1); diff --git a/app/src/main/java/com/wireguard/config/Profile.java b/app/src/main/java/com/wireguard/config/Config.java index a0388427..3fe069a1 100644 --- a/app/src/main/java/com/wireguard/config/Profile.java +++ b/app/src/main/java/com/wireguard/config/Config.java @@ -9,46 +9,43 @@ import android.databinding.ObservableList; import com.wireguard.android.BR; import java.io.BufferedReader; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.util.regex.Pattern; /** - * Represents a wg-quick profile. + * Represents a wg-quick configuration file, its name, and its connection state. */ -public class Profile extends BaseObservable implements Copyable<Profile>, Observable { - public static boolean isNameValid(String name) { - final int IFNAMSIZ = 16; - return !name.contains(" ") && name.getBytes(StandardCharsets.UTF_8).length <= IFNAMSIZ; +public class Config extends BaseObservable implements Copyable<Config>, Observable { + private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_=+.-]{1,16}$"); + + private static boolean isNameValid(final String name) { + return PATTERN.matcher(name).matches(); } private final Interface iface = new Interface(); - private boolean isConnected; + private boolean isEnabled; private String name; private final ObservableList<Peer> peers = new ObservableArrayList<>(); - public Profile(String name) { - super(); - if (!isNameValid(name)) - throw new IllegalArgumentException(); - this.name = name; - } - - private Profile(Profile original) - throws IOException { - this(original.getName()); - parseFrom(original); + @Override + public Config copy() { + final Config copy = new Config(); + copy.copyFrom(this); + return copy; } - public Profile copy() { - try { - return new Profile(this); - } catch (IOException e) { - return null; - } + @Override + public void copyFrom(final Config source) { + iface.copyFrom(source.iface); + isEnabled = source.isEnabled; + name = source.name; + peers.clear(); + for (final Peer peer : source.peers) + peers.add(peer.copy()); } public Interface getInterface() { @@ -56,11 +53,6 @@ public class Profile extends BaseObservable implements Copyable<Profile>, Observ } @Bindable - public boolean getIsConnected() { - return isConnected; - } - - @Bindable public String getName() { return name; } @@ -69,16 +61,24 @@ public class Profile extends BaseObservable implements Copyable<Profile>, Observ return peers; } - public void parseFrom(InputStream stream) + @Bindable + public boolean isEnabled() { + return isEnabled; + } + + public void parseFrom(final InputStream stream) throws IOException { + peers.clear(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(stream, StandardCharsets.UTF_8))) { Peer currentPeer = null; String line; while ((line = reader.readLine()) != null) { - if (line.equals("[Interface]")) { + if (line.isEmpty()) + continue; + if ("[Interface]".equals(line)) { currentPeer = null; - } else if (line.equals("[Peer]")) { + } else if ("[Peer]".equals(line)) { currentPeer = new Peer(); peers.add(currentPeer); } else if (currentPeer == null) { @@ -90,28 +90,23 @@ public class Profile extends BaseObservable implements Copyable<Profile>, Observ } } - public void parseFrom(Profile profile) - throws IOException { - final byte configBytes[] = profile.toString().getBytes(StandardCharsets.UTF_8); - final ByteArrayInputStream configStream = new ByteArrayInputStream(configBytes); - parseFrom(configStream); - } - - public void setIsConnected(boolean isConnected) { - this.isConnected = isConnected; - notifyPropertyChanged(BR.isConnected); + public void setEnabled(final boolean isEnabled) { + this.isEnabled = isEnabled; + notifyPropertyChanged(BR.enabled); } - public void setName(String name) { + public void setName(final String name) { + if (name != null && !name.isEmpty() && !isNameValid(name)) + throw new IllegalArgumentException(); this.name = name; notifyPropertyChanged(BR.name); } @Override public String toString() { - StringBuilder sb = new StringBuilder().append(iface.toString()); - for (Peer peer : peers) - sb.append('\n').append(peer.toString()); + final StringBuilder sb = new StringBuilder().append(iface); + for (final Peer peer : peers) + sb.append('\n').append(peer); return sb.toString(); } } diff --git a/app/src/main/java/com/wireguard/config/Copyable.java b/app/src/main/java/com/wireguard/config/Copyable.java index bde6dd9e..826cfbb5 100644 --- a/app/src/main/java/com/wireguard/config/Copyable.java +++ b/app/src/main/java/com/wireguard/config/Copyable.java @@ -6,4 +6,5 @@ package com.wireguard.config; public interface Copyable<T> { T copy(); + void copyFrom(T source); } diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index 6116a4a7..deb85876 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -12,13 +12,29 @@ import com.wireguard.crypto.KeyEncoding; * Represents the configuration for a WireGuard interface (an [Interface] block). */ -public class Interface extends BaseObservable implements Observable { +public class Interface extends BaseObservable implements Copyable<Interface>, Observable { private String address; private String dns; private String listenPort; private Keypair keypair; private String mtu; + @Override + public Interface copy() { + final Interface copy = new Interface(); + copy.copyFrom(this); + return copy; + } + + @Override + public void copyFrom(final Interface source) { + address = source.address; + dns = source.dns; + listenPort = source.listenPort; + keypair = source.keypair; + mtu = source.mtu; + } + public void generateKeypair() { keypair = new Keypair(); notifyPropertyChanged(BR.privateKey); @@ -55,7 +71,7 @@ public class Interface extends BaseObservable implements Observable { return keypair != null ? keypair.getPublicKey() : null; } - public void parseFrom(String line) { + public void parseFrom(final String line) { final Attribute key = Attribute.match(line); if (key == Attribute.ADDRESS) address = key.parseFrom(line); @@ -67,29 +83,39 @@ public class Interface extends BaseObservable implements Observable { mtu = key.parseFrom(line); else if (key == Attribute.PRIVATE_KEY) keypair = new Keypair(key.parseFrom(line)); + else + throw new IllegalArgumentException(line); } public void setAddress(String address) { + if (address != null && address.isEmpty()) + address = null; this.address = address; notifyPropertyChanged(BR.address); } public void setDns(String dns) { + if (dns != null && dns.isEmpty()) + dns = null; this.dns = dns; notifyPropertyChanged(BR.dns); } public void setListenPort(String listenPort) { + if (listenPort != null && listenPort.isEmpty()) + listenPort = null; this.listenPort = listenPort; notifyPropertyChanged(BR.listenPort); } public void setMtu(String mtu) { + if (mtu != null && mtu.isEmpty()) + mtu = null; this.mtu = mtu; notifyPropertyChanged(BR.mtu); } - public void setPrivateKey(String privateKey) { + public void setPrivateKey(final String privateKey) { if (privateKey != null && !privateKey.isEmpty()) { // Avoid exceptions from Keypair while the user is typing. if (privateKey.length() != KeyEncoding.KEY_LENGTH_BASE64) diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index 64977a16..5a8c6789 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -10,12 +10,27 @@ import com.android.databinding.library.baseAdapters.BR; * Represents the configuration for a WireGuard peer (a [Peer] block). */ -public class Peer extends BaseObservable implements Observable { +public class Peer extends BaseObservable implements Copyable<Peer>, Observable { private String allowedIPs; private String endpoint; private String persistentKeepalive; private String publicKey; + @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; + } + @Bindable public String getAllowedIPs() { return allowedIPs; @@ -36,7 +51,7 @@ public class Peer extends BaseObservable implements Observable { return publicKey; } - public void parseFrom(String line) { + public void parseFrom(final String line) { final Attribute key = Attribute.match(line); if (key == Attribute.ALLOWED_IPS) allowedIPs = key.parseFrom(line); @@ -46,24 +61,34 @@ public class Peer extends BaseObservable implements Observable { persistentKeepalive = key.parseFrom(line); else if (key == Attribute.PUBLIC_KEY) publicKey = key.parseFrom(line); + else + throw new IllegalArgumentException(line); } public void setAllowedIPs(String allowedIPs) { + if (allowedIPs != null && allowedIPs.isEmpty()) + allowedIPs = null; this.allowedIPs = allowedIPs; notifyPropertyChanged(BR.allowedIPs); } public void setEndpoint(String endpoint) { + if (endpoint != null && endpoint.isEmpty()) + endpoint = null; this.endpoint = endpoint; notifyPropertyChanged(BR.endpoint); } public void setPersistentKeepalive(String persistentKeepalive) { + if (persistentKeepalive != null && persistentKeepalive.isEmpty()) + persistentKeepalive = null; this.persistentKeepalive = persistentKeepalive; notifyPropertyChanged(BR.persistentKeepalive); } public void setPublicKey(String publicKey) { + if (publicKey != null && publicKey.isEmpty()) + publicKey = null; this.publicKey = publicKey; notifyPropertyChanged(BR.publicKey); } |