Add advanced option for DNS over HTTPS via Cloudflare

This commit is contained in:
arkon 2020-08-09 20:35:15 -04:00 committed by Jay
parent 7a87714c88
commit 2bb1427162
6 changed files with 65 additions and 39 deletions

View File

@ -153,13 +153,7 @@ object PreferenceKeys {
const val alwaysShowChapterTransition = "always_show_chapter_transition"
@Deprecated("Use the preferences of the source")
fun sourceUsername(sourceId: Long) = "pref_source_username_$sourceId"
@Deprecated("Use the preferences of the source")
fun sourcePassword(sourceId: Long) = "pref_source_password_$sourceId"
fun sourceSharedPref(sourceId: Long) = "source_$sourceId"
const val enableDoh = "enable_doh"
fun trackUsername(syncId: Int) = "pref_mangasync_username_$syncId"

View File

@ -10,7 +10,6 @@ import com.f2prateek.rx.preferences.RxSharedPreferences
import com.tfcporciuncula.flow.FlowSharedPreferences
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.source.Source
import java.io.File
import java.text.DateFormat
import java.text.SimpleDateFormat
@ -125,17 +124,6 @@ class PreferencesHelper(val context: Context) {
fun sourceSorting() = rxPrefs.getInteger(Keys.sourcesSort, 0)
fun sourceUsername(source: Source) = prefs.getString(Keys.sourceUsername(source.id), "")
fun sourcePassword(source: Source) = prefs.getString(Keys.sourcePassword(source.id), "")
fun setSourceCredentials(source: Source, username: String, password: String) {
prefs.edit()
.putString(Keys.sourceUsername(source.id), username)
.putString(Keys.sourcePassword(source.id), password)
.apply()
}
fun trackUsername(sync: TrackService) = prefs.getString(Keys.trackUsername(sync.id), "")
fun trackPassword(sync: TrackService) = prefs.getString(Keys.trackPassword(sync.id), "")
@ -292,4 +280,6 @@ class PreferencesHelper(val context: Context) {
fun shownLongPressCategoryTutorial() = flowPrefs.getBoolean("shown_long_press_category", false)
fun shownHopperSwipeTutorial() = flowPrefs.getBoolean("shown_hopper_swipe", false)
fun enableDoh() = prefs.getBoolean(Keys.enableDoh, false)
}

View File

@ -2,23 +2,52 @@ package eu.kanade.tachiyomi.network
import android.content.Context
import com.chuckerteam.chucker.api.ChuckerInterceptor
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import okhttp3.Cache
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.dnsoverhttps.DnsOverHttps
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.net.InetAddress
class NetworkHelper(context: Context) {
private val preferences: PreferencesHelper by injectLazy()
private val cacheDir = File(context.cacheDir, "network_cache")
private val cacheSize = 5L * 1024 * 1024 // 5 MiB
val cookieManager = AndroidCookieJar()
val client = OkHttpClient.Builder()
val client by lazy {
OkHttpClient.Builder()
.cookieJar(cookieManager)
.cache(Cache(cacheDir, cacheSize))
.addInterceptor(ChuckerInterceptor(context))
.build()
.apply {
if (preferences.enableDoh()) {
dns(
DnsOverHttps.Builder().client(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()
)
}
}.build()
}
val cloudflareClient = client.newBuilder()
.addInterceptor(UserAgentInterceptor())

View File

@ -21,6 +21,7 @@ import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.download.DownloadProvider
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.source.SourceManager
import eu.kanade.tachiyomi.ui.base.controller.DialogController
@ -71,8 +72,7 @@ class SettingsAdvancedController : SettingsController() {
preference {
titleRes = R.string.clean_up_cached_covers
summary = context.getString(
R.string.delete_old_covers_in_library_used_,
coverCache.getChapterCacheSize()
R.string.delete_old_covers_in_library_used_, coverCache.getChapterCacheSize()
)
onClick {
@ -81,7 +81,7 @@ class SettingsAdvancedController : SettingsController() {
}
}
preference {
titleRes = R.string.clean_up_cached_covers_non_library
titleRes = R.string.clear_cached_covers_non_library
summary = context.getString(
R.string.delete_all_covers__not_in_library_used_,
coverCache.getOnlineCoverCacheSize()
@ -114,6 +114,26 @@ class SettingsAdvancedController : SettingsController() {
}
}
}
preferenceCategory {
titleRes = R.string.network
preference {
titleRes = R.string.clear_cookies
onClick {
network.cookieManager.removeAll()
activity?.toast(R.string.cookies_cleared)
}
}
switchPreference {
key = PreferenceKeys.enableDoh
titleRes = R.string.dns_over_https
summaryRes = R.string.requires_app_restart
defaultValue = false
}
}
preferenceCategory {
titleRes = R.string.library
preference {
@ -131,15 +151,6 @@ class SettingsAdvancedController : SettingsController() {
}
preferenceCategory {
preference {
titleRes = R.string.clear_cookies
onClick {
network.cookieManager.removeAll()
activity?.toast(R.string.cookies_cleared)
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager?
if (pm != null) preference {

View File

@ -528,6 +528,9 @@
<!-- Advanced section -->
<string name="clear_chapter_cache">Clear chapter cache</string>
<string name="data_management">Data Management</string>
<string name="network">Network</string>
<string name="dns_over_https" translatable="false">DNS over HTTPS (Cloudflare)</string>
<string name="requires_app_restart">Requires app restart to take effect</string>
<string name="used_">Used: %1$s</string>
<plurals name="cache_cleared">
<item quantity="one">Cache cleared. %d file has been deleted</item>
@ -563,8 +566,7 @@
<string name="clean_up_cached_covers">Clean up cached covers</string>
<string name="delete_old_covers_in_library_used_">Delete old and unused cached covers of
manga in your library that has been updated.\nCurrently using: %1$s</string>
<string name="clean_up_cached_covers_non_library">Clean up cached covers not in
library</string>
<string name="clear_cached_covers_non_library">Clear cached covers not in library</string>
<string name="delete_all_covers__not_in_library_used_">Delete all covers cached that are
not in your library \nCurrently using: %1$s</string>

View File

@ -6,7 +6,7 @@ object Versions {
const val FASTADAPTER = "5.0.0"
const val HYPERION = "0.9.27"
const val NUCLEUS = "3.0.0"
const val OKHTTP = "4.5.0"
const val OKHTTP = "4.8.1"
const val OSS_LICENSE = "17.0.0"
const val RETROFIT = "2.7.2"
const val ROBO_ELECTRIC = "3.1.4"