我想检测是否使用Fiddler或Fiddler等代理监控网络流量。如果是这样,我不希望我的应用程序继续进行。 为实现这一目标,我正在做以下事情:
创建密钥库:
private KeyStore getKeyStore() {
KeyStore keyStore = null;
try {
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = getClass().getResourceAsStream(
"/assets/mycert");
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
System.out.println("ca="
+ ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return keyStore;
}
创建一个信任KeyStore中CA的信任管理器
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
创建使用我们的TrustManager的SSLContext
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
将SSLSocketFactory设置为out connection
connection.setSSLSocketFactory(context.getSocketFactory());
这总是使连接成功。我不知道这是否是实现我想要的正确方法。
或者还有其他正确的方法吗?
提前致谢。