[英]How to handle TLS handshake timeout in QTcpServer?
I'm trying to figure out how to create a timeout for the handshake process in a TLS connection in a QTcpServer
.我试图弄清楚如何在
QTcpServer
的 TLS 连接中为握手过程创建超时。
I tried something like this in the overriden incomingConnection
function:我在覆盖的
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;
}
});
But this doesn't seem to work because I am not calling directly addPendingConnection
function (it get's called in a slot/lamdba which seems to break the pendingConnection
chain.但这似乎不起作用,因为我没有直接调用
addPendingConnection
function (它在 slot/lamdba 中被调用,这似乎破坏了pendingConnection
链。
Does anybody know how can I achieve this timeout in Qt?有谁知道我怎样才能在 Qt 中实现这个超时? The problem at the moment is that a client can open a connection with the server and it never answers the TLS handshake which leads to an useless open connection (that is never closed).
目前的问题是客户端可以打开与服务器的连接,并且它永远不会响应导致无用的打开连接(永远不会关闭)的 TLS 握手。
I ended implementing the TLS handshake timeout this way:我以这种方式结束了 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();
}
});
This code can be added anywhere where is more practical for your project.可以将此代码添加到对您的项目更实用的任何地方。 I added it in a session class that we have (which owns the created socket), this class is created in the end of newConnection slot.
我将它添加到我们拥有的 session class 中(它拥有创建的套接字),这个 class 是在 newConnection 插槽的末尾创建的。 I have tested it and works perfectly.
我已经对其进行了测试并且可以完美运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.