|
|
|
|
@ -4,6 +4,7 @@ import okhttp3.Interceptor
|
|
|
|
|
import okhttp3.Response
|
|
|
|
|
import okhttp3.internal.closeQuietly
|
|
|
|
|
import org.jsoup.Jsoup
|
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.CloudFlareBlockedException
|
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException
|
|
|
|
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
|
|
|
import java.net.HttpURLConnection.HTTP_FORBIDDEN
|
|
|
|
|
@ -17,9 +18,17 @@ class CloudFlareInterceptor : Interceptor {
|
|
|
|
|
val content = response.body?.let { response.peekBody(Long.MAX_VALUE) }?.byteStream()?.use {
|
|
|
|
|
Jsoup.parse(it, Charsets.UTF_8.name(), response.request.url.toString())
|
|
|
|
|
} ?: return response
|
|
|
|
|
if (content.getElementById("challenge-error-title") != null) {
|
|
|
|
|
val hasCaptcha = content.getElementById("challenge-error-title") != null
|
|
|
|
|
val isBlocked = content.selectFirst("h2[data-translate=\"blocked_why_headline\"]") != null
|
|
|
|
|
if (hasCaptcha || isBlocked) {
|
|
|
|
|
val request = response.request
|
|
|
|
|
response.closeQuietly()
|
|
|
|
|
if (isBlocked) {
|
|
|
|
|
throw CloudFlareBlockedException(
|
|
|
|
|
url = request.url.toString(),
|
|
|
|
|
source = request.tag(MangaSource::class.java),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
throw CloudFlareProtectedException(
|
|
|
|
|
url = request.url.toString(),
|
|
|
|
|
source = request.tag(MangaSource::class.java),
|
|
|
|
|
@ -27,6 +36,7 @@ class CloudFlareInterceptor : Interceptor {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|