summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2018-04-30 11:39:12 -0500
committerSamuel Holland <samuel@sholland.org>2018-04-30 11:39:12 -0500
commit7eedf08d4bbe8d15a38cde777d92e72930ffa2e8 (patch)
tree12293719d001f3eef3a54244688279e62e2513a1 /app/src/main/java/com/wireguard/config
parente2636320b7faa372d724732265b380ef85b7d83e (diff)
global: Automatic code formatting
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main/java/com/wireguard/config')
-rw-r--r--app/src/main/java/com/wireguard/config/Attribute.java38
-rw-r--r--app/src/main/java/com/wireguard/config/Config.java170
-rw-r--r--app/src/main/java/com/wireguard/config/Interface.java329
-rw-r--r--app/src/main/java/com/wireguard/config/Peer.java301
4 files changed, 415 insertions, 423 deletions
diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java
index 5ecd21f4..2d8070ac 100644
--- a/app/src/main/java/com/wireguard/config/Attribute.java
+++ b/app/src/main/java/com/wireguard/config/Attribute.java
@@ -30,6 +30,7 @@ enum Attribute {
private static final Map<String, Attribute> KEY_MAP;
private static final Pattern SEPARATOR_PATTERN = Pattern.compile("\\s|=");
+ private static Method parseNumericAddressMethod;
static {
KEY_MAP = new HashMap<>(Attribute.values().length);
@@ -38,6 +39,14 @@ enum Attribute {
}
}
+ static {
+ try {
+ parseNumericAddressMethod = InetAddress.class.getMethod("parseNumericAddress", new Class[]{String.class});
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
private final Pattern pattern;
private final String token;
@@ -46,44 +55,35 @@ enum Attribute {
this.token = token;
}
- public static Attribute match(final CharSequence line) {
- return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]);
- }
-
public static <T> String listToString(final List<T> list) {
return TextUtils.join(", ", list);
}
- public static String[] stringToList(final String string) {
- if (string == null)
- return new String[0];
- return string.trim().split("\\s*,\\s*");
- }
-
- private static Method parseNumericAddressMethod;
- static {
- try {
- parseNumericAddressMethod = InetAddress.class.getMethod("parseNumericAddress", new Class[]{String.class});
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
+ public static Attribute match(final CharSequence line) {
+ return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]);
}
public static InetAddress parseIPString(final String address) {
if (address == null || address.isEmpty())
throw new IllegalArgumentException("Empty address");
try {
- return (InetAddress)parseNumericAddressMethod.invoke(null, new Object[]{address});
+ return (InetAddress) parseNumericAddressMethod.invoke(null, new Object[]{address});
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IllegalArgumentException)
- throw (IllegalArgumentException)e.getCause();
+ throw (IllegalArgumentException) e.getCause();
else
throw new IllegalArgumentException(e.getCause());
}
}
+ public static String[] stringToList(final String string) {
+ if (string == null)
+ return new String[0];
+ return string.trim().split("\\s*,\\s*");
+ }
+
public String composeWith(final Object value) {
return String.format("%s = %s%n", token, value);
}
diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java
index 6330ff5e..7e6ff77c 100644
--- a/app/src/main/java/com/wireguard/config/Config.java
+++ b/app/src/main/java/com/wireguard/config/Config.java
@@ -1,7 +1,5 @@
package com.wireguard.config;
-import com.android.databinding.library.baseAdapters.BR;
-
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
@@ -9,6 +7,8 @@ import android.databinding.ObservableList;
import android.os.Parcel;
import android.os.Parcelable;
+import com.android.databinding.library.baseAdapters.BR;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -22,90 +22,6 @@ import java.util.List;
*/
public class Config {
- public static class Observable extends BaseObservable implements Parcelable {
- private String name;
- private Interface.Observable observableInterface;
- private ObservableList<Peer.Observable> observablePeers;
-
- public Observable(Config parent, String name) {
- this.name = name;
- loadData(parent);
- }
-
- public void loadData(Config parent) {
- this.observableInterface = new Interface.Observable(parent == null ? null : parent.interfaceSection);
- this.observablePeers = new ObservableArrayList<>();
- if (parent != null) {
- for (Peer peer : parent.getPeers())
- this.observablePeers.add(new Peer.Observable(peer));
- }
- }
-
- public void commitData(Config parent) {
- this.observableInterface.commitData(parent.interfaceSection);
- List<Peer> newPeers = new ArrayList<>(this.observablePeers.size());
- for (Peer.Observable observablePeer : this.observablePeers) {
- Peer peer = new Peer();
- observablePeer.commitData(peer);
- newPeers.add(peer);
- }
- parent.peers = newPeers;
- notifyChange();
- }
-
- @Bindable
- public String getName() {
- return name == null ? "" : name;
- }
-
- public void setName(String name) {
- this.name = name;
- notifyPropertyChanged(BR.name);
- }
-
- @Bindable
- public Interface.Observable getInterfaceSection() {
- return observableInterface;
- }
-
- @Bindable
- public ObservableList<Peer.Observable> getPeers() {
- return observablePeers;
- }
-
-
- public static final Creator<Observable> CREATOR = new Creator<Observable>() {
- @Override
- public Observable createFromParcel(final Parcel in) {
- return new Observable(in);
- }
-
- @Override
- public Observable[] newArray(final int size) {
- return new Observable[size];
- }
- };
-
- @Override
- public int describeContents() {
- return 0;
- }
-
- @Override
- public void writeToParcel(final Parcel dest, final int flags) {
- dest.writeString(name);
- dest.writeParcelable(observableInterface, flags);
- dest.writeTypedList(observablePeers);
- }
-
- private Observable(final Parcel in) {
- name = in.readString();
- observableInterface = in.readParcelable(Interface.Observable.class.getClassLoader());
- observablePeers = new ObservableArrayList<>();
- in.readTypedList(observablePeers, Peer.Observable.CREATOR);
- }
- }
-
private final Interface interfaceSection = new Interface();
private List<Peer> peers = new ArrayList<>();
@@ -157,4 +73,86 @@ public class Config {
sb.append('\n').append(peer);
return sb.toString();
}
+
+ public static class Observable extends BaseObservable implements Parcelable {
+ public static final Creator<Observable> CREATOR = new Creator<Observable>() {
+ @Override
+ public Observable createFromParcel(final Parcel in) {
+ return new Observable(in);
+ }
+
+ @Override
+ public Observable[] newArray(final int size) {
+ return new Observable[size];
+ }
+ };
+ private String name;
+ private Interface.Observable observableInterface;
+ private ObservableList<Peer.Observable> observablePeers;
+
+ public Observable(Config parent, String name) {
+ this.name = name;
+ loadData(parent);
+ }
+
+ private Observable(final Parcel in) {
+ name = in.readString();
+ observableInterface = in.readParcelable(Interface.Observable.class.getClassLoader());
+ observablePeers = new ObservableArrayList<>();
+ in.readTypedList(observablePeers, Peer.Observable.CREATOR);
+ }
+
+ public void commitData(Config parent) {
+ this.observableInterface.commitData(parent.interfaceSection);
+ List<Peer> newPeers = new ArrayList<>(this.observablePeers.size());
+ for (Peer.Observable observablePeer : this.observablePeers) {
+ Peer peer = new Peer();
+ observablePeer.commitData(peer);
+ newPeers.add(peer);
+ }
+ parent.peers = newPeers;
+ notifyChange();
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Bindable
+ public Interface.Observable getInterfaceSection() {
+ return observableInterface;
+ }
+
+ @Bindable
+ public String getName() {
+ return name == null ? "" : name;
+ }
+
+ @Bindable
+ public ObservableList<Peer.Observable> getPeers() {
+ return observablePeers;
+ }
+
+ public void loadData(Config parent) {
+ this.observableInterface = new Interface.Observable(parent == null ? null : parent.interfaceSection);
+ this.observablePeers = new ObservableArrayList<>();
+ if (parent != null) {
+ for (Peer peer : parent.getPeers())
+ this.observablePeers.add(new Peer.Observable(peer));
+ }
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ notifyPropertyChanged(BR.name);
+ }
+
+ @Override
+ public void writeToParcel(final Parcel dest, final int flags) {
+ dest.writeString(name);
+ dest.writeParcelable(observableInterface, flags);
+ dest.writeTypedList(observablePeers);
+ }
+ }
}
diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java
index 758b528d..ff69ccaf 100644
--- a/app/src/main/java/com/wireguard/config/Interface.java
+++ b/app/src/main/java/com/wireguard/config/Interface.java
@@ -17,158 +17,34 @@ import java.util.List;
*/
public class Interface {
- public static class Observable extends BaseObservable implements Parcelable {
- private String addresses;
- private String dnses;
- private String publicKey;
- private String privateKey;
- private String listenPort;
- private String mtu;
-
- public Observable(Interface parent) {
- if (parent != null)
- loadData(parent);
- }
-
- public void loadData(Interface parent) {
- this.addresses = parent.getAddressString();
- this.dnses = parent.getDnsString();
- this.publicKey = parent.getPublicKey();
- this.privateKey = parent.getPrivateKey();
- this.listenPort = parent.getListenPortString();
- this.mtu = parent.getMtuString();
- }
-
- public void commitData(Interface parent) {
- parent.setAddressString(this.addresses);
- parent.setDnsString(this.dnses);
- parent.setPrivateKey(this.privateKey);
- parent.setListenPortString(this.listenPort);
- parent.setMtuString(this.mtu);
- loadData(parent);
- notifyChange();
- }
-
- @Bindable
- public String getAddresses() {
- return addresses;
- }
-
- public void setAddresses(String addresses) {
- this.addresses = addresses;
- notifyPropertyChanged(BR.addresses);
- }
-
- @Bindable
- public String getDnses() {
- return dnses;
- }
-
- public void setDnses(String dnses) {
- this.dnses = dnses;
- notifyPropertyChanged(BR.dnses);
- }
-
- @Bindable
- public String getPublicKey() {
- return publicKey;
- }
-
- @Bindable
- public String getPrivateKey() {
- return privateKey;
- }
-
- public void setPrivateKey(String privateKey) {
- this.privateKey = privateKey;
-
- try {
- this.publicKey = new Keypair(privateKey).getPublicKey();
- } catch (IllegalArgumentException ignored) {
- this.publicKey = "";
- }
-
- notifyPropertyChanged(BR.privateKey);
- notifyPropertyChanged(BR.publicKey);
- }
-
- public void generateKeypair() {
- Keypair keypair = new Keypair();
- privateKey = keypair.getPrivateKey();
- publicKey = keypair.getPublicKey();
- notifyPropertyChanged(BR.privateKey);
- notifyPropertyChanged(BR.publicKey);
- }
-
- @Bindable
- public String getListenPort() {
- return listenPort;
- }
-
- public void setListenPort(String listenPort) {
- this.listenPort = listenPort;
- notifyPropertyChanged(BR.listenPort);
- }
-
- @Bindable
- public String getMtu() {
- return mtu;
- }
-
- public void setMtu(String mtu) {
- this.mtu = mtu;
- notifyPropertyChanged(BR.mtu);
- }
-
-
- public static final Creator<Observable> CREATOR = new Creator<Observable>() {
- @Override
- public Observable createFromParcel(final Parcel in) {
- return new Observable(in);
- }
-
- @Override
- public Observable[] newArray(final int size) {
- return new Observable[size];
- }
- };
-
- @Override
- public int describeContents() {
- return 0;
- }
-
- @Override
- public void writeToParcel(final Parcel dest, final int flags) {
- dest.writeString(addresses);
- dest.writeString(dnses);
- dest.writeString(publicKey);
- dest.writeString(privateKey);
- dest.writeString(listenPort);
- dest.writeString(mtu);
- }
-
- private Observable(final Parcel in) {
- addresses = in.readString();
- dnses = in.readString();
- publicKey = in.readString();
- privateKey = in.readString();
- listenPort = in.readString();
- mtu = in.readString();
- }
- }
-
private List<IPCidr> addressList;
private List<InetAddress> dnsList;
private Keypair keypair;
private int listenPort;
private int mtu;
-
public Interface() {
addressList = new LinkedList<>();
dnsList = new LinkedList<>();
}
+ private void addAddresses(String[] addresses) {
+ if (addresses != null && addresses.length > 0) {
+ for (final String addr : addresses) {
+ if (addr.isEmpty())
+ throw new IllegalArgumentException("Address is empty");
+ this.addressList.add(new IPCidr(addr));
+ }
+ }
+ }
+
+ private void addDnses(String[] dnses) {
+ if (dnses != null && dnses.length > 0) {
+ for (final String dns : dnses) {
+ this.dnsList.add(Attribute.parseIPString(dns));
+ }
+ }
+ }
+
private String getAddressString() {
if (addressList.isEmpty())
return null;
@@ -179,6 +55,12 @@ public class Interface {
return addressList.toArray(new IPCidr[addressList.size()]);
}
+ private String getDnsString() {
+ if (dnsList.isEmpty())
+ return null;
+ return Attribute.listToString(getDnsStrings());
+ }
+
private List<String> getDnsStrings() {
List<String> strings = new LinkedList<>();
for (final InetAddress addr : dnsList)
@@ -186,12 +68,6 @@ public class Interface {
return strings;
}
- private String getDnsString() {
- if (dnsList.isEmpty())
- return null;
- return Attribute.listToString(getDnsStrings());
- }
-
public InetAddress[] getDnses() {
return dnsList.toArray(new InetAddress[dnsList.size()]);
}
@@ -244,29 +120,11 @@ public class Interface {
throw new IllegalArgumentException(line);
}
- private void addAddresses(String[] addresses) {
- if (addresses != null && addresses.length > 0) {
- for (final String addr : addresses) {
- if (addr.isEmpty())
- throw new IllegalArgumentException("Address is empty");
- this.addressList.add(new IPCidr(addr));
- }
- }
- }
-
private void setAddressString(final String addressString) {
this.addressList.clear();
addAddresses(Attribute.stringToList(addressString));
}
- private void addDnses(String[] dnses) {
- if (dnses != null && dnses.length > 0) {
- for (final String dns : dnses) {
- this.dnsList.add(Attribute.parseIPString(dns));
- }
- }
- }
-
private void setDnsString(final String dnsString) {
this.dnsList.clear();
addDnses(Attribute.stringToList(dnsString));
@@ -318,4 +176,143 @@ public class Interface {
sb.append(Attribute.PRIVATE_KEY.composeWith(keypair.getPrivateKey()));
return sb.toString();
}
+
+ public static class Observable extends BaseObservable implements Parcelable {
+ public static final Creator<Observable> CREATOR = new Creator<Observable>() {
+ @Override
+ public Observable createFromParcel(final Parcel in) {
+ return new Observable(in);
+ }
+
+ @Override
+ public Observable[] newArray(final int size) {
+ return new Observable[size];
+ }
+ };
+ private String addresses;
+ private String dnses;
+ private String listenPort;
+ private String mtu;
+ private String privateKey;
+ private String publicKey;
+
+ public Observable(Interface parent) {
+ if (parent != null)
+ loadData(parent);
+ }
+
+ private Observable(final Parcel in) {
+ addresses = in.readString();
+ dnses = in.readString();
+ publicKey = in.readString();
+ privateKey = in.readString();
+ listenPort = in.readString();
+ mtu = in.readString();
+ }
+
+ public void commitData(Interface parent) {
+ parent.setAddressString(this.addresses);
+ parent.setDnsString(this.dnses);
+ parent.setPrivateKey(this.privateKey);
+ parent.setListenPortString(this.listenPort);
+ parent.setMtuString(this.mtu);
+ loadData(parent);
+ notifyChange();
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ public void generateKeypair() {
+ Keypair keypair = new Keypair();
+ privateKey = keypair.getPrivateKey();
+ publicKey = keypair.getPublicKey();
+ notifyPropertyChanged(BR.privateKey);
+ notifyPropertyChanged(BR.publicKey);
+ }
+
+ @Bindable
+ public String getAddresses() {
+ return addresses;
+ }
+
+ @Bindable
+ public String getDnses() {
+ return dnses;
+ }
+
+ @Bindable
+ public String getListenPort() {
+ return listenPort;
+ }
+
+ @Bindable
+ public String getMtu() {
+ return mtu;
+ }
+
+ @Bindable
+ public String getPrivateKey() {
+ return privateKey;
+ }
+
+ @Bindable
+ public String getPublicKey() {
+ return publicKey;
+ }
+
+ public void loadData(Interface parent) {
+ this.addresses = parent.getAddressString();
+ this.dnses = parent.getDnsString();
+ this.publicKey = parent.getPublicKey();
+ this.privateKey = parent.getPrivateKey();
+ this.listenPort = parent.getListenPortString();
+ this.mtu = parent.getMtuString();
+ }
+
+ public void setAddresses(String addresses) {
+ this.addresses = addresses;
+ notifyPropertyChanged(BR.addresses);
+ }
+
+ public void setDnses(String dnses) {
+ this.dnses = dnses;
+ notifyPropertyChanged(BR.dnses);
+ }
+
+ public void setListenPort(String listenPort) {
+ this.listenPort = listenPort;
+ notifyPropertyChanged(BR.listenPort);
+ }
+
+ public void setMtu(String mtu) {
+ this.mtu = mtu;
+ notifyPropertyChanged(BR.mtu);
+ }
+
+ public void setPrivateKey(String privateKey) {
+ this.privateKey = privateKey;
+
+ try {
+ this.publicKey = new Keypair(privateKey).getPublicKey();
+ } catch (IllegalArgumentException ignored) {
+ this.publicKey = "";
+ }
+
+ notifyPropertyChanged(BR.privateKey);
+ notifyPropertyChanged(BR.publicKey);
+ }
+
+ @Override
+ public void writeToParcel(final Parcel dest, final int flags) {
+ dest.writeString(addresses);
+ dest.writeString(dnses);
+ dest.writeString(publicKey);
+ dest.writeString(privateKey);
+ dest.writeString(listenPort);
+ dest.writeString(mtu);
+ }
+ }
}
diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java
index 327365b3..9f71089f 100644
--- a/app/src/main/java/com/wireguard/config/Peer.java
+++ b/app/src/main/java/com/wireguard/config/Peer.java
@@ -22,136 +22,26 @@ import java.util.Locale;
*/
public class Peer {
- public static class Observable extends BaseObservable implements Parcelable {
- private String allowedIPs;
- private String endpoint;
- private String persistentKeepalive;
- private String preSharedKey;
- private String publicKey;
-
- public Observable(Peer parent) {
- loadData(parent);
- }
- public static Observable newInstance() {
- return new Observable(new Peer());
- }
-
- public void loadData(Peer parent) {
- this.allowedIPs = parent.getAllowedIPsString();
- this.endpoint = parent.getEndpointString();
- this.persistentKeepalive = parent.getPersistentKeepaliveString();
- this.preSharedKey = parent.getPreSharedKey();
- this.publicKey = parent.getPublicKey();
- }
-
- public void commitData(Peer parent) {
- parent.setAllowedIPsString(this.allowedIPs);
- parent.setEndpointString(this.endpoint);
- parent.setPersistentKeepaliveString(this.persistentKeepalive);
- parent.setPreSharedKey(this.preSharedKey);
- parent.setPublicKey(this.publicKey);
- if (parent.getPublicKey() == null)
- throw new IllegalArgumentException("Peer public key may not be empty");
- loadData(parent);
- notifyChange();
- }
-
- @Bindable
- public String getAllowedIPs() {
- return allowedIPs;
- }
-
- public void setAllowedIPs(String allowedIPs) {
- this.allowedIPs = allowedIPs;
- notifyPropertyChanged(BR.allowedIPs);
- }
-
- @Bindable
- public String getEndpoint() {
- return endpoint;
- }
-
- public void setEndpoint(String endpoint) {
- this.endpoint = endpoint;
- notifyPropertyChanged(BR.endpoint);
- }
-
- @Bindable
- public String getPersistentKeepalive() {
- return persistentKeepalive;
- }
-
- public void setPersistentKeepalive(String persistentKeepalive) {
- this.persistentKeepalive = persistentKeepalive;
- notifyPropertyChanged(BR.persistentKeepalive);
- }
-
- @Bindable
- public String getPreSharedKey() {
- return preSharedKey;
- }
-
- public void setPreSharedKey(String preSharedKey) {
- this.preSharedKey = preSharedKey;
- notifyPropertyChanged(BR.preSharedKey);
- }
-
- @Bindable
- public String getPublicKey() {
- return publicKey;
- }
-
- public void setPublicKey(String publicKey) {
- this.publicKey = publicKey;
- notifyPropertyChanged(BR.publicKey);
- }
-
-
- public static final Creator<Observable> CREATOR = new Creator<Observable>() {
- @Override
- public Observable createFromParcel(final Parcel in) {
- return new Observable(in);
- }
-
- @Override
- public Observable[] newArray(final int size) {
- return new Observable[size];
- }
- };
-
- @Override
- public int describeContents() {
- return 0;
- }
-
- @Override
- public void writeToParcel(final Parcel dest, final int flags) {
- dest.writeString(allowedIPs);
- dest.writeString(endpoint);
- dest.writeString(persistentKeepalive);
- dest.writeString(preSharedKey);
- dest.writeString(publicKey);
- }
-
- private Observable(final Parcel in) {
- allowedIPs = in.readString();
- endpoint = in.readString();
- persistentKeepalive = in.readString();
- preSharedKey = in.readString();
- publicKey = in.readString();
- }
- }
-
private List<IPCidr> allowedIPsList;
private InetSocketAddress endpoint;
private int persistentKeepalive;
private String preSharedKey;
private String publicKey;
-
public Peer() {
allowedIPsList = new LinkedList<>();
}
+ private void addAllowedIPs(String[] allowedIPs) {
+ if (allowedIPs != null && allowedIPs.length > 0) {
+ for (final String allowedIP : allowedIPs) {
+ this.allowedIPsList.add(new IPCidr(allowedIP));
+ }
+ }
+ }
+
+ public IPCidr[] getAllowedIPs() {
+ return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
+ }
private String getAllowedIPsString() {
if (allowedIPsList.isEmpty())
@@ -159,10 +49,6 @@ public class Peer {
return Attribute.listToString(allowedIPsList);
}
- public IPCidr[] getAllowedIPs() {
- return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
- }
-
public InetSocketAddress getEndpoint() {
return endpoint;
}
@@ -173,24 +59,6 @@ public class Peer {
return String.format(Locale.getDefault(), "%s:%d", endpoint.getHostString(), endpoint.getPort());
}
- public String getResolvedEndpointString() throws UnknownHostException {
- if (endpoint == null)
- throw new UnknownHostException("{empty}");
- if (endpoint.isUnresolved())
- endpoint = new InetSocketAddress(endpoint.getHostString(), endpoint.getPort());
- if (endpoint.isUnresolved())
- throw new UnknownHostException(endpoint.getHostString());
- if (endpoint.getAddress() instanceof Inet6Address)
- return String.format(Locale.getDefault(),
- "[%s]:%d",
- endpoint.getAddress().getHostAddress(),
- endpoint.getPort());
- return String.format(Locale.getDefault(),
- "%s:%d",
- endpoint.getAddress().getHostAddress(),
- endpoint.getPort());
- }
-
public int getPersistentKeepalive() {
return persistentKeepalive;
}
@@ -209,6 +77,24 @@ public class Peer {
return publicKey;
}
+ public String getResolvedEndpointString() throws UnknownHostException {
+ if (endpoint == null)
+ throw new UnknownHostException("{empty}");
+ if (endpoint.isUnresolved())
+ endpoint = new InetSocketAddress(endpoint.getHostString(), endpoint.getPort());
+ if (endpoint.isUnresolved())
+ throw new UnknownHostException(endpoint.getHostString());
+ if (endpoint.getAddress() instanceof Inet6Address)
+ return String.format(Locale.getDefault(),
+ "[%s]:%d",
+ endpoint.getAddress().getHostAddress(),
+ endpoint.getPort());
+ return String.format(Locale.getDefault(),
+ "%s:%d",
+ endpoint.getAddress().getHostAddress(),
+ endpoint.getPort());
+ }
+
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == Attribute.ALLOWED_IPS)
@@ -225,14 +111,6 @@ public class Peer {
throw new IllegalArgumentException(line);
}
- private void addAllowedIPs(String[] allowedIPs) {
- if (allowedIPs != null && allowedIPs.length > 0) {
- for (final String allowedIP : allowedIPs) {
- this.allowedIPsList.add(new IPCidr(allowedIP));
- }
- }
- }
-
private void setAllowedIPsString(final String allowedIPsString) {
this.allowedIPsList.clear();
addAllowedIPs(Attribute.stringToList(allowedIPsString));
@@ -246,7 +124,7 @@ public class Peer {
if (endpoint != null && !endpoint.isEmpty()) {
InetSocketAddress constructedEndpoint;
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
- throw new IllegalArgumentException("Forbidden characters in endpoint");
+ throw new IllegalArgumentException("Forbidden characters in endpoint");
URI uri;
try {
uri = new URI("wg://" + endpoint);
@@ -301,4 +179,123 @@ public class Peer {
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
return sb.toString();
}
+
+ public static class Observable extends BaseObservable implements Parcelable {
+ public static final Creator<Observable> CREATOR = new Creator<Observable>() {
+ @Override
+ public Observable createFromParcel(final Parcel in) {
+ return new Observable(in);
+ }
+
+ @Override
+ public Observable[] newArray(final int size) {
+ return new Observable[size];
+ }
+ };
+ private String allowedIPs;
+ private String endpoint;
+ private String persistentKeepalive;
+ private String preSharedKey;
+ private String publicKey;
+
+ public Observable(Peer parent) {
+ loadData(parent);
+ }
+
+ private Observable(final Parcel in) {
+ allowedIPs = in.readString();
+ endpoint = in.readString();
+ persistentKeepalive = in.readString();
+ preSharedKey = in.readString();
+ publicKey = in.readString();
+ }
+
+ public static Observable newInstance() {
+ return new Observable(new Peer());
+ }
+
+ public void commitData(Peer parent) {
+ parent.setAllowedIPsString(this.allowedIPs);
+ parent.setEndpointString(this.endpoint);
+ parent.setPersistentKeepaliveString(this.persistentKeepalive);
+ parent.setPreSharedKey(this.preSharedKey);
+ parent.setPublicKey(this.publicKey);
+ if (parent.getPublicKey() == null)
+ throw new IllegalArgumentException("Peer public key may not be empty");
+ loadData(parent);
+ notifyChange();
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Bindable
+ public String getAllowedIPs() {
+ return allowedIPs;
+ }
+
+ @Bindable
+ public String getEndpoint() {
+ return endpoint;
+ }
+
+ @Bindable
+ public String getPersistentKeepalive() {
+ return persistentKeepalive;
+ }
+
+ @Bindable
+ public String getPreSharedKey() {
+ return preSharedKey;
+ }
+
+ @Bindable
+ public String getPublicKey() {
+ return publicKey;
+ }
+
+ public void loadData(Peer parent) {
+ this.allowedIPs = parent.getAllowedIPsString();
+ this.endpoint = parent.getEndpointString();
+ this.persistentKeepalive = parent.getPersistentKeepaliveString();
+ this.preSharedKey = parent.getPreSharedKey();
+ this.publicKey = parent.getPublicKey();
+ }
+
+ 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 setPreSharedKey(String preSharedKey) {
+ this.preSharedKey = preSharedKey;
+ notifyPropertyChanged(BR.preSharedKey);
+ }
+
+ public void setPublicKey(String publicKey) {
+ this.publicKey = publicKey;
+ notifyPropertyChanged(BR.publicKey);
+ }
+
+ @Override
+ public void writeToParcel(final Parcel dest, final int flags) {
+ dest.writeString(allowedIPs);
+ dest.writeString(endpoint);
+ dest.writeString(persistentKeepalive);
+ dest.writeString(preSharedKey);
+ dest.writeString(publicKey);
+ }
+ }
}