Serviceless rewrite, part 1
Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
@@ -1,32 +1,23 @@
|
||||
package com.wireguard.config;
|
||||
|
||||
import android.databinding.BaseObservable;
|
||||
import android.databinding.Bindable;
|
||||
import android.databinding.Observable;
|
||||
import android.databinding.ObservableArrayList;
|
||||
import android.databinding.ObservableList;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.wireguard.android.BR;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Represents a wg-quick configuration file, its name, and its connection state.
|
||||
*/
|
||||
|
||||
public class Config extends BaseObservable
|
||||
implements Comparable<Config>, Copyable<Config>, Observable, Parcelable {
|
||||
public static final Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() {
|
||||
public class Config extends BaseObservable implements Parcelable {
|
||||
public static final Creator<Config> CREATOR = new Creator<Config>() {
|
||||
@Override
|
||||
public Config createFromParcel(final Parcel in) {
|
||||
return new Config(in);
|
||||
@@ -37,104 +28,22 @@ public class Config extends BaseObservable
|
||||
return new Config[size];
|
||||
}
|
||||
};
|
||||
public static final int NAME_MAX_LENGTH = 16;
|
||||
private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_=+.-]{1,16}$");
|
||||
|
||||
public static boolean isNameValid(final String name) {
|
||||
return name.length() <= NAME_MAX_LENGTH && PATTERN.matcher(name).matches();
|
||||
}
|
||||
|
||||
private final Interface iface;
|
||||
private boolean isEnabled;
|
||||
private boolean isPrimary;
|
||||
private String name;
|
||||
private final Interface interfaceSection;
|
||||
private final ObservableList<Peer> peers = new ObservableArrayList<>();
|
||||
|
||||
public Config() {
|
||||
iface = new Interface();
|
||||
interfaceSection = new Interface();
|
||||
}
|
||||
|
||||
protected Config(final Parcel in) {
|
||||
iface = in.readParcelable(Interface.class.getClassLoader());
|
||||
name = in.readString();
|
||||
// The flattened peers must be recreated to associate them with this config.
|
||||
final List<Peer> flattenedPeers = new LinkedList<>();
|
||||
in.readTypedList(flattenedPeers, Peer.CREATOR);
|
||||
for (final Peer peer : flattenedPeers)
|
||||
addPeer(peer);
|
||||
private Config(final Parcel in) {
|
||||
interfaceSection = in.readParcelable(Interface.class.getClassLoader());
|
||||
in.readTypedList(peers, Peer.CREATOR);
|
||||
}
|
||||
|
||||
public Peer addPeer() {
|
||||
final Peer peer = new Peer(this);
|
||||
peers.add(peer);
|
||||
return peer;
|
||||
}
|
||||
|
||||
private Peer addPeer(final Peer peer) {
|
||||
final Peer copy = peer.copy(this);
|
||||
peers.add(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NonNull final Config config) {
|
||||
return getName().compareTo(config.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Config copy() {
|
||||
final Config copy = new Config();
|
||||
copy.copyFrom(this);
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copyFrom(final Config source) {
|
||||
if (source != null) {
|
||||
iface.copyFrom(source.iface);
|
||||
name = source.name;
|
||||
peers.clear();
|
||||
for (final Peer peer : source.peers)
|
||||
addPeer(peer);
|
||||
} else {
|
||||
iface.copyFrom(null);
|
||||
name = null;
|
||||
peers.clear();
|
||||
}
|
||||
notifyChange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Interface getInterface() {
|
||||
return iface;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ObservableList<Peer> getPeers() {
|
||||
return peers;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public boolean isPrimary() {
|
||||
return isPrimary;
|
||||
}
|
||||
|
||||
public void parseFrom(final InputStream stream)
|
||||
public static Config from(final InputStream stream)
|
||||
throws IOException {
|
||||
peers.clear();
|
||||
final Config config = new Config();
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||
Peer currentPeer = null;
|
||||
@@ -147,10 +56,11 @@ public class Config extends BaseObservable
|
||||
currentPeer = null;
|
||||
inInterfaceSection = true;
|
||||
} else if ("[Peer]".equals(line)) {
|
||||
currentPeer = addPeer();
|
||||
currentPeer = new Peer();
|
||||
config.peers.add(currentPeer);
|
||||
inInterfaceSection = false;
|
||||
} else if (inInterfaceSection) {
|
||||
iface.parse(line);
|
||||
config.interfaceSection.parse(line);
|
||||
} else if (currentPeer != null) {
|
||||
currentPeer.parse(line);
|
||||
} else {
|
||||
@@ -161,28 +71,25 @@ public class Config extends BaseObservable
|
||||
throw new IllegalArgumentException("Could not find any config information");
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setIsEnabled(final boolean isEnabled) {
|
||||
this.isEnabled = isEnabled;
|
||||
notifyPropertyChanged(BR.enabled);
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void setIsPrimary(final boolean isPrimary) {
|
||||
this.isPrimary = isPrimary;
|
||||
notifyPropertyChanged(BR.primary);
|
||||
public Interface getInterface() {
|
||||
return interfaceSection;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
if (name != null && !name.isEmpty() && !isNameValid(name))
|
||||
throw new IllegalArgumentException();
|
||||
this.name = name;
|
||||
notifyPropertyChanged(BR.name);
|
||||
public ObservableList<Peer> getPeers() {
|
||||
return peers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder().append(iface);
|
||||
final StringBuilder sb = new StringBuilder().append(interfaceSection);
|
||||
for (final Peer peer : peers)
|
||||
sb.append('\n').append(peer);
|
||||
return sb.toString();
|
||||
@@ -190,8 +97,7 @@ public class Config extends BaseObservable
|
||||
|
||||
@Override
|
||||
public void writeToParcel(final Parcel dest, final int flags) {
|
||||
dest.writeParcelable(iface, flags);
|
||||
dest.writeString(name);
|
||||
dest.writeParcelable(interfaceSection, flags);
|
||||
dest.writeTypedList(peers);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user