diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index 3f40cfbe0..067435ddb 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -6,13 +6,13 @@ import com.franmontiel.persistentcookiejar.persistence.SerializableCookie import io.legado.app.App import io.legado.app.data.entities.Cookie import io.legado.app.help.coroutine.Coroutine - +import io.legado.app.utils.NetworkUtils object CookieStore : CookiePersistor { fun setCookie(url: String, cookie: String?) { Coroutine.async { - val cookieBean = Cookie(url, cookie ?: "") + val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") App.db.cookieDao().insert(cookieBean) } } @@ -33,12 +33,12 @@ object CookieStore : CookiePersistor { } fun getCookie(url: String): String { - val cookieBean = App.db.cookieDao().get(url) + val cookieBean = App.db.cookieDao().get(NetworkUtils.getSubDomain(url)) return cookieBean?.cookie ?: "" } fun removeCookie(url: String) { - App.db.cookieDao().delete(url) + App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) } private fun cookieToMap(cookie: String): MutableMap { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 56b1f1c0e..561e3a320 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -348,21 +348,9 @@ class AnalyzeUrl( @Throws(Exception::class) fun getImageBytes(tag: String): ByteArray? { - //资源为本站的资源,保留cookie - //图片盗链的不保留当前的cookie,可由js生成图片源站的cookie val cookie = CookieStore.getCookie(tag) - NetworkUtils.getBaseUrl(url)?.let { - val regex: Any - regex = if (it.lastIndexOf(".") != it.indexOf(".")) { - it.substring(it.indexOf(".") + 1).toRegex() - } else { - it.substring(it.lastIndexOf("/") + 1).toRegex() - } - if (regex.containsMatchIn(tag)) { - if (cookie.isNotEmpty()) { - headerMap["Cookie"] += cookie - } - } + if (cookie.isNotEmpty()) { + headerMap["Cookie"] += cookie } headerMap[UA_NAME] = headerMap[UA_NAME] ?: userAgent return if (fieldMap.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index b706a8d5e..4a3e236cb 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -98,6 +98,14 @@ object NetworkUtils { } else url.substring(0, index) } + fun getSubDomain(url: String?): String { + var baseUrl = getBaseUrl(url) + if (baseUrl == null) return "" + return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { + baseUrl.substring(baseUrl.lastIndexOf("/")+1) + } else baseUrl.substring(baseUrl.indexOf(".")+1) + } + /** * Get local Ip address. */