More javafication

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-04-18 05:28:31 +02:00
parent 69f1a35a3f
commit 53d29b312f
9 changed files with 343 additions and 197 deletions

View File

@ -11,6 +11,7 @@ import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State; import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.model.Tunnel.Statistics; import com.wireguard.android.model.Tunnel.Statistics;
import com.wireguard.config.Config; import com.wireguard.config.Config;
import com.wireguard.config.IPCidr;
import com.wireguard.config.Interface; import com.wireguard.config.Interface;
import com.wireguard.config.Peer; import com.wireguard.config.Peer;
import com.wireguard.crypto.KeyEncoding; import com.wireguard.crypto.KeyEncoding;
@ -124,57 +125,6 @@ public final class GoBackend implements Backend {
return getState(tunnel); return getState(tunnel);
} }
private String parseEndpoint(String string) throws Exception {
String[] part;
if (string.charAt(0) == '[') {
int end = string.indexOf(']');
if (end == -1 || end >= string.length() - 2 || string.charAt(end + 1) != ':')
throw new Exception("Invalid endpoint " + string);
part = new String[2];
part[0] = string.substring(1, end);
part[1] = string.substring(end + 2);
} else {
part = string.split(":", 2);
}
if (part.length != 2 || part[0].isEmpty() || part[1].isEmpty())
throw new Exception("Invalid endpoint " + string);
InetAddress address = InetAddress.getByName(part[0]);
int port = -1;
try {
port = Integer.parseInt(part[1], 10);
} catch (Exception e) {
}
if (port < 1)
throw new Exception("Invalid endpoint port " + part[1]);
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
if (socketAddress.getAddress() instanceof Inet4Address)
return socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort();
else
return "[" + socketAddress.getAddress().getHostAddress() + "]:" + socketAddress.getPort();
}
private Pair<String, Integer> parseAddressWithCidr(String in) {
int cidr = -1;
String addr = in;
int slash = in.lastIndexOf('/');
if (slash != -1 && slash < in.length() - 1) {
try {
cidr = Integer.parseInt(in.substring(slash + 1), 10);
addr = in.substring(0, slash);
} catch (Exception e) {
}
}
boolean isV6 = addr.indexOf(':') != -1;
if (isV6 && (cidr > 128 || cidr < 0))
cidr = 128;
else if (!isV6 && (cidr > 32 || cidr < 0))
cidr = 32;
return new Pair<>(addr, cidr);
}
private void setStateInternal(final Tunnel tunnel, final Config config, final State state) private void setStateInternal(final Tunnel tunnel, final Config config, final State state)
throws Exception { throws Exception {
@ -205,20 +155,19 @@ public final class GoBackend implements Backend {
fmt.format("replace_peers=true\n"); fmt.format("replace_peers=true\n");
if (iface.getPrivateKey() != null) if (iface.getPrivateKey() != null)
fmt.format("private_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(iface.getPrivateKey()))); fmt.format("private_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(iface.getPrivateKey())));
if (iface.getListenPort() != null) if (iface.getListenPort() != 0)
fmt.format("listen_port=%d\n", Integer.parseInt(config.getInterface().getListenPort())); fmt.format("listen_port=%d\n", config.getInterface().getListenPort());
for (final Peer peer : config.getPeers()) { for (final Peer peer : config.getPeers()) {
if (peer.getPublicKey() != null) if (peer.getPublicKey() != null)
fmt.format("public_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPublicKey()))); fmt.format("public_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPublicKey())));
if (peer.getPreSharedKey() != null) if (peer.getPreSharedKey() != null)
fmt.format("preshared_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPreSharedKey()))); fmt.format("preshared_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPreSharedKey())));
if (peer.getEndpoint() != null) if (peer.getEndpoint() != null)
fmt.format("endpoint=%s\n", parseEndpoint(peer.getEndpoint())); fmt.format("endpoint=%s\n", peer.getResolvedEndpointString());
if (peer.getPersistentKeepalive() != null) if (peer.getPersistentKeepalive() != 0)
fmt.format("persistent_keepalive_interval=%d\n", Integer.parseInt(peer.getPersistentKeepalive(), 10)); fmt.format("persistent_keepalive_interval=%d\n", peer.getPersistentKeepalive());
for (final String addr : peer.getAllowedIPs()) { for (final IPCidr addr : peer.getAllowedIPs()) {
final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr); fmt.format("allowed_ip=%s\n", addr.toString());
fmt.format("allowed_ip=%s\n", addressCidr.first + "/" + addressCidr.second);
} }
} }
@ -226,30 +175,19 @@ public final class GoBackend implements Backend {
VpnService.Builder builder = service.getBuilder(); VpnService.Builder builder = service.getBuilder();
builder.setSession(tunnel.getName()); builder.setSession(tunnel.getName());
for (final String addr : config.getInterface().getAddresses()) { for (final IPCidr addr : config.getInterface().getAddresses())
final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr); builder.addAddress(addr.getAddress(), addr.getCidr());
final InetAddress address = InetAddress.getByName(addressCidr.first);
builder.addAddress(address.getHostAddress(), addressCidr.second);
}
for (final String addr : config.getInterface().getDnses()) { for (final InetAddress addr : config.getInterface().getDnses())
final InetAddress address = InetAddress.getByName(addr); builder.addDnsServer(addr.getHostAddress());
builder.addDnsServer(address.getHostAddress());
}
for (final Peer peer : config.getPeers()) { for (final Peer peer : config.getPeers()) {
for (final String addr : peer.getAllowedIPs()) { for (final IPCidr addr : peer.getAllowedIPs())
final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr); builder.addRoute(addr.getAddress(), addr.getCidr());
builder.addRoute(addressCidr.first, addressCidr.second);
}
} }
int mtu = -1; int mtu = config.getInterface().getMtu();
try { if (mtu == 0)
mtu = Integer.parseInt(config.getInterface().getMtu(), 10);
} catch (Exception e) {
}
if (mtu < 0)
mtu = 1280; mtu = 1280;
builder.setMtu(mtu); builder.setMtu(mtu);

View File

@ -3,6 +3,7 @@ package com.wireguard.config;
import android.text.TextUtils; import android.text.TextUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -45,19 +46,23 @@ enum Attribute {
return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]); return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]);
} }
public static String listToString(final String[] list) { public static <T> String listToString(final List<T> list) {
return TextUtils.join(", ", list); return TextUtils.join(", ", list);
} }
public static String[] stringToList(final String string) { public static String[] stringToList(final String string) {
return string.trim().split("\\s*,\\s*"); return string.trim().split("\\s*,\\s*", -1);
} }
public String composeWith(final Object value) { public String composeWith(final Object value) {
return String.format("%s = %s%n", token, value); return String.format("%s = %s%n", token, value);
} }
public String composeWith(final String[] value) { public String composeWith(final int value) {
return String.format("%s = %d%n", token, value);
}
public <T> String composeWith(final List<T> value) {
return String.format("%s = %s%n", token, listToString(value)); return String.format("%s = %s%n", token, listToString(value));
} }

View File

@ -0,0 +1,79 @@
package com.wireguard.config;
import android.os.Parcel;
import android.os.Parcelable;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPCidr implements Parcelable {
InetAddress address;
int cidr;
public static final Parcelable.Creator<IPCidr> CREATOR = new Parcelable.Creator<IPCidr>() {
@Override
public IPCidr createFromParcel(final Parcel in) {
return new IPCidr(in);
}
@Override
public IPCidr[] newArray(final int size) {
return new IPCidr[size];
}
};
public IPCidr(String in) throws UnknownHostException {
parse(in);
}
private void parse(String in) throws UnknownHostException {
cidr = -1;
int slash = in.lastIndexOf('/');
if (slash != -1 && slash < in.length() - 1) {
try {
cidr = Integer.parseInt(in.substring(slash + 1), 10);
in = in.substring(0, slash);
} catch (Exception e) {
}
}
address = InetAddress.getByName(in);
if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0))
cidr = 128;
else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0))
cidr = 32;
}
public InetAddress getAddress() {
return address;
}
public int getCidr() {
return cidr;
}
@Override
public String toString() {
return String.format("%s/%d", address.getHostAddress(), cidr);
}
@Override
public void writeToParcel(final Parcel dest, final int flags) {
dest.writeString(this.toString());
}
@Override
public int describeContents() {
return 0;
}
private IPCidr(final Parcel in) {
try {
parse(in.readString());
} catch (Exception e) {
}
}
}

View File

@ -9,6 +9,11 @@ import com.wireguard.android.BR;
import com.wireguard.crypto.KeyEncoding; import com.wireguard.crypto.KeyEncoding;
import com.wireguard.crypto.Keypair; import com.wireguard.crypto.Keypair;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
/** /**
* Represents the configuration for a WireGuard interface (an [Interface] block). * Represents the configuration for a WireGuard interface (an [Interface] block).
*/ */
@ -26,23 +31,30 @@ public class Interface extends BaseObservable implements Parcelable {
} }
}; };
private String[] addressList; private List<IPCidr> addressList;
private String[] dnsList; private List<InetAddress> dnsList;
private Keypair keypair; private Keypair keypair;
private String listenPort; private int listenPort;
private String mtu; private int mtu;
private String privateKey; private String privateKey;
public Interface() { public Interface() {
addressList = new String[0]; addressList = new LinkedList<>();
dnsList = new String[0]; dnsList = new LinkedList<>();
} }
private Interface(final Parcel in) { private Interface(final Parcel in) {
addressList = in.createStringArray(); addressList = in.createTypedArrayList(IPCidr.CREATOR);
dnsList = in.createStringArray(); int dnsItems = in.readInt();
listenPort = in.readString(); dnsList = new LinkedList<>();
mtu = in.readString(); for (int i = 0; i < dnsItems; ++i) {
try {
dnsList.add(InetAddress.getByAddress(in.createByteArray()));
} catch (Exception e) {
}
}
listenPort = in.readInt();
mtu = in.readInt();
setPrivateKey(in.readString()); setPrivateKey(in.readString());
} }
@ -60,34 +72,59 @@ public class Interface extends BaseObservable implements Parcelable {
@Bindable @Bindable
public String getAddressString() { public String getAddressString() {
if (addressList.isEmpty())
return null;
return Attribute.listToString(addressList); return Attribute.listToString(addressList);
} }
@Bindable @Bindable
public String[] getAddresses() { public IPCidr[] getAddresses() {
return addressList; return addressList.toArray(new IPCidr[addressList.size()]);
}
public List<String> getDnsStrings() {
List<String> strings = new LinkedList<>();
for (final InetAddress addr : dnsList)
strings.add(addr.getHostAddress());
return strings;
} }
@Bindable @Bindable
public String getDnsString() { public String getDnsString() {
return Attribute.listToString(dnsList); if (dnsList.isEmpty())
return null;
return Attribute.listToString(getDnsStrings());
} }
@Bindable @Bindable
public String[] getDnses() { public InetAddress[] getDnses() {
return dnsList; return dnsList.toArray(new InetAddress[dnsList.size()]);
} }
@Bindable @Bindable
public String getListenPort() { public int getListenPort() {
return listenPort; return listenPort;
} }
@Bindable @Bindable
public String getMtu() { public String getListenPortString() {
if (listenPort == 0)
return null;
return new Integer(listenPort).toString();
}
@Bindable
public int getMtu() {
return mtu; return mtu;
} }
@Bindable
public String getMtuString() {
if (mtu == 0)
return null;
return new Integer(mtu).toString();
}
@Bindable @Bindable
public String getPrivateKey() { public String getPrivateKey() {
return privateKey; return privateKey;
@ -98,74 +135,90 @@ public class Interface extends BaseObservable implements Parcelable {
return keypair != null ? keypair.getPublicKey() : null; return keypair != null ? keypair.getPublicKey() : null;
} }
public void parse(final String line) { public void parse(final String line) throws UnknownHostException {
final Attribute key = Attribute.match(line); final Attribute key = Attribute.match(line);
if (key == Attribute.ADDRESS) if (key == Attribute.ADDRESS)
addAddresses(key.parseList(line)); addAddresses(key.parseList(line));
else if (key == Attribute.DNS) else if (key == Attribute.DNS)
addDnses(key.parseList(line)); addDnses(key.parseList(line));
else if (key == Attribute.LISTEN_PORT) else if (key == Attribute.LISTEN_PORT)
setListenPort(key.parse(line)); setListenPortString(key.parse(line));
else if (key == Attribute.MTU) else if (key == Attribute.MTU)
setMtu(key.parse(line)); setMtuString(key.parse(line));
else if (key == Attribute.PRIVATE_KEY) else if (key == Attribute.PRIVATE_KEY)
setPrivateKey(key.parse(line)); setPrivateKey(key.parse(line));
else else
throw new IllegalArgumentException(line); throw new IllegalArgumentException(line);
} }
public void addAddresses(String[] addresses) { public void addAddresses(String[] addresses) throws UnknownHostException {
if (addresses == null || addresses.length == 0) if (addresses != null && addresses.length > 0) {
return; for (final String addr : addresses) {
String[] both = new String[addresses.length + this.addressList.length]; if (addr.isEmpty())
System.arraycopy(this.addressList, 0, both, 0, this.addressList.length); throw new UnknownHostException("{empty}");
System.arraycopy(addresses, 0, both, this.addressList.length, addresses.length); this.addressList.add(new IPCidr(addr));
setAddresses(both); }
} }
public void setAddresses(String[] addresses) {
if (addresses == null)
addresses = new String[0];
this.addressList = addresses;
notifyPropertyChanged(BR.addresses); notifyPropertyChanged(BR.addresses);
notifyPropertyChanged(BR.addressString);
} }
public void setAddressString(String addressString) { public void setAddressString(final String addressString) {
setAddresses(Attribute.stringToList(addressString)); this.addressList.clear();
try {
addAddresses(Attribute.stringToList(addressString));
} catch (Exception e) {
this.addressList.clear();
}
} }
public void addDnses(String[] dnses) { public void addDnses(String[] dnses) throws UnknownHostException {
if (dnses == null || dnses.length == 0) if (dnses != null && dnses.length > 0) {
return; for (final String dns : dnses) {
String[] both = new String[dnses.length + this.dnsList.length]; if (dns.isEmpty())
System.arraycopy(this.dnsList, 0, both, 0, this.dnsList.length); throw new UnknownHostException("{empty}");
System.arraycopy(dnses, 0, both, this.dnsList.length, dnses.length); this.dnsList.add(InetAddress.getByName(dns));
setDnses(both); }
} }
public void setDnses(String[] dnses) {
if (dnses == null)
dnses = new String[0];
this.dnsList = dnses;
notifyPropertyChanged(BR.dnses); notifyPropertyChanged(BR.dnses);
notifyPropertyChanged(BR.dnsString);
} }
public void setDnsString(String dnsString) { public void setDnsString(final String dnsString) {
setDnses(Attribute.stringToList(dnsString)); try {
this.dnsList.clear();
addDnses(Attribute.stringToList(dnsString));
} catch (Exception e) {
this.dnsList.clear();
}
} }
public void setListenPort(String listenPort) { public void setListenPort(int listenPort) {
if (listenPort != null && listenPort.isEmpty())
listenPort = null;
this.listenPort = listenPort; this.listenPort = listenPort;
notifyPropertyChanged(BR.listenPort); notifyPropertyChanged(BR.listenPort);
notifyPropertyChanged(BR.listenPortString);
} }
public void setMtu(String mtu) { public void setListenPortString(final String port) {
if (mtu != null && mtu.isEmpty()) if (port != null && !port.isEmpty())
mtu = null; setListenPort(Integer.parseInt(port, 10));
else
setListenPort(0);
}
public void setMtu(int mtu) {
this.mtu = mtu; this.mtu = mtu;
notifyPropertyChanged(BR.mtu); notifyPropertyChanged(BR.mtu);
notifyPropertyChanged(BR.mtuString);
}
public void setMtuString(final String mtu) {
if (mtu != null && !mtu.isEmpty())
setMtu(Integer.parseInt(mtu, 10));
else
setMtu(0);
} }
public void setPrivateKey(String privateKey) { public void setPrivateKey(String privateKey) {
@ -188,13 +241,13 @@ public class Interface extends BaseObservable implements Parcelable {
@Override @Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder().append("[Interface]\n"); final StringBuilder sb = new StringBuilder().append("[Interface]\n");
if (addressList != null && addressList.length > 0) if (!addressList.isEmpty())
sb.append(Attribute.ADDRESS.composeWith(addressList)); sb.append(Attribute.ADDRESS.composeWith(addressList));
if (dnsList != null && dnsList.length > 0) if (!dnsList.isEmpty())
sb.append(Attribute.DNS.composeWith(dnsList)); sb.append(Attribute.DNS.composeWith(getDnsStrings()));
if (listenPort != null) if (listenPort != 0)
sb.append(Attribute.LISTEN_PORT.composeWith(listenPort)); sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
if (mtu != null) if (mtu != 0)
sb.append(Attribute.MTU.composeWith(mtu)); sb.append(Attribute.MTU.composeWith(mtu));
if (privateKey != null) if (privateKey != null)
sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey)); sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey));
@ -203,10 +256,12 @@ public class Interface extends BaseObservable implements Parcelable {
@Override @Override
public void writeToParcel(final Parcel dest, final int flags) { public void writeToParcel(final Parcel dest, final int flags) {
dest.writeStringArray(addressList); dest.writeTypedList(addressList);
dest.writeStringArray(dnsList); dest.writeInt(dnsList.size());
dest.writeString(listenPort); for (final InetAddress addr : dnsList)
dest.writeString(mtu); dest.writeByteArray(addr.getAddress());
dest.writeInt(listenPort);
dest.writeInt(mtu);
dest.writeString(privateKey); dest.writeString(privateKey);
} }
} }

View File

@ -7,6 +7,14 @@ import android.os.Parcelable;
import com.android.databinding.library.baseAdapters.BR; import com.android.databinding.library.baseAdapters.BR;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
/** /**
* Represents the configuration for a WireGuard peer (a [Peer] block). * Represents the configuration for a WireGuard peer (a [Peer] block).
*/ */
@ -24,22 +32,29 @@ public class Peer extends BaseObservable implements Parcelable {
} }
}; };
private String[] allowedIPList; private List<IPCidr> allowedIPsList;
private String endpoint; private InetSocketAddress endpoint;
private String persistentKeepalive; private int persistentKeepalive;
private String preSharedKey; private String preSharedKey;
private String publicKey; private String publicKey;
public Peer() { public Peer() {
allowedIPList = new String[0]; allowedIPsList = new LinkedList<>();
} }
private Peer(final Parcel in) { private Peer(final Parcel in) {
allowedIPList = in.createStringArray(); allowedIPsList = in.createTypedArrayList(IPCidr.CREATOR);
endpoint = in.readString(); String host = in.readString();
persistentKeepalive = in.readString(); int port = in.readInt();
if (host != null && !host.isEmpty() && port > 0)
endpoint = InetSocketAddress.createUnresolved(host, port);
persistentKeepalive = in.readInt();
preSharedKey = in.readString(); preSharedKey = in.readString();
if (preSharedKey != null && preSharedKey.isEmpty())
preSharedKey = null;
publicKey = in.readString(); publicKey = in.readString();
if (publicKey != null && publicKey.isEmpty())
publicKey = null;
} }
public static Peer newInstance() { public static Peer newInstance() {
@ -53,23 +68,53 @@ public class Peer extends BaseObservable implements Parcelable {
@Bindable @Bindable
public String getAllowedIPsString() { return Attribute.listToString(allowedIPList); } public String getAllowedIPsString() {
if (allowedIPsList.isEmpty())
@Bindable return null;
public String[] getAllowedIPs() { return Attribute.listToString(allowedIPsList);
return allowedIPList;
} }
@Bindable @Bindable
public String getEndpoint() { public IPCidr[] getAllowedIPs() {
return allowedIPsList.toArray(new IPCidr[allowedIPsList.size()]);
}
@Bindable
public InetSocketAddress getEndpoint() {
return endpoint; return endpoint;
} }
@Bindable @Bindable
public String getPersistentKeepalive() { public String getEndpointString() {
if (endpoint == null)
return null;
return String.format("%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("[%s]:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
return String.format("%s:%d", endpoint.getAddress().getHostAddress(), endpoint.getPort());
}
@Bindable
public int getPersistentKeepalive() {
return persistentKeepalive; return persistentKeepalive;
} }
@Bindable
public String getPersistentKeepaliveString() {
if (persistentKeepalive == 0)
return null;
return new Integer(persistentKeepalive).toString();
}
@Bindable @Bindable
public String getPreSharedKey() { public String getPreSharedKey() {
return preSharedKey; return preSharedKey;
@ -80,14 +125,14 @@ public class Peer extends BaseObservable implements Parcelable {
return publicKey; return publicKey;
} }
public void parse(final String line) { public void parse(final String line) throws UnknownHostException {
final Attribute key = Attribute.match(line); final Attribute key = Attribute.match(line);
if (key == Attribute.ALLOWED_IPS) if (key == Attribute.ALLOWED_IPS)
addAllowedIPs(key.parseList(line)); addAllowedIPs(key.parseList(line));
else if (key == Attribute.ENDPOINT) else if (key == Attribute.ENDPOINT)
setEndpoint(key.parse(line)); setEndpointString(key.parse(line));
else if (key == Attribute.PERSISTENT_KEEPALIVE) else if (key == Attribute.PERSISTENT_KEEPALIVE)
setPersistentKeepalive(key.parse(line)); setPersistentKeepaliveString(key.parse(line));
else if (key == Attribute.PRESHARED_KEY) else if (key == Attribute.PRESHARED_KEY)
setPreSharedKey(key.parse(line)); setPreSharedKey(key.parse(line));
else if (key == Attribute.PUBLIC_KEY) else if (key == Attribute.PUBLIC_KEY)
@ -96,38 +141,60 @@ public class Peer extends BaseObservable implements Parcelable {
throw new IllegalArgumentException(line); throw new IllegalArgumentException(line);
} }
public void addAllowedIPs(String[] allowedIPs) { public void addAllowedIPs(String[] allowedIPs) throws UnknownHostException {
if (allowedIPs == null || allowedIPs.length == 0) if (allowedIPs != null && allowedIPs.length > 0) {
return; for (final String allowedIP : allowedIPs) {
String[] both = new String[allowedIPs.length + this.allowedIPList.length]; if (allowedIP.isEmpty())
System.arraycopy(this.allowedIPList, 0, both, 0, this.allowedIPList.length); throw new UnknownHostException("{empty}");
System.arraycopy(allowedIPs, 0, both, this.allowedIPList.length, allowedIPs.length); this.allowedIPsList.add(new IPCidr(allowedIP));
setAllowedIPs(both); }
} }
public void setAllowedIPs(String[] allowedIPs) {
if (allowedIPs == null)
allowedIPs = new String[0];
this.allowedIPList = allowedIPs;
notifyPropertyChanged(BR.allowedIPs); notifyPropertyChanged(BR.allowedIPs);
notifyPropertyChanged(BR.allowedIPsString);
} }
public void setAllowedIPsString(String allowedIPsString) { public void setAllowedIPsString(final String allowedIPsString) {
setAllowedIPs(Attribute.stringToList(allowedIPsString)); try {
this.allowedIPsList.clear();
addAllowedIPs(Attribute.stringToList(allowedIPsString));
} catch (Exception e) {
this.allowedIPsList.clear();
}
} }
public void setEndpoint(String endpoint) { public void setEndpoint(InetSocketAddress endpoint) {
if (endpoint != null && endpoint.isEmpty())
endpoint = null;
this.endpoint = endpoint; this.endpoint = endpoint;
notifyPropertyChanged(BR.endpoint); notifyPropertyChanged(BR.endpoint);
notifyPropertyChanged(BR.endpointString);
} }
public void setPersistentKeepalive(String persistentKeepalive) { public void setEndpointString(final String endpoint) {
if (persistentKeepalive != null && persistentKeepalive.isEmpty()) if (endpoint != null && !endpoint.isEmpty()) {
persistentKeepalive = null; InetSocketAddress constructedEndpoint;
try {
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
throw new Exception();
URI uri = new URI("wg://" + endpoint);
constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
} catch (Exception e) {
return; /* XXX: Uh oh. */
}
setEndpoint(constructedEndpoint);
} else
setEndpoint(null);
}
public void setPersistentKeepalive(int persistentKeepalive) {
this.persistentKeepalive = persistentKeepalive; this.persistentKeepalive = persistentKeepalive;
notifyPropertyChanged(BR.persistentKeepalive); notifyPropertyChanged(BR.persistentKeepalive);
notifyPropertyChanged(BR.persistentKeepaliveString);
}
public void setPersistentKeepaliveString(String persistentKeepalive) {
if (persistentKeepalive != null && !persistentKeepalive.isEmpty())
setPersistentKeepalive(Integer.parseInt(persistentKeepalive, 10));
else
setPersistentKeepalive(0);
} }
public void setPreSharedKey(String preSharedKey) { public void setPreSharedKey(String preSharedKey) {
@ -147,11 +214,11 @@ public class Peer extends BaseObservable implements Parcelable {
@Override @Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder().append("[Peer]\n"); final StringBuilder sb = new StringBuilder().append("[Peer]\n");
if (allowedIPList != null) if (!allowedIPsList.isEmpty())
sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPList)); sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPsList));
if (endpoint != null) if (endpoint != null)
sb.append(Attribute.ENDPOINT.composeWith(endpoint)); sb.append(Attribute.ENDPOINT.composeWith(getEndpointString()));
if (persistentKeepalive != null) if (persistentKeepalive != 0)
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive)); sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
if (preSharedKey != null) if (preSharedKey != null)
sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey)); sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey));
@ -162,9 +229,10 @@ public class Peer extends BaseObservable implements Parcelable {
@Override @Override
public void writeToParcel(final Parcel dest, final int flags) { public void writeToParcel(final Parcel dest, final int flags) {
dest.writeStringArray(allowedIPList); dest.writeTypedList(allowedIPsList);
dest.writeString(endpoint); dest.writeString(endpoint == null ? null : endpoint.getHostString());
dest.writeString(persistentKeepalive); dest.writeInt(endpoint == null ? 0 : endpoint.getPort());
dest.writeInt(persistentKeepalive);
dest.writeString(preSharedKey); dest.writeString(preSharedKey);
dest.writeString(publicKey); dest.writeString(publicKey);
} }

View File

@ -86,6 +86,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/endpoint_label" android:layout_below="@+id/endpoint_label"
android:text="@{item.endpoint}" /> android:text="@{item.endpointString}" />
</RelativeLayout> </RelativeLayout>
</layout> </layout>

View File

@ -122,6 +122,7 @@
android:focusable="false" android:focusable="false"
android:hint="@string/hint_generated" android:hint="@string/hint_generated"
android:maxLines="1" android:maxLines="1"
android:contentDescription="@string/public_key_description"
android:onClick="@{ClipboardUtils::copyTextView}" android:onClick="@{ClipboardUtils::copyTextView}"
android:text="@{config.interface.publicKey}" /> android:text="@{config.interface.publicKey}" />
@ -164,7 +165,7 @@
android:layout_alignStart="@+id/generate_private_key_button" android:layout_alignStart="@+id/generate_private_key_button"
android:hint="@string/hint_random" android:hint="@string/hint_random"
android:inputType="number" android:inputType="number"
android:text="@={config.interface.listenPort}" android:text="@={config.interface.listenPortString}"
android:textAlignment="center" /> android:textAlignment="center" />
<TextView <TextView
@ -206,7 +207,7 @@
android:layout_alignStart="@+id/generate_private_key_button" android:layout_alignStart="@+id/generate_private_key_button"
android:hint="@string/hint_automatic" android:hint="@string/hint_automatic"
android:inputType="number" android:inputType="number"
android:text="@={config.interface.mtu}" android:text="@={config.interface.mtuString}"
android:textAlignment="center" /> android:textAlignment="center" />
</RelativeLayout> </RelativeLayout>

View File

@ -116,7 +116,7 @@
android:layout_below="@+id/endpoint_label" android:layout_below="@+id/endpoint_label"
android:layout_toStartOf="@+id/persistent_keepalive_text" android:layout_toStartOf="@+id/persistent_keepalive_text"
android:inputType="textNoSuggestions|textVisiblePassword" android:inputType="textNoSuggestions|textVisiblePassword"
android:text="@={item.endpoint}" /> android:text="@={item.endpointString}" />
<TextView <TextView
android:id="@+id/persistent_keepalive_label" android:id="@+id/persistent_keepalive_label"
@ -136,7 +136,7 @@
android:layout_alignStart="@+id/persistent_keepalive_label" android:layout_alignStart="@+id/persistent_keepalive_label"
android:hint="@string/hint_optional" android:hint="@string/hint_optional"
android:inputType="number" android:inputType="number"
android:text="@={item.persistentKeepalive}" android:text="@={item.persistentKeepaliveString}"
android:textAlignment="center" /> android:textAlignment="center" />
</RelativeLayout> </RelativeLayout>
</layout> </layout>

View File

@ -16,7 +16,7 @@
<string name="addresses">Addresses</string> <string name="addresses">Addresses</string>
<string name="allowed_ips">Allowed IPs</string> <string name="allowed_ips">Allowed IPs</string>
<string name="app_name">WireGuard</string> <string name="app_name">WireGuard</string>
<string name="config_save_error">Unable to configuration for “%s”: %s</string> <string name="config_save_error">Unable to save configuration for “%s”: %s</string>
<string name="config_save_success">Successfully saved configuration for “%s”</string> <string name="config_save_success">Successfully saved configuration for “%s”</string>
<string name="create_activity_title">Create WireGuard Tunnel</string> <string name="create_activity_title">Create WireGuard Tunnel</string>
<string name="create_empty">Create from scratch</string> <string name="create_empty">Create from scratch</string>