zaimanhua: revalidate token if error code not 0 (#5785)

This commit is contained in:
zhongfly 2024-10-30 23:08:00 +08:00 committed by GitHub
parent c37b370553
commit 5d2badc707
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 4 deletions

View File

@ -1,7 +1,7 @@
ext {
extName = 'Zaimanhua'
extClass = '.Zaimanhua'
extVersionCode = 2
extVersionCode = 3
}
apply from: "$rootDir/common.gradle"

View File

@ -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 <reified T> Response.parseAs(): T {
return json.decodeFromString(body.string())
}
inline fun <reified T> ResponseBody.parseAs(): T {
return json.decodeFromString(this.string())
}
fun parseStatus(status: String): Int = when (status) {
"连载中" -> SManga.ONGOING
"已完结" -> SManga.COMPLETED

View File

@ -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<Json>()
@ -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<SimpleResponseDto>().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",

View File

@ -140,6 +140,11 @@ class DataWrapperDto<T>(
val data: T?,
)
@Serializable
class SimpleResponseDto(
val errno: Int = 0,
)
@Serializable
class ResponseDto<T>(
val errno: Int = 0,