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;
|
2017-08-01 04:13:01 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
2017-07-29 13:08:54 +02:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a wg-quick profile.
|
|
|
|
*/
|
|
|
|
|
2017-08-01 04:14:42 +02:00
|
|
|
public class Profile extends BaseObservable implements Copyable<Profile>, Observable {
|
2017-07-30 00:30:33 +02:00
|
|
|
private final Interface iface = new Interface();
|
2017-08-01 04:13:01 +02:00
|
|
|
private boolean isConnected;
|
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
|
|
|
|
|
|
|
public Profile(String name) {
|
2017-08-01 04:13:01 +02:00
|
|
|
super();
|
2017-07-29 13:08:54 +02:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2017-08-01 04:14:42 +02:00
|
|
|
private Profile(Profile original)
|
|
|
|
throws IOException {
|
|
|
|
this(original.getName());
|
|
|
|
final byte configBytes[] = original.toString().getBytes(StandardCharsets.UTF_8);
|
|
|
|
final ByteArrayInputStream configStream = new ByteArrayInputStream(configBytes);
|
|
|
|
parseFrom(configStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Profile copy() {
|
|
|
|
try {
|
|
|
|
return new Profile(this);
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
public Interface getInterface() {
|
|
|
|
return iface;
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 04:13:01 +02:00
|
|
|
@Bindable
|
|
|
|
public boolean getIsConnected() {
|
|
|
|
return isConnected;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseFrom(InputStream stream)
|
|
|
|
throws IOException {
|
|
|
|
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]")) {
|
|
|
|
currentPeer = null;
|
|
|
|
} else if (line.equals("[Peer]")) {
|
|
|
|
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-01 04:13:01 +02:00
|
|
|
public void setIsConnected(boolean isConnected) {
|
|
|
|
this.isConnected = isConnected;
|
|
|
|
notifyPropertyChanged(BR.isConnected);
|
|
|
|
}
|
|
|
|
|
2017-08-08 12:54:12 +02:00
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
notifyPropertyChanged(BR.name);
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:08:54 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2017-07-30 00:30:33 +02:00
|
|
|
StringBuilder sb = new StringBuilder().append(iface.toString());
|
|
|
|
for (Peer peer : peers)
|
2017-07-30 00:40:36 +02:00
|
|
|
sb.append('\n').append(peer.toString());
|
2017-07-30 00:30:33 +02:00
|
|
|
return sb.toString();
|
2017-07-29 13:08:54 +02:00
|
|
|
}
|
|
|
|
}
|