From 9fa6b8cb510ddc38e59d4e8e6820d7e3ed9346cc Mon Sep 17 00:00:00 2001 From: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:06:47 +0500 Subject: [PATCH] move CookieInterceptor to shared lib (#1356) * CookieInterceptor lib * accept list of cookies --- lib/cookieinterceptor/build.gradle.kts | 23 ++++++++ .../cookieinterceptor/CookieInterceptor.kt | 59 +++++++++++++++++++ src/all/mangapark/build.gradle | 4 ++ .../all/mangapark/CookieInterceptor.kt | 46 --------------- .../extension/all/mangapark/MangaPark.kt | 3 +- src/en/mangaplanet/build.gradle | 1 + .../en/mangaplanet/CookieInterceptor.kt | 46 --------------- .../extension/en/mangaplanet/MangaPlanet.kt | 3 +- src/en/toonily/build.gradle | 4 ++ .../extension/en/toonily/CookieInterceptor.kt | 46 --------------- .../tachiyomi/extension/en/toonily/Toonily.kt | 4 +- src/vi/hentaivn/build.gradle | 1 + .../vi/hentaivn/CookieInterceptor.kt | 46 --------------- .../extension/vi/hentaivn/HentaiVN.kt | 6 +- src/zh/mangabz/build.gradle | 3 +- .../extension/zh/mangabz/CookieInterceptor.kt | 46 --------------- .../tachiyomi/extension/zh/mangabz/Mangabz.kt | 3 +- 17 files changed, 106 insertions(+), 238 deletions(-) create mode 100644 lib/cookieinterceptor/build.gradle.kts create mode 100644 lib/cookieinterceptor/src/main/java/eu/kanade/tachiyomi/lib/cookieinterceptor/CookieInterceptor.kt delete mode 100644 src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/CookieInterceptor.kt delete mode 100644 src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/CookieInterceptor.kt delete mode 100644 src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/CookieInterceptor.kt delete mode 100644 src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/CookieInterceptor.kt delete mode 100644 src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/CookieInterceptor.kt diff --git a/lib/cookieinterceptor/build.gradle.kts b/lib/cookieinterceptor/build.gradle.kts new file mode 100644 index 000000000..de414c340 --- /dev/null +++ b/lib/cookieinterceptor/build.gradle.kts @@ -0,0 +1,23 @@ +plugins { + id("com.android.library") + kotlin("android") +} + +android { + compileSdk = AndroidConfig.compileSdk + + defaultConfig { + minSdk = AndroidConfig.minSdk + } + + namespace = "eu.kanade.tachiyomi.lib.cookieinterceptor" +} + +repositories { + mavenCentral() +} + +dependencies { + compileOnly(libs.kotlin.stdlib) + compileOnly(libs.okhttp) +} diff --git a/lib/cookieinterceptor/src/main/java/eu/kanade/tachiyomi/lib/cookieinterceptor/CookieInterceptor.kt b/lib/cookieinterceptor/src/main/java/eu/kanade/tachiyomi/lib/cookieinterceptor/CookieInterceptor.kt new file mode 100644 index 000000000..4b2b891ce --- /dev/null +++ b/lib/cookieinterceptor/src/main/java/eu/kanade/tachiyomi/lib/cookieinterceptor/CookieInterceptor.kt @@ -0,0 +1,59 @@ +package eu.kanade.tachiyomi.lib.cookieinterceptor + +import android.webkit.CookieManager +import okhttp3.Interceptor +import okhttp3.Response + +class CookieInterceptor( + private val domain: String, + private val cookies: List> +) : Interceptor { + constructor(domain: String, cookie: Pair) : this(domain, listOf(cookie)) + + init { + val url = "https://$domain/" + cookies.forEach { + val cookie = "${it.first}=${it.second}; Domain=$domain; Path=/" + setCookie(url, cookie) + } + } + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + if (!request.url.host.endsWith(domain)) return chain.proceed(request) + + val cookieList = request.header("Cookie")?.split("; ") ?: emptyList() + + if (cookies.all { (key, value) -> "$key=$value" in cookieList }) + return chain.proceed(request) + + cookies.forEach { (key, value) -> + setCookie("https://$domain/", "$key=$value; Domain=$domain; Path=/") + } + + val newCookie = buildList(cookieList.size + cookies.size) { + cookieList.filterNotTo(this) { existing -> + cookies.any { (key, _) -> + existing.startsWith("$key=") + } + } + cookies.forEach { (key, value) -> + add("$key=$value") + } + }.joinToString("; ") + + val newRequest = request.newBuilder() + .header("Cookie", newCookie) + .build() + + return chain.proceed(newRequest) + } + + private val cookieManager by lazy { CookieManager.getInstance() } + + private fun setCookie(url: String, value: String) { + try { + cookieManager.setCookie(url, value) + } catch (_: Exception) { } + } +} diff --git a/src/all/mangapark/build.gradle b/src/all/mangapark/build.gradle index 64a873585..95db165dc 100644 --- a/src/all/mangapark/build.gradle +++ b/src/all/mangapark/build.gradle @@ -6,3 +6,7 @@ ext { } apply from: "$rootDir/common.gradle" + +dependencies { + implementation(project(":lib:cookieinterceptor")) +} diff --git a/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/CookieInterceptor.kt b/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/CookieInterceptor.kt deleted file mode 100644 index 09a4691b7..000000000 --- a/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/CookieInterceptor.kt +++ /dev/null @@ -1,46 +0,0 @@ -package eu.kanade.tachiyomi.extension.all.mangapark - -import android.util.Log -import android.webkit.CookieManager -import okhttp3.Interceptor -import okhttp3.Response - -class CookieInterceptor( - private val domain: String, - private val key: String, - private val value: String, -) : Interceptor { - - init { - val url = "https://$domain/" - val cookie = "$key=$value; Domain=$domain; Path=/" - setCookie(url, cookie) - } - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - if (!request.url.host.endsWith(domain)) return chain.proceed(request) - - val cookie = "$key=$value" - val cookieList = request.header("Cookie")?.split("; ") ?: emptyList() - if (cookie in cookieList) return chain.proceed(request) - - setCookie("https://$domain/", "$cookie; Domain=$domain; Path=/") - val prefix = "$key=" - val newCookie = buildList(cookieList.size + 1) { - cookieList.filterNotTo(this) { it.startsWith(prefix) } - add(cookie) - }.joinToString("; ") - val newRequest = request.newBuilder().header("Cookie", newCookie).build() - return chain.proceed(newRequest) - } - - private fun setCookie(url: String, value: String) { - try { - CookieManager.getInstance().setCookie(url, value) - } catch (e: Exception) { - // Probably running on Tachidesk - Log.e("MangaPark", "failed to set cookie", e) - } - } -} diff --git a/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/MangaPark.kt b/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/MangaPark.kt index 13c3d437b..c9c1de551 100644 --- a/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/MangaPark.kt +++ b/src/all/mangapark/src/eu/kanade/tachiyomi/extension/all/mangapark/MangaPark.kt @@ -4,6 +4,7 @@ import android.app.Application import android.widget.Toast import androidx.preference.ListPreference import androidx.preference.PreferenceScreen +import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.interceptor.rateLimitHost @@ -52,7 +53,7 @@ class MangaPark( private val json: Json by injectLazy() override val client = network.cloudflareClient.newBuilder() - .addInterceptor(CookieInterceptor(domain, "nsfw", "2")) + .addNetworkInterceptor(CookieInterceptor(domain, "nsfw" to "2")) .rateLimitHost(apiUrl.toHttpUrl(), 1) .build() diff --git a/src/en/mangaplanet/build.gradle b/src/en/mangaplanet/build.gradle index 6a8362cfe..4a66a5d08 100644 --- a/src/en/mangaplanet/build.gradle +++ b/src/en/mangaplanet/build.gradle @@ -8,5 +8,6 @@ ext { apply from: "$rootDir/common.gradle" dependencies { + implementation(project(":lib:cookieinterceptor")) implementation(project(":lib:speedbinb")) } diff --git a/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/CookieInterceptor.kt b/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/CookieInterceptor.kt deleted file mode 100644 index 9511aff1a..000000000 --- a/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/CookieInterceptor.kt +++ /dev/null @@ -1,46 +0,0 @@ -package eu.kanade.tachiyomi.extension.en.mangaplanet - -import android.util.Log -import android.webkit.CookieManager -import okhttp3.Interceptor -import okhttp3.Response - -class CookieInterceptor( - private val domain: String, - private val key: String, - private val value: String, -) : Interceptor { - - init { - val url = "https://$domain/" - val cookie = "$key=$value; Domain=$domain; Path=/" - setCookie(url, cookie) - } - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - if (!request.url.host.endsWith(domain)) return chain.proceed(request) - - val cookie = "$key=$value" - val cookieList = request.header("Cookie")?.split("; ") ?: emptyList() - if (cookie in cookieList) return chain.proceed(request) - - setCookie("https://$domain/", "$cookie; Domain=$domain; Path=/") - val prefix = "$key=" - val newCookie = buildList(cookieList.size + 1) { - cookieList.filterNotTo(this) { it.startsWith(prefix) } - add(cookie) - }.joinToString("; ") - val newRequest = request.newBuilder().header("Cookie", newCookie).build() - return chain.proceed(newRequest) - } - - private fun setCookie(url: String, value: String) { - try { - CookieManager.getInstance().setCookie(url, value) - } catch (e: Exception) { - // Probably running on Tachidesk - Log.e("MangaPlanet", "failed to set cookie", e) - } - } -} diff --git a/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/MangaPlanet.kt b/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/MangaPlanet.kt index 27be22be6..791009e19 100644 --- a/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/MangaPlanet.kt +++ b/src/en/mangaplanet/src/eu/kanade/tachiyomi/extension/en/mangaplanet/MangaPlanet.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.extension.en.mangaplanet +import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor import eu.kanade.tachiyomi.lib.speedbinb.SpeedBinbInterceptor import eu.kanade.tachiyomi.lib.speedbinb.SpeedBinbReader import eu.kanade.tachiyomi.network.GET @@ -35,7 +36,7 @@ class MangaPlanet : ParsedHttpSource() { override val client = network.client.newBuilder() .addInterceptor(SpeedBinbInterceptor(json)) - .addInterceptor(CookieInterceptor(baseUrl.toHttpUrl().host, "mpaconf", "18")) + .addNetworkInterceptor(CookieInterceptor(baseUrl.toHttpUrl().host, "mpaconf" to "18")) .build() override fun headersBuilder() = super.headersBuilder() diff --git a/src/en/toonily/build.gradle b/src/en/toonily/build.gradle index 29b0ddb15..a3518e6fc 100644 --- a/src/en/toonily/build.gradle +++ b/src/en/toonily/build.gradle @@ -8,3 +8,7 @@ ext { } apply from: "$rootDir/common.gradle" + +dependencies { + implementation(project(":lib:cookieinterceptor")) +} diff --git a/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/CookieInterceptor.kt b/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/CookieInterceptor.kt deleted file mode 100644 index e73d3f1ba..000000000 --- a/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/CookieInterceptor.kt +++ /dev/null @@ -1,46 +0,0 @@ -package eu.kanade.tachiyomi.extension.en.toonily - -import android.util.Log -import android.webkit.CookieManager -import okhttp3.Interceptor -import okhttp3.Response - -class CookieInterceptor( - private val domain: String, - private val key: String, - private val value: String, -) : Interceptor { - - init { - val url = "https://$domain/" - val cookie = "$key=$value; Domain=$domain; Path=/" - setCookie(url, cookie) - } - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - if (!request.url.host.endsWith(domain)) return chain.proceed(request) - - val cookie = "$key=$value" - val cookieList = request.header("Cookie")?.split("; ") ?: emptyList() - if (cookie in cookieList) return chain.proceed(request) - - setCookie("https://$domain/", "$cookie; Domain=$domain; Path=/") - val prefix = "$key=" - val newCookie = buildList(cookieList.size + 1) { - cookieList.filterNotTo(this) { it.startsWith(prefix) } - add(cookie) - }.joinToString("; ") - val newRequest = request.newBuilder().header("Cookie", newCookie).build() - return chain.proceed(newRequest) - } - - private fun setCookie(url: String, value: String) { - try { - CookieManager.getInstance().setCookie(url, value) - } catch (e: Exception) { - // Probably running on Tachidesk - Log.e("Toonily", "failed to set cookie", e) - } - } -} diff --git a/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/Toonily.kt b/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/Toonily.kt index 579002640..3324db7ca 100644 --- a/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/Toonily.kt +++ b/src/en/toonily/src/eu/kanade/tachiyomi/extension/en/toonily/Toonily.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.extension.en.toonily +import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor import eu.kanade.tachiyomi.multisrc.madara.Madara import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.FilterList @@ -17,9 +18,8 @@ class Toonily : Madara( SimpleDateFormat("MMM d, yy", Locale.US), ) { - private val cookieInterceptor = CookieInterceptor(domain, "toonily-mature", "1") override val client: OkHttpClient = super.client.newBuilder() - .addNetworkInterceptor(cookieInterceptor) + .addNetworkInterceptor(CookieInterceptor(domain, "toonily-mature" to "1")) .build() override val mangaSubString = "webtoon" diff --git a/src/vi/hentaivn/build.gradle b/src/vi/hentaivn/build.gradle index 5e7d4a0c5..db7cef895 100644 --- a/src/vi/hentaivn/build.gradle +++ b/src/vi/hentaivn/build.gradle @@ -8,5 +8,6 @@ ext { apply from: "$rootDir/common.gradle" dependencies { + implementation(project(":lib:cookieinterceptor")) implementation(project(":lib:randomua")) } diff --git a/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/CookieInterceptor.kt b/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/CookieInterceptor.kt deleted file mode 100644 index ce312b321..000000000 --- a/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/CookieInterceptor.kt +++ /dev/null @@ -1,46 +0,0 @@ -package eu.kanade.tachiyomi.extension.vi.hentaivn - -import android.util.Log -import android.webkit.CookieManager -import okhttp3.Interceptor -import okhttp3.Response - -class CookieInterceptor( - private val domain: String, - private val key: String, - private val value: String, -) : Interceptor { - - init { - val url = "https://$domain/" - val cookie = "$key=$value; Domain=$domain; Path=/" - setCookie(url, cookie) - } - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - if (!request.url.host.endsWith(domain)) return chain.proceed(request) - - val cookie = "$key=$value" - val cookieList = request.header("Cookie")?.split("; ") ?: emptyList() - if (cookie in cookieList) return chain.proceed(request) - - setCookie("https://$domain/", "$cookie; Domain=$domain; Path=/") - val prefix = "$key=" - val newCookie = buildList(cookieList.size + 1) { - cookieList.filterNotTo(this) { it.startsWith(prefix) } - add(cookie) - }.joinToString("; ") - val newRequest = request.newBuilder().header("Cookie", newCookie).build() - return chain.proceed(newRequest) - } - - private fun setCookie(url: String, value: String) { - try { - CookieManager.getInstance().setCookie(url, value) - } catch (e: Exception) { - // Probably running on Tachidesk - Log.e("HentaiVN", "failed to set cookie", e) - } - } -} diff --git a/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/HentaiVN.kt b/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/HentaiVN.kt index 3e6e02baf..7b7425305 100644 --- a/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/HentaiVN.kt +++ b/src/vi/hentaivn/src/eu/kanade/tachiyomi/extension/vi/hentaivn/HentaiVN.kt @@ -8,6 +8,7 @@ import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import androidx.preference.SwitchPreferenceCompat import eu.kanade.tachiyomi.extension.BuildConfig +import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor import eu.kanade.tachiyomi.lib.randomua.getPrefCustomUA import eu.kanade.tachiyomi.lib.randomua.getPrefUAType import eu.kanade.tachiyomi.lib.randomua.setRandomUserAgent @@ -62,8 +63,9 @@ class HentaiVN : ParsedHttpSource(), ConfigurableSource { val domain = baseUrl.toHttpUrl().host baseClient.newBuilder() - .addNetworkInterceptor(CookieInterceptor(domain, "view1", "1")) - .addNetworkInterceptor(CookieInterceptor(domain, "view4", "1")) + .addNetworkInterceptor( + CookieInterceptor(domain, listOf("view1" to "1", "view4" to "1")), + ) .setRandomUserAgent( preferences.getPrefUAType(), preferences.getPrefCustomUA(), diff --git a/src/zh/mangabz/build.gradle b/src/zh/mangabz/build.gradle index ef719ac8c..85a0ce1a1 100644 --- a/src/zh/mangabz/build.gradle +++ b/src/zh/mangabz/build.gradle @@ -7,5 +7,6 @@ ext { apply from: "$rootDir/common.gradle" dependencies { - implementation project(':lib:unpacker') + implementation(project(":lib:cookieinterceptor")) + implementation(project(":lib:unpacker")) } diff --git a/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/CookieInterceptor.kt b/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/CookieInterceptor.kt deleted file mode 100644 index 6bf204a2d..000000000 --- a/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/CookieInterceptor.kt +++ /dev/null @@ -1,46 +0,0 @@ -package eu.kanade.tachiyomi.extension.zh.mangabz - -import android.util.Log -import android.webkit.CookieManager -import okhttp3.Interceptor -import okhttp3.Response - -class CookieInterceptor( - private val domain: String, - private val key: String, - private val value: String, -) : Interceptor { - - init { - val url = "https://$domain/" - val cookie = "$key=$value; Domain=$domain; Path=/" - setCookie(url, cookie) - } - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - if (!request.url.host.endsWith(domain)) return chain.proceed(request) - - val cookie = "$key=$value" - val cookieList = request.header("Cookie")?.split("; ") ?: emptyList() - if (cookie in cookieList) return chain.proceed(request) - - setCookie("https://$domain/", "$cookie; Domain=$domain; Path=/") - val prefix = "$key=" - val newCookie = buildList(cookieList.size + 1) { - cookieList.filterNotTo(this) { it.startsWith(prefix) } - add(cookie) - }.joinToString("; ") - val newRequest = request.newBuilder().header("Cookie", newCookie).build() - return chain.proceed(newRequest) - } - - private fun setCookie(url: String, value: String) { - try { - CookieManager.getInstance().setCookie(url, value) - } catch (e: Exception) { - // Probably running on Tachidesk - Log.e("Mangabz", "failed to set cookie", e) - } - } -} diff --git a/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/Mangabz.kt b/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/Mangabz.kt index cb59fa6c8..ec113a976 100644 --- a/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/Mangabz.kt +++ b/src/zh/mangabz/src/eu/kanade/tachiyomi/extension/zh/mangabz/Mangabz.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.extension.zh.mangabz import android.app.Application import androidx.preference.PreferenceScreen +import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor import eu.kanade.tachiyomi.lib.unpacker.SubstringExtractor import eu.kanade.tachiyomi.lib.unpacker.Unpacker import eu.kanade.tachiyomi.network.GET @@ -38,7 +39,7 @@ class Mangabz : MangabzTheme("Mangabz"), ConfigurableSource { _baseUrl = "https://" + mirror.domain urlSuffix = mirror.urlSuffix - val cookieInterceptor = CookieInterceptor(mirror.domain, mirror.langCookie, preferences.lang) + val cookieInterceptor = CookieInterceptor(mirror.domain, mirror.langCookie to preferences.lang) client = network.client.newBuilder() .rateLimit(5) .addNetworkInterceptor(cookieInterceptor)