From eed4186aa42182a5268bb34b30d135998839820b Mon Sep 17 00:00:00 2001 From: zhongfly <11155705+zhongfly@users.noreply.github.com> Date: Wed, 30 Oct 2024 23:08:00 +0800 Subject: [PATCH] zaimanhua: revalidate token if error code not 0 (#5785) --- src/zh/zaimanhua/build.gradle | 2 +- .../tachiyomi/extension/zh/zaimanhua/Common.kt | 5 +++++ .../extension/zh/zaimanhua/Zaimanhua.kt | 17 ++++++++++++++--- .../extension/zh/zaimanhua/ZaimanhuaDto.kt | 5 +++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/zh/zaimanhua/build.gradle b/src/zh/zaimanhua/build.gradle index 4a388751a..a34296ae5 100644 --- a/src/zh/zaimanhua/build.gradle +++ b/src/zh/zaimanhua/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Zaimanhua' extClass = '.Zaimanhua' - extVersionCode = 2 + extVersionCode = 3 } apply from: "$rootDir/common.gradle" diff --git a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Common.kt b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Common.kt index 64e93963f..63f67a4e5 100644 --- a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Common.kt +++ b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Common.kt @@ -4,6 +4,7 @@ import eu.kanade.tachiyomi.source.model.SManga import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import okhttp3.Response +import okhttp3.ResponseBody import uy.kohesive.injekt.injectLazy val json: Json by injectLazy() @@ -12,6 +13,10 @@ inline fun Response.parseAs(): T { return json.decodeFromString(body.string()) } +inline fun ResponseBody.parseAs(): T { + return json.decodeFromString(this.string()) +} + fun parseStatus(status: String): Int = when (status) { "连载中" -> SManga.ONGOING "已完结" -> SManga.COMPLETED diff --git a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt index 6c4c0de93..004d87b18 100644 --- a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt +++ b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt @@ -44,6 +44,7 @@ class Zaimanhua : HttpSource(), ConfigurableSource { override val baseUrl = "https://manhua.zaimanhua.com" private val apiUrl = "https://v4api.zaimanhua.com/app/v1" private val accountApiUrl = "https://account-api.zaimanhua.com/v1" + private val checkTokenRegex = Regex("""$apiUrl/comic/(detail|chapter)""") private val json by injectLazy() @@ -58,12 +59,18 @@ class Zaimanhua : HttpSource(), ConfigurableSource { private fun authIntercept(chain: Interceptor.Chain): Response { val request = chain.request() - if (request.url.host != "v4api.zaimanhua.com" || !request.headers["authorization"].isNullOrBlank()) { + if (request.url.host != "v4api.zaimanhua.com" || + (!request.headers["authorization"].isNullOrBlank() && !request.url.toString().contains(checkTokenRegex)) + ) { return chain.proceed(request) } + val response = chain.proceed(request) + if (!request.headers["authorization"].isNullOrBlank() && response.peekBody(Long.MAX_VALUE).parseAs().errno == 0) { + return response + } var token: String = preferences.getString("TOKEN", "")!! - if (token.isBlank() || !isValid(token)) { + if (!isValid(token)) { val username = preferences.getString("USERNAME", "")!! val password = preferences.getString("PASSWORD", "")!! token = getToken(username, password) @@ -71,12 +78,15 @@ class Zaimanhua : HttpSource(), ConfigurableSource { preferences.edit().putString("TOKEN", "").apply() preferences.edit().putString("USERNAME", "").apply() preferences.edit().putString("PASSWORD", "").apply() - return chain.proceed(request) + return response } else { preferences.edit().putString("TOKEN", token).apply() apiHeaders = apiHeaders.newBuilder().setToken(token).build() } + } else if (!request.headers["authorization"].isNullOrBlank() && request.headers["authorization"] == "Bearer $token") { + return response } + val authRequest = request.newBuilder().apply { header("authorization", "Bearer $token") }.build() @@ -90,6 +100,7 @@ class Zaimanhua : HttpSource(), ConfigurableSource { private var apiHeaders = headersBuilder().setToken(preferences.getString("TOKEN", "")!!).build() private fun isValid(token: String): Boolean { + if (token.isBlank()) return false val response = client.newCall( GET( "$accountApiUrl/userInfo/get", diff --git a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/ZaimanhuaDto.kt b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/ZaimanhuaDto.kt index 2fb15e862..2dcf6ad2f 100644 --- a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/ZaimanhuaDto.kt +++ b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/ZaimanhuaDto.kt @@ -140,6 +140,11 @@ class DataWrapperDto( val data: T?, ) +@Serializable +class SimpleResponseDto( + val errno: Int = 0, +) + @Serializable class ResponseDto( val errno: Int = 0,