Fix TCP SSL server pending connection mechanism
This commit is contained in:
parent
c54e55cd96
commit
e0a2bcc09e
@ -161,74 +161,69 @@ SslServer::SslServer(bool sslEnabled, const QSslConfiguration &config, QObject *
|
|||||||
m_sslEnabled(sslEnabled),
|
m_sslEnabled(sslEnabled),
|
||||||
m_config(config)
|
m_config(config)
|
||||||
{
|
{
|
||||||
|
connect(this, &QTcpServer::newConnection, this, [this](){
|
||||||
|
|
||||||
|
QSslSocket *sslSocket = qobject_cast<QSslSocket *>(nextPendingConnection());
|
||||||
|
|
||||||
|
connect(sslSocket, &QSslSocket::disconnected, this, [this, sslSocket](){
|
||||||
|
qCDebug(dcTcpSocketServer()) << "Client socket disconnected:" << sslSocket << sslSocket->peerAddress().toString();;
|
||||||
|
emit socketDisconnected(sslSocket);
|
||||||
|
sslSocket->deleteLater();
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sslSocket, &QSslSocket::readyRead, this, [this, sslSocket](){
|
||||||
|
QByteArray data = sslSocket->readAll();
|
||||||
|
qCDebug(dcTcpSocketServerTraffic()) << "Data from socket" << sslSocket->peerAddress().toString() << data;
|
||||||
|
emit dataAvailable(sslSocket, data);
|
||||||
|
});
|
||||||
|
|
||||||
|
typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError);
|
||||||
|
connect(sslSocket, static_cast<errorSignal>(&QAbstractSocket::error), this, [sslSocket](QAbstractSocket::SocketError error){
|
||||||
|
qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString() << "Explicitly closing the socket connection.";
|
||||||
|
sslSocket->abort();
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(sslSocket, &QSslSocket::encrypted, this, [this, sslSocket](){
|
||||||
|
qCDebug(dcTcpSocketServer()) << "SSL encryption established for" << sslSocket;
|
||||||
|
emit socketConnected(sslSocket);
|
||||||
|
});
|
||||||
|
|
||||||
|
typedef void (QSslSocket:: *sslErrorsSignal)(const QList<QSslError> &);
|
||||||
|
connect(sslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors), this, [](const QList<QSslError> &errors) {
|
||||||
|
qCWarning(dcTcpSocketServer()) << "SSL error occurred in the client connection:";
|
||||||
|
foreach (const QSslError &error, errors) {
|
||||||
|
qCWarning(dcTcpSocketServer()) << "SSL error:" << error.error() << error.errorString();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (m_sslEnabled) {
|
||||||
|
if (sslSocket->isEncrypted()) {
|
||||||
|
qCDebug(dcTcpSocketServer()) << "SSL encryption established for" << sslSocket;
|
||||||
|
emit socketConnected(sslSocket);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
emit socketConnected(sslSocket);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void SslServer::incomingConnection(qintptr socketDescriptor)
|
void SslServer::incomingConnection(qintptr socketDescriptor)
|
||||||
{
|
{
|
||||||
QSslSocket *sslSocket = new QSslSocket(this);
|
QSslSocket *sslSocket = new QSslSocket(this);
|
||||||
qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket;
|
qCDebug(dcTcpSocketServer()) << "New incomming connection. Creating" << sslSocket;
|
||||||
|
|
||||||
connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected);
|
|
||||||
|
|
||||||
connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead);
|
|
||||||
|
|
||||||
typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError);
|
|
||||||
connect(sslSocket, static_cast<errorSignal>(&QAbstractSocket::error), this, &SslServer::onSocketError);
|
|
||||||
|
|
||||||
connect(sslSocket, &QSslSocket::encrypted, this, [this, sslSocket](){
|
|
||||||
qCDebug(dcTcpSocketServer()) << "SSL encryption established for" << sslSocket;
|
|
||||||
emit socketConnected(sslSocket);
|
|
||||||
});
|
|
||||||
|
|
||||||
typedef void (QSslSocket:: *sslErrorsSignal)(const QList<QSslError> &);
|
|
||||||
connect(sslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors), this, [](const QList<QSslError> &errors) {
|
|
||||||
qCWarning(dcTcpSocketServer()) << "SSL error occurred in the client connection:";
|
|
||||||
foreach (const QSslError &error, errors) {
|
|
||||||
qCWarning(dcTcpSocketServer()) << "SSL error:" << error.error() << error.errorString();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
if (!sslSocket->setSocketDescriptor(socketDescriptor)) {
|
if (!sslSocket->setSocketDescriptor(socketDescriptor)) {
|
||||||
qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor.";
|
qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor" << sslSocket << "Discard connection...";
|
||||||
delete sslSocket;
|
delete sslSocket;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//addPendingConnection(sslSocket);
|
|
||||||
|
|
||||||
if (m_sslEnabled) {
|
if (m_sslEnabled) {
|
||||||
qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket;
|
qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket;
|
||||||
sslSocket->setSslConfiguration(m_config);
|
sslSocket->setSslConfiguration(m_config);
|
||||||
sslSocket->startServerEncryption();
|
sslSocket->startServerEncryption();
|
||||||
} else {
|
|
||||||
emit socketConnected(sslSocket);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void SslServer::onSocketDisconnected()
|
addPendingConnection(sslSocket);
|
||||||
{
|
|
||||||
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
|
|
||||||
qCDebug(dcTcpSocketServer()) << "Client socket disconnected:" << sslSocket << sslSocket->peerAddress().toString();;
|
|
||||||
emit socketDisconnected(sslSocket);
|
|
||||||
sslSocket->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
void SslServer::onSocketReadyRead()
|
|
||||||
{
|
|
||||||
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
|
|
||||||
QByteArray data = sslSocket->readAll();
|
|
||||||
qCDebug(dcTcpSocketServerTraffic()) << "Data from socket" << sslSocket->peerAddress().toString() << data;
|
|
||||||
emit dataAvailable(sslSocket, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SslServer::onSocketError(QAbstractSocket::SocketError error)
|
|
||||||
{
|
|
||||||
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
|
|
||||||
qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString();
|
|
||||||
qCWarning(dcTcpSocketServer()) << "Explicitly closing the socket connection.";
|
|
||||||
sslSocket->abort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,7 +37,6 @@
|
|||||||
|
|
||||||
namespace remoteproxy {
|
namespace remoteproxy {
|
||||||
|
|
||||||
|
|
||||||
class SslServer: public QTcpServer
|
class SslServer: public QTcpServer
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -57,11 +56,6 @@ private:
|
|||||||
bool m_sslEnabled = false;
|
bool m_sslEnabled = false;
|
||||||
QSslConfiguration m_config;
|
QSslConfiguration m_config;
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onSocketDisconnected();
|
|
||||||
void onSocketReadyRead();
|
|
||||||
void onSocketError(QAbstractSocket::SocketError);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user