2017-07-29 13:08:54 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
2017-08-01 04:13:01 +02:00
|
|
|
import android.databinding.BaseObservable;
|
|
|
|
import android.databinding.Bindable;
|
|
|
|
import android.databinding.Observable;
|
2017-07-30 00:30:33 +02:00
|
|
|
import android.databinding.ObservableArrayList;
|
|
|
|
import android.databinding.ObservableList;
|
2017-07-29 13:08:54 +02:00
|
|
|
|
2017-08-01 04:13:01 +02:00
|
|
|
import com.wireguard.android.BR;
|
|
|
|
|
2017-07-29 13:08:54 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2017-07-30 00:30:33 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2017-08-13 14:24:03 +02:00
|
|
|
import java.util.regex.Pattern;
|
2017-07-29 13:08:54 +02:00
|
|
|
|
|
|
|
/**
|
2017-08-13 14:24:03 +02:00
|
|
|
* Represents a wg-quick configuration file, its name, and its connection state.
|
2017-07-29 13:08:54 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
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();
|
2017-08-08 12:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
private final Interface iface = new Interface();
|
2017-08-13 14:24:03 +02:00
|
|
|
private boolean isEnabled;
|
2017-08-08 12:54:12 +02:00
|
|
|
private String name;
|
2017-07-30 00:30:33 +02:00
|
|
|
private final ObservableList<Peer> peers = new ObservableArrayList<>();
|
2017-07-29 13:08:54 +02:00
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
@Override
|
|
|
|
public Config copy() {
|
|
|
|
final Config copy = new Config();
|
|
|
|
copy.copyFrom(this);
|
|
|
|
return copy;
|
2017-08-01 04:14:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
@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());
|
2017-08-01 04:14:42 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
public Interface getInterface() {
|
|
|
|
return iface;
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 12:54:12 +02:00
|
|
|
@Bindable
|
2017-07-29 13:08:54 +02:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
public ObservableList<Peer> getPeers() {
|
|
|
|
return peers;
|
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
@Bindable
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseFrom(final InputStream stream)
|
2017-07-30 00:30:33 +02:00
|
|
|
throws IOException {
|
2017-08-13 14:24:03 +02:00
|
|
|
peers.clear();
|
2017-07-30 00:30:33 +02:00
|
|
|
try (BufferedReader reader = new BufferedReader(
|
|
|
|
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
|
|
|
Peer currentPeer = null;
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
2017-08-13 14:24:03 +02:00
|
|
|
if (line.isEmpty())
|
|
|
|
continue;
|
|
|
|
if ("[Interface]".equals(line)) {
|
2017-07-30 00:30:33 +02:00
|
|
|
currentPeer = null;
|
2017-08-13 14:24:03 +02:00
|
|
|
} else if ("[Peer]".equals(line)) {
|
2017-07-30 00:30:33 +02:00
|
|
|
currentPeer = new Peer();
|
|
|
|
peers.add(currentPeer);
|
|
|
|
} else if (currentPeer == null) {
|
|
|
|
iface.parseFrom(line);
|
|
|
|
} else {
|
|
|
|
currentPeer.parseFrom(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 07:57:27 +02:00
|
|
|
public void setIsEnabled(final boolean isEnabled) {
|
2017-08-13 14:24:03 +02:00
|
|
|
this.isEnabled = isEnabled;
|
|
|
|
notifyPropertyChanged(BR.enabled);
|
2017-08-01 04:13:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
public void setName(final String name) {
|
|
|
|
if (name != null && !name.isEmpty() && !isNameValid(name))
|
|
|
|
throw new IllegalArgumentException();
|
2017-08-08 12:54:12 +02:00
|
|
|
this.name = name;
|
|
|
|
notifyPropertyChanged(BR.name);
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:08:54 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2017-08-13 14:24:03 +02:00
|
|
|
final StringBuilder sb = new StringBuilder().append(iface);
|
|
|
|
for (final Peer peer : peers)
|
|
|
|
sb.append('\n').append(peer);
|
2017-07-30 00:30:33 +02:00
|
|
|
return sb.toString();
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|
|
|
|
}
|