From 63e1082b97787e4406332e54b00a11fb2c74e381 Mon Sep 17 00:00:00 2001 From: Mitchell Syer Date: Wed, 24 Jan 2024 17:49:51 -0500 Subject: [PATCH] Minor fixes for FlareSolverr (#851) * Minor fixes for FlareSolverr * Weird crash but ok --- .../network/PersistentCookieStore.kt | 4 ++- .../interceptor/CloudflareInterceptor.kt | 28 +++++++++++++------ .../graphql/mutations/SettingsMutation.kt | 5 ++++ .../tachidesk/graphql/types/SettingsType.kt | 17 +++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/server/src/main/kotlin/eu/kanade/tachiyomi/network/PersistentCookieStore.kt b/server/src/main/kotlin/eu/kanade/tachiyomi/network/PersistentCookieStore.kt index cbe82b9d2..90429b8c2 100644 --- a/server/src/main/kotlin/eu/kanade/tachiyomi/network/PersistentCookieStore.kt +++ b/server/src/main/kotlin/eu/kanade/tachiyomi/network/PersistentCookieStore.kt @@ -85,7 +85,9 @@ class PersistentCookieStore(context: Context) : CookieStore { it.toHttpCookie() } - fun get(url: HttpUrl) = get(url.toUri().host) + fun get(url: HttpUrl): List { + return get(url.toUri().host ?: return emptyList()) + } override fun add( uri: URI?, 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 032cb8467..d9fb36437 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 @@ -4,6 +4,10 @@ import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.awaitSuccess import eu.kanade.tachiyomi.network.parseAs +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -23,14 +27,13 @@ import suwayomi.tachidesk.server.serverConfig import uy.kohesive.injekt.injectLazy import java.io.IOException import kotlin.time.Duration.Companion.seconds +import kotlin.time.toJavaDuration class CloudflareInterceptor( private val setUserAgent: (String) -> Unit, ) : Interceptor { private val logger = KotlinLogging.logger {} - private val network: NetworkHelper by injectLazy() - override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() @@ -80,6 +83,18 @@ class CloudflareInterceptor( object CFClearance { private val logger = KotlinLogging.logger {} private val network: NetworkHelper by injectLazy() + private val client by lazy { + @Suppress("OPT_IN_USAGE") + serverConfig.flareSolverrTimeout + .map { timeoutInt -> + val timeout = timeoutInt.seconds + network.client.newBuilder() + .callTimeout(timeout.plus(10.seconds).toJavaDuration()) + .readTimeout(timeout.plus(5.seconds).toJavaDuration()) + .build() + } + .stateIn(GlobalScope, SharingStarted.Eagerly, network.client) + } private val json: Json by injectLazy() private val jsonMediaType = "application/json".toMediaType() private val mutex = Mutex() @@ -142,10 +157,11 @@ object CFClearance { setUserAgent: (String) -> Unit, originalRequest: Request, ): Request { + val timeout = serverConfig.flareSolverrTimeout.value.seconds val flareSolverResponse = with(json) { mutex.withLock { - network.client.newCall( + client.value.newCall( POST( url = serverConfig.flareSolverrUrl.value.removeSuffix("/") + "/v1", body = @@ -158,11 +174,7 @@ object CFClearance { FlareSolverCookie(it.name, it.value) }, returnOnlyCookies = true, - maxTimeout = - serverConfig.flareSolverrTimeout.value - .seconds - .inWholeMilliseconds - .toInt(), + maxTimeout = timeout.inWholeMilliseconds.toInt(), ), ).toRequestBody(jsonMediaType), ), diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt index 9ee358e4a..3e495dc17 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/SettingsMutation.kt @@ -86,6 +86,11 @@ class SettingsMutation { // local source updateSetting(settings.localSourcePath, serverConfig.localSourcePath) + + // cloudflare bypass + updateSetting(settings.flareSolverrEnabled, serverConfig.flareSolverrEnabled) + updateSetting(settings.flareSolverrUrl, serverConfig.flareSolverrUrl) + updateSetting(settings.flareSolverrTimeout, serverConfig.flareSolverrTimeout) } fun setSettings(input: SetSettingsInput): SetSettingsPayload { diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt index 32c526bc0..4f4a8c828 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SettingsType.kt @@ -71,6 +71,11 @@ interface Settings : Node { // local source val localSourcePath: String? + + // cloudflare bypass + val flareSolverrEnabled: Boolean? + val flareSolverrUrl: String? + val flareSolverrTimeout: Int? } data class PartialSettingsType( @@ -118,6 +123,10 @@ data class PartialSettingsType( override val backupTTL: Int?, // local source override val localSourcePath: String?, + // cloudflare bypass + override val flareSolverrEnabled: Boolean?, + override val flareSolverrUrl: String?, + override val flareSolverrTimeout: Int?, ) : Settings class SettingsType( @@ -165,6 +174,10 @@ class SettingsType( override val backupTTL: Int, // local source override val localSourcePath: String, + // cloudflare bypass + override val flareSolverrEnabled: Boolean?, + override val flareSolverrUrl: String?, + override val flareSolverrTimeout: Int?, ) : Settings { constructor(config: ServerConfig = serverConfig) : this( config.ip.value, @@ -211,5 +224,9 @@ class SettingsType( config.backupTTL.value, // local source config.localSourcePath.value, + // cloudflare bypass + config.flareSolverrEnabled.value, + config.flareSolverrUrl.value, + config.flareSolverrTimeout.value, ) }