Created
September 21, 2020 17:58
-
-
Save Doko-Demo-Doa/05b24449714a5e0ad25aa24b64a1692a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yourapp; | |
import java.net.Inet4Address; | |
import java.net.InetAddress; | |
import java.net.UnknownHostException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import okhttp3.Dns; | |
public class CustomDNS implements Dns { | |
@Override | |
public List<InetAddress> lookup(String hostname) throws UnknownHostException { | |
InetAddress[] addresses = InetAddress.getAllByName(hostname); | |
if (addresses == null || addresses.length == 0) { | |
Dns.SYSTEM.lookup(hostname); | |
} | |
// Get IPv4 first. | |
ArrayList<InetAddress> result = new ArrayList<>(); | |
for (InetAddress address : addresses) { | |
if (address instanceof Inet4Address) { | |
result.add(address); | |
} | |
} | |
for (InetAddress address : addresses) { | |
if (!(address instanceof Inet4Address)) { | |
result.add(address); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment