2017-07-29 13:08:54 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
2017-08-01 04:13:01 +02:00
|
|
|
import android.databinding.BaseObservable;
|
2017-07-30 00:30:33 +02:00
|
|
|
import android.databinding.ObservableArrayList;
|
|
|
|
import android.databinding.ObservableList;
|
2017-08-24 06:47:44 +02:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2017-08-01 04:13:01 +02:00
|
|
|
|
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-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
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public class Config extends BaseObservable implements Parcelable {
|
|
|
|
public static final Creator<Config> CREATOR = new Creator<Config>() {
|
2017-08-24 06:47:44 +02:00
|
|
|
@Override
|
|
|
|
public Config createFromParcel(final Parcel in) {
|
|
|
|
return new Config(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Config[] newArray(final int size) {
|
|
|
|
return new Config[size];
|
|
|
|
}
|
|
|
|
};
|
2017-08-08 12:54:35 +02:00
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private final Interface interfaceSection;
|
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-24 06:47:44 +02:00
|
|
|
public Config() {
|
2018-01-01 09:06:37 +01:00
|
|
|
interfaceSection = new Interface();
|
2017-08-24 06:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
private Config(final Parcel in) {
|
|
|
|
interfaceSection = in.readParcelable(Interface.class.getClassLoader());
|
|
|
|
in.readTypedList(peers, Peer.CREATOR);
|
2017-08-24 06:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public static Config from(final InputStream stream)
|
2017-07-30 00:30:33 +02:00
|
|
|
throws IOException {
|
2018-01-01 09:06:37 +01:00
|
|
|
final Config config = new Config();
|
2017-07-30 00:30:33 +02:00
|
|
|
try (BufferedReader reader = new BufferedReader(
|
|
|
|
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
|
|
|
Peer currentPeer = null;
|
|
|
|
String line;
|
2017-11-26 23:45:41 +01:00
|
|
|
boolean inInterfaceSection = false;
|
2017-07-30 00:30:33 +02:00
|
|
|
while ((line = reader.readLine()) != null) {
|
2017-11-26 23:45:41 +01:00
|
|
|
if (line.isEmpty() || line.startsWith("#"))
|
2017-08-13 14:24:03 +02:00
|
|
|
continue;
|
|
|
|
if ("[Interface]".equals(line)) {
|
2017-07-30 00:30:33 +02:00
|
|
|
currentPeer = null;
|
2017-11-26 23:45:41 +01:00
|
|
|
inInterfaceSection = true;
|
2017-08-13 14:24:03 +02:00
|
|
|
} else if ("[Peer]".equals(line)) {
|
2018-01-01 09:06:37 +01:00
|
|
|
currentPeer = new Peer();
|
|
|
|
config.peers.add(currentPeer);
|
2017-11-26 23:45:41 +01:00
|
|
|
inInterfaceSection = false;
|
|
|
|
} else if (inInterfaceSection) {
|
2018-01-01 09:06:37 +01:00
|
|
|
config.interfaceSection.parse(line);
|
2017-11-26 23:45:41 +01:00
|
|
|
} else if (currentPeer != null) {
|
2017-08-24 06:43:58 +02:00
|
|
|
currentPeer.parse(line);
|
2017-11-26 23:45:41 +01:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Invalid configuration line: " + line);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-26 23:45:41 +01:00
|
|
|
if (!inInterfaceSection && currentPeer == null) {
|
2017-11-29 03:30:23 +01:00
|
|
|
throw new IllegalArgumentException("Could not find any config information");
|
2017-11-26 23:45:41 +01:00
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
return config;
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@Override
|
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
2017-08-01 04:13:01 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public Interface getInterface() {
|
|
|
|
return interfaceSection;
|
2017-08-16 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
public ObservableList<Peer> getPeers() {
|
|
|
|
return peers;
|
2017-08-08 12:54:12 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 13:08:54 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2018-01-01 09:06:37 +01:00
|
|
|
final StringBuilder sb = new StringBuilder().append(interfaceSection);
|
2017-08-13 14:24:03 +02:00
|
|
|
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
|
|
|
}
|
2017-08-17 10:32:01 +02:00
|
|
|
|
2017-08-24 06:47:44 +02:00
|
|
|
@Override
|
|
|
|
public void writeToParcel(final Parcel dest, final int flags) {
|
2018-01-01 09:06:37 +01:00
|
|
|
dest.writeParcelable(interfaceSection, flags);
|
2017-08-24 06:47:44 +02:00
|
|
|
dest.writeTypedList(peers);
|
|
|
|
}
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|