Profile: Parse config file to a string per attribute
This parser should be able to handle any valid WireGuard or wg-quick configuration file. It separates the file into a single interface object and a peer object for each peer. All "[Interface]" sections in the file are combined into the one object. For now, later lines in a block with the same key overwrite earlier lines. This is only relevant for attributes that are lists, such as Address and AllowedIPs, where additional lines may be expected to append to the list. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -1,56 +1,65 @@
|
||||
package com.wireguard.config;
|
||||
|
||||
import android.databinding.BaseObservable;
|
||||
import android.databinding.Bindable;
|
||||
import android.databinding.Observable;
|
||||
|
||||
import com.wireguard.android.BR;
|
||||
import android.databinding.ObservableArrayList;
|
||||
import android.databinding.ObservableList;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Represents a wg-quick profile.
|
||||
*/
|
||||
|
||||
public class Profile extends BaseObservable implements Observable {
|
||||
private String config;
|
||||
public class Profile {
|
||||
private final Interface iface = new Interface();
|
||||
private final String name;
|
||||
private final ObservableList<Peer> peers = new ObservableArrayList<>();
|
||||
|
||||
public Profile(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void fromStream(InputStream stream)
|
||||
throws IOException {
|
||||
final StringBuilder sb = new StringBuilder(stream.available());
|
||||
String line;
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
|
||||
while ((line = reader.readLine()) != null)
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
setConfig(sb.toString());
|
||||
public Interface getInterface() {
|
||||
return iface;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
@Bindable
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setConfig(String config) {
|
||||
this.config = config;
|
||||
notifyPropertyChanged(BR.config);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getConfig();
|
||||
StringBuilder sb = new StringBuilder().append(iface.toString());
|
||||
for (Peer peer : peers)
|
||||
sb.append(peer.toString());
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user