超时SSL握手

时间:2014-12-20 19:50:42

标签: python ssl twisted connection-timeout

我正在尝试将TimeoutMixin合并到SSL协议中。但是,当超时发生并且它调用transport.loseConnection()时没有任何反应。我认为这与this code in TLSMemoryBIOProtocol

有关
def _shutdownTLS(self):
    """
    Initiate, or reply to, the shutdown handshake of the TLS layer.
    """
    try:
        shutdownSuccess = self._tlsConnection.shutdown()
    except Error:
        # Mid-handshake, a call to shutdown() can result in a
        # WantWantReadError, or rather an SSL_ERR_WANT_READ; but pyOpenSSL
        # doesn't allow us to get at the error.  See:
        # https://github.com/pyca/pyopenssl/issues/91
        shutdownSuccess = False
    self._flushSendBIO()
    if shutdownSuccess:
        # Both sides have shutdown, so we can start closing lower-level
        # transport. This will also happen if we haven't started
        # negotiation at all yet, in which case shutdown succeeds
        # immediately.
        self.transport.loseConnection()

问题是在握手发生之前发生了超时。在服务器端,它有一个打开的端口侦听连接,但服务器已冻结,无法进行正确的握手。该代码片段看起来无法执行TLS关闭,然后什么都不做。

我的问题是:

如何在SSL握手上设置超时?如果在合理的时间内没有发生握手,如何正确地断开连接?此外,无论TLS连接是否切断,上述代码段是否有任何问题都被删除以删除底层的低级连接? (无所事事,无限期悬挂似乎不是正确的做法)

修改

如果之前发送了任何数据并且没有发送任何数据,那么似乎会发生对loseConnection的调用失败。

1 个答案:

答案 0 :(得分:0)

loseConnection是有序连接关闭的API。如果您想突然终止连接,abortConnection就是您的API。