dnsjava 解析域名IP,支持ipv6

2020-03-24  本文已影响0人  马克_唐卡

前奏:安装依赖……

直接代码:

fun dnsResolver(url: String, isIpv4: Boolean): ArrayList<String> {

    val ips = ArrayList<String>()
    val resolver = SimpleResolver("114.114.114.114")
    // 此处决定ip类型
    val lookup = Lookup(url, if(isIpv4) Type.A else Type.AAAA)
    lookup.setResolver(resolver)
    val cache = Cache()
    lookup.setCache(cache)
    lookup.run()
    if (lookup.getResult() === Lookup.SUCCESSFUL) {
        val records = lookup.getAnswers()
        for (record in records) {
            ips.add(record.rdataToString())
        }
    }

    return ips
}
上一篇下一篇

猜你喜欢

热点阅读