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.cloudstream3.mvvm.normalSafeApiCall 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 org.conscrypt.Conscrypt import java.io.File import java.security.Security fun Requests.initClient(context: Context): OkHttpClient { normalSafeApiCall { Security.insertProviderAt(Conscrypt.newProvider(), 1) } 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() 5 -> addDNSWatchDns() 6 -> addQuad9Dns() } } // Needs to be build as otherwise the other builders will change this object .build() return baseClient } //val Request.cookies: Map // get() { // return this.headers.getCookies("Cookie") // } 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, cookie: Map ): 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() }