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>
This commit is contained in:
Samuel Holland
2017-08-13 07:24:03 -05:00
parent c72d30a1af
commit 5e55d196be
49 changed files with 1193 additions and 1218 deletions
@@ -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);
@@ -9,57 +9,49 @@ 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;
@Override
public Config copy() {
final Config copy = new Config();
copy.copyFrom(this);
return copy;
}
private Profile(Profile original)
throws IOException {
this(original.getName());
parseFrom(original);
}
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() {
return iface;
}
@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 setEnabled(final boolean isEnabled) {
this.isEnabled = isEnabled;
notifyPropertyChanged(BR.enabled);
}
public void setIsConnected(boolean isConnected) {
this.isConnected = isConnected;
notifyPropertyChanged(BR.isConnected);
}
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();
}
}
@@ -6,4 +6,5 @@ package com.wireguard.config;
public interface Copyable<T> {
T copy();
void copyFrom(T source);
}
@@ -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)
@@ -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);
}