diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-04-27 18:29:14 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-04-27 18:34:52 +0200 |
commit | 693228985df3aab03a53d9ae68aed7d309c4f6bd (patch) | |
tree | 3676e38dfe0d171a2ec677b3b58d5f046650e45c /app/src/main | |
parent | 9c6f9135e9eda8a9cceebf1eb1980c9b06ce3741 (diff) |
Do not do DNS lookups for IPs
This involves reflection, which is a bummer, but it's better than doing
unnecessary DNS lookups.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main')
4 files changed, 29 insertions, 14 deletions
diff --git a/app/src/main/java/com/wireguard/config/Attribute.java b/app/src/main/java/com/wireguard/config/Attribute.java index c73e9f2e..c98588b2 100644 --- a/app/src/main/java/com/wireguard/config/Attribute.java +++ b/app/src/main/java/com/wireguard/config/Attribute.java @@ -2,6 +2,9 @@ package com.wireguard.config; import android.text.TextUtils; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.InetAddress; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -54,6 +57,30 @@ enum Attribute { 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 InetAddress parseIPString(final String address) { + if (address == null || address.isEmpty()) + throw new IllegalArgumentException("Empty address"); + try { + 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(); + else + throw new IllegalArgumentException(e.getCause()); + } + } + public String composeWith(final Object value) { return String.format("%s = %s%n", token, value); } diff --git a/app/src/main/java/com/wireguard/config/IPCidr.java b/app/src/main/java/com/wireguard/config/IPCidr.java index 245141ac..07fccad4 100644 --- a/app/src/main/java/com/wireguard/config/IPCidr.java +++ b/app/src/main/java/com/wireguard/config/IPCidr.java @@ -39,11 +39,7 @@ public class IPCidr implements Parcelable { } catch (Exception e) { } } - try { - address = InetAddress.getByName(in); - } catch (UnknownHostException e) { - throw new IllegalArgumentException(e); - } + address = Attribute.parseIPString(in); if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0)) cidr = 128; else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0)) diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index 3f775389..03121072 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -172,13 +172,7 @@ public class Interface extends BaseObservable implements Parcelable { public void addDnses(String[] dnses) { if (dnses != null && dnses.length > 0) { for (final String dns : dnses) { - if (dns.isEmpty()) - throw new IllegalArgumentException("DNS is empty"); - try { - this.dnsList.add(InetAddress.getByName(dns)); - } catch (UnknownHostException e) { - throw new IllegalArgumentException(e); - } + this.dnsList.add(Attribute.parseIPString(dns)); } } notifyPropertyChanged(BR.dnses); diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index c2b23e9f..9895f447 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -144,8 +144,6 @@ public class Peer extends BaseObservable implements Parcelable { public void addAllowedIPs(String[] allowedIPs) { if (allowedIPs != null && allowedIPs.length > 0) { for (final String allowedIP : allowedIPs) { - if (allowedIP.isEmpty()) - throw new IllegalArgumentException("AllowedIP is empty"); this.allowedIPsList.add(new IPCidr(allowedIP)); } } |