Lightweight way to resolve DNS SRV records on Android

有些话、适合烂在心里 提交于 2019-12-03 13:34:01

问题


What is the most resource-efficient way to do SRV record lookups on Android, e.g. in an XMPP client like yaxim?

I am aware of:

  • JNDI, which is part of JavaSE but not in Android
  • dnsjava, which adds 800 KByte of class files (580KByte after ProGuard, so it will probably be hard to separate the files only needed for SRV lookup)
  • native tools like dig, nslookup, etc., which, when compiled statically, have a footprint similar to dnsjava and in addition make your app native-code dependant

I have read Querying the DNS service records to find the hostname and TCP/IP, but it only lists JNDI and dnsjava.

For sure I am not the first one to encounter this problem and there must be some lightweight DNS SRV resolver in Java :-)

Edit: bonus points for providing DNSSEC verification / DANE certificate querying.


回答1:


This might be a bit late, but a reference to this question may help:

Java DNS Lookup for SRV records

The answer there is using an external library (the most recent version, February 2015 as of this writing), is about 310KB, and under the BSD license. You can find it at http://www.dnsjava.org/download/. It all comes within one JAR, and it's been released since 1999.




回答2:


Stumbled over this question. Since there was no accepted answer and dnsjava has been sort of ruled out in question, already, I took this question to Google once again and stumbled over minidns. It supports DNSSEC, comes with a separate API for querying SRV records and I was successful in integrating it with an Android application prototype.

In my app/build.gradle I was adding this:

dependencies {
    implementation "org.minidns:minidns-hla:0.3.2"
}

After that I was capable of implementing a query like this using Kotlin:

package com.example.app

import android.os.AsyncTask
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

import kotlinx.android.synthetic.main.activity_main.*
import android.util.Log
import org.minidns.hla.ResolverApi
import java.io.IOException


class MainActivity : AppCompatActivity() {
    private val serviceName = "_mysrv._tcp.example.com"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        FetchSrvRecord().execute(serviceName)
    }

    inner class FetchSrvRecord() : AsyncTask<String, Int, String>() {
        override fun doInBackground(names: Array<String>): String {
            try {
                val result = ResolverApi.INSTANCE.resolveSrv(names[0])
                if (result.wasSuccessful()) {
                    val srvRecords = result.sortedSrvResolvedAddresses
                    for (record in srvRecords) {
                        return "https://" + record.srv.target.toString()
                    }
                }
            } catch (e: IOException) {
                Log.e("PoC", "failed IO", e)
            } catch (e: Throwable) {
                Log.e("PoC", "failed", e)
            }

            return "https://example.com"
        }

        override fun onPostExecute(url: String) {
            super.onPostExecute(url);

            Log.d("PoC", "got $url");
        }
    }
}

I'm not that familiar with Java/Android there was no obviously available file I could find as output of compiling that library so can't tell the library's impact on your apk's size.



来源:https://stackoverflow.com/questions/14661522/lightweight-way-to-resolve-dns-srv-records-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!