From bd2dfaad2eaccf03fe019dff4235c1c6c713e75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Fran=C3=A7a=20da=20Silva?= Date: Mon, 9 Mar 2020 15:10:10 -0300 Subject: [PATCH] Add an UserAgent Interceptor to Cloudflare Client (#2685) --- .../network/CloudflareInterceptor.kt | 12 +--------- .../kanade/tachiyomi/network/NetworkHelper.kt | 1 + .../tachiyomi/network/UserAgentInterceptor.kt | 22 +++++++++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/network/UserAgentInterceptor.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt index 02dbe4644..919f110d9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt @@ -57,17 +57,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { .firstOrNull { it.name == "cf_clearance" } resolveWithWebView(originalRequest, oldCookie) - // Avoid use empty User-Agent - return if (originalRequest.header("User-Agent").isNullOrEmpty()) { - val newRequest = originalRequest - .newBuilder() - .removeHeader("User-Agent") - .addHeader("User-Agent", HttpSource.DEFAULT_USERAGENT) - .build() - chain.proceed(newRequest) - } else { - chain.proceed(originalRequest) - } + return chain.proceed(originalRequest) } catch (e: Exception) { // Because OkHttp's enqueue only handles IOExceptions, wrap the exception so that // we don't crash the entire app diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt index c19fbdd39..fb4c6d486 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -19,6 +19,7 @@ class NetworkHelper(context: Context) { .build() val cloudflareClient = client.newBuilder() + .addInterceptor(UserAgentInterceptor()) .addInterceptor(CloudflareInterceptor(context)) .build() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/UserAgentInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/UserAgentInterceptor.kt new file mode 100644 index 000000000..52d37a77a --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/network/UserAgentInterceptor.kt @@ -0,0 +1,22 @@ +package eu.kanade.tachiyomi.network + +import eu.kanade.tachiyomi.source.online.HttpSource +import okhttp3.Interceptor +import okhttp3.Response + +class UserAgentInterceptor : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { + val originalRequest = chain.request() + + return if (originalRequest.header("User-Agent").isNullOrEmpty()) { + val newRequest = originalRequest + .newBuilder() + .removeHeader("User-Agent") + .addHeader("User-Agent", HttpSource.DEFAULT_USERAGENT) + .build() + chain.proceed(newRequest) + } else { + chain.proceed(originalRequest) + } + } +}