|
|
|
|
@ -6,12 +6,14 @@ import java.net.InetAddress; |
|
|
|
|
import java.net.UnknownHostException; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
import okhttp3.Dns; |
|
|
|
|
import okhttp3.dnsoverhttps.DnsOverHttps; |
|
|
|
|
|
|
|
|
|
public class OkDns implements Dns { |
|
|
|
|
|
|
|
|
|
private final Pattern IP = Pattern.compile("\\b(?:(?:[0-9]{1,3}\\.){3}[0-9]{1,3}|[0-9a-fA-F:]{2,39})\\b"); |
|
|
|
|
private final HashMap<String, String> map; |
|
|
|
|
private DnsOverHttps doh; |
|
|
|
|
|
|
|
|
|
@ -31,10 +33,19 @@ public class OkDns implements Dns { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isAddress(String input) { |
|
|
|
|
try { |
|
|
|
|
return IP.matcher(input).find(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
|
@Override |
|
|
|
|
public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException { |
|
|
|
|
return (doh != null ? doh : Dns.SYSTEM).lookup(map.containsKey(hostname) ? map.get(hostname) : hostname); |
|
|
|
|
String target = map.containsKey(hostname) ? map.get(hostname) : hostname; |
|
|
|
|
return isAddress(target) ? List.of(InetAddress.getByName(target)) : (doh != null ? doh : Dns.SYSTEM).lookup(target); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|