mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
fixed doh for webview and glide
This commit is contained in:
parent
b8195f2ec6
commit
76fe9e0d3b
5 changed files with 60 additions and 4 deletions
|
@ -109,6 +109,7 @@ dependencies {
|
||||||
|
|
||||||
implementation 'com.github.bumptech.glide:glide:4.12.0'
|
implementation 'com.github.bumptech.glide:glide:4.12.0'
|
||||||
kapt 'com.github.bumptech.glide:compiler: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'
|
implementation 'jp.wasabeef:glide-transformations:4.0.0'
|
||||||
|
|
||||||
|
|
|
@ -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
|
* 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
|
DnsOverHttps
|
||||||
.Builder()
|
.Builder()
|
||||||
.client(build())
|
.client(build())
|
||||||
|
|
|
@ -23,7 +23,7 @@ private val DEFAULT_DATA: Map<String, String> = mapOf()
|
||||||
private val DEFAULT_COOKIES: Map<String, String> = mapOf()
|
private val DEFAULT_COOKIES: Map<String, String> = mapOf()
|
||||||
private val DEFAULT_REFERER: String? = null
|
private val DEFAULT_REFERER: String? = null
|
||||||
|
|
||||||
fun Context.initRequestClient() {
|
fun Context.initRequestClient(): OkHttpClient {
|
||||||
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
|
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
val dns = settingsManager.getInt(this.getString(R.string.dns_pref), 0)
|
val dns = settingsManager.getInt(this.getString(R.string.dns_pref), 0)
|
||||||
baseClient = OkHttpClient.Builder()
|
baseClient = OkHttpClient.Builder()
|
||||||
|
@ -44,6 +44,7 @@ fun Context.initRequestClient() {
|
||||||
}
|
}
|
||||||
// Needs to be build as otherwise the other builders will change this object
|
// Needs to be build as otherwise the other builders will change this object
|
||||||
.build()
|
.build()
|
||||||
|
return baseClient
|
||||||
}
|
}
|
||||||
|
|
||||||
/** WARNING! CAN ONLY BE READ ONCE */
|
/** WARNING! CAN ONLY BE READ ONCE */
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.network
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.net.http.SslError
|
import android.net.http.SslError
|
||||||
import android.webkit.*
|
import android.webkit.*
|
||||||
|
import androidx.core.view.contains
|
||||||
import com.lagradost.cloudstream3.AcraApplication
|
import com.lagradost.cloudstream3.AcraApplication
|
||||||
import com.lagradost.cloudstream3.utils.Coroutines.main
|
import com.lagradost.cloudstream3.utils.Coroutines.main
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
@ -10,6 +11,7 @@ import kotlinx.coroutines.runBlocking
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
import java.net.URI
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class WebViewResolver(val interceptUrl: Regex) : Interceptor {
|
class WebViewResolver(val interceptUrl: Regex) : Interceptor {
|
||||||
|
@ -56,7 +58,7 @@ class WebViewResolver(val interceptUrl: Regex) : Interceptor {
|
||||||
request: WebResourceRequest
|
request: WebResourceRequest
|
||||||
): WebResourceResponse? {
|
): WebResourceResponse? {
|
||||||
val webViewUrl = request.url.toString()
|
val webViewUrl = request.url.toString()
|
||||||
// println("Override url $webViewUrl")
|
|
||||||
if (interceptUrl.containsMatchIn(webViewUrl)) {
|
if (interceptUrl.containsMatchIn(webViewUrl)) {
|
||||||
fixedRequest = getRequestCreator(
|
fixedRequest = getRequestCreator(
|
||||||
webViewUrl,
|
webViewUrl,
|
||||||
|
@ -71,7 +73,27 @@ class WebViewResolver(val interceptUrl: Regex) : Interceptor {
|
||||||
println("Web-view request finished: $webViewUrl")
|
println("Web-view request finished: $webViewUrl")
|
||||||
destroyWebView()
|
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?) {
|
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
|
||||||
|
@ -99,4 +121,18 @@ class WebViewResolver(val interceptUrl: Regex) : Interceptor {
|
||||||
return null
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,12 +1,18 @@
|
||||||
package com.lagradost.cloudstream3.utils
|
package com.lagradost.cloudstream3.utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
import com.bumptech.glide.GlideBuilder
|
import com.bumptech.glide.GlideBuilder
|
||||||
|
import com.bumptech.glide.Registry
|
||||||
import com.bumptech.glide.annotation.GlideModule
|
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.engine.DiskCacheStrategy
|
||||||
|
import com.bumptech.glide.load.model.GlideUrl
|
||||||
import com.bumptech.glide.module.AppGlideModule
|
import com.bumptech.glide.module.AppGlideModule
|
||||||
import com.bumptech.glide.request.RequestOptions
|
import com.bumptech.glide.request.RequestOptions
|
||||||
import com.bumptech.glide.signature.ObjectKey
|
import com.bumptech.glide.signature.ObjectKey
|
||||||
|
import com.lagradost.cloudstream3.network.initRequestClient
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
@GlideModule
|
@GlideModule
|
||||||
class GlideModule : AppGlideModule() {
|
class GlideModule : AppGlideModule() {
|
||||||
|
@ -18,4 +24,16 @@ class GlideModule : AppGlideModule() {
|
||||||
.signature(ObjectKey(System.currentTimeMillis().toShort()))
|
.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)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue