2022-05-02 16:58:27 +00:00
|
|
|
package com.lagradost.cloudstream3.network
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import com.lagradost.cloudstream3.R
|
|
|
|
import com.lagradost.cloudstream3.USER_AGENT
|
|
|
|
import com.lagradost.nicehttp.Requests
|
|
|
|
import com.lagradost.nicehttp.ignoreAllSSLErrors
|
|
|
|
import okhttp3.Cache
|
|
|
|
import okhttp3.Headers
|
|
|
|
import okhttp3.Headers.Companion.toHeaders
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
fun Requests.initClient(context: Context): OkHttpClient {
|
|
|
|
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
val dns = settingsManager.getInt(context.getString(R.string.dns_pref), 0)
|
|
|
|
baseClient = OkHttpClient.Builder()
|
|
|
|
.followRedirects(true)
|
|
|
|
.followSslRedirects(true)
|
|
|
|
.ignoreAllSSLErrors()
|
|
|
|
.cache(
|
|
|
|
// Note that you need to add a ResponseInterceptor to make this 100% active.
|
|
|
|
// The server response dictates if and when stuff should be cached.
|
|
|
|
Cache(
|
|
|
|
directory = File(context.cacheDir, "http_cache"),
|
|
|
|
maxSize = 50L * 1024L * 1024L // 50 MiB
|
|
|
|
)
|
|
|
|
).apply {
|
|
|
|
when (dns) {
|
|
|
|
1 -> addGoogleDns()
|
|
|
|
2 -> addCloudFlareDns()
|
|
|
|
// 3 -> addOpenDns()
|
|
|
|
4 -> addAdGuardDns()
|
2022-09-10 16:37:48 +00:00
|
|
|
5 -> addDNSWatchDns()
|
|
|
|
6 -> addQuad9Dns()
|
2022-05-02 16:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Needs to be build as otherwise the other builders will change this object
|
|
|
|
.build()
|
|
|
|
return baseClient
|
|
|
|
}
|
|
|
|
|
2022-08-21 18:14:26 +00:00
|
|
|
//val Request.cookies: Map<String, String>
|
|
|
|
// get() {
|
|
|
|
// return this.headers.getCookies("Cookie")
|
|
|
|
// }
|
2022-05-02 16:58:27 +00:00
|
|
|
|
|
|
|
private val DEFAULT_HEADERS = mapOf("user-agent" to USER_AGENT)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set headers > Set cookies > Default headers > Default Cookies
|
|
|
|
* TODO REMOVE AND REPLACE WITH NICEHTTP
|
|
|
|
*/
|
|
|
|
fun getHeaders(
|
|
|
|
headers: Map<String, String>,
|
|
|
|
cookie: Map<String, String>
|
|
|
|
): Headers {
|
|
|
|
val cookieMap =
|
|
|
|
if (cookie.isNotEmpty()) mapOf(
|
|
|
|
"Cookie" to cookie.entries.joinToString(" ") {
|
|
|
|
"${it.key}=${it.value};"
|
|
|
|
}) else mapOf()
|
|
|
|
val tempHeaders = (DEFAULT_HEADERS + headers + cookieMap)
|
|
|
|
return tempHeaders.toHeaders()
|
|
|
|
}
|