18 #include <QtNetwork/QSslCipher>
19 #include <QtNetwork/QSslSocket>
26 , _timer(new QTimer(this))
28 connect(
_timer, &QTimer::timeout, [=]() {
31 const qint64 deadline = QDateTime::currentMSecsSinceEpoch() - 10000;
32 QMutableHashIterator<QSslSocket*, qint64> it(
_pending);
33 while (it.hasNext()) {
35 if (it.value() < deadline) {
36 it.key()->blockSignals(
true);
37 it.key()->deleteLater();
49 for (QSslSocket *sock : keys) {
61 static int certFails = 0;
65 if (++certFails > 5) {
68 ::close(
int(socketDescriptor));
71 auto *serverSocket =
new QSslSocket(
nullptr);
72 connect(serverSocket, QOverload<
const QList<QSslError> &>::of(&QSslSocket::sslErrors),
this, &
SslServer::sslErrors);
73 serverSocket->setPrivateKey(key);
74 serverSocket->setLocalCertificate(cert);
75 serverSocket->setPeerVerifyMode(QSslSocket::VerifyNone);
76 serverSocket->setProtocol(QSsl::SecureProtocols);
77 if (serverSocket->setSocketDescriptor(socketDescriptor)) {
78 _pending.insert(serverSocket, QDateTime::currentMSecsSinceEpoch());
80 connect(serverSocket, &QSslSocket::encrypted, [=]() {
81 serverSocket->disconnect(
this);
82 if (
_pending.remove(serverSocket) == 0) {
84 qDebug() <<
"Encryption event for socket that is not pending!?";
86 this->addPendingConnection(serverSocket);
90 serverSocket->startServerEncryption();
92 qDebug() <<
"Failed to setSocketDescriptor on new SSL Socket";
93 serverSocket->deleteLater();
99 qDebug() <<
"Client caused sslErrors before connection:";
100 for (
const auto & error : errors) {
101 qDebug() << error.errorString();
void incomingConnection(qintptr handle) override
Handle incomming connection.
void sslErrors(const QList< QSslError > &errors)
SslServer(QObject *parent)
QHash< QSslSocket *, qint64 > _pending
bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)