Created
December 27, 2018 08:21
-
-
Save moko256/a836d008e6aafc7b8e602610395f83c0 to your computer and use it in GitHub Desktop.
Patch for twitlatte : [TEST ONLY] Trust all NON-SECURE certificate
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
Index: app/src/main/java/com/github/moko256/twitlatte/net/OkHttpHolder.kt | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- app/src/main/java/com/github/moko256/twitlatte/net/OkHttpHolder.kt (date 1545880602000) | |
+++ app/src/main/java/com/github/moko256/twitlatte/net/OkHttpHolder.kt (date 1545880740787) | |
@@ -30,5 +30,6 @@ | |
.followSslRedirects(false) | |
.connectTimeout(20000, TimeUnit.MILLISECONDS) | |
.readTimeout(120000, TimeUnit.MILLISECONDS) | |
+ .hostnameVerifier { _, _ -> true } | |
.replaceSocketFactoryIfNeeded() | |
.build() | |
\ No newline at end of file | |
Index: app/src/main/java/com/github/moko256/twitlatte/net/OkHttpReplacer.kt | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
--- app/src/main/java/com/github/moko256/twitlatte/net/OkHttpReplacer.kt (date 1545880602000) | |
+++ app/src/main/java/com/github/moko256/twitlatte/net/OkHttpReplacer.kt (date 1545898731035) | |
@@ -16,11 +16,12 @@ | |
package com.github.moko256.twitlatte.net | |
-import android.os.Build | |
+import android.annotation.SuppressLint | |
import okhttp3.OkHttpClient | |
import java.security.KeyStore | |
import java.security.KeyStoreException | |
import java.security.NoSuchAlgorithmException | |
+import java.security.cert.X509Certificate | |
import javax.net.ssl.TrustManagerFactory | |
import javax.net.ssl.X509TrustManager | |
@@ -31,25 +32,32 @@ | |
*/ | |
fun OkHttpClient.Builder.replaceSocketFactoryIfNeeded(): OkHttpClient.Builder = apply { | |
- if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { | |
- try { | |
+ try { | |
- val trustManager = systemDefaultTrustManager() | |
+ val trustManager = object: X509TrustManager { | |
+ @SuppressLint("TrustAllX509TrustManager") | |
+ override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {} | |
+ | |
+ @SuppressLint("TrustAllX509TrustManager") | |
+ override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {} | |
+ | |
+ override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray() | |
+ | |
+ } | |
- sslSocketFactory( | |
- SSLSocketFactoryCompat(trustManager), | |
- trustManager | |
- ) | |
+ sslSocketFactory( | |
+ SSLSocketFactoryCompat(trustManager), | |
+ trustManager | |
+ ) | |
- } catch (e: NoSuchFieldException) { | |
- e.printStackTrace() | |
- } catch (e: IllegalAccessException) { | |
- e.printStackTrace() | |
- } catch (e: NoSuchAlgorithmException) { | |
- e.printStackTrace() | |
- } catch (e: KeyStoreException) { | |
- e.printStackTrace() | |
- } | |
+ } catch (e: NoSuchFieldException) { | |
+ e.printStackTrace() | |
+ } catch (e: IllegalAccessException) { | |
+ e.printStackTrace() | |
+ } catch (e: NoSuchAlgorithmException) { | |
+ e.printStackTrace() | |
+ } catch (e: KeyStoreException) { | |
+ e.printStackTrace() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment