MiniDNS / minidns

DNS library for Android and Java SE
Other
215 stars 61 forks source link

DNS Queries over HTTPS (DoH) support #122

Open rekire opened 2 years ago

rekire commented 2 years ago

I played today a bit more with your library and build a DoH client (in Kotlin) on top.

class DohResult(query: DnsMessage, data: ByteArray?) :
    DnsQueryResult(QueryMethod.tcp, query, DnsMessage(data))

class DohResolver : DnsClient() {
    override fun query(queryBuilder: DnsMessage.Builder): DnsQueryResult {
        val q = queryBuilder.setId(0).build()
        val output = ByteArrayOutputStream()
        q.writeTo(output, false)

        val client = OkHttpClient()
        val query = Base64.encodeToString(output.toByteArray()).trimEnd('=')
        val request = Request.Builder()
            .url("https://dns.google/dns-query?dns=$query")
            .header("Content-Type", "application/dns-message")
            .build()

        val result = client.newCall(request).execute()
        return DohResult(q, result.body?.bytes())
    }
}

@Test
fun doh() {
    println(DohResolver().query(Question("github.com", Record.TYPE.MX)))
}

Are you interested in a PR? I would improve the code before. Just wondering if this could be interested for you at all.

Flowdalic commented 2 years ago

Are you interested in a PR?

I always welcome contributions. :smile: However, no contribution is guranteed to be accepted as is, and, depending on the extent of the contribution, it may take a while to review. That said, here is some first, hopefully relevant, feedback:

That's it from the top of my head. It's always easier to discuss actual code, so feel encouraged to submit the a PR. ;-)

rekire commented 2 years ago

My code snip it above is a simple proof of concept which works for me.

Thank you for your feedback. I'll check if can fulfill your requirements and then I'll open a PR for that.

Sami32 commented 4 months ago

If it can be of some help?

https://github.com/dnsjava/dnsjava/commit/9af9a0f031056608648163e7c9bdc64d3bccbd2a

Duplicate of #50

dugsmith commented 4 months ago

Thank you @Sami32. It looks to me that something more like @rekire's solution would work better for Android, though. Am I missing something?