diff options
author | Samuel Holland <samuel@sholland.org> | 2017-07-29 17:30:33 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-07-29 17:30:33 -0500 |
commit | 748d780a47ecf3fb11c048939eb1bdc20654b37a (patch) | |
tree | 028cf53f279c354db64ec1c7f32bc17ae1c9bc09 /app/src/main/java/com/wireguard/config/Peer.java | |
parent | 0494dd1404baec0d93944ceba888852a88b2acdb (diff) |
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>
Diffstat (limited to 'app/src/main/java/com/wireguard/config/Peer.java')
-rw-r--r-- | app/src/main/java/com/wireguard/config/Peer.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java new file mode 100644 index 00000000..64977a16 --- /dev/null +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -0,0 +1,83 @@ +package com.wireguard.config; + +import android.databinding.BaseObservable; +import android.databinding.Bindable; +import android.databinding.Observable; + +import com.android.databinding.library.baseAdapters.BR; + +/** + * Represents the configuration for a WireGuard peer (a [Peer] block). + */ + +public class Peer extends BaseObservable implements Observable { + private String allowedIPs; + private String endpoint; + private String persistentKeepalive; + private String publicKey; + + @Bindable + public String getAllowedIPs() { + return allowedIPs; + } + + @Bindable + public String getEndpoint() { + return endpoint; + } + + @Bindable + public String getPersistentKeepalive() { + return persistentKeepalive; + } + + @Bindable + public String getPublicKey() { + return publicKey; + } + + public void parseFrom(String line) { + final Attribute key = Attribute.match(line); + if (key == Attribute.ALLOWED_IPS) + allowedIPs = key.parseFrom(line); + else if (key == Attribute.ENDPOINT) + endpoint = key.parseFrom(line); + else if (key == Attribute.PERSISTENT_KEEPALIVE) + persistentKeepalive = key.parseFrom(line); + else if (key == Attribute.PUBLIC_KEY) + publicKey = key.parseFrom(line); + } + + public void setAllowedIPs(String allowedIPs) { + this.allowedIPs = allowedIPs; + notifyPropertyChanged(BR.allowedIPs); + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + notifyPropertyChanged(BR.endpoint); + } + + public void setPersistentKeepalive(String persistentKeepalive) { + this.persistentKeepalive = persistentKeepalive; + notifyPropertyChanged(BR.persistentKeepalive); + } + + public void setPublicKey(String publicKey) { + this.publicKey = publicKey; + notifyPropertyChanged(BR.publicKey); + } + + public String toString() { + final StringBuilder sb = new StringBuilder().append("[Peer]\n"); + if (allowedIPs != null) + sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPs)); + if (endpoint != null) + sb.append(Attribute.ENDPOINT.composeWith(endpoint)); + if (persistentKeepalive != null) + sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive)); + if (publicKey != null) + sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey)); + return sb.toString(); + } +} |