From 20f357cb12a198a71e5f0e6a177e19395475605f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 15 Aug 2024 12:18:41 +0300 Subject: [PATCH] Handle invalid proxy settings --- .../kotatsu/core/exceptions/ProxyConfigException.kt | 5 +++++ .../core/exceptions/resolve/ExceptionResolver.kt | 11 +++++++++++ .../kotatsu/core/network/AppProxySelector.kt | 8 ++++++-- .../org/koitharu/kotatsu/core/util/ext/Throwable.kt | 13 +++++++------ .../kotatsu/settings/NetworkSettingsFragment.kt | 8 ++++---- .../koitharu/kotatsu/settings/SettingsActivity.kt | 6 ++++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/ProxyConfigException.kt diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/ProxyConfigException.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/ProxyConfigException.kt new file mode 100644 index 000000000..79754f5d1 --- /dev/null +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/ProxyConfigException.kt @@ -0,0 +1,5 @@ +package org.koitharu.kotatsu.core.exceptions + +import java.net.ProtocolException + +class ProxyConfigException : ProtocolException("Wrong proxy configuration") diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/ExceptionResolver.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/ExceptionResolver.kt index 62d5f5a2d..0241c200a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/ExceptionResolver.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/ExceptionResolver.kt @@ -15,6 +15,7 @@ import org.koitharu.kotatsu.alternatives.ui.AlternativesActivity import org.koitharu.kotatsu.browser.BrowserActivity import org.koitharu.kotatsu.browser.cloudflare.CloudFlareActivity import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException +import org.koitharu.kotatsu.core.exceptions.ProxyConfigException import org.koitharu.kotatsu.core.exceptions.UnsupportedSourceException import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.ui.BaseActivity.BaseActivityEntryPoint @@ -25,6 +26,7 @@ import org.koitharu.kotatsu.parsers.exception.AuthRequiredException import org.koitharu.kotatsu.parsers.exception.NotFoundException import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaSource +import org.koitharu.kotatsu.settings.SettingsActivity import org.koitharu.kotatsu.settings.sources.auth.SourceAuthActivity import java.security.cert.CertPathValidatorException import javax.net.ssl.SSLException @@ -74,6 +76,13 @@ class ExceptionResolver : ActivityResultCallback { false } + is ProxyConfigException -> { + context?.run { + startActivity(SettingsActivity.newProxySettingsIntent(this)) + } + false + } + is NotFoundException -> { openInBrowser(e.url) false @@ -144,6 +153,8 @@ class ExceptionResolver : ActivityResultCallback { is SSLException, is CertPathValidatorException -> R.string.fix + is ProxyConfigException -> R.string.settings + else -> 0 } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/network/AppProxySelector.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/network/AppProxySelector.kt index 87a410ba6..1424f1f31 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/network/AppProxySelector.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/network/AppProxySelector.kt @@ -1,8 +1,9 @@ package org.koitharu.kotatsu.core.network +import okio.IOException +import org.koitharu.kotatsu.core.exceptions.ProxyConfigException import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug -import java.io.IOException import java.net.InetSocketAddress import java.net.Proxy import java.net.ProxySelector @@ -31,9 +32,12 @@ class AppProxySelector( val type = settings.proxyType val address = settings.proxyAddress val port = settings.proxyPort - if (type == Proxy.Type.DIRECT || address.isNullOrEmpty() || port == 0) { + if (type == Proxy.Type.DIRECT) { return Proxy.NO_PROXY } + if (address.isNullOrEmpty() || port == 0) { + throw ProxyConfigException() + } cachedProxy?.let { val addr = it.address() as? InetSocketAddress if (addr != null && it.type() == type && addr.port == port && addr.hostString == address) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Throwable.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Throwable.kt index dd38ab04d..19b89ba43 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Throwable.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Throwable.kt @@ -7,6 +7,7 @@ import androidx.collection.arraySetOf import coil.network.HttpException import okio.FileNotFoundException import okio.IOException +import okio.ProtocolException import org.acra.ktx.sendWithAcra import org.jsoup.HttpStatusException import org.koitharu.kotatsu.R @@ -17,6 +18,7 @@ import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException import org.koitharu.kotatsu.core.exceptions.EmptyHistoryException import org.koitharu.kotatsu.core.exceptions.IncompatiblePluginException import org.koitharu.kotatsu.core.exceptions.NoDataReceivedException +import org.koitharu.kotatsu.core.exceptions.ProxyConfigException import org.koitharu.kotatsu.core.exceptions.SyncApiException import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions import org.koitharu.kotatsu.core.exceptions.UnsupportedFileException @@ -43,7 +45,7 @@ fun Throwable.getDisplayMessage(resources: Resources): String = when (this) { is CloudFlareBlockedException -> resources.getString(R.string.blocked_by_server_message) is ActivityNotFoundException, is UnsupportedOperationException, - -> resources.getString(R.string.operation_not_supported) + -> resources.getString(R.string.operation_not_supported) is TooManyRequestExceptions -> resources.getString(R.string.too_many_requests_message) is UnsupportedFileException -> resources.getString(R.string.text_file_not_supported) @@ -51,14 +53,13 @@ fun Throwable.getDisplayMessage(resources: Resources): String = when (this) { is FileNotFoundException -> resources.getString(R.string.file_not_found) is AccessDeniedException -> resources.getString(R.string.no_access_to_file) is EmptyHistoryException -> resources.getString(R.string.history_is_empty) + is ProxyConfigException -> resources.getString(R.string.invalid_proxy_configuration) is SyncApiException, - is ContentUnavailableException, - -> message + is ContentUnavailableException -> message is ParseException -> shortMessage is UnknownHostException, - is SocketTimeoutException, - -> resources.getString(R.string.network_error) + is SocketTimeoutException -> resources.getString(R.string.network_error) is NoDataReceivedException -> resources.getString(R.string.error_no_data_received) is IncompatiblePluginException -> resources.getString(R.string.plugin_incompatible) @@ -80,7 +81,7 @@ fun Throwable.getDisplayIcon() = when (this) { is CloudFlareProtectedException -> R.drawable.ic_bot_large is UnknownHostException, is SocketTimeoutException, - -> R.drawable.ic_plug_large + is ProtocolException -> R.drawable.ic_plug_large is CloudFlareBlockedException -> R.drawable.ic_denied_large diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/NetworkSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/NetworkSettingsFragment.kt index 16af2c294..4f9827d57 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/NetworkSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/NetworkSettingsFragment.kt @@ -63,10 +63,10 @@ class NetworkSettingsFragment : val type = settings.proxyType val address = settings.proxyAddress val port = settings.proxyPort - summary = if (type == Proxy.Type.DIRECT || address.isNullOrEmpty() || port == 0) { - context.getString(R.string.disabled) - } else { - "$address:$port" + summary = when { + type == Proxy.Type.DIRECT -> context.getString(R.string.disabled) + address.isNullOrEmpty() || port == 0 -> context.getString(R.string.invalid_proxy_configuration) + else -> "$address:$port" } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt index 7fc0bfd28..2f8d648d1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt @@ -112,6 +112,7 @@ class SettingsActivity : ACTION_HISTORY -> UserDataSettingsFragment() ACTION_TRACKER -> TrackerSettingsFragment() ACTION_SOURCES -> SourcesSettingsFragment() + ACTION_PROXY -> ProxySettingsFragment() ACTION_MANAGE_DOWNLOADS -> DownloadsSettingsFragment() ACTION_SOURCE -> SourceSettingsFragment.newInstance( MangaSource(intent.getStringExtra(EXTRA_SOURCE)), @@ -144,6 +145,7 @@ class SettingsActivity : private const val ACTION_SOURCES = "${BuildConfig.APPLICATION_ID}.action.MANAGE_SOURCES" private const val ACTION_MANAGE_SOURCES = "${BuildConfig.APPLICATION_ID}.action.MANAGE_SOURCES_LIST" private const val ACTION_MANAGE_DOWNLOADS = "${BuildConfig.APPLICATION_ID}.action.MANAGE_DOWNLOADS" + private const val ACTION_PROXY = "${BuildConfig.APPLICATION_ID}.action.MANAGE_PROXY" private const val EXTRA_SOURCE = "source" private const val HOST_ABOUT = "about" private const val HOST_SYNC_SETTINGS = "sync-settings" @@ -162,6 +164,10 @@ class SettingsActivity : Intent(context, SettingsActivity::class.java) .setAction(ACTION_TRACKER) + fun newProxySettingsIntent(context: Context) = + Intent(context, SettingsActivity::class.java) + .setAction(ACTION_PROXY) + fun newHistorySettingsIntent(context: Context) = Intent(context, SettingsActivity::class.java) .setAction(ACTION_HISTORY) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 52e423fcf..2973b7831 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -672,4 +672,5 @@ External/plugin Incompatible plugin or internal error. Make sure you are using the latest version of the plugin and Kotatsu Connection is OK + Invalid proxy configuration