From f9aa8a1c93a1aa622d743607fb1d17cb8b33a6aa Mon Sep 17 00:00:00 2001 From: bapeey <90949336+bapeey@users.noreply.github.com> Date: Wed, 17 Jan 2024 21:31:30 -0500 Subject: [PATCH] TMO & LectorManga: Ignore ssl in the problematic CDN (#336) Ignore ssl errors --- src/es/lectormanga/build.gradle | 2 +- .../extension/es/lectormanga/LectorManga.kt | 90 ++++++++++--------- src/es/tumangaonline/build.gradle | 2 +- .../es/tumangaonline/TuMangaOnline.kt | 80 ++++++++--------- 4 files changed, 88 insertions(+), 86 deletions(-) diff --git a/src/es/lectormanga/build.gradle b/src/es/lectormanga/build.gradle index 15d9663c1..42143f2d7 100644 --- a/src/es/lectormanga/build.gradle +++ b/src/es/lectormanga/build.gradle @@ -2,7 +2,7 @@ ext { extName = 'LectorManga' pkgNameSuffix = 'es.lectormanga' extClass = '.LectorManga' - extVersionCode = 33 + extVersionCode = 34 isNsfw = true } diff --git a/src/es/lectormanga/src/eu/kanade/tachiyomi/extension/es/lectormanga/LectorManga.kt b/src/es/lectormanga/src/eu/kanade/tachiyomi/extension/es/lectormanga/LectorManga.kt index 0b5b0c5ce..fe48ddfdc 100644 --- a/src/es/lectormanga/src/eu/kanade/tachiyomi/extension/es/lectormanga/LectorManga.kt +++ b/src/es/lectormanga/src/eu/kanade/tachiyomi/extension/es/lectormanga/LectorManga.kt @@ -1,15 +1,12 @@ package eu.kanade.tachiyomi.extension.es.lectormanga +import android.annotation.SuppressLint import android.app.Application import android.content.SharedPreferences -import android.os.Handler -import android.os.Looper -import android.view.View -import android.webkit.WebView -import android.webkit.WebViewClient import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.asObservableSuccess +import eu.kanade.tachiyomi.network.interceptor.rateLimit import eu.kanade.tachiyomi.network.interceptor.rateLimitHost import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.model.Filter @@ -31,9 +28,13 @@ import org.jsoup.nodes.Element import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.security.SecureRandom +import java.security.cert.X509Certificate import java.text.SimpleDateFormat import java.util.Locale -import java.util.concurrent.CountDownLatch +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.X509TrustManager class LectorManga : ConfigurableSource, ParsedHttpSource() { @@ -71,8 +72,42 @@ class LectorManga : ConfigurableSource, ParsedHttpSource() { return this } - private var loadWebView = true + private fun OkHttpClient.Builder.ignoreAllSSLErrors(): OkHttpClient.Builder { + val naiveTrustManager = @SuppressLint("CustomX509TrustManager") + object : X509TrustManager { + override fun getAcceptedIssuers(): Array = emptyArray() + override fun checkClientTrusted(certs: Array, authType: String) = Unit + override fun checkServerTrusted(certs: Array, authType: String) = Unit + } + + val insecureSocketFactory = SSLContext.getInstance("TLSv1.2").apply { + val trustAllCerts = arrayOf(naiveTrustManager) + init(null, trustAllCerts, SecureRandom()) + }.socketFactory + + sslSocketFactory(insecureSocketFactory, naiveTrustManager) + hostnameVerifier { _, _ -> true } + return this + } + + private val ignoreSslClient = network.client.newBuilder() + .ignoreAllSSLErrors() + .followRedirects(false) + .rateLimit( + preferences.getString(IMAGE_CDN_RATELIMIT_PREF, IMAGE_CDN_RATELIMIT_PREF_DEFAULT_VALUE)!!.toInt(), + 60, + ) + .build() + override val client: OkHttpClient = network.client.newBuilder() + .addInterceptor { chain -> + val request = chain.request() + val url = request.url + if (url.host.contains("japanreader.com")) { + return@addInterceptor ignoreSslClient.newCall(request).execute() + } + chain.proceed(request) + } .rateLimitHost( baseUrl.toHttpUrl(), preferences.getString(WEB_RATELIMIT_PREF, WEB_RATELIMIT_PREF_DEFAULT_VALUE)!!.toInt(), @@ -83,39 +118,6 @@ class LectorManga : ConfigurableSource, ParsedHttpSource() { preferences.getString(IMAGE_CDN_RATELIMIT_PREF, IMAGE_CDN_RATELIMIT_PREF_DEFAULT_VALUE)!!.toInt(), 60, ) - .addInterceptor { chain -> - val request = chain.request() - val url = request.url - if (url.host.contains("japanreader.com") && loadWebView) { - val handler = Handler(Looper.getMainLooper()) - val latch = CountDownLatch(1) - var webView: WebView? = null - handler.post { - val webview = WebView(Injekt.get()) - webView = webview - webview.settings.domStorageEnabled = true - webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null) - webview.settings.useWideViewPort = false - webview.settings.loadWithOverviewMode = false - - webview.webViewClient = object : WebViewClient() { - override fun onPageFinished(view: WebView?, url: String?) { - latch.countDown() - } - } - - val headers = mutableMapOf() - headers["Referer"] = baseUrl - - webview.loadUrl(url.toString(), headers) - } - - latch.await() - loadWebView = false - handler.post { webView?.destroy() } - } - chain.proceed(request) - } .build() override fun popularMangaRequest(page: Int) = GET("$baseUrl/library?order_item=likes_count&order_dir=desc&type=&filter_by=title&page=$page", headers) @@ -629,8 +631,8 @@ class LectorManga : ConfigurableSource, ParsedHttpSource() { // Ratelimit permits per second for main website private const val WEB_RATELIMIT_PREF_TITLE = "Ratelimit por minuto para el sitio web" - // This value affects network request amount to TMO url. Lower this value may reduce the chance to get HTTP 429 error, but loading speed will be slower too. Tachiyomi restart required. \nCurrent value: %s - private const val WEB_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red a la URL de TMO. Reducir este valor puede disminuir la posibilidad de obtener un error HTTP 429, pero la velocidad de descarga será más lenta. Se requiere reiniciar Tachiyomi. \nValor actual: %s" + // This value affects network request amount to TMO url. Lower this value may reduce the chance to get HTTP 429 error, but loading speed will be slower too. App restart required. \nCurrent value: %s + private const val WEB_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red a la URL de TMO. Reducir este valor puede disminuir la posibilidad de obtener un error HTTP 429, pero la velocidad de descarga será más lenta. Se requiere reiniciar la app. \nValor actual: %s" private const val WEB_RATELIMIT_PREF_DEFAULT_VALUE = "8" private const val IMAGE_CDN_RATELIMIT_PREF = "imgCDNRatelimitPreference" @@ -638,8 +640,8 @@ class LectorManga : ConfigurableSource, ParsedHttpSource() { // Ratelimit permits per second for image CDN private const val IMAGE_CDN_RATELIMIT_PREF_TITLE = "Ratelimit por minuto para descarga de imágenes" - // This value affects network request amount for loading image. Lower this value may reduce the chance to get error when loading image, but loading speed will be slower too. Tachiyomi restart required. \nCurrent value: %s - private const val IMAGE_CDN_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red para descargar imágenes. Reducir este valor puede disminuir errores al cargar imagenes, pero la velocidad de descarga será más lenta. Se requiere reiniciar Tachiyomi. \nValor actual: %s" + // This value affects network request amount for loading image. Lower this value may reduce the chance to get error when loading image, but loading speed will be slower too. App restart required. \nCurrent value: %s + private const val IMAGE_CDN_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red para descargar imágenes. Reducir este valor puede disminuir errores al cargar imagenes, pero la velocidad de descarga será más lenta. Se requiere reiniciar la app. \nValor actual: %s" private const val IMAGE_CDN_RATELIMIT_PREF_DEFAULT_VALUE = "50" private val ENTRIES_ARRAY = arrayOf("1", "2", "3", "5", "6", "7", "8", "9", "10", "15", "20", "30", "40", "50", "100") diff --git a/src/es/tumangaonline/build.gradle b/src/es/tumangaonline/build.gradle index 1a2d474f8..34b53d439 100644 --- a/src/es/tumangaonline/build.gradle +++ b/src/es/tumangaonline/build.gradle @@ -2,7 +2,7 @@ ext { extName = 'TuMangaOnline' pkgNameSuffix = 'es.tumangaonline' extClass = '.TuMangaOnline' - extVersionCode = 48 + extVersionCode = 49 isNsfw = true } diff --git a/src/es/tumangaonline/src/eu/kanade/tachiyomi/extension/es/tumangaonline/TuMangaOnline.kt b/src/es/tumangaonline/src/eu/kanade/tachiyomi/extension/es/tumangaonline/TuMangaOnline.kt index 3c25a96f4..f93ef449d 100644 --- a/src/es/tumangaonline/src/eu/kanade/tachiyomi/extension/es/tumangaonline/TuMangaOnline.kt +++ b/src/es/tumangaonline/src/eu/kanade/tachiyomi/extension/es/tumangaonline/TuMangaOnline.kt @@ -1,15 +1,12 @@ package eu.kanade.tachiyomi.extension.es.tumangaonline +import android.annotation.SuppressLint import android.app.Application import android.content.SharedPreferences -import android.os.Handler -import android.os.Looper -import android.view.View -import android.webkit.WebChromeClient -import android.webkit.WebView import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.asObservableSuccess +import eu.kanade.tachiyomi.network.interceptor.rateLimit import eu.kanade.tachiyomi.network.interceptor.rateLimitHost import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.model.Filter @@ -31,9 +28,13 @@ import org.jsoup.nodes.Element import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.security.SecureRandom +import java.security.cert.X509Certificate import java.text.SimpleDateFormat import java.util.Locale -import java.util.concurrent.CountDownLatch +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.X509TrustManager class TuMangaOnline : ConfigurableSource, ParsedHttpSource() { @@ -66,40 +67,39 @@ class TuMangaOnline : ConfigurableSource, ParsedHttpSource() { return this } - private var loadWebView = true + private fun OkHttpClient.Builder.ignoreAllSSLErrors(): OkHttpClient.Builder { + val naiveTrustManager = @SuppressLint("CustomX509TrustManager") + object : X509TrustManager { + override fun getAcceptedIssuers(): Array = emptyArray() + override fun checkClientTrusted(certs: Array, authType: String) = Unit + override fun checkServerTrusted(certs: Array, authType: String) = Unit + } + + val insecureSocketFactory = SSLContext.getInstance("TLSv1.2").apply { + val trustAllCerts = arrayOf(naiveTrustManager) + init(null, trustAllCerts, SecureRandom()) + }.socketFactory + + sslSocketFactory(insecureSocketFactory, naiveTrustManager) + hostnameVerifier { _, _ -> true } + return this + } + + private val ignoreSslClient = network.client.newBuilder() + .ignoreAllSSLErrors() + .followRedirects(false) + .rateLimit( + preferences.getString(IMAGE_CDN_RATELIMIT_PREF, IMAGE_CDN_RATELIMIT_PREF_DEFAULT_VALUE)!!.toInt(), + 60, + ) + .build() + override val client: OkHttpClient = network.client.newBuilder() .addInterceptor { chain -> val request = chain.request() val url = request.url - if (url.host.contains("japanreader.com") && loadWebView) { - val handler = Handler(Looper.getMainLooper()) - val latch = CountDownLatch(1) - var webView: WebView? = null - handler.post { - val webview = WebView(Injekt.get()) - webView = webview - webview.settings.domStorageEnabled = true - webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null) - webview.settings.useWideViewPort = false - webview.settings.loadWithOverviewMode = false - - webview.webChromeClient = object : WebChromeClient() { - override fun onProgressChanged(view: WebView?, newProgress: Int) { - if (newProgress == 100) { - latch.countDown() - } - } - } - - val headers = mutableMapOf() - headers["Referer"] = baseUrl - - webview.loadUrl(url.toString(), headers) - } - - latch.await() - loadWebView = false - handler.post { webView?.destroy() } + if (url.host.contains("japanreader.com")) { + return@addInterceptor ignoreSslClient.newCall(request).execute() } chain.proceed(request) } @@ -656,8 +656,8 @@ class TuMangaOnline : ConfigurableSource, ParsedHttpSource() { // Ratelimit permits per second for main website private const val WEB_RATELIMIT_PREF_TITLE = "Ratelimit por minuto para el sitio web" - // This value affects network request amount to TMO url. Lower this value may reduce the chance to get HTTP 429 error, but loading speed will be slower too. Tachiyomi restart required. \nCurrent value: %s - private const val WEB_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red a la URL de TMO. Reducir este valor puede disminuir la posibilidad de obtener un error HTTP 429, pero la velocidad de descarga será más lenta. Se requiere reiniciar Tachiyomi. \nValor actual: %s" + // This value affects network request amount to TMO url. Lower this value may reduce the chance to get HTTP 429 error, but loading speed will be slower too. App restart required. \nCurrent value: %s + private const val WEB_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red a la URL de TMO. Reducir este valor puede disminuir la posibilidad de obtener un error HTTP 429, pero la velocidad de descarga será más lenta. Se requiere reiniciar la app. \nValor actual: %s" private const val WEB_RATELIMIT_PREF_DEFAULT_VALUE = "8" private const val IMAGE_CDN_RATELIMIT_PREF = "imgCDNRatelimitPreference" @@ -665,8 +665,8 @@ class TuMangaOnline : ConfigurableSource, ParsedHttpSource() { // Ratelimit permits per second for image CDN private const val IMAGE_CDN_RATELIMIT_PREF_TITLE = "Ratelimit por minuto para descarga de imágenes" - // This value affects network request amount for loading image. Lower this value may reduce the chance to get error when loading image, but loading speed will be slower too. Tachiyomi restart required. \nCurrent value: %s - private const val IMAGE_CDN_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red para descargar imágenes. Reducir este valor puede disminuir errores al cargar imagenes, pero la velocidad de descarga será más lenta. Se requiere reiniciar Tachiyomi. \nValor actual: %s" + // This value affects network request amount for loading image. Lower this value may reduce the chance to get error when loading image, but loading speed will be slower too. App restart required. \nCurrent value: %s + private const val IMAGE_CDN_RATELIMIT_PREF_SUMMARY = "Este valor afecta la cantidad de solicitudes de red para descargar imágenes. Reducir este valor puede disminuir errores al cargar imagenes, pero la velocidad de descarga será más lenta. Se requiere reiniciar la app. \nValor actual: %s" private const val IMAGE_CDN_RATELIMIT_PREF_DEFAULT_VALUE = "50" private val ENTRIES_ARRAY = listOf(1, 2, 3, 5, 6, 7, 8, 9, 10, 15, 20, 30, 40, 50, 100).map { i -> i.toString() }.toTypedArray()