Skip to content

Commit

Permalink
增信任所有https url的接口功能,并进一步优化!
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyongkui committed Jul 18, 2017
1 parent 057c67d commit 92be713
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 32 deletions.
72 changes: 60 additions & 12 deletions novate/src/main/java/com/tamic/novate/Novate.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public final class Novate {
private Map<Object, Observable<ResponseBody>> downMaps = new HashMap<Object, Observable<ResponseBody>>() {
};
private Observable.Transformer exceptTransformer = null;

public static final String KEY_CACHE = "Novate_Http_cache";
private static final int DEFAULT_TIMEOUT = 15;
private static final int DEFAULT_MAXIDLE_CONNECTIONS = 5;
private static final long DEFAULT_KEEP_ALIVEDURATION = 8;
Expand Down Expand Up @@ -152,7 +154,6 @@ public final class Novate {
* create ApiService
*/
public <T> T create(final Class<T> service) {

return retrofit.create(service);
}

Expand Down Expand Up @@ -181,16 +182,26 @@ public <T> T call(Observable<T> observable, ResponseCallback callback) {
.subscribe(new RxSubscriber<T, ResponseBody>(observable.getClass().getSimpleName(), callback));
}


/*public <R> Observable<R> compose() {
return schedulersIo(okhttpBuilder);
}
*/

/**
* Thread IO
* @param observable
*/
public <T> Observable<T> schedulersIo(Observable<T> observable) {
return observable.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(Schedulers.io());
}

/**
* Thread mainThread
* @param observable
*/
public <T> Observable<T> schedulersMain(Observable<T> observable) {
return observable.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
Expand All @@ -211,6 +222,13 @@ public <T> T execute(NovateRequest request, BaseSubscriber<T> subscriber) {
return call(request, subscriber);
}

/**
* call NovateRequest
* @param request
* @param subscriber
* @param <T>
* @return
*/
private <T> T call(NovateRequest request, BaseSubscriber<T> subscriber) {
return (T) createRx(request)
.compose(schedulersTransformer)
Expand Down Expand Up @@ -1574,7 +1592,7 @@ public static final class Builder {
private int writeTimeout = DEFAULT_TIMEOUT;
private int readTimeout = DEFAULT_TIMEOUT;
private int default_maxidle_connections = DEFAULT_MAXIDLE_CONNECTIONS;
private long default_keep_aliveduration = DEFAULT_MAXIDLE_CONNECTIONS;
private long default_keep_aliveduration = DEFAULT_KEEP_ALIVEDURATION;
private long cacheMaxSize = DEFAULT_CACHEMAXSIZE;
private int cacheTimeout = DEFAULT_MAX_STALE;
private okhttp3.Call.Factory callFactory;
Expand All @@ -1583,6 +1601,7 @@ public static final class Builder {
private Object tag;
private Boolean isCookie = false;
private Boolean isCache = true;
private Boolean isSkip = false;
private List<InputStream> certificateList;
private HostnameVerifier hostnameVerifier;
private CertificatePinner certificatePinner;
Expand Down Expand Up @@ -1887,18 +1906,35 @@ public Builder cookieManager(NovateCookieManager cookie) {
}

/**
*
*skipSSLSocketFactory
*/
public Builder skipSSLSocketFactory(boolean isSkip) {
this.isSkip = isSkip;
return this;
}

/**
* addSSLSocketFactory
*/
public Builder addSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
if (sslSocketFactory == null) throw new NullPointerException("sslSocketFactory == null");
this.sslSocketFactory = sslSocketFactory;
return this;
}

/**
* HostnameVerifier
* @param hostnameVerifier
* @return Builder
*/
public Builder addHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}

/**
* addCertificatePinner
*/
public Builder addCertificatePinner(CertificatePinner certificatePinner) {
this.certificatePinner = certificatePinner;
return this;
Expand All @@ -1922,6 +1958,7 @@ public Builder addSSL(String[] hosts, int[] certificates) {
}

public Builder addNetworkInterceptor(Interceptor interceptor) {
if (interceptor == null) throw new NullPointerException("interceptor == null");
okhttpBuilder.addNetworkInterceptor(interceptor);
return this;
}
Expand Down Expand Up @@ -2023,17 +2060,23 @@ public Novate build() {
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
}

if (sslSocketFactory != null) {
if (isSkip) {
okhttpBuilder.sslSocketFactory(NovateHttpsFactroy.getSSLSocketFactory(),
NovateHttpsFactroy.creatX509TrustManager());

okhttpBuilder.hostnameVerifier(NovateHttpsFactroy.creatSkipHostnameVerifier());
}

if (!isSkip && sslSocketFactory != null) {
okhttpBuilder.sslSocketFactory(sslSocketFactory);
}

if (hostnameVerifier != null) {
okhttpBuilder.hostnameVerifier(hostnameVerifier);
}


if (httpCacheDirectory == null) {
httpCacheDirectory = new File(mContext.getCacheDir(), "Novate_Http_cache");
httpCacheDirectory = new File(mContext.getCacheDir(), KEY_CACHE);
}

if (isCache) {
Expand Down Expand Up @@ -2077,7 +2120,7 @@ public Novate build() {
* <p>If unset, a new connection pool will be used.
*/
if (connectionPool == null) {
connectionPool = new ConnectionPool(default_maxidle_connections, default_maxidle_connections, TimeUnit.SECONDS);
connectionPool = new ConnectionPool(default_maxidle_connections, default_keep_aliveduration, TimeUnit.SECONDS);
}
okhttpBuilder.connectionPool(connectionPool);

Expand All @@ -2102,6 +2145,11 @@ public Novate build() {
}

if (isCookie) {
/**
* Returns a modifiable list of interceptors that observe a single network request and response.
* These interceptors must call {@link Interceptor.Chain#proceed} exactly once: it is an error
* for a network interceptor to short-circuit or repeat a network request.
*/
okhttpBuilder.addInterceptor(new ReceivedCookiesInterceptor(context));
okhttpBuilder.addInterceptor(new AddCookiesInterceptor(context, ""));
}
Expand Down Expand Up @@ -2187,16 +2235,16 @@ public Observable<?> call(Throwable throwable) {
@Deprecated
public interface ResponseCallBack<T> {

public void onStart();
void onStart();

public void onCompleted();
void onCompleted();

public abstract void onError(Throwable e);
void onError(Throwable e);

@Deprecated
public abstract void onSuccee(NovateResponse<T> response);
void onSuccee(NovateResponse<T> response);

public void onsuccess(int code, String msg, T response, String originalResponse);
void onsuccess(int code, String msg, T response, String originalResponse);

}
}
Expand Down
76 changes: 56 additions & 20 deletions novate/src/main/java/com/tamic/novate/NovateHttpsFactroy.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -98,7 +99,7 @@ protected static SSLSocketFactory getSSLSocketFactory(Context context, int[] cer
* set HostnameVerifier
* {@link HostnameVerifier}
*/
protected static HostnameVerifier getHostnameVerifier(final String[] hostUrls) {
public static HostnameVerifier getHostnameVerifier(final String[] hostUrls) {

HostnameVerifier TRUSTED_VERIFIER = new HostnameVerifier() {

Expand All @@ -112,7 +113,6 @@ public boolean verify(String hostname, SSLSession session) {
return ret;
}
};

return TRUSTED_VERIFIER;
}

Expand All @@ -138,6 +138,16 @@ protected static String getPins(Context context, int certificate) {

}

public static SSLSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, creatTrustManager(), new SecureRandom());
return sslContext.getSocketFactory();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* get SSLSocketFactory
*
Expand Down Expand Up @@ -181,6 +191,7 @@ public static SSLSocketFactory creatSSLSocketFactory(Context context, String nam

/**
* get SkipHostnameVerifier
*
* @return
*/
public static HostnameVerifier creatSkipHostnameVerifier() {
Expand All @@ -195,24 +206,49 @@ public boolean verify(String s, SSLSession sslSession) {


/**
* TrustManager
* X509TrustManager
*/
private static TrustManager[] creatTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
return trustAllCerts;
public static X509TrustManager creatX509TrustManager() {

return 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[0];
}
};

}

/**
* TrustManager
*/
public static TrustManager[] creatTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
return trustAllCerts;
}
}

0 comments on commit 92be713

Please sign in to comment.