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()
}
fun get(url: HttpUrl) = get(url.toUri().host)
fun get(url: HttpUrl): List<Cookie> {
return get(url.toUri().host ?: return emptyList())
}
override fun add(
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.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),
),

View File

@@ -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 {

View File

@@ -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,
)
}