Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions buildSrc/src/main/kotlin/RandomUaCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import com.diffplug.spotless.FormatterFunc
import com.diffplug.spotless.FormatterStep
import java.io.Serializable

object RandomUaCheck {
fun create(): FormatterStep = FormatterStep.create(
"randomua-requires-getMangaUrl",
State(),
State::toFormatter,
)

private class State : Serializable {
fun toFormatter() = FormatterFunc { content ->
if ("package keiyoushi.lib.randomua" !in content &&
"keiyoushi.lib.randomua" in content &&
"override fun getMangaUrl(" !in content
) {
throw AssertionError(
"usage of :lib:randomua requires override of getMangaUrl()",
)
}
content
}
}
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/keiyoushi.lint.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ spotless {
))
trimTrailingWhitespace()
endWithNewline()
addStep(RandomUaCheck.create())
}

java {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/lib-android.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
kotlin {
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlinx.serialization.ExperimentalSerializationApi")
freeCompilerArgs.add("-Xcontext-parameters")
}
}

Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/lib-multisrc.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
kotlin {
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlinx.serialization.ExperimentalSerializationApi")
freeCompilerArgs.add("-Xcontext-parameters")
}
}

Expand Down
1 change: 1 addition & 0 deletions common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ android {
kotlin {
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlinx.serialization.ExperimentalSerializationApi")
freeCompilerArgs.add("-Xcontext-parameters")
}
}

Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ android {
kotlin {
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlinx.serialization.ExperimentalSerializationApi")
freeCompilerArgs.add("-Xcontext-parameters")
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/randomua/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id("lib-android")
}

dependencies {
compileOnly("com.squareup.okhttp3:okhttp-brotli:5.3.2")
}
85 changes: 85 additions & 0 deletions lib/randomua/src/keiyoushi/lib/randomua/Helper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package keiyoushi.lib.randomua

import android.os.Looper
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.await
import keiyoushi.utils.parseAs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.Serializable
import okhttp3.CacheControl
import okhttp3.brotli.BrotliInterceptor
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get

private var userAgent: String? = null
private val client = Injekt.get<NetworkHelper>().client.newBuilder()
.addNetworkInterceptor { chain ->
chain.proceed(chain.request()).newBuilder()
.header("Cache-Control", "max-age=${24 * 60 * 60}")
.removeHeader("Pragma")
.removeHeader("Expires")
.build()
}
.apply {
val index = networkInterceptors().indexOfFirst { it is BrotliInterceptor }
if (index >= 0) interceptors().add(networkInterceptors().removeAt(index))
}
.build()

internal fun getRandomUserAgent(
userAgentType: UserAgentType,
filterInclude: List<String>,
filterExclude: List<String>,
): String? {
if (!userAgent.isNullOrEmpty()) return userAgent

// avoid network on main thread when webview screen accesses headers
val uaRequest = if (Looper.myLooper() == Looper.getMainLooper()) {
GET(UA_DB_URL, cache = CacheControl.FORCE_CACHE)
} else {
GET(UA_DB_URL)
}

val uaResponse = runBlocking(Dispatchers.IO) { client.newCall(uaRequest).await() }

if (!uaResponse.isSuccessful) {
uaResponse.close()
return null
}

val userAgentList = uaResponse.parseAs<UserAgentList>()

return when (userAgentType) {
UserAgentType.DESKTOP -> userAgentList.desktop
UserAgentType.MOBILE -> userAgentList.mobile
else -> error("Expected UserAgentType.DESKTOP or UserAgentType.MOBILE but got UserAgentType.${userAgentType.name} instead")
}
.filter {
filterInclude.isEmpty() || filterInclude.any { filter ->
it.contains(filter, ignoreCase = true)
}
}
.filterNot {
filterExclude.any { filter ->
it.contains(filter, ignoreCase = true)
}
}
.randomOrNull()
.also { userAgent = it }
}

private const val UA_DB_URL = "https://keiyoushi.github.io/user-agents/user-agents.json"

enum class UserAgentType {
MOBILE,
DESKTOP,
OFF,
}

@Serializable
private class UserAgentList(
val desktop: List<String>,
val mobile: List<String>,
)
121 changes: 0 additions & 121 deletions lib/randomua/src/keiyoushi/lib/randomua/RandomUserAgentInterceptor.kt

This file was deleted.

This file was deleted.

Loading