From d132087b3ca55b96724b48d36c2b82cdfe5d6533 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 6 Jul 2018 03:40:18 +0200 Subject: PeerEditor: add exclude private IPs functionality Signed-off-by: Jason A. Donenfeld --- app/src/main/java/com/wireguard/config/Peer.java | 33 ++++++++++++++++++++++++ app/src/main/res/layout/tunnel_editor_peer.xml | 13 +++++++++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index 1d69beb2..35a58499 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -20,6 +20,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -245,11 +247,40 @@ public class Peer { return 0; } + private static final String DEFAULT_ROUTE_V4 = "0.0.0.0/0"; + private static final List DEFAULT_ROUTE_MOD_RFC1918_V4 = Arrays.asList("0.0.0.0/5", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3", "64.0.0.0/2", "128.0.0.0/3", "160.0.0.0/5", "168.0.0.0/6", "172.0.0.0/12", "172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9", "173.0.0.0/8", "174.0.0.0/7", "176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16", "192.170.0.0/15", "192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4"); + + public void toggleExcludePrivateIPs() { + final HashSet ips = new HashSet<>(Arrays.asList(Attribute.stringToList(allowedIPs))); + final boolean hasDefaultRoute = ips.contains(DEFAULT_ROUTE_V4); + final boolean hasDefaultRouteModRFC1918 = ips.containsAll(DEFAULT_ROUTE_MOD_RFC1918_V4); + if (hasDefaultRoute && hasDefaultRouteModRFC1918) + ips.removeAll(DEFAULT_ROUTE_MOD_RFC1918_V4); + else if (hasDefaultRoute) { + ips.remove(DEFAULT_ROUTE_V4); + ips.addAll(DEFAULT_ROUTE_MOD_RFC1918_V4); + } else if (hasDefaultRouteModRFC1918) { + ips.removeAll(DEFAULT_ROUTE_MOD_RFC1918_V4); + ips.add(DEFAULT_ROUTE_V4); + } + setAllowedIPs(Attribute.iterableToString(ips)); + } + @Bindable public String getAllowedIPs() { return allowedIPs; } + @Bindable + public boolean getAllowedIPsContainsDefaultRoute() { + return Arrays.asList(Attribute.stringToList(allowedIPs)).contains(DEFAULT_ROUTE_V4); + } + + @Bindable + public boolean getAllowedIPsContainsDefaultRouteModRFC1918() { + return Arrays.asList(Attribute.stringToList(allowedIPs)).containsAll(DEFAULT_ROUTE_MOD_RFC1918_V4); + } + @Bindable public String getEndpoint() { return endpoint; @@ -281,6 +312,8 @@ public class Peer { public void setAllowedIPs(final String allowedIPs) { this.allowedIPs = allowedIPs; notifyPropertyChanged(BR.allowedIPs); + notifyPropertyChanged(BR.allowedIPsContainsDefaultRoute); + notifyPropertyChanged(BR.allowedIPsContainsDefaultRouteModRFC1918); } public void setEndpoint(final String endpoint) { diff --git a/app/src/main/res/layout/tunnel_editor_peer.xml b/app/src/main/res/layout/tunnel_editor_peer.xml index 309ef7bb..5a84fd92 100644 --- a/app/src/main/res/layout/tunnel_editor_peer.xml +++ b/app/src/main/res/layout/tunnel_editor_peer.xml @@ -4,6 +4,7 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b44e3121..b4105d2d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,6 +43,7 @@ Please obtain root access and try again Error bringing up tunnel: %s Excluded Applications + Exclude Private IPs Generate (auto) (generated) -- cgit v1.2.3