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>
This commit is contained in:
parent
9c6f9135e9
commit
693228985d
@ -2,6 +2,9 @@ package com.wireguard.config;
|
|||||||
|
|
||||||
import android.text.TextUtils;
|
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.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -54,6 +57,30 @@ enum Attribute {
|
|||||||
return string.trim().split("\\s*,\\s*");
|
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) {
|
public String composeWith(final Object value) {
|
||||||
return String.format("%s = %s%n", token, value);
|
return String.format("%s = %s%n", token, value);
|
||||||
}
|
}
|
||||||
|
@ -39,11 +39,7 @@ public class IPCidr implements Parcelable {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
address = Attribute.parseIPString(in);
|
||||||
address = InetAddress.getByName(in);
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
}
|
|
||||||
if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0))
|
if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0))
|
||||||
cidr = 128;
|
cidr = 128;
|
||||||
else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0))
|
else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0))
|
||||||
|
@ -172,13 +172,7 @@ public class Interface extends BaseObservable implements Parcelable {
|
|||||||
public void addDnses(String[] dnses) {
|
public void addDnses(String[] dnses) {
|
||||||
if (dnses != null && dnses.length > 0) {
|
if (dnses != null && dnses.length > 0) {
|
||||||
for (final String dns : dnses) {
|
for (final String dns : dnses) {
|
||||||
if (dns.isEmpty())
|
this.dnsList.add(Attribute.parseIPString(dns));
|
||||||
throw new IllegalArgumentException("DNS is empty");
|
|
||||||
try {
|
|
||||||
this.dnsList.add(InetAddress.getByName(dns));
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
notifyPropertyChanged(BR.dnses);
|
notifyPropertyChanged(BR.dnses);
|
||||||
|
@ -144,8 +144,6 @@ public class Peer extends BaseObservable implements Parcelable {
|
|||||||
public void addAllowedIPs(String[] allowedIPs) {
|
public void addAllowedIPs(String[] allowedIPs) {
|
||||||
if (allowedIPs != null && allowedIPs.length > 0) {
|
if (allowedIPs != null && allowedIPs.length > 0) {
|
||||||
for (final String allowedIP : allowedIPs) {
|
for (final String allowedIP : allowedIPs) {
|
||||||
if (allowedIP.isEmpty())
|
|
||||||
throw new IllegalArgumentException("AllowedIP is empty");
|
|
||||||
this.allowedIPsList.add(new IPCidr(allowedIP));
|
this.allowedIPsList.add(new IPCidr(allowedIP));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user