Add code to do a DNS MX lookup

This commit is contained in:
cketti 2023-05-15 22:11:30 +02:00
parent 32d76d04ae
commit 37196c6431
4 changed files with 52 additions and 0 deletions

View file

@ -8,6 +8,7 @@ dependencies {
compileOnly(libs.xmlpull)
implementation(libs.okhttp)
implementation(libs.minidns.hla)
testImplementation(libs.kxml2)
testImplementation(libs.jsoup)

View file

@ -0,0 +1,13 @@
package app.k9mail.autodiscovery.autoconfig
import org.minidns.hla.ResolverApi
import org.minidns.record.MX
class MiniDnsMxResolver : MxResolver {
override fun lookup(domain: String): List<String> {
val result = ResolverApi.INSTANCE.resolve(domain, MX::class.java)
return result.answersOrEmptySet
.sortedBy { it.priority }
.map { it.target.toString() }
}
}

View file

@ -0,0 +1,8 @@
package app.k9mail.autodiscovery.autoconfig
/**
* Look up MX records for a domain.
*/
interface MxResolver {
fun lookup(domain: String): List<String>
}

View file

@ -0,0 +1,30 @@
package app.k9mail.autodiscovery.autoconfig
import assertk.all
import assertk.assertThat
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.index
import assertk.assertions.isEqualTo
import kotlin.test.Ignore
import kotlin.test.Test
class MiniDnsMxResolverTest {
private val resolver = MiniDnsMxResolver()
@Test
@Ignore("Requires internet")
fun `MX lookup for known domain`() {
val result = resolver.lookup("thunderbird.net")
assertThat(result).all {
index(0).isEqualTo("aspmx.l.google.com")
containsExactlyInAnyOrder(
"aspmx.l.google.com",
"alt1.aspmx.l.google.com",
"alt2.aspmx.l.google.com",
"alt4.aspmx.l.google.com",
"alt3.aspmx.l.google.com",
)
}
}
}