HTTPS IP直连存在的问题

2019-09-26  本文已影响0人  游侠_6fb7

IP直连的好处

  1. 省去DNS解析过程,减少耗时;
  2. 就近接入,减少耗时;
  3. 避免DNS劫持;
  4. 终端有多个IP接入时,有容灾能力;

回顾网络请求握手过程

HTTP TCP三次握手流程
20190917164025.jpg
握手目的:

为了保证通讯双方建立的连接是可靠的。
同时,为了保证性能,握手的次数要求尽可能少。
  那么什么才算是连接可靠?
通讯双方建立的连接可靠”就是要确保双方的发送和接收功能都正常。
  在握手前,双方的发送和接收能力尚未确认。三次最可靠;
握手过程

HTTPS连接过程

三次握手之后,连接请求ca证书


Https连接过程.png

问题

  1. 证书HOST校验问题;
    证书校验用的是域名校验,而不是iP地址,OkHostnameVerifier替换为域名;
  2. SNI问题;
    一个域名对应多个iP地址,证书校验时没有找到自己设置的IP地址,报错,在SSLSocketFactory的实现类中设置自己的域名;
  3. 连接复用问题;
    具体判断是否可复用问题在RealConnection#isEligible();
boolean equalsNonHost(Address that) {
    return this.dns.equals(that.dns)
        && this.proxyAuthenticator.equals(that.proxyAuthenticator)
        && this.protocols.equals(that.protocols)
        && this.connectionSpecs.equals(that.connectionSpecs)
        && this.proxySelector.equals(that.proxySelector)
        && equal(this.proxy, that.proxy)
        && equal(this.sslSocketFactory, that.sslSocketFactory)
        && equal(this.hostnameVerifier, that.hostnameVerifier)
        && equal(this.certificatePinner, that.certificatePinner)
        && this.url().port() == that.url().port();
  }

&& equal(this.sslSocketFactory, that.sslSocketFactory)
&& equal(this.hostnameVerifier, that.hostnameVerifier)
修改equals方法

  1. 兼容性问题;
  2. Session复用问题
    参考文档:
    HTTPS IP直连问题小结
    HTTPS和TCP协议三次握手设计
上一篇 下一篇

猜你喜欢

热点阅读