Support pre-5.0 without GMS

Fix #1912
This commit is contained in:
topjohnwu 2019-10-11 01:46:15 -04:00
parent c3e00c279d
commit 674d272eaa
8 changed files with 37 additions and 26 deletions

View File

@ -94,7 +94,7 @@ dependencies {
implementation "com.squareup.retrofit2:converter-scalars:${vRetrofit}" implementation "com.squareup.retrofit2:converter-scalars:${vRetrofit}"
implementation "com.squareup.retrofit2:adapter-rxjava2:${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:okhttp:${vOkHttp}"
implementation "com.squareup.okhttp3:logging-interceptor:${vOkHttp}" implementation "com.squareup.okhttp3:logging-interceptor:${vOkHttp}"

View File

@ -13,7 +13,6 @@ import com.topjohnwu.magisk.data.database.RepoDatabase_Impl
import com.topjohnwu.magisk.di.ActivityTracker import com.topjohnwu.magisk.di.ActivityTracker
import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.di.koinModules
import com.topjohnwu.magisk.extensions.get import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.net.Networking
import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.LocaleManager
import com.topjohnwu.magisk.utils.RootUtils import com.topjohnwu.magisk.utils.RootUtils
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
@ -50,8 +49,6 @@ open class App : Application() {
} }
registerActivityLifecycleCallbacks(get<ActivityTracker>()) registerActivityLifecycleCallbacks(get<ActivityTracker>())
Networking.init(base)
LocaleManager.setLocale(this) LocaleManager.setLocale(this)
} }

View File

@ -1,11 +1,14 @@
package com.topjohnwu.magisk.di package com.topjohnwu.magisk.di
import android.content.Context
import com.squareup.moshi.JsonAdapter import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.Const import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.data.network.GithubApiServices import com.topjohnwu.magisk.data.network.GithubApiServices
import com.topjohnwu.magisk.data.network.GithubRawServices import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.net.Networking
import com.topjohnwu.magisk.net.NoSSLv3SocketFactory
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import org.koin.dsl.module import org.koin.dsl.module
@ -16,14 +19,14 @@ import retrofit2.converter.scalars.ScalarsConverterFactory
import se.ansman.kotshi.KotshiJsonAdapterFactory import se.ansman.kotshi.KotshiJsonAdapterFactory
val networkingModule = module { val networkingModule = module {
single { createOkHttpClient() } single { createOkHttpClient(get()) }
single { createMoshiConverterFactory() } single { createRetrofit(get()) }
single { createRetrofit(get(), get()) }
single { createApiService<GithubRawServices>(get(), Const.Url.GITHUB_RAW_URL) } single { createApiService<GithubRawServices>(get(), Const.Url.GITHUB_RAW_URL) }
single { createApiService<GithubApiServices>(get(), Const.Url.GITHUB_API_URL) } single { createApiService<GithubApiServices>(get(), Const.Url.GITHUB_API_URL) }
} }
fun createOkHttpClient(): OkHttpClient { @Suppress("DEPRECATION")
fun createOkHttpClient(context: Context): OkHttpClient {
val builder = OkHttpClient.Builder() val builder = OkHttpClient.Builder()
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
@ -33,6 +36,10 @@ fun createOkHttpClient(): OkHttpClient {
builder.addInterceptor(httpLoggingInterceptor) builder.addInterceptor(httpLoggingInterceptor)
} }
if (!Networking.init(context)) {
builder.sslSocketFactory(NoSSLv3SocketFactory())
}
return builder.build() return builder.build()
} }
@ -43,13 +50,10 @@ fun createMoshiConverterFactory(): MoshiConverterFactory {
return MoshiConverterFactory.create(moshi) return MoshiConverterFactory.create(moshi)
} }
fun createRetrofit( fun createRetrofit(okHttpClient: OkHttpClient): Retrofit.Builder {
okHttpClient: OkHttpClient,
converterFactory: MoshiConverterFactory
): Retrofit.Builder {
return Retrofit.Builder() return Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(converterFactory) .addConverterFactory(createMoshiConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient) .client(okHttpClient)
} }

View File

@ -3,6 +3,7 @@ apply plugin: 'com.android.library'
android { android {
defaultConfig { defaultConfig {
vectorDrawables.useSupportLibrary = true vectorDrawables.useSupportLibrary = true
consumerProguardFiles 'proguard-rules.pro'
} }
} }

View File

@ -19,3 +19,7 @@
# If you keep the line number information, uncomment this to # If you keep the line number information, uncomment this to
# hide the original source file name. # hide the original source file name.
#-renamesourcefileattribute SourceFile #-renamesourcefileattribute SourceFile
-keepclassmembers class * implements javax.net.ssl.SSLSocketFactory {
** delegate;
}

View File

@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.topjohnwu.shared"> package="com.topjohnwu.shared">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

View File

@ -35,7 +35,7 @@ public class Networking {
return request(url, "GET"); return request(url, "GET");
} }
public static void init(Context context) { public static boolean init(Context context) {
try { try {
// Try installing new SSL provider from Google Play Service // Try installing new SSL provider from Google Play Service
Context gms = context.createPackageContext("com.google.android.gms", Context gms = context.createPackageContext("com.google.android.gms",
@ -45,10 +45,14 @@ public class Networking {
.getMethod("insertProvider", Context.class) .getMethod("insertProvider", Context.class)
.invoke(null, gms); .invoke(null, gms);
} catch (Exception e) { } 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()); HttpsURLConnection.setDefaultSSLSocketFactory(new NoSSLv3SocketFactory());
return false;
}
} }
return true;
} }
public static boolean checkNetworkStatus(Context context) { public static boolean checkNetworkStatus(Context context) {

View File

@ -11,18 +11,18 @@ import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory; 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 @Override
public String[] getDefaultCipherSuites() { public String[] getDefaultCipherSuites() {
return base.getDefaultCipherSuites(); return delegate.getDefaultCipherSuites();
} }
@Override @Override
public String[] getSupportedCipherSuites() { public String[] getSupportedCipherSuites() {
return base.getSupportedCipherSuites(); return delegate.getSupportedCipherSuites();
} }
private Socket createSafeSocket(Socket socket) { private Socket createSafeSocket(Socket socket) {
@ -40,31 +40,31 @@ class NoSSLv3SocketFactory extends SSLSocketFactory {
@Override @Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException { 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 @Override
public Socket createSocket() throws IOException { public Socket createSocket() throws IOException {
return createSafeSocket(base.createSocket()); return createSafeSocket(delegate.createSocket());
} }
@Override @Override
public Socket createSocket(String host, int port) throws IOException { public Socket createSocket(String host, int port) throws IOException {
return createSafeSocket(base.createSocket(host, port)); return createSafeSocket(delegate.createSocket(host, port));
} }
@Override @Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { 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 @Override
public Socket createSocket(InetAddress host, int port) throws IOException { public Socket createSocket(InetAddress host, int port) throws IOException {
return createSafeSocket(base.createSocket(host, port)); return createSafeSocket(delegate.createSocket(host, port));
} }
@Override @Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { 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));
} }
} }