Skip to content

Commit fb54d02

Browse files
authored
Fix SSL issues (#2655)
1 parent 788189c commit fb54d02

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa
11691169
}
11701170

11711171
override fun onCreate(savedInstanceState: Bundle?) {
1172-
app.initClient(this)
1172+
app.initClient(this, ignoreSSL = false)
1173+
@OptIn(UnsafeSSL::class)
1174+
insecureApp.initClient(this, ignoreSSL = true)
1175+
11731176
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
11741177

11751178
setLastError(this)
@@ -2059,4 +2062,4 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa
20592062
false
20602063
}
20612064
}
2062-
}
2065+
}

app/src/main/java/com/lagradost/cloudstream3/network/RequestsHelper.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.lagradost.cloudstream3.network
22

33
import android.content.Context
44
import androidx.preference.PreferenceManager
5+
import com.lagradost.cloudstream3.Prerelease
56
import com.lagradost.cloudstream3.R
67
import com.lagradost.cloudstream3.USER_AGENT
78
import com.lagradost.cloudstream3.mvvm.safe
@@ -15,19 +16,38 @@ import org.conscrypt.Conscrypt
1516
import java.io.File
1617
import java.security.Security
1718

19+
// Backwards compatible constructor, mark as deprecated later
1820
fun Requests.initClient(context: Context) {
1921
this.baseClient = buildDefaultClient(context)
2022
}
2123

24+
/** Only use ignoreSSL if you know what you are doing*/
25+
@Prerelease
26+
fun Requests.initClient(context: Context, ignoreSSL: Boolean = false) {
27+
this.baseClient = buildDefaultClient(context, ignoreSSL)
28+
}
29+
30+
31+
// Backwards compatible constructor, mark as deprecated later
2232
fun buildDefaultClient(context: Context): OkHttpClient {
33+
return buildDefaultClient(context, false)
34+
}
35+
36+
/** Only use ignoreSSL if you know what you are doing*/
37+
@Prerelease
38+
fun buildDefaultClient(context: Context, ignoreSSL: Boolean = false): OkHttpClient {
2339
safe { Security.insertProviderAt(Conscrypt.newProvider(), 1) }
2440

2541
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
2642
val dns = settingsManager.getInt(context.getString(R.string.dns_pref), 0)
2743
val baseClient = OkHttpClient.Builder()
2844
.followRedirects(true)
2945
.followSslRedirects(true)
30-
.ignoreAllSSLErrors()
46+
.apply {
47+
if (ignoreSSL) {
48+
ignoreAllSSLErrors()
49+
}
50+
}
3151
.cache(
3252
// Note that you need to add a ResponseInterceptor to make this 100% active.
3353
// The server response dictates if and when stuff should be cached.
@@ -52,11 +72,6 @@ fun buildDefaultClient(context: Context): OkHttpClient {
5272
return baseClient
5373
}
5474

55-
//val Request.cookies: Map<String, String>
56-
// get() {
57-
// return this.headers.getCookies("Cookie")
58-
// }
59-
6075
private val DEFAULT_HEADERS = mapOf("user-agent" to USER_AGENT)
6176

6277
/**

library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ annotation class Prerelease
5555
)
5656
annotation class InternalAPI
5757

58+
@Retention(AnnotationRetention.BINARY) // This is only an IDE hint, and will not be used in the runtime
59+
@RequiresOptIn(
60+
message = "Only use this if you know what you are doing and you need to bypass the SSL certificate checks. Never use this for sensitive network requests such as logins.",
61+
level = RequiresOptIn.Level.WARNING
62+
)
63+
annotation class UnsafeSSL
64+
5865
/**
5966
* Defines the constant for the all languages preference, if this is set then it is
6067
* the equivalent of all languages being set

library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainActivity.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import com.lagradost.nicehttp.ResponseParser
88
import kotlin.reflect.KClass
99

1010
// Short name for requests client to make it nicer to use
11-
12-
var app = Requests(responseParser = object : ResponseParser {
11+
private val jacksonResponseParser = object : ResponseParser {
1312
val mapper: ObjectMapper = jacksonObjectMapper().configure(
1413
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
1514
false
@@ -30,6 +29,18 @@ var app = Requests(responseParser = object : ResponseParser {
3029
override fun writeValueAsString(obj: Any): String {
3130
return mapper.writeValueAsString(obj)
3231
}
33-
}).apply {
32+
}
33+
34+
/** The default networking helper. This helper performs SSL checks.
35+
* If you need to make requests to websites with invalid SSL certificates use insecureApp instead. */
36+
var app = Requests(responseParser = jacksonResponseParser).apply {
37+
defaultHeaders = mapOf("user-agent" to USER_AGENT)
38+
}
39+
40+
/** Same as the default app networking helper, but this instance ignores SSL certificates.
41+
* This should NEVER be used for sensitive networking operations such as logins. Only use this when required. */
42+
@Prerelease
43+
@UnsafeSSL
44+
var insecureApp = Requests(responseParser = jacksonResponseParser).apply {
3445
defaultHeaders = mapOf("user-agent" to USER_AGENT)
3546
}

0 commit comments

Comments
 (0)