From 72f297f45729103d2cacf03c193e3ccc151f3852 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Wed, 14 Apr 2021 20:14:59 -0400 Subject: [PATCH] DoH Google + More updates to cloudflare / useragent from upstream Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com> --- app/build.gradle.kts | 6 +-- .../java/eu/kanade/tachiyomi/Migrations.kt | 15 ++++++ .../data/preference/PreferenceKeys.kt | 2 +- .../data/preference/PreferencesHelper.kt | 2 +- .../tachiyomi/network/AndroidCookieJar.kt | 4 +- .../kanade/tachiyomi/network/DohProviders.kt | 40 ++++++++++++++++ .../tachiyomi/source/online/HttpSource.kt | 2 +- .../ui/reader/loader/ChapterLoader.kt | 6 +-- .../ui/reader/loader/HttpPageLoader.kt | 47 ++++++++++--------- .../ui/setting/SettingsAdvancedController.kt | 19 +++++--- app/src/main/res/values/strings.xml | 6 ++- buildSrc/src/main/kotlin/Dependencies.kt | 3 +- 12 files changed, 110 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/network/DohProviders.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a10ebcb657..589fd5cd5f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -130,9 +130,9 @@ dependencies { implementation("com.github.tfcporciuncula:flow-preferences:1.3.4") // Network client - implementation("com.squareup.okhttp3:okhttp:${Versions.OKHTTP}") - implementation("com.squareup.okhttp3:logging-interceptor:${Versions.OKHTTP}") - implementation("com.squareup.okhttp3:okhttp-dnsoverhttps:${Versions.OKHTTP}") + implementation("com.squareup.okhttp3:okhttp:${Versions.okhttp}") + implementation("com.squareup.okhttp3:logging-interceptor:${Versions.okhttp}") + implementation("com.squareup.okhttp3:okhttp-dnsoverhttps:${Versions.okhttp}") implementation("com.squareup.okio:okio:2.10.0") // Chucker diff --git a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt index 4c10eb9514..b4a97664e6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt @@ -1,13 +1,17 @@ package eu.kanade.tachiyomi +import androidx.core.content.edit +import androidx.preference.PreferenceManager import eu.kanade.tachiyomi.data.backup.BackupCreatorJob import eu.kanade.tachiyomi.data.download.DownloadProvider import eu.kanade.tachiyomi.data.library.LibraryUpdateJob +import eu.kanade.tachiyomi.data.preference.PreferenceKeys import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.updater.UpdaterJob import eu.kanade.tachiyomi.extension.ExtensionUpdateJob +import eu.kanade.tachiyomi.network.PREF_DOH_CLOUDFLARE import eu.kanade.tachiyomi.ui.library.LibraryPresenter import eu.kanade.tachiyomi.util.system.toast import uy.kohesive.injekt.Injekt @@ -96,6 +100,17 @@ object Migrations { context.toast(R.string.myanimelist_relogin) } } + if (oldVersion < 71) { + // Migrate DNS over HTTPS setting + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val wasDohEnabled = prefs.getBoolean("enable_doh", false) + if (wasDohEnabled) { + prefs.edit { + putInt(PreferenceKeys.dohProvider, PREF_DOH_CLOUDFLARE) + remove("enable_doh") + } + } + } return true } return false 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 4cbca1968a..23dfa36351 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 @@ -191,7 +191,7 @@ object PreferenceKeys { const val createLegacyBackup = "create_legacy_backup" - const val enableDoh = "enable_doh" + const val dohProvider = "doh_provider" const val showNsfwSource = "show_nsfw_source" const val showNsfwExtension = "show_nsfw_extension" 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 d44fd0da95..2e3e131a2b 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 @@ -366,5 +366,5 @@ class PreferencesHelper(val context: Context) { fun labelNsfwExtension() = prefs.getBoolean(Keys.labelNsfwExtension, true) fun createLegacyBackup() = flowPrefs.getBoolean(Keys.createLegacyBackup, true) - fun enableDoh() = prefs.getBoolean(Keys.enableDoh, false) + fun dohProvider() = prefs.getInt(Keys.dohProvider, -1) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt b/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt index c93b217ee0..c436b0dbd8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt @@ -12,9 +12,7 @@ class AndroidCookieJar : CookieJar { override fun saveFromResponse(url: HttpUrl, cookies: List) { val urlString = url.toString() - for (cookie in cookies) { - manager.setCookie(urlString, cookie.toString()) - } + cookies.forEach { manager.setCookie(urlString, it.toString()) } } override fun loadForRequest(url: HttpUrl): List { diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/DohProviders.kt b/app/src/main/java/eu/kanade/tachiyomi/network/DohProviders.kt new file mode 100644 index 0000000000..982910f529 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/network/DohProviders.kt @@ -0,0 +1,40 @@ +package eu.kanade.tachiyomi.network + +import okhttp3.HttpUrl.Companion.toHttpUrl +import okhttp3.OkHttpClient +import okhttp3.dnsoverhttps.DnsOverHttps +import java.net.InetAddress + +/** + * Based on https://github.com/square/okhttp/blob/ef5d0c83f7bbd3a0c0534e7ca23cbc4ee7550f3b/okhttp-dnsoverhttps/src/test/java/okhttp3/dnsoverhttps/DohProviders.java + */ + +const val PREF_DOH_CLOUDFLARE = 1 +const val PREF_DOH_GOOGLE = 2 + +fun OkHttpClient.Builder.dohCloudflare() = dns( + DnsOverHttps.Builder().client(build()) + .url("https://cloudflare-dns.com/dns-query".toHttpUrl()) + .bootstrapDnsHosts( + InetAddress.getByName("162.159.36.1"), + InetAddress.getByName("162.159.46.1"), + InetAddress.getByName("1.1.1.1"), + InetAddress.getByName("1.0.0.1"), + InetAddress.getByName("162.159.132.53"), + InetAddress.getByName("2606:4700:4700::1111"), + InetAddress.getByName("2606:4700:4700::1001"), + InetAddress.getByName("2606:4700:4700::0064"), + InetAddress.getByName("2606:4700:4700::6400") + ) + .build() +) + +fun OkHttpClient.Builder.dohGoogle() = dns( + DnsOverHttps.Builder().client(build()) + .url("https://dns.google/dns-query".toHttpUrl()) + .bootstrapDnsHosts( + InetAddress.getByName("8.8.4.4"), + InetAddress.getByName("8.8.8.8") + ) + .build() +) 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 1ef533f810..81f705c66f 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 @@ -34,7 +34,7 @@ abstract class HttpSource : CatalogueSource { // * Preferences that a source may need. // */ // val preferences: SharedPreferences by lazy { -// Injekt.get().getSharedPreferences("source_$id", Context.MODE_PRIVATE) +// Injekt.get().getSharedPreferences(source.getPreferenceKey(), Context.MODE_PRIVATE) // } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt index 280423ae09..1b8c7a0080 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt @@ -34,10 +34,10 @@ class ChapterLoader( return Observable.just(chapter) .doOnNext { chapter.state = ReaderChapter.State.Loading } .observeOn(Schedulers.io()) - .flatMap { + .flatMap { readerChapter -> Timber.d("Loading pages for ${chapter.chapter.name}") - val loader = getPageLoader(it) + val loader = getPageLoader(readerChapter) chapter.pageLoader = loader loader.getPages().take(1).doOnNext { pages -> @@ -47,7 +47,7 @@ class ChapterLoader( .observeOn(AndroidSchedulers.mainThread()) .doOnNext { pages -> if (pages.isEmpty()) { - throw Exception("Page list is empty") + throw Exception(downloadManager.context.getString(R.string.no_pages_found)) } chapter.state = ReaderChapter.State.Loaded(pages) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index d81f64b878..6d6575e748 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -106,7 +106,8 @@ class HttpPageLoader( .getPageListFromCache(chapter.chapter) .onErrorResumeNext { source.fetchPageList(chapter.chapter) } .map { pages -> - pages.mapIndexed { index, page -> // Don't trust sources and use our own indexing + pages.mapIndexed { index, page -> + // Don't trust sources and use our own indexing ReaderPage(index, page.url, page.imageUrl) } } @@ -231,28 +232,32 @@ class HttpPageLoader( private fun HttpSource.getCachedImage(page: ReaderPage): Observable { val imageUrl = page.imageUrl ?: return Observable.just(page) - return Observable.just(page).flatMap { - if (!chapterCache.isImageInCache(imageUrl)) { - cacheImage(page) - } else { - Observable.just(page) + return Observable.just(page) + .flatMap { + if (!chapterCache.isImageInCache(imageUrl)) { + cacheImage(page) + } else { + Observable.just(page) + } } - }.doOnNext { - val readerTheme = preferences.readerTheme().get() - if (readerTheme >= 2) { - val stream = chapterCache.getImageFile(imageUrl).inputStream() - val image = BitmapFactory.decodeStream(stream) - page.bg = ImageUtil.autoSetBackground( - image, - readerTheme == 2, - preferences.context - ) - page.bgType = PagerPageHolder.getBGType(readerTheme, preferences.context) - stream.close() + .doOnNext { + val readerTheme = preferences.readerTheme().get() + if (readerTheme >= 2) { + val stream = chapterCache.getImageFile(imageUrl).inputStream() + val image = BitmapFactory.decodeStream(stream) + page.bg = ImageUtil.autoSetBackground( + image, + readerTheme == 2, + preferences.context + ) + page.bgType = PagerPageHolder.getBGType(readerTheme, preferences.context) + stream.close() + } + page.stream = { chapterCache.getImageFile(imageUrl).inputStream() } + page.status = Page.READY } - page.stream = { chapterCache.getImageFile(imageUrl).inputStream() } - page.status = Page.READY - }.doOnError { page.status = Page.ERROR }.onErrorReturn { page } + .doOnError { page.status = Page.ERROR } + .onErrorReturn { page } } /** 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 fac983d8b8..0109e80aab 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 @@ -23,6 +23,8 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Target import eu.kanade.tachiyomi.data.preference.PreferenceKeys import eu.kanade.tachiyomi.network.NetworkHelper +import eu.kanade.tachiyomi.network.PREF_DOH_CLOUDFLARE +import eu.kanade.tachiyomi.network.PREF_DOH_GOOGLE import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.CrashLogUtil @@ -164,12 +166,17 @@ class SettingsAdvancedController : SettingsController() { } } - switchPreference { - key = "enable_doh" - key = PreferenceKeys.enableDoh - titleRes = R.string.dns_over_https - summaryRes = R.string.requires_app_restart - defaultValue = false + intListPreference(activity) { + key = PreferenceKeys.dohProvider + titleRes = R.string.doh + entriesRes = arrayOf(R.string.disabled, R.string.cloudflare, R.string.google) + entryValues = listOf(-1, PREF_DOH_CLOUDFLARE, PREF_DOH_GOOGLE) + + defaultValue = -1 + onChange { + activity?.toast(R.string.requires_app_restart) + true + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 275d84dde3..a76ca50054 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -51,6 +51,7 @@ Chapters removed. Chapter not found No chapters found + No pages found Remove %1$d downloaded chapter? Remove %1$d downloaded chapters? @@ -630,7 +631,10 @@ Saves error logs to a file for sharing with the developers Crash logs saved Network - DNS over HTTPS (Cloudflare) + DNS over HTTPS + Disabled + Cloudflare + Google Requires app restart to take effect Used: %1$s diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 869e31a9e6..ada4f79b39 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -6,7 +6,6 @@ object Versions { const val FASTADAPTER = "5.0.0" const val HYPERION = "0.9.27" const val NUCLEUS = "3.0.0" - const val OKHTTP = "4.8.1" const val OSS_LICENSE = "17.0.0" const val RETROFIT = "2.7.2" const val KOTLINSERIALIZATION = "1.0.1" @@ -65,7 +64,7 @@ object Versions { const val moshi = "1.9.3" const val nucleus = "3.0.0" const val numberSlidingPicker = "1.0.3" - const val okhttp = "4.8.1" + const val okhttp = "5.0.0-alpha.2" const val okio = "2.6.0" const val photoView = "2.3.0" const val reactiveNetwork = "0.13.0"