提交 27c1a745 编写于 作者: G guoshuyu

#3174 HostnameVerifier

上级 beefaa09
......@@ -151,7 +151,7 @@
<activity
android:name=".simple.SimplePlayer"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:screenOrientation="portrait"
android:screenOrientation="landscape"
android:theme="@style/Theme.AppCompat.Full" />
<activity
android:name=".simple.SimpleDetailActivityMode1"
......
......@@ -18,6 +18,7 @@ import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.cache.ProxyCacheManager;
import com.shuyu.gsyvideoplayer.model.GSYModel;
import com.shuyu.gsyvideoplayer.player.BasePlayerManager;
import com.shuyu.gsyvideoplayer.player.IPlayerInitSuccessListener;
......@@ -27,11 +28,17 @@ import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.squareup.leakcanary.LeakCanary;
import java.io.File;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import static com.google.android.exoplayer2.util.Util.inferContentType;
/**
Created by shuyu on 2016/11/11.
* Created by shuyu on 2016/11/11.
*/
public class GSYApplication extends MultiDexApplication {
......@@ -71,6 +78,8 @@ public class GSYApplication extends MultiDexApplication {
//IjkPlayerManager.setLogLevel(IjkMediaPlayer.IJK_LOG_SILENT);
GSYVideoType.setRenderType(GSYVideoType.SUFRACE);
ExoSourceManager.setExoMediaSourceInterceptListener(new ExoMediaSourceInterceptListener() {
@Override
public MediaSource getMediaSource(String dataSource, boolean preview, boolean cacheEnable, boolean isLooping, File cacheDir) {
......@@ -102,6 +111,30 @@ public class GSYApplication extends MultiDexApplication {
}
});*/
ProxyCacheManager.instance().setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
ProxyCacheManager.instance().setTrustAllCerts(trustAllCerts);
}
......
......@@ -40,10 +40,10 @@ dependencies {
api viewDependencies.ijkplayer_java
api viewDependencies.ijkplayer_exo
api viewDependencies.transitionseverywhere
//api project(':gsyVideoPlayer-proxy_cache')
api project(':gsyVideoPlayer-proxy_cache')
//api project(':gsyVideoPlayer-base')
api "com.shuyu:gsyVideoPlayer-base:$gsyVideoVersion"
api "com.shuyu:gsyvideoplayer-androidvideocache:$gsyVideoVersion"
//api "com.shuyu:gsyvideoplayer-androidvideocache:$gsyVideoVersion"
}
......
......@@ -16,6 +16,9 @@ import java.io.File;
import java.io.IOException;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.TrustManager;
import tv.danmaku.ijk.media.player.IMediaPlayer;
/**
......@@ -44,6 +47,10 @@ public class ProxyCacheManager implements ICacheManager, CacheListener {
protected ProxyCacheUserAgentHeadersInjector userAgentHeadersInjector = new ProxyCacheUserAgentHeadersInjector();
private HostnameVerifier v;
private TrustManager[] trustAllCerts;
/**
* 单例管理器
*/
......@@ -169,6 +176,8 @@ public class ProxyCacheManager implements ICacheManager, CacheListener {
builder.maxCacheSize(DEFAULT_MAX_SIZE);
}
builder.headerInjector(userAgentHeadersInjector);
builder.hostnameVerifier(v);
builder.trustAllCerts(trustAllCerts);
if (fileNameGenerator != null) {
builder.fileNameGenerator(fileNameGenerator);
}
......@@ -192,6 +201,8 @@ public class ProxyCacheManager implements ICacheManager, CacheListener {
} else {
builder.maxCacheSize(DEFAULT_MAX_SIZE);
}
builder.hostnameVerifier(v);
builder.trustAllCerts(trustAllCerts);
return builder.build();
}
......@@ -245,4 +256,20 @@ public class ProxyCacheManager implements ICacheManager, CacheListener {
public static void clearFileNameGenerator() {
ProxyCacheManager.fileNameGenerator = null;
}
public HostnameVerifier getHostnameVerifier() {
return v;
}
public void setHostnameVerifier(HostnameVerifier v) {
this.v = v;
}
public TrustManager[] getTrustAllCerts() {
return trustAllCerts;
}
public void setTrustAllCerts(TrustManager[] trustAllCerts) {
this.trustAllCerts = trustAllCerts;
}
}
......@@ -7,6 +7,9 @@ import com.danikula.videocache.sourcestorage.SourceInfoStorage;
import java.io.File;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.TrustManager;
/**
* Configuration for proxy cache.
*
......@@ -19,13 +22,17 @@ class Config {
public final DiskUsage diskUsage;
public final SourceInfoStorage sourceInfoStorage;
public final HeaderInjector headerInjector;
public final HostnameVerifier v;
public final TrustManager[] trustAllCerts;
Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) {
Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector, HostnameVerifier v, TrustManager[] trustAllCerts) {
this.cacheRoot = cacheRoot;
this.fileNameGenerator = fileNameGenerator;
this.diskUsage = diskUsage;
this.sourceInfoStorage = sourceInfoStorage;
this.headerInjector = headerInjector;
this.v = v;
this.trustAllCerts = trustAllCerts;
}
File generateCacheFile(String url) {
......
......@@ -26,6 +26,9 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.TrustManager;
import static com.danikula.videocache.Preconditions.checkAllNotNull;
import static com.danikula.videocache.Preconditions.checkNotNull;
......@@ -276,7 +279,7 @@ public class HttpProxyCacheServer {
// There is no way to determine that client closed connection http://stackoverflow.com/a/10241044/999458
// So just to prevent log flooding don't log stacktrace
} catch (IOException e) {
// onError(new ProxyCacheException("Error closing socket input stream", e));
// onError(new ProxyCacheException("Error closing socket input stream", e));
}
}
......@@ -345,6 +348,8 @@ public class HttpProxyCacheServer {
private DiskUsage diskUsage;
private SourceInfoStorage sourceInfoStorage;
private HeaderInjector headerInjector;
private HostnameVerifier v;
private TrustManager[] trustAllCerts;
public Builder(Context context) {
this.sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(context);
......@@ -433,6 +438,18 @@ public class HttpProxyCacheServer {
return this;
}
public Builder hostnameVerifier(HostnameVerifier v) {
this.v = checkNotNull(v);
return this;
}
public Builder trustAllCerts(TrustManager[] trustAllCerts) {
this.trustAllCerts = checkNotNull(trustAllCerts);
return this;
}
/**
* Builds new instance of {@link HttpProxyCacheServer}.
*
......@@ -444,7 +461,7 @@ public class HttpProxyCacheServer {
}
private Config buildConfig() {
return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector);
return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector, v, trustAllCerts);
}
}
......
......@@ -79,7 +79,7 @@ final class HttpProxyCacheServerClients {
}
private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector);
HttpUrlSource source = new HttpUrlSource(url, config.sourceInfoStorage, config.headerInjector, config.v, config.trustAllCerts);
FileCache cache = new FileCache(config.generateCacheFile(url), config.diskUsage);
HttpProxyCache httpProxyCache = new HttpProxyCache(source, cache);
httpProxyCache.registerCacheListener(uiCacheListener);
......
......@@ -45,21 +45,25 @@ public class HttpUrlSource implements Source {
private static final int MAX_REDIRECTS = 5;
private final SourceInfoStorage sourceInfoStorage;
private final HeaderInjector headerInjector;
private final HostnameVerifier v;
private final TrustManager[] trustAllCerts;
private SourceInfo sourceInfo;
private HttpURLConnection connection;
private InputStream inputStream;
public HttpUrlSource(String url) {
this(url, SourceInfoStorageFactory.newEmptySourceInfoStorage());
public HttpUrlSource(String url, HostnameVerifier v, TrustManager[] trustAllCerts) {
this(url, SourceInfoStorageFactory.newEmptySourceInfoStorage(), v, trustAllCerts);
}
public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage) {
this(url, sourceInfoStorage, new EmptyHeadersInjector());
public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HostnameVerifier v, TrustManager[] trustAllCerts) {
this(url, sourceInfoStorage, new EmptyHeadersInjector(), v, trustAllCerts);
}
public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) {
public HttpUrlSource(String url, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector, HostnameVerifier v, TrustManager[] trustAllCerts) {
this.sourceInfoStorage = checkNotNull(sourceInfoStorage);
this.headerInjector = checkNotNull(headerInjector);
this.v = v;
this.trustAllCerts = trustAllCerts;
SourceInfo sourceInfo = sourceInfoStorage.get(url);
this.sourceInfo = sourceInfo != null ? sourceInfo :
new SourceInfo(url, Integer.MIN_VALUE, ProxyCacheUtils.getSupposablyMime(url));
......@@ -69,6 +73,8 @@ public class HttpUrlSource implements Source {
this.sourceInfo = source.sourceInfo;
this.sourceInfoStorage = source.sourceInfoStorage;
this.headerInjector = source.headerInjector;
this.trustAllCerts = source.trustAllCerts;
this.v = source.v;
}
@Override
......@@ -163,32 +169,9 @@ public class HttpUrlSource implements Source {
int redirectCount = 0;
String url = this.sourceInfo.url;
do {
if (url.startsWith("https")) {
/**去除证书限制**/
if (url.startsWith("https") && v != null && trustAllCerts != null) {
connection = (HttpURLConnection) new URL(url).openConnection();
((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
((HttpsURLConnection) connection).setHostnameVerifier(v);
// Install the all-trusting trust manager
final SSLContext sslContext;
try {
......@@ -197,18 +180,12 @@ public class HttpUrlSource implements Source {
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
((HttpsURLConnection) connection).setHostnameVerifier(v);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
/**去除证书限制**/
} else {
connection = (HttpURLConnection) new URL(url).openConnection();
}
......
......@@ -93,7 +93,7 @@ class Pinger {
private boolean pingServer() throws ProxyCacheException {
String pingUrl = getPingUrl();
HttpUrlSource source = new HttpUrlSource(pingUrl);
HttpUrlSource source = new HttpUrlSource(pingUrl, null, null);
try {
byte[] expectedResponse = PING_RESPONSE.getBytes();
source.open(0);
......
......@@ -35,7 +35,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//
// api project(':gsyVideoPlayer-java')
api project(':gsyVideoPlayer-java')
// api project(':gsyVideoPlayer-exo_player2')
// api project(':gsyVideoPlayer-armv5')
// api project(':gsyVideoPlayer-armv7a')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册