Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import okhttp3.Protocol
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okhttp3.internal.canParseAsIpAddress
import okhttp3.internal.platform.Platform
import okhttp3.internal.publicsuffix.PublicSuffixDatabase

Expand All @@ -53,6 +54,7 @@ class DnsOverHttps internal constructor(
@get:JvmName("resolvePrivateAddresses") val resolvePrivateAddresses: Boolean,
@get:JvmName("resolvePublicAddresses") val resolvePublicAddresses: Boolean
) : Dns {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you revert this line.

@Throws(UnknownHostException::class)
override fun lookup(hostname: String): List<InetAddress> {
if (!resolvePrivateAddresses || !resolvePublicAddresses) {
Expand All @@ -67,7 +69,24 @@ class DnsOverHttps internal constructor(
}
}

return lookupHttps(hostname)
return try {
lookupHttps(hostname)
} catch (bestFailure: UnknownHostException) {
val address = if (hostname.startsWith("[") && hostname.endsWith("]")) {
hostname.substring(1, hostname.length - 1)
} else {
hostname
}
if (address.canParseAsIpAddress()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, I forgot this was a thing. Can you invert this now?

Also if that function doesn't worry about [], we can probably skip that here also.

See

assertThat(parse("http://[::1]/").host()).isEqualTo("::1");

Which confirms the HttpUrl strips the [].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that the HttpUrl will handle the square braces before calling dns lookup, so we don't need to check and strips the [] here and just remove the test case for hostname "[::1]"?

try {
InetAddress.getAllByName(address).toList()
} catch (e: UnknownHostException) {
throw bestFailure
}
} else {
throw bestFailure
}
}
}

@Throws(UnknownHostException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ public class DnsOverHttpsTest {
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
}

@Test public void getIPAddressHost() throws Exception {
dns = buildLocalhost(bootstrapClient, true);

String hostIPv4 = "127.0.0.1";
assertThat(dns.lookup(hostIPv4)).contains(address(hostIPv4));

String hostIPv6 = "2a03:2880:f029:11:face:b00c:0:2";
assertThat(dns.lookup(hostIPv6)).contains(address(hostIPv6));

String hostIPv6Loopback = "::1";
assertThat(dns.lookup(hostIPv6Loopback)).contains(address(hostIPv6Loopback));
assertThat(dns.lookup("[::1]")).contains(address(hostIPv6Loopback));
}

@Test public void getIpv6() throws Exception {
server.enqueue(dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c00050001"
Expand Down