From 4ee1d72b6f8278d84da6f75d218a51261d175e18 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 --- .../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 26838629d..2a1ca25d6 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 @@ -59,6 +59,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 dae6d2e3b..7f958c97c 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 @@ -294,6 +294,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, DomainManga.SHOW_ALL.toInt()) 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 ec7af77f1..921d556e0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -60,4 +60,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 b697a9f16..2e43e01b7 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 5a3789eec..e5d1c2656 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 e738feaa1..358652416 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) } /** @@ -368,8 +369,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 cd77a21d2..b8cc5916d 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 @@ -33,6 +33,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 @@ -215,6 +216,28 @@ class SettingsAdvancedController( 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 b063e5ac7..d58f437f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -468,6 +468,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