|
|
|
@ -3,6 +3,7 @@ package io.legado.app.utils |
|
|
|
|
import android.net.ConnectivityManager |
|
|
|
|
import android.net.NetworkCapabilities |
|
|
|
|
import android.os.Build |
|
|
|
|
import okhttp3.internal.publicsuffix.PublicSuffixDatabase |
|
|
|
|
import splitties.systemservices.connectivityManager |
|
|
|
|
import timber.log.Timber |
|
|
|
|
import java.net.InetAddress |
|
|
|
@ -11,7 +12,6 @@ import java.net.SocketException |
|
|
|
|
import java.net.URL |
|
|
|
|
import java.util.* |
|
|
|
|
import java.util.regex.Pattern |
|
|
|
|
import okhttp3.internal.publicsuffix.PublicSuffixDatabase |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Suppress("unused", "MemberVisibilityCanBePrivate") |
|
|
|
@ -147,7 +147,7 @@ object NetworkUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取二级域名,供cookie保存和读取 |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* http://1.2.3.4 => http://1.2.3.4 |
|
|
|
|
* https://www.example.com => https://example.com |
|
|
|
|
* http://www.biquge.com.cn => http://biquge.com.cn |
|
|
|
@ -155,11 +155,11 @@ object NetworkUtils { |
|
|
|
|
*/ |
|
|
|
|
fun getSubDomain(url: String?): String { |
|
|
|
|
val baseUrl = getBaseUrl(url) ?: return "" |
|
|
|
|
val url = URL(baseUrl) |
|
|
|
|
val schema: String = url.getProtocol() |
|
|
|
|
val host: String = url.getHost() |
|
|
|
|
val mURL = URL(baseUrl) |
|
|
|
|
val schema: String = mURL.protocol |
|
|
|
|
val host: String = mURL.host |
|
|
|
|
//判断是否为ip |
|
|
|
|
if(isIPAddress(host)) return baseUrl |
|
|
|
|
if (isIPAddress(host)) return baseUrl |
|
|
|
|
//PublicSuffixDatabase处理域名 |
|
|
|
|
val domain = PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) |
|
|
|
|
return if (domain == null) baseUrl else "${schema}://${domain}" |
|
|
|
@ -206,7 +206,7 @@ object NetworkUtils { |
|
|
|
|
/** |
|
|
|
|
* Check if valid IPV6 address. |
|
|
|
|
*/ |
|
|
|
|
fun isIPv6Address(input: String?): Boolean { |
|
|
|
|
fun isIPv6Address(input: String?): Boolean { |
|
|
|
|
return input != null && IPV6_PATTERN.matcher(input).matches() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -222,14 +222,14 @@ object NetworkUtils { |
|
|
|
|
*/ |
|
|
|
|
private val IPV4_PATTERN = Pattern.compile( |
|
|
|
|
"^(" + "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" + |
|
|
|
|
"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" |
|
|
|
|
"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Ipv6 address check. |
|
|
|
|
*/ |
|
|
|
|
private val IPV6_PATTERN = Pattern.compile( |
|
|
|
|
"^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1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