Skip to content

Commit

Permalink
Minor fixes for FlareSolverr (#851)
Browse files Browse the repository at this point in the history
* Minor fixes for FlareSolverr

* Weird crash but ok
  • Loading branch information
Syer10 authored Jan 24, 2024
1 parent 285f228 commit 63e1082
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 =
Expand All @@ -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),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
}

0 comments on commit 63e1082

Please sign in to comment.