-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e494b26
refactor: reuse the same OkHttp client instance across sessions
MohamadJaara 8dd4fd7
bump okHttp to 4.11.0
MohamadJaara 0a90e55
detekt
MohamadJaara 32a7c77
Merge branch 'develop' into refactor/reuse-okhttp-instance
MohamadJaara 675f6ac
move comment to its correct place
MohamadJaara 0e1bcb2
Merge branch 'develop' into refactor/reuse-okhttp-instance
MohamadJaara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,67 +37,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 | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function was creating a new builder while it was not used There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -110,5 +113,4 @@ fun OkHttpClient.Builder.ignoreAllSSLErrors(): OkHttpClient.Builder { | |
|
||
sslSocketFactory(insecureSocketFactory, naiveTrustManager) | ||
hostnameVerifier { _, _ -> true } | ||
return this | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.