From 674d272eaad79bec58f8e14dedf8506fa5d67101 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 11 Oct 2019 01:46:15 -0400 Subject: [PATCH] Support pre-5.0 without GMS Fix #1912 --- app/build.gradle | 2 +- app/src/main/java/com/topjohnwu/magisk/App.kt | 3 --- .../topjohnwu/magisk/di/NetworkingModule.kt | 22 +++++++++++-------- shared/build.gradle | 1 + shared/proguard-rules.pro | 4 ++++ shared/src/main/AndroidManifest.xml | 1 + .../com/topjohnwu/magisk/net/Networking.java | 10 ++++++--- .../magisk/net/NoSSLv3SocketFactory.java | 20 ++++++++--------- 8 files changed, 37 insertions(+), 26 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b20bfe555..626847d6d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,7 +94,7 @@ dependencies { implementation "com.squareup.retrofit2:converter-scalars:${vRetrofit}" implementation "com.squareup.retrofit2:adapter-rxjava2:${vRetrofit}" - def vOkHttp = '3.12.2' + def vOkHttp = '3.12.6' implementation "com.squareup.okhttp3:okhttp:${vOkHttp}" implementation "com.squareup.okhttp3:logging-interceptor:${vOkHttp}" diff --git a/app/src/main/java/com/topjohnwu/magisk/App.kt b/app/src/main/java/com/topjohnwu/magisk/App.kt index 1224d400f..b742d5175 100644 --- a/app/src/main/java/com/topjohnwu/magisk/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/App.kt @@ -13,7 +13,6 @@ import com.topjohnwu.magisk.data.database.RepoDatabase_Impl import com.topjohnwu.magisk.di.ActivityTracker import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.extensions.get -import com.topjohnwu.magisk.net.Networking import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.RootUtils import com.topjohnwu.superuser.Shell @@ -50,8 +49,6 @@ open class App : Application() { } registerActivityLifecycleCallbacks(get()) - - Networking.init(base) LocaleManager.setLocale(this) } diff --git a/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt index 3824a0c97..b814eb375 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/NetworkingModule.kt @@ -1,11 +1,14 @@ package com.topjohnwu.magisk.di +import android.content.Context import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.Const import com.topjohnwu.magisk.data.network.GithubApiServices import com.topjohnwu.magisk.data.network.GithubRawServices +import com.topjohnwu.magisk.net.Networking +import com.topjohnwu.magisk.net.NoSSLv3SocketFactory import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import org.koin.dsl.module @@ -16,14 +19,14 @@ import retrofit2.converter.scalars.ScalarsConverterFactory import se.ansman.kotshi.KotshiJsonAdapterFactory val networkingModule = module { - single { createOkHttpClient() } - single { createMoshiConverterFactory() } - single { createRetrofit(get(), get()) } + single { createOkHttpClient(get()) } + single { createRetrofit(get()) } single { createApiService(get(), Const.Url.GITHUB_RAW_URL) } single { createApiService(get(), Const.Url.GITHUB_API_URL) } } -fun createOkHttpClient(): OkHttpClient { +@Suppress("DEPRECATION") +fun createOkHttpClient(context: Context): OkHttpClient { val builder = OkHttpClient.Builder() if (BuildConfig.DEBUG) { @@ -33,6 +36,10 @@ fun createOkHttpClient(): OkHttpClient { builder.addInterceptor(httpLoggingInterceptor) } + if (!Networking.init(context)) { + builder.sslSocketFactory(NoSSLv3SocketFactory()) + } + return builder.build() } @@ -43,13 +50,10 @@ fun createMoshiConverterFactory(): MoshiConverterFactory { return MoshiConverterFactory.create(moshi) } -fun createRetrofit( - okHttpClient: OkHttpClient, - converterFactory: MoshiConverterFactory -): Retrofit.Builder { +fun createRetrofit(okHttpClient: OkHttpClient): Retrofit.Builder { return Retrofit.Builder() .addConverterFactory(ScalarsConverterFactory.create()) - .addConverterFactory(converterFactory) + .addConverterFactory(createMoshiConverterFactory()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(okHttpClient) } diff --git a/shared/build.gradle b/shared/build.gradle index bb94b657e..7251be266 100644 --- a/shared/build.gradle +++ b/shared/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'com.android.library' android { defaultConfig { vectorDrawables.useSupportLibrary = true + consumerProguardFiles 'proguard-rules.pro' } } diff --git a/shared/proguard-rules.pro b/shared/proguard-rules.pro index f1b424510..e0a7aa840 100644 --- a/shared/proguard-rules.pro +++ b/shared/proguard-rules.pro @@ -19,3 +19,7 @@ # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile + +-keepclassmembers class * implements javax.net.ssl.SSLSocketFactory { + ** delegate; +} diff --git a/shared/src/main/AndroidManifest.xml b/shared/src/main/AndroidManifest.xml index 9f68eba7a..82038fd93 100644 --- a/shared/src/main/AndroidManifest.xml +++ b/shared/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + diff --git a/shared/src/main/java/com/topjohnwu/magisk/net/Networking.java b/shared/src/main/java/com/topjohnwu/magisk/net/Networking.java index 10cbee3dc..c23961340 100644 --- a/shared/src/main/java/com/topjohnwu/magisk/net/Networking.java +++ b/shared/src/main/java/com/topjohnwu/magisk/net/Networking.java @@ -35,7 +35,7 @@ public class Networking { return request(url, "GET"); } - public static void init(Context context) { + public static boolean init(Context context) { try { // Try installing new SSL provider from Google Play Service Context gms = context.createPackageContext("com.google.android.gms", @@ -45,10 +45,14 @@ public class Networking { .getMethod("insertProvider", Context.class) .invoke(null, gms); } catch (Exception e) { - // Failed to update SSL provider, use NoSSLv3SocketFactory on SDK < 21 - if (Build.VERSION.SDK_INT < 21) + if (Build.VERSION.SDK_INT < 21) { + // Failed to update SSL provider, use NoSSLv3SocketFactory on SDK < 21 + // and return false to notify potential issues HttpsURLConnection.setDefaultSSLSocketFactory(new NoSSLv3SocketFactory()); + return false; + } } + return true; } public static boolean checkNetworkStatus(Context context) { diff --git a/shared/src/main/java/com/topjohnwu/magisk/net/NoSSLv3SocketFactory.java b/shared/src/main/java/com/topjohnwu/magisk/net/NoSSLv3SocketFactory.java index b7a5774f2..c55bf4761 100644 --- a/shared/src/main/java/com/topjohnwu/magisk/net/NoSSLv3SocketFactory.java +++ b/shared/src/main/java/com/topjohnwu/magisk/net/NoSSLv3SocketFactory.java @@ -11,18 +11,18 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; -class NoSSLv3SocketFactory extends SSLSocketFactory { +public class NoSSLv3SocketFactory extends SSLSocketFactory { - private final static SSLSocketFactory base = HttpsURLConnection.getDefaultSSLSocketFactory(); + private final static SSLSocketFactory delegate = HttpsURLConnection.getDefaultSSLSocketFactory(); @Override public String[] getDefaultCipherSuites() { - return base.getDefaultCipherSuites(); + return delegate.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { - return base.getSupportedCipherSuites(); + return delegate.getSupportedCipherSuites(); } private Socket createSafeSocket(Socket socket) { @@ -40,31 +40,31 @@ class NoSSLv3SocketFactory extends SSLSocketFactory { @Override public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { - return createSafeSocket(base.createSocket(s, host, port, autoClose)); + return createSafeSocket(delegate.createSocket(s, host, port, autoClose)); } @Override public Socket createSocket() throws IOException { - return createSafeSocket(base.createSocket()); + return createSafeSocket(delegate.createSocket()); } @Override public Socket createSocket(String host, int port) throws IOException { - return createSafeSocket(base.createSocket(host, port)); + return createSafeSocket(delegate.createSocket(host, port)); } @Override public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { - return createSafeSocket(base.createSocket(host, port, localHost, localPort)); + return createSafeSocket(delegate.createSocket(host, port, localHost, localPort)); } @Override public Socket createSocket(InetAddress host, int port) throws IOException { - return createSafeSocket(base.createSocket(host, port)); + return createSafeSocket(delegate.createSocket(host, port)); } @Override public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { - return createSafeSocket(base.createSocket(address, port, localAddress, localPort)); + return createSafeSocket(delegate.createSocket(address, port, localAddress, localPort)); } }