summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--app/src/main/java/com/wireguard/android/backend/GoBackend.java83
-rw-r--r--app/src/main/java/com/wireguard/config/Attribute.java19
-rw-r--r--app/src/main/java/com/wireguard/config/Interface.java97
-rw-r--r--app/src/main/java/com/wireguard/config/Peer.java43
-rw-r--r--app/src/main/res/layout/tunnel_detail_peer.xml2
-rw-r--r--app/src/main/res/layout/tunnel_editor_fragment.xml4
-rw-r--r--app/src/main/res/layout/tunnel_editor_peer.xml2
7 files changed, 151 insertions, 99 deletions
diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
index 6467ba2c..7b397f05 100644
--- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -21,7 +21,6 @@ import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Formatter;
import java.util.Set;
-import java.util.Vector;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -127,16 +126,15 @@ public final class GoBackend implements Backend {
private String parseEndpoint(String string) throws Exception {
String[] part;
- if (string.charAt(0) == '[') { // ipv6
+ if (string.charAt(0) == '[') {
int end = string.indexOf(']');
- if (end == -1 || string.charAt(end+1) != ':')
+ 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);
- Log.d(TAG, "PP " + part[0] + " " + part[1]);
- } else { // ipv4
+ } else {
part = string.split(":", 2);
}
@@ -144,7 +142,13 @@ public final class GoBackend implements Backend {
throw new Exception("Invalid endpoint " + string);
InetAddress address = InetAddress.getByName(part[0]);
- int port = Integer.valueOf(part[1]);
+ 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();
@@ -152,31 +156,23 @@ public final class GoBackend implements Backend {
return "[" + socketAddress.getAddress().getHostAddress() + "]:" + socketAddress.getPort();
}
- private Vector<Pair<String, Integer>> parseAllowedIps(String string) throws Exception {
- Vector<Pair<String, Integer>> ret = new Vector<>();
- for (final String allowedIp : string.split(" *, *")) {
- String[] part = allowedIp.split("/", 2);
- if (part.length > 2)
- throw new Exception("Invalid allowed ips string " + string);
-
+ 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 {
- InetAddress address = InetAddress.getByName(part[0]);
- int networkPrefixLength;
- if (part.length == 2) {
- networkPrefixLength = Integer.valueOf(part[1]);
- if (networkPrefixLength < 0 || networkPrefixLength > 128
- || (address instanceof Inet4Address && networkPrefixLength > 32))
- throw new Exception();
- } else {
- networkPrefixLength = (address instanceof Inet4Address) ? 32 : 128;
- }
-
- ret.add(new Pair<>(address.getHostAddress(), networkPrefixLength));
+ cidr = Integer.parseInt(in.substring(slash + 1), 10);
+ addr = in.substring(0, slash);
} catch (Exception e) {
- throw new Exception("Invalid allowed ips string " + string);
}
}
- return ret;
+ 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)
@@ -216,15 +212,13 @@ public final class GoBackend implements Backend {
fmt.format("public_key=%s\n", KeyEncoding.keyToHex(KeyEncoding.keyFromBase64(peer.getPublicKey())));
if (peer.getPreSharedKey() != null)
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()));
- }
if (peer.getPersistentKeepalive() != null)
- fmt.format("persistent_keepalive_interval=%d\n", Integer.parseInt(peer.getPersistentKeepalive()));
- if (peer.getAllowedIPs() != null) {
- for (final Pair<String, Integer> allowedIp : parseAllowedIps(peer.getAllowedIPs())) {
- fmt.format("allowed_ip=%s\n", allowedIp.first + "/" + allowedIp.second);
- }
+ fmt.format("persistent_keepalive_interval=%d\n", Integer.parseInt(peer.getPersistentKeepalive(), 10));
+ for (final String addr : peer.getAllowedIPs()) {
+ final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
+ fmt.format("allowed_ip=%s\n", addressCidr.first + "/" + addressCidr.second);
}
}
@@ -232,22 +226,21 @@ public final class GoBackend implements Backend {
VpnService.Builder builder = service.getBuilder();
builder.setSession(tunnel.getName());
- for (final String addressString : config.getInterface().getAddress().split(" *, *")) {
- InetAddress address = InetAddress.getByName(addressString);
- builder.addAddress(address.getHostAddress(), (address instanceof Inet4Address) ? 32 : 128);
+ for (final String addr : config.getInterface().getAddresses()) {
+ final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
+ final InetAddress address = InetAddress.getByName(addressCidr.first);
+ builder.addAddress(address.getHostAddress(), addressCidr.second);
}
- if (config.getInterface().getDns() != null) {
- for (final String dnsString : config.getInterface().getDns().split(" *, *")) {
- builder.addDnsServer(InetAddress.getByName(dnsString).getHostAddress());
- }
+ for (final String addr : config.getInterface().getDnses()) {
+ final InetAddress address = InetAddress.getByName(addr);
+ builder.addDnsServer(address.getHostAddress());
}
for (final Peer peer : config.getPeers()) {
- if (peer.getAllowedIPs() != null) {
- for (final Pair<String, Integer> allowedIp : parseAllowedIps(peer.getAllowedIPs())) {
- builder.addRoute(allowedIp.first, allowedIp.second);
- }
+ for (final String addr : peer.getAllowedIPs()) {
+ final Pair<String, Integer> addressCidr = parseAddressWithCidr(addr);
+ builder.addRoute(addressCidr.first, addressCidr.second);
}
}
diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java
index ee7fea33..50dbc38d 100644
--- a/app/src/main/java/com/wireguard/config/Attribute.java
+++ b/app/src/main/java/com/wireguard/config/Attribute.java
@@ -1,5 +1,7 @@
package com.wireguard.config;
+import android.text.TextUtils;
+
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
@@ -43,10 +45,22 @@ enum Attribute {
return KEY_MAP.get(SEPARATOR_PATTERN.split(line)[0]);
}
+ public static String listToString(final String[] list) {
+ return TextUtils.join(", ", list);
+ }
+
+ public static String[] stringToList(final String string) {
+ return string.trim().split("\\s*,\\s*");
+ }
+
public String composeWith(final Object value) {
return String.format("%s = %s%n", token, value);
}
+ public String composeWith(final String[] value) {
+ return String.format("%s = %s%n", token, listToString(value));
+ }
+
public String getToken() {
return token;
}
@@ -55,4 +69,9 @@ enum Attribute {
final Matcher matcher = pattern.matcher(line);
return matcher.matches() ? matcher.group(1) : null;
}
+
+ public String[] parseList(final CharSequence line) {
+ final Matcher matcher = pattern.matcher(line);
+ return matcher.matches() ? stringToList(matcher.group(1)) : null;
+ }
}
diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java
index b13f78a0..faf70cf6 100644
--- a/app/src/main/java/com/wireguard/config/Interface.java
+++ b/app/src/main/java/com/wireguard/config/Interface.java
@@ -26,20 +26,21 @@ public class Interface extends BaseObservable implements Parcelable {
}
};
- private String address;
- private String dns;
+ private String[] addressList;
+ private String[] dnsList;
private Keypair keypair;
private String listenPort;
private String mtu;
private String privateKey;
public Interface() {
- // Do nothing.
+ addressList = new String[0];
+ dnsList = new String[0];
}
private Interface(final Parcel in) {
- address = in.readString();
- dns = in.readString();
+ addressList = in.createStringArray();
+ dnsList = in.createStringArray();
listenPort = in.readString();
mtu = in.readString();
setPrivateKey(in.readString());
@@ -58,13 +59,23 @@ public class Interface extends BaseObservable implements Parcelable {
}
@Bindable
- public String getAddress() {
- return address;
+ public String getAddressString() {
+ return Attribute.listToString(addressList);
}
@Bindable
- public String getDns() {
- return dns;
+ public String[] getAddresses() {
+ return addressList;
+ }
+
+ @Bindable
+ public String getDnsString() {
+ return Attribute.listToString(dnsList);
+ }
+
+ @Bindable
+ public String[] getDnses() {
+ return dnsList;
}
@Bindable
@@ -90,9 +101,9 @@ public class Interface extends BaseObservable implements Parcelable {
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == Attribute.ADDRESS)
- addAddress(key.parse(line));
+ addAddresses(key.parseList(line));
else if (key == Attribute.DNS)
- addDns(key.parse(line));
+ addDnses(key.parseList(line));
else if (key == Attribute.LISTEN_PORT)
setListenPort(key.parse(line));
else if (key == Attribute.MTU)
@@ -103,32 +114,44 @@ public class Interface extends BaseObservable implements Parcelable {
throw new IllegalArgumentException(line);
}
- public void addAddress(String address) {
- if (address != null && address.isEmpty())
- address = null;
- setAddress((this.address == null) ? address
- : this.address + ", " + address);
+ public void addAddresses(String[] addresses) {
+ if (addresses == null || addresses.length == 0)
+ return;
+ String[] both = new String[addresses.length + this.addressList.length];
+ System.arraycopy(this.addressList, 0, both, 0, this.addressList.length);
+ System.arraycopy(addresses, 0, both, this.addressList.length, addresses.length);
+ setAddresses(both);
+ }
+
+ public void setAddresses(String[] addresses) {
+ if (addresses == null)
+ addresses = new String[0];
+ this.addressList = addresses;
+ notifyPropertyChanged(BR.addresses);
+ }
+
+ public void setAddressString(String addressString) {
+ setAddresses(Attribute.stringToList(addressString));
}
- public void setAddress(String address) {
- if (address != null && address.isEmpty())
- address = null;
- this.address = address;
- notifyPropertyChanged(BR.address);
+ public void addDnses(String[] dnses) {
+ if (dnses == null || dnses.length == 0)
+ return;
+ String[] both = new String[dnses.length + this.dnsList.length];
+ System.arraycopy(this.dnsList, 0, both, 0, this.dnsList.length);
+ System.arraycopy(dnses, 0, both, this.dnsList.length, dnses.length);
+ setDnses(both);
}
- public void addDns(String dns) {
- if (dns != null && dns.isEmpty())
- dns = null;
- setDns((this.dns == null) ? dns
- : this.dns + ", " + dns);
+ public void setDnses(String[] dnses) {
+ if (dnses == null)
+ dnses = new String[0];
+ this.dnsList = dnses;
+ notifyPropertyChanged(BR.dnses);
}
- public void setDns(String dns) {
- if (dns != null && dns.isEmpty())
- dns = null;
- this.dns = dns;
- notifyPropertyChanged(BR.dns);
+ public void setDnsString(String dnsString) {
+ setDnses(Attribute.stringToList(dnsString));
}
public void setListenPort(String listenPort) {
@@ -165,10 +188,10 @@ public class Interface extends BaseObservable implements Parcelable {
@Override
public String toString() {
final StringBuilder sb = new StringBuilder().append("[Interface]\n");
- if (address != null)
- sb.append(Attribute.ADDRESS.composeWith(address));
- if (dns != null)
- sb.append(Attribute.DNS.composeWith(dns));
+ if (addressList != null && addressList.length > 0)
+ sb.append(Attribute.ADDRESS.composeWith(addressList));
+ if (dnsList != null && dnsList.length > 0)
+ sb.append(Attribute.DNS.composeWith(dnsList));
if (listenPort != null)
sb.append(Attribute.LISTEN_PORT.composeWith(listenPort));
if (mtu != null)
@@ -180,8 +203,8 @@ public class Interface extends BaseObservable implements Parcelable {
@Override
public void writeToParcel(final Parcel dest, final int flags) {
- dest.writeString(address);
- dest.writeString(dns);
+ dest.writeStringArray(addressList);
+ dest.writeStringArray(dnsList);
dest.writeString(listenPort);
dest.writeString(mtu);
dest.writeString(privateKey);
diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java
index ac576d3e..954a220a 100644
--- a/app/src/main/java/com/wireguard/config/Peer.java
+++ b/app/src/main/java/com/wireguard/config/Peer.java
@@ -24,18 +24,18 @@ public class Peer extends BaseObservable implements Parcelable {
}
};
- private String allowedIPs;
+ private String[] allowedIPList;
private String endpoint;
private String persistentKeepalive;
private String preSharedKey;
private String publicKey;
public Peer() {
- // Do nothing.
+ allowedIPList = new String[0];
}
private Peer(final Parcel in) {
- allowedIPs = in.readString();
+ allowedIPList = in.createStringArray();
endpoint = in.readString();
persistentKeepalive = in.readString();
preSharedKey = in.readString();
@@ -51,9 +51,13 @@ public class Peer extends BaseObservable implements Parcelable {
return 0;
}
+
+ @Bindable
+ public String getAllowedIPsString() { return Attribute.listToString(allowedIPList); }
+
@Bindable
- public String getAllowedIPs() {
- return allowedIPs;
+ public String[] getAllowedIPs() {
+ return allowedIPList;
}
@Bindable
@@ -79,7 +83,7 @@ public class Peer extends BaseObservable implements Parcelable {
public void parse(final String line) {
final Attribute key = Attribute.match(line);
if (key == Attribute.ALLOWED_IPS)
- setAllowedIPs(key.parse(line));
+ addAllowedIPs(key.parseList(line));
else if (key == Attribute.ENDPOINT)
setEndpoint(key.parse(line));
else if (key == Attribute.PERSISTENT_KEEPALIVE)
@@ -92,13 +96,26 @@ public class Peer extends BaseObservable implements Parcelable {
throw new IllegalArgumentException(line);
}
- public void setAllowedIPs(String allowedIPs) {
- if (allowedIPs != null && allowedIPs.isEmpty())
- allowedIPs = null;
- this.allowedIPs = allowedIPs;
+ public void addAllowedIPs(String[] allowedIPs) {
+ if (allowedIPs == null || allowedIPs.length == 0)
+ return;
+ String[] both = new String[allowedIPs.length + this.allowedIPList.length];
+ System.arraycopy(this.allowedIPList, 0, both, 0, this.allowedIPList.length);
+ System.arraycopy(allowedIPs, 0, both, this.allowedIPList.length, allowedIPs.length);
+ setAllowedIPs(both);
+ }
+
+ public void setAllowedIPs(String[] allowedIPs) {
+ if (allowedIPs == null)
+ allowedIPs = new String[0];
+ this.allowedIPList = allowedIPs;
notifyPropertyChanged(BR.allowedIPs);
}
+ public void setAllowedIPsString(String allowedIPsString) {
+ setAllowedIPs(Attribute.stringToList(allowedIPsString));
+ }
+
public void setEndpoint(String endpoint) {
if (endpoint != null && endpoint.isEmpty())
endpoint = null;
@@ -130,8 +147,8 @@ public class Peer extends BaseObservable implements Parcelable {
@Override
public String toString() {
final StringBuilder sb = new StringBuilder().append("[Peer]\n");
- if (allowedIPs != null)
- sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPs));
+ if (allowedIPList != null)
+ sb.append(Attribute.ALLOWED_IPS.composeWith(allowedIPList));
if (endpoint != null)
sb.append(Attribute.ENDPOINT.composeWith(endpoint));
if (persistentKeepalive != null)
@@ -145,7 +162,7 @@ public class Peer extends BaseObservable implements Parcelable {
@Override
public void writeToParcel(final Parcel dest, final int flags) {
- dest.writeString(allowedIPs);
+ dest.writeStringArray(allowedIPList);
dest.writeString(endpoint);
dest.writeString(persistentKeepalive);
dest.writeString(preSharedKey);
diff --git a/app/src/main/res/layout/tunnel_detail_peer.xml b/app/src/main/res/layout/tunnel_detail_peer.xml
index 80ab5cff..d2b0d3d8 100644
--- a/app/src/main/res/layout/tunnel_detail_peer.xml
+++ b/app/src/main/res/layout/tunnel_detail_peer.xml
@@ -69,7 +69,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/allowed_ips_label"
- android:text="@{item.allowedIPs}" />
+ android:text="@{item.allowedIPsString}" />
<TextView
android:id="@+id/endpoint_label"
diff --git a/app/src/main/res/layout/tunnel_editor_fragment.xml b/app/src/main/res/layout/tunnel_editor_fragment.xml
index 69fd27f0..86c6082e 100644
--- a/app/src/main/res/layout/tunnel_editor_fragment.xml
+++ b/app/src/main/res/layout/tunnel_editor_fragment.xml
@@ -143,7 +143,7 @@
android:layout_below="@+id/addresses_label"
android:layout_toStartOf="@+id/listen_port_text"
android:inputType="textNoSuggestions|textVisiblePassword"
- android:text="@={config.interface.address}" />
+ android:text="@={config.interface.addressString}" />
<TextView
android:id="@+id/listen_port_label"
@@ -185,7 +185,7 @@
android:layout_below="@+id/dns_servers_label"
android:layout_toStartOf="@+id/mtu_text"
android:inputType="textNoSuggestions|textVisiblePassword"
- android:text="@={config.interface.dns}" />
+ android:text="@={config.interface.dnsString}" />
<TextView
android:id="@+id/mtu_label"
diff --git a/app/src/main/res/layout/tunnel_editor_peer.xml b/app/src/main/res/layout/tunnel_editor_peer.xml
index ea731349..aedcb18f 100644
--- a/app/src/main/res/layout/tunnel_editor_peer.xml
+++ b/app/src/main/res/layout/tunnel_editor_peer.xml
@@ -96,7 +96,7 @@
android:layout_height="wrap_content"
android:layout_below="@+id/allowed_ips_label"
android:inputType="textNoSuggestions|textVisiblePassword"
- android:text="@={item.allowedIPs}" />
+ android:text="@={item.allowedIPsString}" />
<TextView
android:id="@+id/endpoint_label"