From b2d4e5ab846bcb5807785a0d1bfb82e9c5caeec6 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 11 Apr 2021 13:10:03 -0400 Subject: [PATCH] Add Google DoH provider --- app/build.gradle.kts | 2 +- .../java/eu/kanade/tachiyomi/Migrations.kt | 12 ++++++ .../data/preference/PreferenceKeys.kt | 2 +- .../data/preference/PreferencesHelper.kt | 2 +- .../kanade/tachiyomi/network/DohProviders.kt | 40 +++++++++++++++++++ .../kanade/tachiyomi/network/NetworkHelper.kt | 25 ++---------- .../ui/setting/SettingsAdvancedController.kt | 27 +++++++++++-- app/src/main/res/values/strings.xml | 2 +- 8 files changed, 82 insertions(+), 30 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 d255055af..c3acefae1 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -29,7 +29,7 @@ android { minSdkVersion(AndroidConfig.minSdk) targetSdkVersion(AndroidConfig.targetSdk) testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" - versionCode = 56 + versionCode = 57 versionName = "0.10.9" buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"") diff --git a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt index f413f34a6..d3d929b43 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt @@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper 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.LibrarySort import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.widget.ExtendedNavigationView @@ -127,6 +128,17 @@ object Migrations { context.toast(R.string.myanimelist_relogin) } } + if (oldVersion < 57) { + // 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 } 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 9069ab6ac..9ef69e07b 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 searchPinnedSourcesOnly = "search_pinned_sources_only" - const val enableDoh = "enable_doh" + const val dohProvider = "doh_provider" const val defaultChapterFilterByRead = "default_chapter_filter_by_read" 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 7415f019c..0768487ba 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 @@ -279,7 +279,7 @@ class PreferencesHelper(val context: Context) { fun trustedSignatures() = flowPrefs.getStringSet("trusted_signatures", emptySet()) - fun enableDoh() = prefs.getBoolean(Keys.enableDoh, false) + fun dohProvider() = prefs.getInt(Keys.dohProvider, -1) fun lastSearchQuerySearchSettings() = flowPrefs.getString("last_search_query", "") 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 000000000..982910f52 --- /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/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt index 0f31ee10b..4b729fdea 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -4,13 +4,10 @@ import android.content.Context import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.data.preference.PreferencesHelper import okhttp3.Cache -import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.OkHttpClient -import okhttp3.dnsoverhttps.DnsOverHttps import okhttp3.logging.HttpLoggingInterceptor import uy.kohesive.injekt.injectLazy import java.io.File -import java.net.InetAddress import java.util.concurrent.TimeUnit class NetworkHelper(context: Context) { @@ -38,25 +35,9 @@ class NetworkHelper(context: Context) { builder.addInterceptor(httpLoggingInterceptor) } - if (preferences.enableDoh()) { - builder.dns( - DnsOverHttps.Builder().client(builder.build()) - .url("https://cloudflare-dns.com/dns-query".toHttpUrl()) - .bootstrapDnsHosts( - listOf( - 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() - ) + when (preferences.dohProvider()) { + PREF_DOH_CLOUDFLARE -> builder.dohCloudflare() + PREF_DOH_GOOGLE -> builder.dohGoogle() } builder.build() 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 636e3faa6..3012b447b 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 @@ -17,9 +17,13 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Target 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.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.CrashLogUtil import eu.kanade.tachiyomi.util.preference.defaultValue +import eu.kanade.tachiyomi.util.preference.intListPreference +import eu.kanade.tachiyomi.util.preference.onChange import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.preference import eu.kanade.tachiyomi.util.preference.preferenceCategory @@ -124,11 +128,26 @@ class SettingsAdvancedController : SettingsController() { activity?.toast(R.string.cookies_cleared) } } - switchPreference { - key = Keys.enableDoh + intListPreference { + key = Keys.dohProvider titleRes = R.string.pref_dns_over_https - summaryRes = R.string.requires_app_restart - defaultValue = false + entries = arrayOf( + context.getString(R.string.disabled), + "Cloudflare", + "Google", + ) + entryValues = arrayOf( + "-1", + PREF_DOH_CLOUDFLARE.toString(), + PREF_DOH_GOOGLE.toString(), + ) + defaultValue = "-1" + summary = "%s" + + 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 121d455da..b3d3f8d6a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -417,7 +417,7 @@ Network Clear cookies - DNS over HTTPS (Cloudflare) + DNS over HTTPS Requires app restart to take effect Cookies cleared Data