Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cleartext traffic check #122

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ext {
compileSdkVersion = 28
buildToolsVersion = '28.0.3'

android = 'org.robolectric:android-all:9-robolectric-4913185-2'

appCompat = 'androidx.appcompat:appcompat:1.0.0'
material = 'com.google.android.material:material:1.0.0'
multiDex = 'androidx.multidex:multidex:2.0.0'
Expand Down
1 change: 1 addition & 0 deletions scarlet-websocket-okhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies {
implementation project(':scarlet-core-internal')
implementation rootProject.ext.rxJava
implementation rootProject.ext.kotlinStdlib
compileOnly rootProject.ext.android

testImplementation project(':scarlet')
testImplementation project(':scarlet-websocket-mockwebserver')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package com.tinder.scarlet.websocket.okhttp

import android.os.Build
import android.security.NetworkSecurityPolicy
import com.tinder.scarlet.WebSocket
import com.tinder.scarlet.websocket.okhttp.request.RequestFactory
import com.tinder.scarlet.websocket.okhttp.request.StaticUrlRequestFactory
Expand All @@ -16,5 +18,16 @@ fun OkHttpClient.newWebSocketFactory(requestFactory: RequestFactory): WebSocket.
}

fun OkHttpClient.newWebSocketFactory(url: String): WebSocket.Factory {
if (url.startsWith("ws://")) {
aaronweihe marked this conversation as resolved.
Show resolved Hide resolved
try {
if (Build.VERSION.SDK_INT >= 23 &&
!NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted()) {
akindofyoga marked this conversation as resolved.
Show resolved Hide resolved
throw RuntimeException("Android configuration does not permit cleartext traffic.")
aaronweihe marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (_: ClassNotFoundException) {
// Not running on Android
}
}

return newWebSocketFactory(StaticUrlRequestFactory(url))
}