fixed doh for webview and glide

This commit is contained in:
Blatzar 2021-10-18 18:04:05 +02:00
parent b8195f2ec6
commit 76fe9e0d3b
5 changed files with 60 additions and 4 deletions

View file

@ -109,6 +109,7 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'
implementation 'jp.wasabeef:glide-transformations:4.0.0'

View file

@ -9,7 +9,7 @@ import java.net.InetAddress
* Based on https://github.com/tachiyomiorg/tachiyomi/blob/master/app/src/main/java/eu/kanade/tachiyomi/network/DohProviders.kt
*/
private fun OkHttpClient.Builder.addGenericDns(url: String, ips: List<String>) = dns(
fun OkHttpClient.Builder.addGenericDns(url: String, ips: List<String>) = dns(
DnsOverHttps
.Builder()
.client(build())

View file

@ -23,7 +23,7 @@ private val DEFAULT_DATA: Map<String, String> = mapOf()
private val DEFAULT_COOKIES: Map<String, String> = mapOf()
private val DEFAULT_REFERER: String? = null
fun Context.initRequestClient() {
fun Context.initRequestClient(): OkHttpClient {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
val dns = settingsManager.getInt(this.getString(R.string.dns_pref), 0)
baseClient = OkHttpClient.Builder()
@ -44,6 +44,7 @@ fun Context.initRequestClient() {
}
// Needs to be build as otherwise the other builders will change this object
.build()
return baseClient
}
/** WARNING! CAN ONLY BE READ ONCE */

View file

@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.network
import android.annotation.SuppressLint
import android.net.http.SslError
import android.webkit.*
import androidx.core.view.contains
import com.lagradost.cloudstream3.AcraApplication
import com.lagradost.cloudstream3.utils.Coroutines.main
import kotlinx.coroutines.delay
@ -10,6 +11,7 @@ import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.net.URI
import java.util.concurrent.TimeUnit
class WebViewResolver(val interceptUrl: Regex) : Interceptor {
@ -56,7 +58,7 @@ class WebViewResolver(val interceptUrl: Regex) : Interceptor {
request: WebResourceRequest
): WebResourceResponse? {
val webViewUrl = request.url.toString()
// println("Override url $webViewUrl")
if (interceptUrl.containsMatchIn(webViewUrl)) {
fixedRequest = getRequestCreator(
webViewUrl,
@ -71,7 +73,27 @@ class WebViewResolver(val interceptUrl: Regex) : Interceptor {
println("Web-view request finished: $webViewUrl")
destroyWebView()
}
return super.shouldInterceptRequest(view, request)
return try {
when {
// suppress favicon requests as we don't display them anywhere
webViewUrl.endsWith("/favicon.ico") -> WebResourceResponse("image/png", null, null)
webViewUrl.contains("recaptcha") -> super.shouldInterceptRequest(view, request)
request.method == "GET" -> get(
webViewUrl,
headers = request.requestHeaders
).toWebResourceResponse()
request.method == "POST" -> post(
webViewUrl,
headers = request.requestHeaders
).toWebResourceResponse()
else -> return super.shouldInterceptRequest(view, request)
}
} catch (e: Exception) {
null
}
}
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
@ -99,4 +121,18 @@ class WebViewResolver(val interceptUrl: Regex) : Interceptor {
return null
}
fun Response.toWebResourceResponse(): WebResourceResponse {
val contentTypeValue = this.header("Content-Type")
// 1. contentType. 2. charset
val typeRegex = Regex("""(.*);(?:.*charset=(.*)(?:|;)|)""")
return if (contentTypeValue != null) {
val found = typeRegex.find(contentTypeValue ?: "")
val contentType = found?.groupValues?.getOrNull(1)?.ifBlank { null } ?: contentTypeValue
val charset = found?.groupValues?.getOrNull(2)?.ifBlank { null }
WebResourceResponse(contentType, charset, this.body?.byteStream())
} else {
WebResourceResponse("application/octet-stream", null, this.body?.byteStream())
}
}
}

View file

@ -1,12 +1,18 @@
package com.lagradost.cloudstream3.utils
import android.content.Context
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey
import com.lagradost.cloudstream3.network.initRequestClient
import java.io.InputStream
@GlideModule
class GlideModule : AppGlideModule() {
@ -18,4 +24,16 @@ class GlideModule : AppGlideModule() {
.signature(ObjectKey(System.currentTimeMillis().toShort()))
}
}
// Needed for DOH
// https://stackoverflow.com/a/61634041
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
val client = context.initRequestClient()
registry.replace(
GlideUrl::class.java,
InputStream::class.java,
OkHttpUrlLoader.Factory(client)
)
super.registerComponents(context, glide, registry)
}
}