From 2580455cd51559b78e7d2bd89fa7b486cbd204a8 Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Thu, 16 Dec 2021 21:18:47 +0100 Subject: [PATCH] fixed smelly code --- .../animeproviders/TenshiProvider.kt | 9 +-------- .../cloudstream3/network/DdosGuardKiller.kt | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt b/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt index 59cf4347..4836a38d 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/animeproviders/TenshiProvider.kt @@ -31,14 +31,7 @@ class TenshiProvider : MainAPI() { override val hasQuickSearch = false override val hasMainPage = true override val supportedTypes = setOf(TvType.Anime, TvType.AnimeMovie, TvType.ONA) - private var ddosGuardKiller: DdosGuardKiller? = null - - // Because otherwise Network on main thread when just initializing the provider - init { - thread { - ddosGuardKiller = DdosGuardKiller(true) - } - } + private var ddosGuardKiller = DdosGuardKiller(true) /*private fun loadToken(): Boolean { return try { diff --git a/app/src/main/java/com/lagradost/cloudstream3/network/DdosGuardKiller.kt b/app/src/main/java/com/lagradost/cloudstream3/network/DdosGuardKiller.kt index 96acce70..147d5c48 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/network/DdosGuardKiller.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/network/DdosGuardKiller.kt @@ -1,5 +1,6 @@ package com.lagradost.cloudstream3.network +import androidx.annotation.AnyThread import com.lagradost.cloudstream3.app import okhttp3.Interceptor import okhttp3.Request @@ -9,26 +10,31 @@ import okhttp3.Response * @param alwaysBypass will pre-emptively fetch ddos guard cookies if true. * If false it will only try to get cookies when a request returns 403 * */ +// As seen in https://github.com/anime-dl/anime-downloader/blob/master/anime_downloader/sites/erairaws.py + +@AnyThread class DdosGuardKiller(private val alwaysBypass: Boolean) : Interceptor { val savedCookiesMap = mutableMapOf>() - // As seen in https://github.com/anime-dl/anime-downloader/blob/master/anime_downloader/sites/erairaws.py - private val resp = app.get( - "https://check.ddos-guard.net/check.js" - ).text - private val ddosBypassPath = Regex("'(.*?)'").find(resp)?.groupValues?.get(1) + private var ddosBypassPath: String? = null override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() if (alwaysBypass) return bypassDdosGuard(request) val response = chain.proceed(request) - return if (response.code == 403){ + return if (response.code == 403) { bypassDdosGuard(request) } else response } private fun bypassDdosGuard(request: Request): Response { + ddosBypassPath = ddosBypassPath ?: Regex("'(.*?)'").find( + app.get( + "https://check.ddos-guard.net/check.js" + ).text + )?.groupValues?.get(1) + val cookies = savedCookiesMap[request.url.host] // If no cookies are found fetch and save em.