From a022f69c890b6a0c70d2538cf4a58a202a91ad1b Mon Sep 17 00:00:00 2001 From: Vetle Ledaal Date: Mon, 8 Aug 2022 20:28:02 +0000 Subject: [PATCH] Koushoku: bypass block (#12913) --- src/en/koushoku/build.gradle | 2 +- .../en/koushoku/GoogleTranslateInterceptor.kt | 38 ++++++++++ .../GoogleTranslateNetworkInterceptor.kt | 42 +++++++++++ .../extension/en/koushoku/Koushoku.kt | 7 +- .../en/koushoku/KoushokuWebViewInterceptor.kt | 73 ------------------- 5 files changed, 85 insertions(+), 77 deletions(-) create mode 100644 src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateInterceptor.kt create mode 100644 src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateNetworkInterceptor.kt delete mode 100644 src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/KoushokuWebViewInterceptor.kt diff --git a/src/en/koushoku/build.gradle b/src/en/koushoku/build.gradle index 4239295d7..da76171da 100644 --- a/src/en/koushoku/build.gradle +++ b/src/en/koushoku/build.gradle @@ -5,7 +5,7 @@ ext { extName = 'Koushoku' pkgNameSuffix = 'en.koushoku' extClass = '.Koushoku' - extVersionCode = 13 + extVersionCode = 14 isNsfw = true } diff --git a/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateInterceptor.kt b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateInterceptor.kt new file mode 100644 index 000000000..06513ba8e --- /dev/null +++ b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateInterceptor.kt @@ -0,0 +1,38 @@ +package eu.kanade.tachiyomi.extension.en.koushoku + +import okhttp3.HttpUrl +import okhttp3.Interceptor +import okhttp3.Response + +class GoogleTranslateInterceptor : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + + return if (request.url.host == "koushoku.org") { + val newUrl = googlify(request.url) + val newRequest = request.newBuilder() + .url(newUrl) + .build() + + chain.proceed(newRequest) + } else { + chain.proceed(request) + } + } + + private fun googlify(url: HttpUrl): HttpUrl { + val newHost = url.host + .replace("-", "--") + .replace('.', '-') + .plus(".translate.goog") + + return url.newBuilder() + .host(newHost) + // `_x_tr_sl` and `_x_tr_tl` must be different + .addQueryParameter("_x_tr_sl", "en") + .addQueryParameter("_x_tr_tl", "jp") + .addQueryParameter("_x_tr_hl", "en") + .build() + } +} diff --git a/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateNetworkInterceptor.kt b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateNetworkInterceptor.kt new file mode 100644 index 000000000..5c6b51038 --- /dev/null +++ b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/GoogleTranslateNetworkInterceptor.kt @@ -0,0 +1,42 @@ +package eu.kanade.tachiyomi.extension.en.koushoku + +import okhttp3.HttpUrl +import okhttp3.HttpUrl.Companion.toHttpUrl +import okhttp3.Interceptor +import okhttp3.Response + +class GoogleTranslateNetworkInterceptor : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + val response = chain.proceed(request) + + if ( + response.request.url.host == "koushoku-org.translate.goog" && + response.request.url.pathSegments.getOrNull(0) == "d" && + response.code in 300..399 + ) { + val newUrl = degooglify(response.headers["Location"]!!.toHttpUrl()) + + return response.newBuilder() + .header("Location", newUrl.toString()) + .build() + } + + return response + } + + private fun degooglify(url: HttpUrl): HttpUrl { + val newHost = url.host + .substringBeforeLast(".translate.goog") + .replace('-', '.') + .replace("..", "-") + + return url.newBuilder() + .host(newHost) + .removeAllQueryParameters("_x_tr_sl") + .removeAllQueryParameters("_x_tr_tl") + .removeAllQueryParameters("_x_tr_hl") + .build() + } +} diff --git a/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/Koushoku.kt b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/Koushoku.kt index a75ecb577..a2af912c1 100644 --- a/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/Koushoku.kt +++ b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/Koushoku.kt @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.extension.en.koushoku import eu.kanade.tachiyomi.network.GET 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.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.MangasPage @@ -38,8 +38,9 @@ class Koushoku : ParsedHttpSource() { override val supportsLatest = true override val client: OkHttpClient = network.cloudflareClient.newBuilder() - .addInterceptor(KoushokuWebViewInterceptor()) - .rateLimit(1, 4) + .addInterceptor(GoogleTranslateInterceptor()) + .addNetworkInterceptor(GoogleTranslateNetworkInterceptor()) + .rateLimitHost("https://ksk-h7glm2.xyz".toHttpUrl(), 1) .build() override fun headersBuilder(): Headers.Builder = super.headersBuilder() diff --git a/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/KoushokuWebViewInterceptor.kt b/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/KoushokuWebViewInterceptor.kt deleted file mode 100644 index 6bcc34335..000000000 --- a/src/en/koushoku/src/eu/kanade/tachiyomi/extension/en/koushoku/KoushokuWebViewInterceptor.kt +++ /dev/null @@ -1,73 +0,0 @@ -package eu.kanade.tachiyomi.extension.en.koushoku - -import android.app.Application -import android.os.Handler -import android.os.Looper -import android.webkit.WebView -import android.webkit.WebViewClient -import okhttp3.Interceptor -import okhttp3.Response -import org.jsoup.Jsoup -import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.get -import java.io.IOException -import java.util.concurrent.CountDownLatch - -class KoushokuWebViewInterceptor : Interceptor { - - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - val response = chain.proceed(request) - - if (response.headers("Content-Type").any { it.contains("text/html") }) { - val responseBody = response.peekBody(1 * 1024 * 1024).string() - if (response.code == 403) { - val document = Jsoup.parse(responseBody) - if (document.selectFirst("h1")?.text()?.contains(Regex("banned$")) == true) { - throw IOException("You have been banned. Check WebView for details.") - } - } - - if (response.networkResponse != null) { - try { - proceedWithWebView(response, responseBody) - } catch (e: Exception) { - throw IOException(e) - } - } - } - - return response - } - - private fun proceedWithWebView(response: Response, responseBody: String) { - val latch = CountDownLatch(1) - val handler = Handler(Looper.getMainLooper()) - - handler.post { - val webView = WebView(Injekt.get()) - with(webView.settings) { - loadsImagesAutomatically = false - userAgentString = response.request.header("User-Agent") - } - - webView.webViewClient = object : WebViewClient() { - override fun onPageFinished(view: WebView, url: String) { - webView.stopLoading() - webView.destroy() - latch.countDown() - } - } - - webView.loadDataWithBaseURL( - response.request.url.toString(), - responseBody, - "text/html", - "utf-8", - null - ) - } - - latch.await() - } -}