Skip to content

Commit

Permalink
fix injekt
Browse files Browse the repository at this point in the history
  • Loading branch information
cuong-tran committed Dec 25, 2024
1 parent a582195 commit 22b3559
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class AppModule(val app: Application) : InjektModule {
addSingletonFactory { ChapterCache(app, get(), get()) }
addSingletonFactory { CoverCache(app) }

addSingletonFactory { NetworkHelper(app, get(), BuildConfig.DEBUG) }
addSingletonFactory { NetworkHelper(app, get(), get(), BuildConfig.DEBUG) }
addSingletonFactory { JavaScriptEngine(app) }

addSingletonFactory<SourceManager> { AndroidSourceManager(app, get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.network.interceptor.FlareSolverrInterceptor
import eu.kanade.tachiyomi.network.interceptor.IgnoreGzipInterceptor
import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor
import eu.kanade.tachiyomi.network.interceptor.UserAgentInterceptor
import kotlinx.serialization.json.Json
import logcat.LogPriority
import okhttp3.Cache
import okhttp3.Headers
Expand All @@ -25,6 +26,9 @@ import kotlin.random.Random
open /* SY <-- */ class NetworkHelper(
private val context: Context,
private val preferences: NetworkPreferences,
// KMK -->
private val json: Json,
// KMK <--
// SY -->
val isDebugBuild: Boolean,
// SY <--
Expand Down Expand Up @@ -64,7 +68,13 @@ open /* SY <-- */ class NetworkHelper(
.addInterceptor(UserAgentInterceptor(::defaultUserAgentProvider))
.addNetworkInterceptor(IgnoreGzipInterceptor())
.addNetworkInterceptor(BrotliInterceptor)
.addNetworkInterceptor(FlareSolverrInterceptor(preferences))
.addNetworkInterceptor(
FlareSolverrInterceptor(
preferences = preferences,
network = this,
json = json,
),
)

if (isDebugBuild) {
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okio.IOException
import uy.kohesive.injekt.injectLazy
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

class FlareSolverrInterceptor(private val preferences: NetworkPreferences) : Interceptor {
class FlareSolverrInterceptor(
private val preferences: NetworkPreferences,
private val network: NetworkHelper,
private val json: Json,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

Expand All @@ -47,7 +50,12 @@ class FlareSolverrInterceptor(private val preferences: NetworkPreferences) : Int

val request =
runBlocking {
CFClearance.resolveWithFlareSolverr(originalRequest)
CFClearance.resolveWithFlareSolverr(
originalRequest = originalRequest,
networkPreferences = preferences,
network = network,
json = json,
)
}

chain.proceed(request)
Expand All @@ -59,11 +67,7 @@ class FlareSolverrInterceptor(private val preferences: NetworkPreferences) : Int
}

object CFClearance {
private val network: NetworkHelper by injectLazy()
private val json: Json by injectLazy()
private val jsonMediaType = "application/json".toMediaType()
private val networkPreferences: NetworkPreferences by injectLazy()
private val flareSolverrUrl = networkPreferences.flareSolverrUrl().get()
private val mutex = Mutex()

@Serializable
Expand Down Expand Up @@ -122,9 +126,13 @@ class FlareSolverrInterceptor(private val preferences: NetworkPreferences) : Int

suspend fun resolveWithFlareSolverr(
originalRequest: Request,
networkPreferences: NetworkPreferences,
network: NetworkHelper,
json: Json,
cookieManager: CookieManager = CookieManager.getInstance(),
): Request {
val flareSolverTag = "FlareSolverr"
val flareSolverrUrl = networkPreferences.flareSolverrUrl().get()

Log.d(flareSolverTag, "Requesting challenge solution for ${originalRequest.url}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class HttpSource : CatalogueSource {
// SY -->
protected val network: NetworkHelper by lazy {
val network = Injekt.get<NetworkHelper>()
object : NetworkHelper(Injekt.get<Application>(), Injekt.get(), network.isDebugBuild) {
object : NetworkHelper(Injekt.get<Application>(), Injekt.get(), Injekt.get(), network.isDebugBuild) {
override val client: OkHttpClient
get() = delegate?.networkHttpClient ?: network.client
.newBuilder()
Expand Down

0 comments on commit 22b3559

Please sign in to comment.