From 86aaf28046626bc97da1f24d85caab0ccadb5644 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Mon, 24 Apr 2023 19:16:21 +0330 Subject: [PATCH] better initialization --- .../interceptor/CloudflareInterceptor.kt | 25 +++++++++++++------ .../suwayomi/tachidesk/server/ServerConfig.kt | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/server/src/main/kotlin/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt b/server/src/main/kotlin/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt index 659b62355..a9e8db7a2 100644 --- a/server/src/main/kotlin/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt +++ b/server/src/main/kotlin/eu/kanade/tachiyomi/network/interceptor/CloudflareInterceptor.kt @@ -40,8 +40,9 @@ class CloudflareInterceptor : Interceptor { logger.debug { "Cloudflare anti-bot is on, CloudflareInterceptor is kicking in..." } - if (!serverConfig.webviewEnabled) + if (!serverConfig.webviewEnabled) { throw CloudflareBypassException("Webview is disabled, enable it in server config") + } return try { originalResponse.close() @@ -72,7 +73,12 @@ object CFClearance { private val logger = KotlinLogging.logger {} private val network: NetworkHelper by injectLazy() - init { + private var undetectedChromeInitialized = false + + fun initializeUndetectedChrome() { + if (undetectedChromeInitialized) { + return + } SharedInterpreter().use { jep -> val uc = "/home/armor/programming/github-clones/undetected-chromedriver" @@ -81,6 +87,7 @@ object CFClearance { jep.exec("import undetected_chromedriver") // Cache import } + undetectedChromeInitialized = true } fun resolveWithWebView(originalRequest: Request): Request { @@ -88,6 +95,7 @@ object CFClearance { logger.debug { "resolveWithWebView($url)" } + initializeUndetectedChrome() val cookies = SharedInterpreter().use { jep -> try { jep.exec("import undetected_chromedriver as uc") @@ -110,7 +118,7 @@ object CFClearance { // } jep.exec("driver.get('$url')") - getCookies(jep, url) + getCookies(jep) } finally { jep.exec("driver.quit()") } @@ -152,15 +160,17 @@ object CFClearance { fun getWebViewUserAgent(): String { return try { - if (!serverConfig.webviewEnabled) + if (!serverConfig.webviewEnabled) { throw CloudflareBypassException("Webview is disabled, enable it in server config") + } + initializeUndetectedChrome() SharedInterpreter().use { jep -> jep.exec("import undetected_chromedriver as uc") jep.exec("options = uc.ChromeOptions()") - jep.exec("options.add_argument('--headless')") - jep.exec("options.add_argument('--disable-gpu')") +// jep.exec("options.add_argument('--headless')") +// jep.exec("options.add_argument('--disable-gpu')") jep.exec("driver = uc.Chrome(options=options)") jep.exec("userAgent = driver.execute_script('return navigator.userAgent')") @@ -188,7 +198,7 @@ object CFClearance { ) private val json by DI.global.instance() - private fun getCookies(jep: SharedInterpreter, url: String): List { + private fun getCookies(jep: SharedInterpreter): List { val challengeResolved = waitForChallengeResolve(jep) return if (challengeResolved) { @@ -238,4 +248,3 @@ object CFClearance { } } private class CloudflareBypassException(message: String?) : Exception(message) - diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt index cad35b211..3e86c76c7 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerConfig.kt @@ -44,7 +44,7 @@ class ServerConfig(config: Config, moduleName: String = MODULE_NAME) : SystemPro // misc val debugLogsEnabled: Boolean = debugLogsEnabled(GlobalConfigManager.config) val systemTrayEnabled: Boolean by overridableConfig - val webviewEnabled : Boolean by overridableConfig + val webviewEnabled: Boolean by overridableConfig companion object { fun register(config: Config) = ServerConfig(config.getConfig(MODULE_NAME))