在 Android 上解析 DNS SRV 记录的轻量级方法 [英] Lightweight way to resolve DNS SRV records on Android

查看:21
本文介绍了在 Android 上解析 DNS SRV 记录的轻量级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 上进行 SRV 记录查找的最节省资源的方法是什么,例如在像 yaxim 之类的 XMPP 客户端中?

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

我知道:

  • JNDI,它是 JavaSE 的一部分,但不是在安卓中
  • dnsjava,增加了 800 KB 的类文件(ProGuard 之后是 580 KB,所以可能会很难分隔仅用于 SRV 查找所需的文件)
  • dig、nslookup 等原生工具,在静态编译时,其占用空间类似于 dnsjava,此外还使您的应用程序依赖于原生代码
  • 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

我已阅读查询DNS服务记录,找到主机名和TCP/IP,但只列出JNDI和dnsjava.

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

我肯定不是第一个遇到这个问题的人,Java 中肯定有一些轻量级的 DNS SRV 解析器 :-)

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

提供 DNSSEC 验证/DANE 证书查询的奖励积分.

bonus points for providing DNSSEC verification / DANE certificate querying.

推荐答案

偶然发现了这个问题.由于没有公认的答案并且 dnsjava 已经被排除在外,我已经再次将这个问题带到谷歌并偶然发现 minidns.它支持 DNSSEC,带有用于查询 SRV 记录的单独 API,我成功地将它与 Android 应用程序原型集成.

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.

在我的 app/build.gradle 中,我添加了这个:

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

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

在那之后,我能够使用 Kotlin 实现这样的查询:

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");
        }
    }
}

我对 Java/Android 不太熟悉,我找不到明显可用的文件作为编译该库的输出,因此无法判断该库对您的 apk 大小的影响.

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.

这篇关于在 Android 上解析 DNS SRV 记录的轻量级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆