package com.rc.httpcore.config

import android.text.TextUtils
import android.util.Log
import okhttp3.Dns
import java.net.InetAddress
import java.net.UnknownHostException


class OkHttpDNS : Dns {
    private var mIpString: String? = null

    @Throws(UnknownHostException::class)
    override fun lookup(hostname: String): List<InetAddress> {
//        Log.d("OkHttpDNS", "hostname----:$hostname")
        if (TextUtils.isEmpty(hostname) || null == hostname) return Dns.SYSTEM.lookup(hostname)

        //根据域名获取IP
        val ips = InetAddress.getAllByName(hostname)
        for (inetAddress in ips) {
            mIpString = inetAddress.hostAddress
//            Log.d("OkHttpDNS", "mIpString----:$mIpString")
        }
        return if (null != mIpString && !TextUtils.isEmpty(mIpString)) {
            listOf(*InetAddress.getAllByName(mIpString))
        } else {
            Dns.SYSTEM.lookup(hostname)
        }
    }
}