move CookieInterceptor to shared lib (#1356)

* CookieInterceptor lib

* accept list of cookies
This commit is contained in:
AwkwardPeak7 2024-02-18 16:06:47 +05:00 committed by Draff
parent 8e7bba9d87
commit 9fa6b8cb51
17 changed files with 106 additions and 238 deletions

View File

@ -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)
}

View File

@ -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<Pair<String, String>>
) : Interceptor {
constructor(domain: String, cookie: Pair<String, String>) : 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) { }
}
}

View File

@ -6,3 +6,7 @@ ext {
} }
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"
dependencies {
implementation(project(":lib:cookieinterceptor"))
}

View File

@ -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)
}
}
}

View File

@ -4,6 +4,7 @@ import android.app.Application
import android.widget.Toast import android.widget.Toast
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.interceptor.rateLimitHost import eu.kanade.tachiyomi.network.interceptor.rateLimitHost
@ -52,7 +53,7 @@ class MangaPark(
private val json: Json by injectLazy() private val json: Json by injectLazy()
override val client = network.cloudflareClient.newBuilder() override val client = network.cloudflareClient.newBuilder()
.addInterceptor(CookieInterceptor(domain, "nsfw", "2")) .addNetworkInterceptor(CookieInterceptor(domain, "nsfw" to "2"))
.rateLimitHost(apiUrl.toHttpUrl(), 1) .rateLimitHost(apiUrl.toHttpUrl(), 1)
.build() .build()

View File

@ -8,5 +8,6 @@ ext {
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"
dependencies { dependencies {
implementation(project(":lib:cookieinterceptor"))
implementation(project(":lib:speedbinb")) implementation(project(":lib:speedbinb"))
} }

View File

@ -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)
}
}
}

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.extension.en.mangaplanet 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.SpeedBinbInterceptor
import eu.kanade.tachiyomi.lib.speedbinb.SpeedBinbReader import eu.kanade.tachiyomi.lib.speedbinb.SpeedBinbReader
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
@ -35,7 +36,7 @@ class MangaPlanet : ParsedHttpSource() {
override val client = network.client.newBuilder() override val client = network.client.newBuilder()
.addInterceptor(SpeedBinbInterceptor(json)) .addInterceptor(SpeedBinbInterceptor(json))
.addInterceptor(CookieInterceptor(baseUrl.toHttpUrl().host, "mpaconf", "18")) .addNetworkInterceptor(CookieInterceptor(baseUrl.toHttpUrl().host, "mpaconf" to "18"))
.build() .build()
override fun headersBuilder() = super.headersBuilder() override fun headersBuilder() = super.headersBuilder()

View File

@ -8,3 +8,7 @@ ext {
} }
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"
dependencies {
implementation(project(":lib:cookieinterceptor"))
}

View File

@ -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)
}
}
}

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.extension.en.toonily 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.multisrc.madara.Madara
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.FilterList
@ -17,9 +18,8 @@ class Toonily : Madara(
SimpleDateFormat("MMM d, yy", Locale.US), SimpleDateFormat("MMM d, yy", Locale.US),
) { ) {
private val cookieInterceptor = CookieInterceptor(domain, "toonily-mature", "1")
override val client: OkHttpClient = super.client.newBuilder() override val client: OkHttpClient = super.client.newBuilder()
.addNetworkInterceptor(cookieInterceptor) .addNetworkInterceptor(CookieInterceptor(domain, "toonily-mature" to "1"))
.build() .build()
override val mangaSubString = "webtoon" override val mangaSubString = "webtoon"

View File

@ -8,5 +8,6 @@ ext {
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"
dependencies { dependencies {
implementation(project(":lib:cookieinterceptor"))
implementation(project(":lib:randomua")) implementation(project(":lib:randomua"))
} }

View File

@ -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)
}
}
}

View File

@ -8,6 +8,7 @@ import androidx.preference.ListPreference
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreferenceCompat import androidx.preference.SwitchPreferenceCompat
import eu.kanade.tachiyomi.extension.BuildConfig 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.getPrefCustomUA
import eu.kanade.tachiyomi.lib.randomua.getPrefUAType import eu.kanade.tachiyomi.lib.randomua.getPrefUAType
import eu.kanade.tachiyomi.lib.randomua.setRandomUserAgent import eu.kanade.tachiyomi.lib.randomua.setRandomUserAgent
@ -62,8 +63,9 @@ class HentaiVN : ParsedHttpSource(), ConfigurableSource {
val domain = baseUrl.toHttpUrl().host val domain = baseUrl.toHttpUrl().host
baseClient.newBuilder() baseClient.newBuilder()
.addNetworkInterceptor(CookieInterceptor(domain, "view1", "1")) .addNetworkInterceptor(
.addNetworkInterceptor(CookieInterceptor(domain, "view4", "1")) CookieInterceptor(domain, listOf("view1" to "1", "view4" to "1")),
)
.setRandomUserAgent( .setRandomUserAgent(
preferences.getPrefUAType(), preferences.getPrefUAType(),
preferences.getPrefCustomUA(), preferences.getPrefCustomUA(),

View File

@ -7,5 +7,6 @@ ext {
apply from: "$rootDir/common.gradle" apply from: "$rootDir/common.gradle"
dependencies { dependencies {
implementation project(':lib:unpacker') implementation(project(":lib:cookieinterceptor"))
implementation(project(":lib:unpacker"))
} }

View File

@ -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)
}
}
}

View File

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.extension.zh.mangabz
import android.app.Application import android.app.Application
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.lib.cookieinterceptor.CookieInterceptor
import eu.kanade.tachiyomi.lib.unpacker.SubstringExtractor import eu.kanade.tachiyomi.lib.unpacker.SubstringExtractor
import eu.kanade.tachiyomi.lib.unpacker.Unpacker import eu.kanade.tachiyomi.lib.unpacker.Unpacker
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
@ -38,7 +39,7 @@ class Mangabz : MangabzTheme("Mangabz"), ConfigurableSource {
_baseUrl = "https://" + mirror.domain _baseUrl = "https://" + mirror.domain
urlSuffix = mirror.urlSuffix 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() client = network.client.newBuilder()
.rateLimit(5) .rateLimit(5)
.addNetworkInterceptor(cookieInterceptor) .addNetworkInterceptor(cookieInterceptor)