Merge branch 'master' of github.com:gedoor/legado

pull/1500/head
gedoor 3 years ago
commit f188417de3
  1. 47
      app/src/main/java/io/legado/app/utils/NetworkUtils.kt

@ -11,6 +11,7 @@ import java.net.SocketException
import java.net.URL import java.net.URL
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
@Suppress("unused", "MemberVisibilityCanBePrivate") @Suppress("unused", "MemberVisibilityCanBePrivate")
@ -147,22 +148,21 @@ object NetworkUtils {
/** /**
* 获取二级域名供cookie保存和读取 * 获取二级域名供cookie保存和读取
* *
* 1.2.3.4 => 1.2.3.4 * http://1.2.3.4 => http://1.2.3.4
* www.example.com => example.com * https://www.example.com => https://example.com
* www.example.com.cn => example.com.cn * http://www.biquge.com.cn => http://biquge.com.cn
* 未实现www.content.example.com => example.com * http://www.content.example.com => http://example.com
*/ */
fun getSubDomain(url: String?): String { fun getSubDomain(url: String?): String {
val baseUrl = getBaseUrl(url) ?: return "" val baseUrl = getBaseUrl(url) ?: return ""
//baseUrl为IPv4时,直接返回 val url = URL(baseUrl)
//IPv6暂时不考虑支持 val schema: String = url.getProtocol()
if (isIPv4Address(baseUrl)) { val host: String = url.getHost()
return baseUrl //判断是否为ip
} if(isIPAddress(host)) return baseUrl
//td do:利用https://github.com/publicsuffix/list,https://github.com/Kevin-Sangeelee/PublicSuffixList实现二级域名返回 //PublicSuffixDatabase处理域名
return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { val domain = PublicSuffixDatabase.get().getEffectiveTldPlusOne(host)
baseUrl return if (domain == null) baseUrl else "${schema}://${domain}"
} else baseUrl.substring(baseUrl.indexOf(".") + 1)
} }
/** /**
@ -203,6 +203,20 @@ object NetworkUtils {
return input != null && IPV4_PATTERN.matcher(input).matches() return input != null && IPV4_PATTERN.matcher(input).matches()
} }
/**
* Check if valid IPV6 address.
*/
fun isIPv6Address(input: String?): Boolean {
return input != null && IPV6_PATTERN.matcher(input).matches()
}
/**
* Check if valid IP address.
*/
fun isIPAddress(input: String?): Boolean {
return isIPv4Address(input) || isIPv6Address(input)
}
/** /**
* Ipv4 address check. * Ipv4 address check.
*/ */
@ -211,4 +225,11 @@ object NetworkUtils {
"([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,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,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}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((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}))|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