Commit 765b5128 authored by topjohnwu's avatar topjohnwu

Add settings to disable DoH

Close #3130
parent 8a338de6
...@@ -9,6 +9,7 @@ import com.topjohnwu.magisk.BuildConfig ...@@ -9,6 +9,7 @@ import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.core.magiskdb.SettingsDao import com.topjohnwu.magisk.core.magiskdb.SettingsDao
import com.topjohnwu.magisk.core.magiskdb.StringDao import com.topjohnwu.magisk.core.magiskdb.StringDao
import com.topjohnwu.magisk.core.utils.BiometricHelper import com.topjohnwu.magisk.core.utils.BiometricHelper
import com.topjohnwu.magisk.core.utils.defaultLocale
import com.topjohnwu.magisk.core.utils.refreshLocale import com.topjohnwu.magisk.core.utils.refreshLocale
import com.topjohnwu.magisk.data.preference.PreferenceModel import com.topjohnwu.magisk.data.preference.PreferenceModel
import com.topjohnwu.magisk.data.repository.DBConfig import com.topjohnwu.magisk.data.repository.DBConfig
...@@ -54,6 +55,7 @@ object Config : PreferenceModel, DBConfig { ...@@ -54,6 +55,7 @@ object Config : PreferenceModel, DBConfig {
const val THEME_ORDINAL = "theme_ordinal" const val THEME_ORDINAL = "theme_ordinal"
const val BOOT_ID = "boot_id" const val BOOT_ID = "boot_id"
const val ASKED_HOME = "asked_home" const val ASKED_HOME = "asked_home"
const val DOH = "doh"
// system state // system state
const val MAGISKHIDE = "magiskhide" const val MAGISKHIDE = "magiskhide"
...@@ -126,6 +128,7 @@ object Config : PreferenceModel, DBConfig { ...@@ -126,6 +128,7 @@ object Config : PreferenceModel, DBConfig {
var themeOrdinal by preference(Key.THEME_ORDINAL, Theme.Piplup.ordinal) var themeOrdinal by preference(Key.THEME_ORDINAL, Theme.Piplup.ordinal)
var suReAuth by preference(Key.SU_REAUTH, false) var suReAuth by preference(Key.SU_REAUTH, false)
var checkUpdate by preference(Key.CHECK_UPDATES, true) var checkUpdate by preference(Key.CHECK_UPDATES, true)
var doh by preference(Key.DOH, defaultLocale.country == "CN")
var magiskHide by preference(Key.MAGISKHIDE, true) var magiskHide by preference(Key.MAGISKHIDE, true)
var showSystemApp by preference(Key.SHOW_SYSTEM_APP, false) var showSystemApp by preference(Key.SHOW_SYSTEM_APP, false)
......
...@@ -3,6 +3,7 @@ package com.topjohnwu.magisk.di ...@@ -3,6 +3,7 @@ package com.topjohnwu.magisk.di
import android.content.Context import android.content.Context
import android.os.Build import android.os.Build
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.data.network.GithubApiServices import com.topjohnwu.magisk.data.network.GithubApiServices
...@@ -32,22 +33,12 @@ val networkingModule = module { ...@@ -32,22 +33,12 @@ val networkingModule = module {
single { createMarkwon(get(), get()) } single { createMarkwon(get(), get()) }
} }
@Suppress("DEPRECATION") private class DnsResolver(client: OkHttpClient) : Dns {
fun createOkHttpClient(context: Context): OkHttpClient {
val builder = OkHttpClient.Builder()
// val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
// level = HttpLoggingInterceptor.Level.HEADERS
// }
// builder.addInterceptor(httpLoggingInterceptor)
if (!Networking.init(context)) { private var dohError = false
Info.hasGMS = false private val poisonedHosts = listOf("raw.githubusercontent.com")
if (Build.VERSION.SDK_INT < 21) private val doh by lazy {
builder.sslSocketFactory(NoSSLv3SocketFactory()) DnsOverHttps.Builder().client(client)
}
val doh = DnsOverHttps.Builder().client(builder.build())
.url(HttpUrl.get("https://cloudflare-dns.com/dns-query")) .url(HttpUrl.get("https://cloudflare-dns.com/dns-query"))
.bootstrapDnsHosts(listOf( .bootstrapDnsHosts(listOf(
InetAddress.getByName("162.159.36.1"), InetAddress.getByName("162.159.36.1"),
...@@ -62,21 +53,37 @@ fun createOkHttpClient(context: Context): OkHttpClient { ...@@ -62,21 +53,37 @@ fun createOkHttpClient(context: Context): OkHttpClient {
)) ))
.resolvePrivateAddresses(true) /* To make PublicSuffixDatabase never used */ .resolvePrivateAddresses(true) /* To make PublicSuffixDatabase never used */
.build() .build()
}
var skipDoH = false override fun lookup(hostname: String): List<InetAddress> {
builder.dns { hostname -> return if (!dohError && Config.doh && poisonedHosts.contains(hostname)) {
// Only resolve via DoH for known DNS polluted hostnames
if (!skipDoH && hostname == "raw.githubusercontent.com") {
try { try {
doh.lookup(hostname) doh.lookup(hostname)
} catch (e: UnknownHostException) { } catch (e: UnknownHostException) {
skipDoH = true dohError = true
Dns.SYSTEM.lookup(hostname) Dns.SYSTEM.lookup(hostname)
} }
} else { } else {
Dns.SYSTEM.lookup(hostname) Dns.SYSTEM.lookup(hostname)
} }
} }
}
@Suppress("DEPRECATION")
fun createOkHttpClient(context: Context): OkHttpClient {
val builder = OkHttpClient.Builder()
// val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
// level = HttpLoggingInterceptor.Level.HEADERS
// }
// builder.addInterceptor(httpLoggingInterceptor)
if (!Networking.init(context)) {
Info.hasGMS = false
if (Build.VERSION.SDK_INT < 21)
builder.sslSocketFactory(NoSSLv3SocketFactory())
}
builder.dns(DnsResolver(builder.build()))
return builder.build() return builder.build()
} }
......
...@@ -174,6 +174,15 @@ object UpdateChecker : BaseSettingsItem.Toggle() { ...@@ -174,6 +174,15 @@ object UpdateChecker : BaseSettingsItem.Toggle() {
} }
} }
object DoHToggle : BaseSettingsItem.Toggle() {
override val title = R.string.settings_doh_title.asTransitive()
override val description = R.string.settings_doh_description.asTransitive()
override var value = Config.doh
set(value) = setV(value, field, { field = it }) {
Config.doh = it
}
}
// check whether is module already installed beforehand? // check whether is module already installed beforehand?
object SystemlessHosts : BaseSettingsItem.Blank() { object SystemlessHosts : BaseSettingsItem.Blank() {
override val title = R.string.settings_hosts_title.asTransitive() override val title = R.string.settings_hosts_title.asTransitive()
......
...@@ -58,7 +58,7 @@ class SettingsViewModel( ...@@ -58,7 +58,7 @@ class SettingsViewModel(
// Manager // Manager
list.addAll(listOf( list.addAll(listOf(
Manager, Manager,
UpdateChannel, UpdateChannelUrl, UpdateChecker, DownloadPath UpdateChannel, UpdateChannelUrl, DoHToggle, UpdateChecker, DownloadPath
)) ))
if (Info.env.isActive) { if (Info.env.isActive) {
list.add(ClearRepoCache) list.add(ClearRepoCache)
......
...@@ -175,6 +175,8 @@ ...@@ -175,6 +175,8 @@
<string name="no_biometric">Unsupported device or no biometric settings are enabled</string> <string name="no_biometric">Unsupported device or no biometric settings are enabled</string>
<string name="settings_customization">Customization</string> <string name="settings_customization">Customization</string>
<string name="setting_add_shortcut_summary">Add a pretty shortcut in the home screen in case the name and icon are difficult to recognize after hiding the app</string> <string name="setting_add_shortcut_summary">Add a pretty shortcut in the home screen in case the name and icon are difficult to recognize after hiding the app</string>
<string name="settings_doh_title">DNS over HTTPS</string>
<string name="settings_doh_description">Workaround DNS poisoning in some nations</string>
<string name="multiuser_mode">Multiuser Mode</string> <string name="multiuser_mode">Multiuser Mode</string>
<string name="settings_owner_only">Device Owner Only</string> <string name="settings_owner_only">Device Owner Only</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment