From 7c7bd72c8e1170966b396384d190437da12c3e32 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 14 Jul 2022 23:01:19 -0400 Subject: [PATCH] Make default user agent string configurable (cherry picked from commit 4ee1d72b6f8278d84da6f75d218a51261d175e18) --- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 2 ++ .../kanade/tachiyomi/network/NetworkHelper.kt | 4 ++++ .../interceptor/CloudflareInterceptor.kt | 3 +-- .../interceptor/UserAgentInterceptor.kt | 8 +++++-- .../tachiyomi/source/online/HttpSource.kt | 7 ++---- .../ui/setting/SettingsAdvancedController.kt | 23 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 026f40722f..83bdac3717 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -63,6 +63,8 @@ object PreferenceKeys { const val dohProvider = "doh_provider" + const val defaultUserAgent = "default_user_agent" + const val defaultChapterFilterByRead = "default_chapter_filter_by_read" const val defaultChapterFilterByDownloaded = "default_chapter_filter_by_downloaded" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 9fbe9fe03d..92de28c6e6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -297,6 +297,8 @@ class PreferencesHelper(val context: Context) { fun dohProvider() = prefs.getInt(Keys.dohProvider, -1) + fun defaultUserAgent() = flowPrefs.getString(Keys.defaultUserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44") + fun lastSearchQuerySearchSettings() = flowPrefs.getString("last_search_query", "") fun filterChapterByRead() = prefs.getInt(Keys.defaultChapterFilterByRead, Manga.SHOW_ALL) 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 ea3106cb86..4eaa5e839f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -59,4 +59,8 @@ class NetworkHelper(context: Context) { .addInterceptor(CloudflareInterceptor(context)) .build() } + + val defaultUserAgent by lazy { + preferences.defaultUserAgent().get() + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt index b697a9f167..2e43e01b71 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt @@ -9,7 +9,6 @@ import android.widget.Toast import androidx.core.content.ContextCompat import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.network.NetworkHelper -import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.system.DeviceUtil import eu.kanade.tachiyomi.util.system.WebViewClientCompat @@ -109,7 +108,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { // Avoid sending empty User-Agent, Chromium WebView will reset to default if empty webview.settings.userAgentString = request.header("User-Agent") - ?: HttpSource.DEFAULT_USER_AGENT + ?: networkHelper.defaultUserAgent webview.webViewClient = object : WebViewClientCompat() { override fun onPageFinished(view: WebView, url: String) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UserAgentInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UserAgentInterceptor.kt index 5a3789eeca..e5d1c26566 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UserAgentInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/interceptor/UserAgentInterceptor.kt @@ -1,10 +1,14 @@ package eu.kanade.tachiyomi.network.interceptor -import eu.kanade.tachiyomi.source.online.HttpSource +import eu.kanade.tachiyomi.network.NetworkHelper import okhttp3.Interceptor import okhttp3.Response +import uy.kohesive.injekt.injectLazy class UserAgentInterceptor : Interceptor { + + private val networkHelper: NetworkHelper by injectLazy() + override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() @@ -12,7 +16,7 @@ class UserAgentInterceptor : Interceptor { val newRequest = originalRequest .newBuilder() .removeHeader("User-Agent") - .addHeader("User-Agent", HttpSource.DEFAULT_USER_AGENT) + .addHeader("User-Agent", networkHelper.defaultUserAgent) .build() chain.proceed(newRequest) } else { diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt index 0a390de7cc..1868581ea2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt @@ -15,6 +15,7 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import rx.Observable +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.net.URI import java.net.URISyntaxException @@ -67,7 +68,7 @@ abstract class HttpSource : CatalogueSource { * Headers builder for requests. Implementations can override this method for custom headers. */ protected open fun headersBuilder() = Headers.Builder().apply { - add("User-Agent", DEFAULT_USER_AGENT) + add("User-Agent", network.defaultUserAgent) } /** @@ -369,8 +370,4 @@ abstract class HttpSource : CatalogueSource { * Returns the list of filters for the source. */ override fun getFilterList() = FilterList() - - companion object { - const val DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44" - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt index 666058b926..f93fd8b93e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt @@ -32,6 +32,7 @@ import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.preference.bindTo import eu.kanade.tachiyomi.util.preference.defaultValue +import eu.kanade.tachiyomi.util.preference.editTextPreference import eu.kanade.tachiyomi.util.preference.entriesRes import eu.kanade.tachiyomi.util.preference.intListPreference import eu.kanade.tachiyomi.util.preference.listPreference @@ -210,6 +211,28 @@ class SettingsAdvancedController : SettingsController() { true } } + editTextPreference { + key = Keys.defaultUserAgent + titleRes = R.string.pref_user_agent_string + text = preferences.defaultUserAgent().get() + summary = network.defaultUserAgent + + onChange { + activity?.toast(R.string.requires_app_restart) + true + } + } + if (preferences.defaultUserAgent().isSet()) { + preference { + key = "pref_reset_user_agent" + titleRes = R.string.pref_reset_user_agent_string + + onClick { + preferences.defaultUserAgent().delete() + activity?.toast(R.string.requires_app_restart) + } + } + } } preferenceCategory { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2632544b80..7bdbc96b3d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -466,6 +466,8 @@ Network Clear cookies DNS over HTTPS (DoH) + Default user agent string + Reset default user agent string Requires app restart to take effect Cookies cleared Data