mirror of
https://github.com/square/okhttp.git
synced 2025-07-29 17:41:17 +03:00
Update dependency com.diffplug.spotless:spotless-plugin-gradle to v7 (#8702)
* Update dependency com.diffplug.spotless:spotless-plugin-gradle to v7 * Reformat --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jake Wharton <jw@squareup.com>
This commit is contained in:
@ -202,23 +202,27 @@ class DnsOverHttps internal constructor(
|
||||
hostname: String,
|
||||
type: Int,
|
||||
): Request =
|
||||
Request.Builder().header("Accept", DNS_MESSAGE.toString()).apply {
|
||||
val query = DnsRecordCodec.encodeQuery(hostname, type)
|
||||
Request
|
||||
.Builder()
|
||||
.header("Accept", DNS_MESSAGE.toString())
|
||||
.apply {
|
||||
val query = DnsRecordCodec.encodeQuery(hostname, type)
|
||||
|
||||
if (post) {
|
||||
url(url)
|
||||
.cacheUrlOverride(
|
||||
url.newBuilder()
|
||||
.addQueryParameter("hostname", hostname).build(),
|
||||
)
|
||||
.post(query.toRequestBody(DNS_MESSAGE))
|
||||
} else {
|
||||
val encoded = query.base64Url().replace("=", "")
|
||||
val requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build()
|
||||
if (post) {
|
||||
url(url)
|
||||
.cacheUrlOverride(
|
||||
url
|
||||
.newBuilder()
|
||||
.addQueryParameter("hostname", hostname)
|
||||
.build(),
|
||||
).post(query.toRequestBody(DNS_MESSAGE))
|
||||
} else {
|
||||
val encoded = query.base64Url().replace("=", "")
|
||||
val requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build()
|
||||
|
||||
url(requestUrl)
|
||||
}
|
||||
}.build()
|
||||
url(requestUrl)
|
||||
}
|
||||
}.build()
|
||||
|
||||
class Builder {
|
||||
internal var client: OkHttpClient? = null
|
||||
@ -299,8 +303,6 @@ class DnsOverHttps internal constructor(
|
||||
}
|
||||
}
|
||||
|
||||
internal fun isPrivateHost(host: String): Boolean {
|
||||
return PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) == null
|
||||
}
|
||||
internal fun isPrivateHost(host: String): Boolean = PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) == null
|
||||
}
|
||||
}
|
||||
|
@ -37,28 +37,29 @@ internal object DnsRecordCodec {
|
||||
host: String,
|
||||
type: Int,
|
||||
): ByteString =
|
||||
Buffer().apply {
|
||||
writeShort(0) // query id
|
||||
writeShort(256) // flags with recursion
|
||||
writeShort(1) // question count
|
||||
writeShort(0) // answerCount
|
||||
writeShort(0) // authorityResourceCount
|
||||
writeShort(0) // additional
|
||||
Buffer()
|
||||
.apply {
|
||||
writeShort(0) // query id
|
||||
writeShort(256) // flags with recursion
|
||||
writeShort(1) // question count
|
||||
writeShort(0) // answerCount
|
||||
writeShort(0) // authorityResourceCount
|
||||
writeShort(0) // additional
|
||||
|
||||
val nameBuf = Buffer()
|
||||
val labels = host.split('.').dropLastWhile { it.isEmpty() }
|
||||
for (label in labels) {
|
||||
val utf8ByteCount = label.utf8Size()
|
||||
require(utf8ByteCount == label.length.toLong()) { "non-ascii hostname: $host" }
|
||||
nameBuf.writeByte(utf8ByteCount.toInt())
|
||||
nameBuf.writeUtf8(label)
|
||||
}
|
||||
nameBuf.writeByte(0) // end
|
||||
val nameBuf = Buffer()
|
||||
val labels = host.split('.').dropLastWhile { it.isEmpty() }
|
||||
for (label in labels) {
|
||||
val utf8ByteCount = label.utf8Size()
|
||||
require(utf8ByteCount == label.length.toLong()) { "non-ascii hostname: $host" }
|
||||
nameBuf.writeByte(utf8ByteCount.toInt())
|
||||
nameBuf.writeUtf8(label)
|
||||
}
|
||||
nameBuf.writeByte(0) // end
|
||||
|
||||
nameBuf.copyTo(this, 0, nameBuf.size)
|
||||
writeShort(type)
|
||||
writeShort(1) // CLASS_IN
|
||||
}.readByteString()
|
||||
nameBuf.copyTo(this, 0, nameBuf.size)
|
||||
writeShort(type)
|
||||
writeShort(1) // CLASS_IN
|
||||
}.readByteString()
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun decodeAnswers(
|
||||
|
@ -59,7 +59,8 @@ class DnsOverHttpsTest {
|
||||
private val cacheFs = FakeFileSystem()
|
||||
private val eventListener = RecordingEventListener()
|
||||
private val bootstrapClient =
|
||||
OkHttpClient.Builder()
|
||||
OkHttpClient
|
||||
.Builder()
|
||||
.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
|
||||
.eventListener(eventListener)
|
||||
.build()
|
||||
@ -186,8 +187,7 @@ class DnsOverHttpsTest {
|
||||
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
|
||||
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
|
||||
"0003b00049df00112",
|
||||
)
|
||||
.newBuilder()
|
||||
).newBuilder()
|
||||
.setHeader("cache-control", "private, max-age=298")
|
||||
.build(),
|
||||
)
|
||||
@ -229,8 +229,7 @@ class DnsOverHttpsTest {
|
||||
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
|
||||
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
|
||||
"0003b00049df00112",
|
||||
)
|
||||
.newBuilder()
|
||||
).newBuilder()
|
||||
.setHeader("cache-control", "private, max-age=298")
|
||||
.build(),
|
||||
)
|
||||
@ -271,8 +270,7 @@ class DnsOverHttpsTest {
|
||||
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
|
||||
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
|
||||
"0003b00049df00112",
|
||||
)
|
||||
.newBuilder()
|
||||
).newBuilder()
|
||||
.setHeader("cache-control", "max-age=1")
|
||||
.build(),
|
||||
)
|
||||
@ -292,8 +290,7 @@ class DnsOverHttpsTest {
|
||||
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
|
||||
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
|
||||
"0003b00049df00112",
|
||||
)
|
||||
.newBuilder()
|
||||
).newBuilder()
|
||||
.setHeader("cache-control", "max-age=1")
|
||||
.build(),
|
||||
)
|
||||
@ -307,19 +304,18 @@ class DnsOverHttpsTest {
|
||||
assertThat(cacheEvents()).containsExactly("CacheMiss")
|
||||
}
|
||||
|
||||
private fun cacheEvents(): List<String> {
|
||||
return eventListener.recordedEventTypes().filter { it.contains("Cache") }.also {
|
||||
private fun cacheEvents(): List<String> =
|
||||
eventListener.recordedEventTypes().filter { it.contains("Cache") }.also {
|
||||
eventListener.clearAllEvents()
|
||||
}
|
||||
}
|
||||
|
||||
private fun dnsResponse(s: String): MockResponse {
|
||||
return MockResponse.Builder()
|
||||
private fun dnsResponse(s: String): MockResponse =
|
||||
MockResponse
|
||||
.Builder()
|
||||
.body(Buffer().write(s.decodeHex()))
|
||||
.addHeader("content-type", "application/dns-message")
|
||||
.addHeader("content-length", s.length / 2)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildLocalhost(
|
||||
bootstrapClient: OkHttpClient,
|
||||
@ -327,7 +323,9 @@ class DnsOverHttpsTest {
|
||||
post: Boolean = false,
|
||||
): DnsOverHttps {
|
||||
val url = server.url("/lookup?ct")
|
||||
return DnsOverHttps.Builder().client(bootstrapClient)
|
||||
return DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.includeIPv6(includeIPv6)
|
||||
.resolvePrivateAddresses(true)
|
||||
.url(url)
|
||||
|
@ -39,9 +39,7 @@ class DnsRecordCodecTest {
|
||||
private fun encodeQuery(
|
||||
host: String,
|
||||
type: Int,
|
||||
): String {
|
||||
return DnsRecordCodec.encodeQuery(host, type).base64Url().replace("=", "")
|
||||
}
|
||||
): String = DnsRecordCodec.encodeQuery(host, type).base64Url().replace("=", "")
|
||||
|
||||
@Test
|
||||
fun testGoogleDotComEncodingWithIPv6() {
|
||||
|
@ -26,73 +26,73 @@ import okhttp3.OkHttpClient
|
||||
* https://github.com/curl/curl/wiki/DNS-over-HTTPS
|
||||
*/
|
||||
object DohProviders {
|
||||
private fun buildGoogle(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildGoogle(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://dns.google/dns-query".toHttpUrl())
|
||||
.bootstrapDnsHosts(getByIp("8.8.4.4"), getByIp("8.8.8.8"))
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildGooglePost(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildGooglePost(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://dns.google/dns-query".toHttpUrl())
|
||||
.bootstrapDnsHosts(getByIp("8.8.4.4"), getByIp("8.8.8.8"))
|
||||
.post(true)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildCloudflareIp(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildCloudflareIp(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://1.1.1.1/dns-query".toHttpUrl())
|
||||
.includeIPv6(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildCloudflare(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildCloudflare(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://1.1.1.1/dns-query".toHttpUrl())
|
||||
.bootstrapDnsHosts(getByIp("1.1.1.1"), getByIp("1.0.0.1"))
|
||||
.includeIPv6(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildCloudflarePost(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildCloudflarePost(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://cloudflare-dns.com/dns-query".toHttpUrl())
|
||||
.bootstrapDnsHosts(getByIp("1.1.1.1"), getByIp("1.0.0.1"))
|
||||
.includeIPv6(false)
|
||||
.post(true)
|
||||
.build()
|
||||
}
|
||||
|
||||
fun buildCleanBrowsing(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
fun buildCleanBrowsing(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://doh.cleanbrowsing.org/doh/family-filter/".toHttpUrl())
|
||||
.includeIPv6(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildChantra(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildChantra(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://dns.dnsoverhttps.net/dns-query".toHttpUrl())
|
||||
.includeIPv6(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun buildCryptoSx(bootstrapClient: OkHttpClient): DnsOverHttps {
|
||||
return DnsOverHttps.Builder()
|
||||
private fun buildCryptoSx(bootstrapClient: OkHttpClient): DnsOverHttps =
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url("https://doh.crypto.sx/dns-query".toHttpUrl())
|
||||
.includeIPv6(false)
|
||||
.build()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun providers(
|
||||
@ -100,8 +100,8 @@ object DohProviders {
|
||||
http2Only: Boolean,
|
||||
workingOnly: Boolean,
|
||||
getOnly: Boolean,
|
||||
): List<DnsOverHttps> {
|
||||
return buildList {
|
||||
): List<DnsOverHttps> =
|
||||
buildList {
|
||||
add(buildGoogle(client))
|
||||
if (!getOnly) {
|
||||
add(buildGooglePost(client))
|
||||
@ -117,14 +117,12 @@ object DohProviders {
|
||||
}
|
||||
add(buildChantra(client))
|
||||
}
|
||||
}
|
||||
|
||||
private fun getByIp(host: String): InetAddress {
|
||||
return try {
|
||||
private fun getByIp(host: String): InetAddress =
|
||||
try {
|
||||
InetAddress.getByName(host)
|
||||
} catch (e: UnknownHostException) {
|
||||
// unlikely
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,8 @@ fun main() {
|
||||
val url = "https://dns.cloudflare.com/.not-so-well-known/run-dmc-query".toHttpUrl()
|
||||
val badProviders =
|
||||
listOf(
|
||||
DnsOverHttps.Builder()
|
||||
DnsOverHttps
|
||||
.Builder()
|
||||
.client(bootstrapClient)
|
||||
.url(url)
|
||||
.post(true)
|
||||
@ -84,7 +85,8 @@ fun main() {
|
||||
println("cached first run\n****************\n")
|
||||
names = listOf("google.com", "graph.facebook.com")
|
||||
bootstrapClient =
|
||||
bootstrapClient.newBuilder()
|
||||
bootstrapClient
|
||||
.newBuilder()
|
||||
.cache(dnsCache)
|
||||
.build()
|
||||
dnsProviders =
|
||||
|
Reference in New Issue
Block a user