Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lagradost.cloudstream3.network

import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
import okhttp3.dnsoverhttps.DnsOverHttps
import java.net.InetAddress
Expand Down Expand Up @@ -105,3 +107,16 @@ fun OkHttpClient.Builder.addCanadianShieldDns() = (
"149.112.122.10",
)
))

fun String.toDohUrlOrNull(): HttpUrl? = trim().toHttpUrlOrNull()?.takeIf { it.isHttps }

fun OkHttpClient.Builder.addCustomDns(url: String?) =
url?.toDohUrlOrNull()?.let { dohUrl ->
dns(
DnsOverHttps
.Builder()
.client(build())
.url(dohUrl)
.build()
)
} ?: this
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ fun buildDefaultClient(context: Context, ignoreSSL: Boolean = false): OkHttpClie
6 -> addQuad9Dns()
7 -> addDnsSbDns()
8 -> addCanadianShieldDns()
9 -> addCustomDns(
settingsManager.getString(context.getString(R.string.dns_custom_url_key), "")
)
}
}
// Needs to be build as otherwise the other builders will change this object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import android.content.Intent
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -14,6 +19,7 @@ import androidx.compose.ui.res.integerArrayResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.PreviewLightDark
import com.lagradost.cloudstream3.APIHolder
import com.lagradost.cloudstream3.AllLanguagesName
Expand All @@ -27,6 +33,7 @@ import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.insecureApp
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.network.initClient
import com.lagradost.cloudstream3.network.toDohUrlOrNull
import com.lagradost.cloudstream3.ui.settings.SettingsFragmentScreen.launchSafe
import com.lagradost.cloudstream3.ui.settings.SettingsProvidersScreen.toStringRes
import com.lagradost.cloudstream3.utils.BatteryOptimizationChecker.isAppRestricted
Expand All @@ -35,8 +42,14 @@ import com.lagradost.cloudstream3.utils.SubtitleHelper.fromTagToLanguageName
import com.lagradost.cloudstream3.utils.SubtitleHelper.getNameNextToFlagEmoji
import com.lagradost.cloudstream4.AppSettings
import com.lagradost.cloudstream4.compose.ActionDialog
import com.lagradost.cloudstream4.compose.BlackButton
import com.lagradost.cloudstream4.compose.BlackTextField
import com.lagradost.cloudstream4.compose.PHONE
import com.lagradost.cloudstream4.compose.WhiteButton
import com.lagradost.cloudstream4.compose.isLayout
import com.lagradost.cloudstream4.generated.resources.Res
import com.lagradost.cloudstream4.generated.resources.cancel
import com.lagradost.cloudstream4.generated.resources.ok
import com.lagradost.cloudstream4.rememberAppSettings
import com.lagradost.cloudstream4.theme.CloudStreamPreviewTheme
import com.lagradost.safefile.SafeFile
Expand All @@ -47,6 +60,58 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentMap

object SettingsGeneralScreen : SearchableSettings {
private const val CUSTOM_DNS = 9

private fun reinitializeClients() {
(CloudStreamApp.context)?.let { ctx ->
app.initClient(ctx, ignoreSSL = false)
@OptIn(UnsafeSSL::class)
insecureApp.initClient(ctx, ignoreSSL = true)
}
}

@Composable
private fun DnsCustomDialog(url: String, dismiss: () -> Unit, confirm: (String) -> Unit) {
var value by remember { mutableStateOf(url) }
val save = {
val dohUrl = value.toDohUrlOrNull()
if (dohUrl == null) {
showToast(R.string.error_invalid_url)
} else {
confirm(dohUrl.toString())
}
}

AlertDialog(
containerColor = MaterialTheme.colorScheme.background,
onDismissRequest = dismiss,
title = { Text(text = stringResource(R.string.custom)) },
text = {
BlackTextField(
value = value,
placeHolder = stringResource(R.string.dns_custom_url_hint),
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Uri),
keyboardActions = KeyboardActions(onDone = { save() }),
singleLine = true
) { newValue ->
value = newValue
}
},
confirmButton = {
WhiteButton(
text = org.jetbrains.compose.resources.stringResource(Res.string.ok),
onClick = save
)
},
dismissButton = {
BlackButton(
text = org.jetbrains.compose.resources.stringResource(Res.string.cancel),
onClick = dismiss
)
}
)
}

@Composable
override fun getTitleRes(): String = stringResource(R.string.category_general)

Expand Down Expand Up @@ -86,9 +151,24 @@ object SettingsGeneralScreen : SearchableSettings {
val downloadPathVisual by settings.general.downloadPathVisual.collectAsState()
//val downloadPath by settings.general.downloadPath.collectAsState()

val dnsCustomUrl by settings.general.dnsCustomUrl.collectAsState()
var isDnsCustomShown by remember { mutableStateOf(false) }
var isBatteryShown by remember { mutableStateOf(false) }
val context = LocalContext.current

if (isDnsCustomShown) {
DnsCustomDialog(
url = dnsCustomUrl,
dismiss = { isDnsCustomShown = false },
confirm = { newUrl ->
settings.general.dnsCustomUrl.set(newUrl)
settings.general.dns.set(CUSTOM_DNS)
reinitializeClients()
isDnsCustomShown = false
}
)
}

if (isBatteryShown) {
ActionDialog(
icon = painterResource(R.drawable.battery_alert_24px),
Expand Down Expand Up @@ -224,12 +304,12 @@ object SettingsGeneralScreen : SearchableSettings {
stringArrayResource(R.array.dns_pref)
).toMap(),
onValueChanged = { newValue ->
settings.general.dns.set(newValue) // Must set the value before initClient is called
(CloudStreamApp.context)?.let { ctx ->
app.initClient(ctx, ignoreSSL = false)
@OptIn(UnsafeSSL::class)
insecureApp.initClient(ctx, ignoreSSL = true)
if (newValue == CUSTOM_DNS) {
isDnsCustomShown = true
return@ListPreference false
}
settings.general.dns.set(newValue) // Must set the value before initClient is called
reinitializeClients()
return@ListPreference true
},
),
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<item>Quad9</item>
<item>DNS.SB</item>
<item>Canadian Shield</item>
<item>@string/custom</item>
</array>
<array name="dns_pref_values">
<item>0</item>
Expand All @@ -35,6 +36,7 @@
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
</array>

<array name="apk_installer_pref">
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/donottranslate-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<string name="random_button_key">random_button_key</string>
<string name="provider_lang_key">provider_lang_key</string>
<string name="dns_key">dns_key</string>
<string name="dns_custom_url_key">dns_custom_url_key</string>
<string name="dns_custom_url_hint">https://...</string>
<string name="jsdelivr_proxy_key">jsdelivr_proxy_key</string>
<string name="download_path_key">download_path_key</string>
<string name="download_parallel_key">download_parallel_key</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class GeneralPreferences(preferences: PreferenceStore) {

/** Please note that it used R.array.dns_pref before, this was a bug and caused this setting to be language sensitive **/
val dns = preferences.getInt("dns_key", 0)
val dnsCustomUrl = preferences.getString("dns_custom_url_key", "")

/** Very weird setKey based usage, just having this setting might not do it */
val jsdelivrProxy = preferences.getBoolean("jsdelivr_proxy_key", false)
Expand Down