package com.flightfeather.uav.common.net
|
|
import org.apache.commons.httpclient.ConnectTimeoutException
|
import org.apache.commons.httpclient.HttpClientError
|
import org.apache.commons.httpclient.params.HttpConnectionParams
|
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory
|
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory
|
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory
|
import java.io.IOException
|
import java.net.InetAddress
|
import java.net.Socket
|
import java.net.UnknownHostException
|
import java.security.cert.CertificateException
|
import java.security.cert.X509Certificate
|
import javax.net.ssl.*
|
|
|
/**
|
* 通过java 进行访问有时候会有证书进行拦截
|
*/
|
object SkipCertificateValidation {
|
/**
|
* 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
|
* @throws Exception
|
*/
|
@Throws(Exception::class)
|
fun ignoreSsl() {
|
val hv = HostnameVerifier { urlHostName, session ->
|
println("Warning: URL Host: " + urlHostName + " vs. " + session.peerHost)
|
true
|
}
|
trustAllHttpsCertificates()
|
HttpsURLConnection.setDefaultHostnameVerifier(hv)
|
}
|
|
@Throws(Exception::class)
|
private fun trustAllHttpsCertificates() {
|
val trustAllCerts = arrayOfNulls<TrustManager>(1)
|
val tm: TrustManager = MiTM()
|
trustAllCerts[0] = tm
|
val sc = SSLContext.getInstance("SSL")
|
sc.init(null, trustAllCerts, null)
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
|
}
|
|
internal class MiTM : X509TrustManager {
|
override fun getAcceptedIssuers(): Array<X509Certificate>? {
|
return null
|
}
|
|
@Throws(CertificateException::class)
|
override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {
|
return
|
}
|
|
@Throws(CertificateException::class)
|
override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {
|
return
|
}
|
}
|
|
class MySecureProtocolSocketFactory : ProtocolSocketFactory {
|
//这里添加一个属性,主要目的就是来获取ssl跳过验证
|
private var sslContext: SSLContext? = null
|
|
/**
|
* 判断获取SSLContext
|
* @return
|
*/
|
private val sSLContext: SSLContext?
|
get() {
|
if (sslContext == null) {
|
sslContext = createEasySSLContext()
|
}
|
return sslContext
|
}
|
|
//后面的方法基本上就是带入相关参数就可以了
|
/*
|
* (non-Javadoc)
|
*
|
* @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
|
* int, java.net.InetAddress, int)
|
*/
|
@Throws(IOException::class, UnknownHostException::class)
|
override fun createSocket(host: String, port: Int, clientHost: InetAddress?, clientPort: Int): Socket {
|
return sSLContext!!.socketFactory.createSocket(host, port, clientHost, clientPort)
|
}
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
|
* int, java.net.InetAddress, int,
|
* org.apache.commons.httpclient.params.HttpConnectionParams)
|
*/
|
@Throws(IOException::class, UnknownHostException::class, ConnectTimeoutException::class)
|
override fun createSocket(
|
host: String, port: Int, localAddress: InetAddress?, localPort: Int,
|
params: HttpConnectionParams?
|
): Socket {
|
requireNotNull(params) { "Parameters may not be null" }
|
val timeout: Int = params.connectionTimeout
|
return if (timeout == 0) {
|
createSocket(host, port, localAddress, localPort)
|
} else {
|
ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout)
|
}
|
}
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
|
*/
|
@Throws(IOException::class, UnknownHostException::class)
|
override fun createSocket(host: String, port: Int): Socket {
|
return sSLContext!!.socketFactory.createSocket(host, port)
|
}
|
|
/*
|
* (non-Javadoc)
|
*
|
* @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
|
*/
|
// @Throws(IOException::class, UnknownHostException::class)
|
// override fun createSocket(socket: Socket?, host: String?, port: Int, autoClose: Boolean): Socket {
|
// return sSLContext!!.socketFactory.createSocket(socket, host, port, autoClose)
|
// }
|
|
companion object {
|
/**
|
* 这个创建一个获取SSLContext的方法,导入MyX509TrustManager进行初始化
|
* @return
|
*/
|
private fun createEasySSLContext(): SSLContext {
|
return try {
|
val context = SSLContext.getInstance("SSL")
|
context.init(
|
null, arrayOf(MiTM()),
|
null
|
)
|
context
|
} catch (e: Exception) {
|
throw HttpClientError(e.toString())
|
}
|
}
|
}
|
}
|
}
|