Reputation: 66390
I would like to create a custom DNS to utilise CloudFlare 1.1.1.1 and 1.0.0.1 for all my RetroFit connections. This should replace the SYSTEM DNS.
So I did this:
import okhttp3.Dns
import java.net.InetAddress
class CustomDns : Dns {
override fun lookup(hostname: String): List<InetAddress> {
TODO("Not yet implemented")
}
}
I'm a bit lost as in how to implement the abstract lookup function.
The documentation says:
Returns the IP addresses of hostname, in the order they will be attempted by OkHttp. If a connection to an address fails, OkHttp will retry the connection with the next address until either a connection is made, the set of IP addresses is exhausted, or a limit is exceeded.abstract fun lookup(hostname:String):List
Does it mean I create a HashMap. The key will be the domain of my API, and value will contain a list of the IP addresses that my API resolves to? Doesn't seem quite right to me.
Is there a way to use just 1.1.1.1 as DNS resolver within this implementation?
Upvotes: 6
Views: 8777
Reputation: 13488
If you are happy to use Dns over Https then you can use okhttp-doh to connect to 1.1.1.1
val appCache = Cache(File("cacheDir", "okhttpcache"), 10 * 1024 * 1024)
val bootstrapClient = OkHttpClient.Builder().cache(appCache).build()
val dns = DnsOverHttps.Builder().client(bootstrapClient)
.url("https://1.1.1.1/dns-query".toHttpUrl())
.build()
val client = bootstrapClient.newBuilder().dns(dns).build()
Other example DNS providers https://github.com/square/okhttp/blob/master/okhttp-dnsoverhttps/src/test/java/okhttp3/dnsoverhttps/DohProviders.java
If you want to use regular DNS but you would either need to make these changs on the device, outside the app. Or provide your own implementation, perhaps implementing it based on Netty or similar?
Upvotes: 5