Skip to content

Commit 381fd95

Browse files
author
xj
committed
fix?
1 parent 84f28f7 commit 381fd95

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010

1111
defaultConfig {
1212
applicationId "com.xjs.ehviewer"
13-
minSdkVersion 23
13+
minSdkVersion 16
1414
targetSdkVersion 28
1515
versionCode 107
1616
versionName "1.7.6.09"
@@ -73,7 +73,8 @@ dependencies {
7373
implementation 'androidx.cardview:cardview:1.0.0'
7474

7575
implementation 'com.google.android.material:material:1.0.0'
76-
implementation 'com.google.firebase:firebase-core:16.0.9'
76+
// implementation 'com.google.firebase:firebase-core:16.0.9'
77+
implementation 'com.google.firebase:firebase-analytics:17.4.1'
7778
//noinspection OutdatedLibrary
7879
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
7980
implementation 'com.getkeepsafe.relinker:relinker:1.3.1'

app/src/main/java/com/hippo/ehviewer/EhApplication.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import android.content.ServiceConnection;
2626
import android.content.pm.PackageInfo;
2727
import android.content.pm.PackageManager;
28+
import android.net.SSLCertificateSocketFactory;
2829
import android.os.AsyncTask;
30+
import android.os.Build;
2931
import android.os.Debug;
3032
import android.util.Log;
3133
import androidx.annotation.NonNull;
@@ -48,6 +50,7 @@
4850
import com.hippo.image.Image;
4951
import com.hippo.image.ImageBitmap;
5052
import com.hippo.network.StatusCodeException;
53+
import com.hippo.network.Tls12SocketFactory;
5154
import com.hippo.text.Html;
5255
import com.hippo.unifile.UniFile;
5356
import com.hippo.util.BitmapUtils;
@@ -65,8 +68,12 @@
6568
import java.util.List;
6669
import java.util.concurrent.TimeUnit;
6770

71+
import javax.net.ssl.SSLContext;
72+
6873
import okhttp3.Cache;
74+
import okhttp3.ConnectionSpec;
6975
import okhttp3.OkHttpClient;
76+
import okhttp3.TlsVersion;
7077

7178
public class EhApplication extends RecordingApplication {
7279

@@ -303,14 +310,17 @@ public static EhProxySelector getEhProxySelector(@NonNull Context context) {
303310
public static OkHttpClient getOkHttpClient(@NonNull Context context) {
304311
EhApplication application = ((EhApplication) context.getApplicationContext());
305312
if (application.mOkHttpClient == null) {
306-
application.mOkHttpClient = new OkHttpClient.Builder()
313+
application.mOkHttpClient = enableTls120nPreLollipop(new OkHttpClient.Builder()
314+
.followRedirects(true)
315+
.followSslRedirects(true)
316+
.retryOnConnectionFailure(true)
307317
.connectTimeout(10, TimeUnit.SECONDS)
308318
.readTimeout(10, TimeUnit.SECONDS)
309319
.writeTimeout(10, TimeUnit.SECONDS)
310320
.cookieJar(getEhCookieStore(application))
311321
.dns(new EhDns(application))
312322
.proxySelector(getEhProxySelector(application))
313-
.build();
323+
).build();
314324
}
315325
return application.mOkHttpClient;
316326
}
@@ -471,4 +481,30 @@ public void unbindService(ServiceConnection conn) {
471481
ExceptionUtils.throwIfFatal(t);
472482
}
473483
}
484+
485+
public static OkHttpClient.Builder enableTls120nPreLollipop(OkHttpClient.Builder client){
486+
//
487+
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT <= 22){
488+
try{
489+
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
490+
sslContext.init(null,null,null);
491+
client.sslSocketFactory(new Tls12SocketFactory(sslContext.getSocketFactory()));
492+
493+
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
494+
.tlsVersions(TlsVersion.TLS_1_2)
495+
.build();
496+
497+
List<ConnectionSpec> specs = new ArrayList<>();
498+
specs.add(cs);
499+
specs.add(ConnectionSpec.COMPATIBLE_TLS);
500+
specs.add(ConnectionSpec.CLEARTEXT);
501+
502+
client.connectionSpecs(specs);
503+
}catch (Exception exc){
504+
Log.e("OkHttpTLSCompat","Error while setting TLS 1.2", exc);
505+
}
506+
}
507+
508+
return client;
509+
}
474510
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.hippo.network;
2+
import java.io.IOException;
3+
import java.net.InetAddress;
4+
import java.net.Socket;
5+
import java.net.UnknownHostException;
6+
7+
import javax.net.ssl.SSLSocket;
8+
import javax.net.ssl.SSLSocketFactory;
9+
10+
/**
11+
* Enables TLS v1.2 when creating SSLSockets.
12+
* <p/>
13+
* For some reason, android supports TLS v1.2 from API 16, but enables it by
14+
* default only from API 20.
15+
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html
16+
* @see SSLSocketFactory
17+
*/
18+
public class Tls12SocketFactory extends SSLSocketFactory {
19+
private static final String[] TLS_V12_ONLY = {"TLSv1.2"};
20+
21+
final SSLSocketFactory delegate;
22+
23+
public Tls12SocketFactory(SSLSocketFactory base) {
24+
this.delegate = base;
25+
}
26+
27+
@Override
28+
public String[] getDefaultCipherSuites() {
29+
return delegate.getDefaultCipherSuites();
30+
}
31+
32+
@Override
33+
public String[] getSupportedCipherSuites() {
34+
return delegate.getSupportedCipherSuites();
35+
}
36+
37+
@Override
38+
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
39+
return patch(delegate.createSocket(s, host, port, autoClose));
40+
}
41+
42+
@Override
43+
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
44+
return patch(delegate.createSocket(host, port));
45+
}
46+
47+
@Override
48+
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
49+
return patch(delegate.createSocket(host, port, localHost, localPort));
50+
}
51+
52+
@Override
53+
public Socket createSocket(InetAddress host, int port) throws IOException {
54+
return patch(delegate.createSocket(host, port));
55+
}
56+
57+
@Override
58+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
59+
return patch(delegate.createSocket(address, port, localAddress, localPort));
60+
}
61+
62+
private Socket patch(Socket s) {
63+
if (s instanceof SSLSocket) {
64+
((SSLSocket) s).setEnabledProtocols(TLS_V12_ONLY);
65+
}
66+
return s;
67+
}
68+
}

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ buildscript {
99
}
1010
dependencies {
1111
classpath 'com.android.tools.build:gradle:4.0.0'
12-
classpath 'com.google.gms:google-services:4.3.0'
12+
// classpath 'com.google.gms:google-services:4.3.0'
13+
classpath 'com.google.gms:google-services:4.3.3'
1314
classpath 'io.fabric.tools:gradle:1.28.0'
1415

1516
// NOTE: Do not place your application dependencies here; they belong

0 commit comments

Comments
 (0)