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

refactor: reuse okhttp instance #2065

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cryptobox-android = "1.1.5"
android-security = "1.1.0-alpha06"
ktor = "2.3.4"
okio = "3.4.0"
ok-http = "4.10.0"
ok-http = "4.11.0"
mockative = "1.4.1"
android-work = "2.8.1"
android-test-runner = "1.5.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ actual fun defaultHttpEngine(
ignoreSSLCertificates: Boolean,
certificatePinning: CertificatePinning
): HttpClientEngine {
if (serverConfigDTOApiProxy != null) {
throw IllegalArgumentException("Proxy is not supported on iOS")
}
if (proxyCredentials != null) {
throw IllegalArgumentException("Proxy is not supported on iOS")
}

return Darwin.create {
pipelining = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.wire.kalium.network.api.base.model.ProxyCredentialsDTO
import com.wire.kalium.network.session.CertificatePinning
import com.wire.kalium.network.tools.ServerConfigDTO
import com.wire.kalium.network.tools.isProxyRequired
import com.wire.kalium.util.DelicateKaliumApi
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.okhttp.OkHttp
import okhttp3.CertificatePinner
Expand All @@ -37,67 +38,70 @@ import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

private object OkHttpSingleton {
private val sharedClient = OkHttpClient.Builder().apply {
pingInterval(WEBSOCKET_PING_INTERVAL_MILLIS, TimeUnit.MILLISECONDS)
.connectTimeout(WEBSOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(WEBSOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(WEBSOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
}.build()

fun createNew(block: OkHttpClient.Builder.() -> Unit): OkHttpClient {
return sharedClient.newBuilder().apply(block).build()
}
}

actual fun defaultHttpEngine(
serverConfigDTOApiProxy: ServerConfigDTO.ApiProxy?,
proxyCredentials: ProxyCredentialsDTO?,
ignoreSSLCertificates: Boolean,
certificatePinning: CertificatePinning
): HttpClientEngine = OkHttp.create {
OkHttpSingleton.createNew {
if (certificatePinning.isNotEmpty()) {
val certPinner = CertificatePinner.Builder().apply {
certificatePinning.forEach { (cert, hosts) ->
hosts.forEach { host ->
add(host, cert)
}
}
}.build()
certificatePinner(certPinner)
}

val okHttpClient = OkHttpClient.Builder()
if (ignoreSSLCertificates) ignoreAllSSLErrors()

if (certificatePinning.isNotEmpty()) {
val certPinner = CertificatePinner.Builder().apply {
certificatePinning.forEach { (cert, hosts) ->
hosts.forEach { host ->
add(host, cert)
// OkHttp doesn't support configuring ping intervals dynamically,
// so they must be set when creating the Engine
// See https://youtrack.jetbrains.com/issue/KTOR-4752
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hasn't this comment lost its context? Shouldn't it be where the pingInternval is configured.

if (isProxyRequired(serverConfigDTOApiProxy)) {
if (serverConfigDTOApiProxy?.needsAuthentication == true) {
if (proxyCredentials == null) error("Credentials does not exist")
with(proxyCredentials) {
Authenticator.setDefault(object : Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(username, password?.toCharArray())
}
})
}
}
}.build()
okHttpClient.certificatePinner(certPinner)
}

if (ignoreSSLCertificates) okHttpClient.ignoreAllSSLErrors()

okHttpClient.pingInterval(WEBSOCKET_PING_INTERVAL_MILLIS, TimeUnit.MILLISECONDS)
.connectTimeout(WEBSOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(WEBSOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(WEBSOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
val proxy = Proxy(
Proxy.Type.SOCKS,
InetSocketAddress.createUnresolved(serverConfigDTOApiProxy?.host, serverConfigDTOApiProxy!!.port)
)

// OkHttp doesn't support configuring ping intervals dynamically,
// so they must be set when creating the Engine
// See https://youtrack.jetbrains.com/issue/KTOR-4752
if (isProxyRequired(serverConfigDTOApiProxy)) {
if (serverConfigDTOApiProxy?.needsAuthentication == true) {
if (proxyCredentials == null) error("Credentials does not exist")
with(proxyCredentials) {
Authenticator.setDefault(object : Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(username, password?.toCharArray())
}
})
}
proxy(proxy)
}

val proxy = Proxy(
Proxy.Type.SOCKS,
InetSocketAddress.createUnresolved(serverConfigDTOApiProxy?.host, serverConfigDTOApiProxy!!.port)
)

val clientWithProxy = okHttpClient.proxy(proxy).build()
preconfigured = clientWithProxy
webSocketFactory = KaliumWebSocketFactory(clientWithProxy)

} else {
val client = okHttpClient.build()
preconfigured = client
webSocketFactory = KaliumWebSocketFactory(client)
}.also {
preconfigured = it
webSocketFactory = KaliumWebSocketFactory(it)
}
}

fun OkHttpClient.Builder.ignoreAllSSLErrors(): OkHttpClient.Builder {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function was creating a new builder while it was not used

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch !

val naiveTrustManager = @Suppress("CustomX509TrustManager")
object : X509TrustManager {
private fun OkHttpClient.Builder.ignoreAllSSLErrors() {
val naiveTrustManager = object : X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) = Unit
override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) = Unit
Expand All @@ -110,5 +114,4 @@ fun OkHttpClient.Builder.ignoreAllSSLErrors(): OkHttpClient.Builder {

sslSocketFactory(insecureSocketFactory, naiveTrustManager)
hostnameVerifier { _, _ -> true }
return this
}
Loading