Minor fixes for FlareSolverr (#851)

* Minor fixes for FlareSolverr

* Weird crash but ok
This commit is contained in:
Mitchell Syer
2024-01-24 17:49:51 -05:00
committed by GitHub
parent 285f228660
commit 63e1082b97
4 changed files with 45 additions and 9 deletions

View File

@@ -85,7 +85,9 @@ class PersistentCookieStore(context: Context) : CookieStore {
it.toHttpCookie() it.toHttpCookie()
} }
fun get(url: HttpUrl) = get(url.toUri().host) fun get(url: HttpUrl): List<Cookie> {
return get(url.toUri().host ?: return emptyList())
}
override fun add( override fun add(
uri: URI?, uri: URI?,

View File

@@ -4,6 +4,10 @@ import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.awaitSuccess import eu.kanade.tachiyomi.network.awaitSuccess
import eu.kanade.tachiyomi.network.parseAs 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.runBlocking
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
@@ -23,14 +27,13 @@ import suwayomi.tachidesk.server.serverConfig
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.io.IOException import java.io.IOException
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration
class CloudflareInterceptor( class CloudflareInterceptor(
private val setUserAgent: (String) -> Unit, private val setUserAgent: (String) -> Unit,
) : Interceptor { ) : Interceptor {
private val logger = KotlinLogging.logger {} private val logger = KotlinLogging.logger {}
private val network: NetworkHelper by injectLazy()
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request() val originalRequest = chain.request()
@@ -80,6 +83,18 @@ class CloudflareInterceptor(
object CFClearance { object CFClearance {
private val logger = KotlinLogging.logger {} private val logger = KotlinLogging.logger {}
private val network: NetworkHelper by injectLazy() 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 json: Json by injectLazy()
private val jsonMediaType = "application/json".toMediaType() private val jsonMediaType = "application/json".toMediaType()
private val mutex = Mutex() private val mutex = Mutex()
@@ -142,10 +157,11 @@ object CFClearance {
setUserAgent: (String) -> Unit, setUserAgent: (String) -> Unit,
originalRequest: Request, originalRequest: Request,
): Request { ): Request {
val timeout = serverConfig.flareSolverrTimeout.value.seconds
val flareSolverResponse = val flareSolverResponse =
with(json) { with(json) {
mutex.withLock { mutex.withLock {
network.client.newCall( client.value.newCall(
POST( POST(
url = serverConfig.flareSolverrUrl.value.removeSuffix("/") + "/v1", url = serverConfig.flareSolverrUrl.value.removeSuffix("/") + "/v1",
body = body =
@@ -158,11 +174,7 @@ object CFClearance {
FlareSolverCookie(it.name, it.value) FlareSolverCookie(it.name, it.value)
}, },
returnOnlyCookies = true, returnOnlyCookies = true,
maxTimeout = maxTimeout = timeout.inWholeMilliseconds.toInt(),
serverConfig.flareSolverrTimeout.value
.seconds
.inWholeMilliseconds
.toInt(),
), ),
).toRequestBody(jsonMediaType), ).toRequestBody(jsonMediaType),
), ),

View File

@@ -86,6 +86,11 @@ class SettingsMutation {
// local source // local source
updateSetting(settings.localSourcePath, serverConfig.localSourcePath) 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 { fun setSettings(input: SetSettingsInput): SetSettingsPayload {

View File

@@ -71,6 +71,11 @@ interface Settings : Node {
// local source // local source
val localSourcePath: String? val localSourcePath: String?
// cloudflare bypass
val flareSolverrEnabled: Boolean?
val flareSolverrUrl: String?
val flareSolverrTimeout: Int?
} }
data class PartialSettingsType( data class PartialSettingsType(
@@ -118,6 +123,10 @@ data class PartialSettingsType(
override val backupTTL: Int?, override val backupTTL: Int?,
// local source // local source
override val localSourcePath: String?, override val localSourcePath: String?,
// cloudflare bypass
override val flareSolverrEnabled: Boolean?,
override val flareSolverrUrl: String?,
override val flareSolverrTimeout: Int?,
) : Settings ) : Settings
class SettingsType( class SettingsType(
@@ -165,6 +174,10 @@ class SettingsType(
override val backupTTL: Int, override val backupTTL: Int,
// local source // local source
override val localSourcePath: String, override val localSourcePath: String,
// cloudflare bypass
override val flareSolverrEnabled: Boolean?,
override val flareSolverrUrl: String?,
override val flareSolverrTimeout: Int?,
) : Settings { ) : Settings {
constructor(config: ServerConfig = serverConfig) : this( constructor(config: ServerConfig = serverConfig) : this(
config.ip.value, config.ip.value,
@@ -211,5 +224,9 @@ class SettingsType(
config.backupTTL.value, config.backupTTL.value,
// local source // local source
config.localSourcePath.value, config.localSourcePath.value,
// cloudflare bypass
config.flareSolverrEnabled.value,
config.flareSolverrUrl.value,
config.flareSolverrTimeout.value,
) )
} }