Commit 076962e5 authored by Ricki Hirner's avatar Ricki Hirner 🐑

Specify DNS server for dnsjava explicitly

parent 04306ed9
......@@ -8,13 +8,18 @@
package at.bitfire.davdroid;
import android.annotation.TargetApi
import android.content.Context
import android.net.ConnectivityManager
import android.os.Build
import at.bitfire.davdroid.log.Logger
import okhttp3.HttpUrl
import org.xbill.DNS.Record
import org.xbill.DNS.SRVRecord
import org.xbill.DNS.TXTRecord
import org.xbill.DNS.*
import java.util.*
/**
* Some WebDAV and related network utility methods
*/
object DavUtils {
@JvmStatic
......@@ -39,6 +44,26 @@ object DavUtils {
return "/"
}
fun prepareLookup(context: Context, lookup: Lookup) {
@TargetApi(Build.VERSION_CODES.O)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Since Android 8, the system properties net.dns1, net.dns2, ... are not available anymore.
The current version of dnsjava relies on these properties to find the default name servers,
so we have to add the servers explicitly (fortunately, there's an Android API to
get the active DNS servers). */
val connectivity = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeLink = connectivity.getLinkProperties(connectivity.activeNetwork)
val simpleResolvers = activeLink.dnsServers.map {
Logger.log.fine("Using DNS server ${it.hostAddress}")
val resolver = SimpleResolver()
resolver.setAddress(it)
resolver
}
val resolver = ExtendedResolver(simpleResolvers.toTypedArray())
lookup.setResolver(resolver)
}
}
fun selectSRVRecord(records: Array<Record>?): SRVRecord? {
val srvRecords = records?.filterIsInstance(SRVRecord::class.java)
srvRecords?.let {
......
......@@ -299,7 +299,9 @@ class DavResourceFinder(
val query = "_${service.wellKnownName}s._tcp.$domain"
log.fine("Looking up SRV records for $query")
val srv = DavUtils.selectSRVRecord(Lookup(query, Type.SRV).run())
val srvLookup = Lookup(query, Type.SRV)
DavUtils.prepareLookup(context, srvLookup)
val srv = DavUtils.selectSRVRecord(srvLookup.run())
if (srv != null) {
// choose SRV record to use (query may return multiple SRV records)
scheme = "https"
......@@ -315,7 +317,9 @@ class DavResourceFinder(
}
// look for TXT record too (for initial context path)
paths.addAll(DavUtils.pathsFromTXTRecords(Lookup(query, Type.TXT).run()))
val txtLookup = Lookup(query, Type.TXT)
DavUtils.prepareLookup(context, txtLookup)
paths.addAll(DavUtils.pathsFromTXTRecords(txtLookup.run()))
// if there's TXT record and if it it's wrong, try well-known
paths.add("/.well-known/" + service.wellKnownName)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment