diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-07-06 03:40:18 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-07-06 16:28:46 +0200 |
commit | d132087b3ca55b96724b48d36c2b82cdfe5d6533 (patch) | |
tree | 99d7ff2af12c41dac7cf72e998ffa545836a0b59 /app/src/main/java/com/wireguard/config | |
parent | 124f186983f95648867ba1fdd2bec1e098ae95ba (diff) |
PeerEditor: add exclude private IPs functionality
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/config')
-rw-r--r-- | app/src/main/java/com/wireguard/config/Peer.java | 33 |
1 files changed, 33 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 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,12 +247,41 @@ public class Peer { return 0; } + private static final String DEFAULT_ROUTE_V4 = "0.0.0.0/0"; + private static final List<String> 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<String> 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) { |