Do not try to resolve localhost with search domains (#30779)

* Prepopulate cache with localhost entries
This commit is contained in:
Johan Andrén 2021-10-20 08:03:12 +02:00 committed by GitHub
parent 73adc3194a
commit f561146fa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View file

@ -183,8 +183,6 @@ class AsyncDnsResolverIntegrationSpec
}
"resolve localhost even though ndots is greater than 0" in {
// This currently works because the nameserver resolves localhost, but in future should work because we've
// implemented proper support for /etc/hosts
val name = "localhost"
val answer = resolve(name, DnsProtocol.Ip(ipv6 = false))
withClue(answer) {

View file

@ -39,6 +39,29 @@ private[io] final class AsyncDnsResolver(
implicit val ec: ExecutionContextExecutor = context.dispatcher
// avoid ever looking up localhost by pre-populating cache
{
val loopback = InetAddress.getLoopbackAddress
val (ipv4Address, ipv6Address) = loopback match {
case ipv6: Inet6Address => (InetAddress.getByName("127.0.0.1"), ipv6)
case ipv4: Inet4Address => (ipv4, InetAddress.getByName("::1"))
case unknown => throw new IllegalArgumentException(s"Loopback address was [$unknown]")
}
cache.put(
"localhost" -> Ip(),
DnsProtocol.Resolved("localhost", ARecord("localhost", Ttl.effectivelyForever, loopback) :: Nil),
Ttl.effectivelyForever)
cache.put(
"localhost" -> Ip(ipv6 = false, ipv4 = true),
DnsProtocol.Resolved("localhost", ARecord("localhost", Ttl.effectivelyForever, ipv4Address) :: Nil),
Ttl.effectivelyForever)
cache.put(
"localhost" -> Ip(ipv6 = true, ipv4 = false),
DnsProtocol.Resolved("localhost", ARecord("localhost", Ttl.effectivelyForever, ipv6Address) :: Nil),
Ttl.effectivelyForever)
}
// For ask to DNS Client
implicit val timeout: Timeout = Timeout(settings.ResolveTimeout)