Skip to content

Commit

Permalink
Implement FlareSolverr (Suwayomi#844)
Browse files Browse the repository at this point in the history
* Implement FlareSolverr

* Oops
  • Loading branch information
Syer10 authored Jan 23, 2024
1 parent 9121a63 commit d658e07
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 195 deletions.
51 changes: 33 additions & 18 deletions server/src/main/kotlin/eu/kanade/tachiyomi/network/NetworkHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,29 @@ package eu.kanade.tachiyomi.network
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// import android.content.Context
// import eu.kanade.tachiyomi.BuildConfig
// import eu.kanade.tachiyomi.data.preference.PreferencesHelper
// import okhttp3.HttpUrl.Companion.toHttpUrl
// import okhttp3.dnsoverhttps.DnsOverHttps
// import okhttp3.logging.HttpLoggingInterceptor
// import uy.kohesive.injekt.injectLazy
import android.content.Context
import eu.kanade.tachiyomi.network.interceptor.CloudflareInterceptor
import eu.kanade.tachiyomi.network.interceptor.IgnoreGzipInterceptor
import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor
import eu.kanade.tachiyomi.network.interceptor.UserAgentInterceptor
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import mu.KotlinLogging
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.brotli.BrotliInterceptor
import okhttp3.logging.HttpLoggingInterceptor
import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource
import java.io.File
import java.net.CookieHandler
import java.net.CookieManager
import java.net.CookiePolicy
import java.util.concurrent.TimeUnit

@Suppress("UNUSED_PARAMETER")
class NetworkHelper(context: Context) {
// private val preferences: PreferencesHelper by injectLazy()

Expand All @@ -48,6 +47,26 @@ class NetworkHelper(context: Context) {
}
// Tachidesk <--

private val userAgent =
MutableStateFlow(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
)

fun defaultUserAgentProvider(): String {
return userAgent.value
}

init {
@OptIn(DelicateCoroutinesApi::class)
userAgent
.drop(1)
.onEach {
GetCatalogueSource.unregisterAllCatalogueSources() // need to reset the headers
}
.launchIn(GlobalScope)
}

private val baseClientBuilder: OkHttpClient.Builder
get() {
val builder =
Expand All @@ -63,7 +82,7 @@ class NetworkHelper(context: Context) {
),
)
.addInterceptor(UncaughtExceptionInterceptor())
.addInterceptor(UserAgentInterceptor())
.addInterceptor(UserAgentInterceptor(::defaultUserAgentProvider))
.addNetworkInterceptor(IgnoreGzipInterceptor())
.addNetworkInterceptor(BrotliInterceptor)

Expand All @@ -78,14 +97,14 @@ class NetworkHelper(context: Context) {
}
},
).apply {
level = HttpLoggingInterceptor.Level.BASIC
level = HttpLoggingInterceptor.Level.HEADERS
}
builder.addNetworkInterceptor(httpLoggingInterceptor)
// }

// builder.addInterceptor(
// CloudflareInterceptor(context, cookieJar, ::defaultUserAgentProvider),
// )
builder.addInterceptor(
CloudflareInterceptor(setUserAgent = { userAgent.value = it }),
)

// when (preferences.dohProvider().get()) {
// PREF_DOH_CLOUDFLARE -> builder.dohCloudflare()
Expand All @@ -108,9 +127,5 @@ class NetworkHelper(context: Context) {
// val client by lazy { baseClientBuilder.cache(Cache(cacheDir, cacheSize)).build() }
val client by lazy { baseClientBuilder.build() }

val cloudflareClient by lazy {
client.newBuilder()
.addInterceptor(CloudflareInterceptor())
.build()
}
val cloudflareClient by lazy { client }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ class PersistentCookieStore(context: Context) : CookieStore {
private val lock = ReentrantLock()

init {
for ((key, value) in prefs.all) {
@Suppress("UNCHECKED_CAST")
val cookies = value as? Set<String>
if (cookies != null) {
val domains =
prefs.all.keys.map { it.substringBeforeLast(".") }
.toSet()
domains.forEach { domain ->
val cookies = prefs.getStringSet(domain, emptySet())
if (!cookies.isNullOrEmpty()) {
try {
val url = "http://$key".toHttpUrlOrNull() ?: continue
val url = "http://$domain".toHttpUrlOrNull() ?: return@forEach
val nonExpiredCookies =
cookies.mapNotNull { Cookie.parse(url, it) }
.filter { !it.hasExpired() }
cookieMap.put(key, nonExpiredCookies)
cookieMap[domain] = nonExpiredCookies
} catch (e: Exception) {
// Ignore
}
Expand Down
Loading

0 comments on commit d658e07

Please sign in to comment.