繁体   English   中英

如何处理 QTcpServer 中的 TLS 握手超时?

[英]How to handle TLS handshake timeout in QTcpServer?

我试图弄清楚如何在QTcpServer的 TLS 连接中为握手过程创建超时。

我在覆盖的incomingConnection function中尝试了这样的事情:

QSslSocket * const tlsSocket = static_cast<QSslSocket*>(socket);
    connect(tlsSocket, &QSslSocket::encrypted, this, [this, tlsSocket](){ addPendingConnection(tlsSocket); });
    tlsSocket->setLocalCertificate(m_serverCertificate);
    tlsSocket->setPrivateKey(m_serverPrivateKey);
    tlsSocket->setProtocol(QSsl::SecureProtocols);
    tlsSocket->startServerEncryption();

    // We will have a handshake timeout of 30 seconds
    QTimer::singleShot(30*1000, this, [this, tlsSocket]() {
        if(!tlsSocket->isEncrypted()) {
            // If no handshake initialized from the client close the connection
            delete tlsSocket;
        }
    });

但这似乎不起作用,因为我没有直接调用addPendingConnection function (它在 slot/lamdba 中被调用,这似乎破坏了pendingConnection链。

有谁知道我怎样才能在 Qt 中实现这个超时? 目前的问题是客户端可以打开与服务器的连接,并且它永远不会响应导致无用的打开连接(永远不会关闭)的 TLS 握手。

我以这种方式结束了 TLS 握手超时:

// We will have a handshake timeout of 30 seconds (same as firefox today)
QTimer::singleShot(30*1000, this, [this]() {

    // we use dynamic_cast because this may be or not an encrypted socket
    QSslSocket * const tlsSocket = dynamic_cast<QSslSocket*>(m_socket);

    if(tlsSocket != nullptr && !tlsSocket->isEncrypted()) {
        qWarning() << "TLS Handshake timeout for connection from " <<
                      tlsSocket->peerAddress().toString() << ":" << tlsSocket->peerPort();
        tlsSocket->close();
    }
});

可以将此代码添加到对您的项目更实用的任何地方。 我将它添加到我们拥有的 session class 中(它拥有创建的套接字),这个 class 是在 newConnection 插槽的末尾创建的。 我已经对其进行了测试并且可以完美运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM