move CookieInterceptor to shared lib (#1356)
* CookieInterceptor lib * accept list of cookies
This commit is contained in:
parent
8e7bba9d87
commit
9fa6b8cb51
|
@ -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)
|
||||
}
|
|
@ -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) { }
|
||||
}
|
||||
}
|
|
@ -6,3 +6,7 @@ ext {
|
|||
}
|
||||
|
||||
apply from: "$rootDir/common.gradle"
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lib:cookieinterceptor"))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -8,5 +8,6 @@ ext {
|
|||
apply from: "$rootDir/common.gradle"
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lib:cookieinterceptor"))
|
||||
implementation(project(":lib:speedbinb"))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -8,3 +8,7 @@ ext {
|
|||
}
|
||||
|
||||
apply from: "$rootDir/common.gradle"
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lib:cookieinterceptor"))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
|
|
|
@ -8,5 +8,6 @@ ext {
|
|||
apply from: "$rootDir/common.gradle"
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lib:cookieinterceptor"))
|
||||
implementation(project(":lib:randomua"))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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(),
|
||||
|
|
|
@ -7,5 +7,6 @@ ext {
|
|||
apply from: "$rootDir/common.gradle"
|
||||
|
||||
dependencies {
|
||||
implementation project(':lib:unpacker')
|
||||
implementation(project(":lib:cookieinterceptor"))
|
||||
implementation(project(":lib:unpacker"))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue