Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public class SSLPinningTurboModule extends NativeSSLPinningSpec implements KeyCh
private Promise promise;
private static String alias;
private static ReactApplicationContext reactContext;
private static OkHttpClient sharedClient;

public static OkHttpClient getSharedOkHttpClient() {
if (sharedClient != null) {
return sharedClient;
}
if (alias != null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.cookieJar(new ReactCookieJarContainer());

SSLSocketFactory sslSocketFactory = getSSLFactory(alias);
X509TrustManager trustManager = getTrustManagerFactory();
if (sslSocketFactory != null) {
builder.sslSocketFactory(sslSocketFactory, trustManager);
}

sharedClient = builder.build();
return sharedClient;
}
return null;
}

public SSLPinningTurboModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -61,6 +85,10 @@ public void apply(OkHttpClient.Builder builder) {
}

protected OkHttpClient getOkHttpClient() {
OkHttpClient shared = getSharedOkHttpClient();
if (shared != null) {
return shared;
}
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import okhttp3.WebSocket
import okhttp3.WebSocketListener
import org.json.JSONArray
import org.json.JSONObject
import chat.rocket.reactnative.networking.SSLPinningTurboModule
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
Expand All @@ -23,9 +24,11 @@ class DDPClient {

companion object {
private const val TAG = "RocketChat.DDPClient"
private val sharedClient = OkHttpClient.Builder()
.pingInterval(30, TimeUnit.SECONDS)
.build()
private val sharedClient: OkHttpClient by lazy {
SSLPinningTurboModule.getSharedOkHttpClient() ?: OkHttpClient.Builder()
.pingInterval(30, TimeUnit.SECONDS)
.build()
}
}

private var webSocket: WebSocket? = null
Expand Down Expand Up @@ -342,8 +345,7 @@ class DDPClient {
normalizedHost = normalizedHost.removePrefix("https://")
}
normalizedHost.startsWith("http://") -> {
useSsl = false
normalizedHost = normalizedHost.removePrefix("http://")
throw IllegalStateException("DDPClient does not support plaintext http:// servers — use https://")
}
else -> {
useSsl = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject
import chat.rocket.reactnative.networking.SSLPinningTurboModule
import java.io.IOException

/**
Expand Down Expand Up @@ -40,7 +41,9 @@ class MediaCallsAnswerRequest(
companion object {
private const val TAG = "RocketChat.MediaCallsAnswerRequest"
private val JSON_MEDIA_TYPE = "application/json; charset=utf-8".toMediaType()
private val httpClient = OkHttpClient()
private val httpClient: OkHttpClient by lazy {
SSLPinningTurboModule.getSharedOkHttpClient() ?: OkHttpClient()
}
private val mainHandler = Handler(Looper.getMainLooper())

@JvmStatic
Expand Down
Loading