-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
249 additions
and
153 deletions.
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
103 changes: 103 additions & 0 deletions
103
app/src/main/java/com/seafile/seadroid2/framework/http/UnsafeOkHttpClient.java
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
package com.seafile.seadroid2.framework.http; | ||
|
||
import com.blankj.utilcode.util.CollectionUtils; | ||
import com.seafile.seadroid2.account.Account; | ||
|
||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
|
||
import okhttp3.ConnectionSpec; | ||
import okhttp3.Interceptor; | ||
import okhttp3.OkHttpClient; | ||
|
||
public class UnsafeOkHttpClient extends BaseOkHttpClient { | ||
public UnsafeOkHttpClient() { | ||
super(null); | ||
} | ||
|
||
public UnsafeOkHttpClient(Account account) { | ||
super(account); | ||
} | ||
|
||
private final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { | ||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
} | ||
|
||
@Override | ||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { | ||
} | ||
|
||
@Override | ||
public X509Certificate[] getAcceptedIssuers() { | ||
return new X509Certificate[]{}; | ||
} | ||
} | ||
}; | ||
|
||
public OkHttpClient.Builder getBuilder() { | ||
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
|
||
|
||
try { | ||
// Install the all-trusting trust manager | ||
final SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
// Create an ssl socket factory with our all-trusting manager | ||
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | ||
|
||
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
builder.connectionSpecs(Arrays.asList( | ||
ConnectionSpec.MODERN_TLS, | ||
ConnectionSpec.COMPATIBLE_TLS, | ||
ConnectionSpec.CLEARTEXT)); | ||
builder.cache(cache); | ||
builder.hostnameVerifier(new HostnameVerifier() { | ||
@Override | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
}); | ||
//cache control | ||
builder.interceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR); | ||
builder.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR); | ||
|
||
//add interceptors | ||
List<Interceptor> interceptors = getInterceptors(); | ||
if (!CollectionUtils.isEmpty(interceptors)) { | ||
for (Interceptor i : interceptors) { | ||
builder.interceptors().add(i); | ||
} | ||
} | ||
|
||
//timeout | ||
builder.writeTimeout(DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS); | ||
builder.readTimeout(DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS); | ||
builder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.MILLISECONDS); | ||
|
||
return builder; | ||
} | ||
|
||
@Override | ||
public OkHttpClient getOkClient() { | ||
OkHttpClient.Builder builder = getBuilder(); | ||
return builder.build(); | ||
} | ||
|
||
|
||
} |
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
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
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
Oops, something went wrong.