2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5 * Copyright (C) 2006 Ishan Arora <ishan@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
35 #include <QNetworkProxy>
37 #include <QSslConfiguration>
39 #include <QStringList>
42 #include "base/algorithm.h"
43 #include "base/utils/net.h"
44 #include "connection.h"
48 const int KEEP_ALIVE_DURATION
= 7 * 1000; // milliseconds
49 const int CONNECTIONS_LIMIT
= 500;
50 const int CONNECTIONS_SCAN_INTERVAL
= 2; // seconds
52 QList
<QSslCipher
> safeCipherList()
54 const QStringList badCiphers
{"idea", "rc4"};
55 const QList
<QSslCipher
> allCiphers
{QSslConfiguration::supportedCiphers()};
56 QList
<QSslCipher
> safeCiphers
;
57 std::copy_if(allCiphers
.cbegin(), allCiphers
.cend(), std::back_inserter(safeCiphers
), [&badCiphers
](const QSslCipher
&cipher
)
59 return std::none_of(badCiphers
.cbegin(), badCiphers
.cend(), [&cipher
](const QString
&badCipher
)
61 return cipher
.name().contains(badCipher
, Qt::CaseInsensitive
);
70 Server::Server(IRequestHandler
*requestHandler
, QObject
*parent
)
72 , m_requestHandler(requestHandler
)
75 setProxy(QNetworkProxy::NoProxy
);
77 QSslConfiguration sslConf
{QSslConfiguration::defaultConfiguration()};
78 sslConf
.setCiphers(safeCipherList());
79 QSslConfiguration::setDefaultConfiguration(sslConf
);
81 auto *dropConnectionTimer
= new QTimer(this);
82 connect(dropConnectionTimer
, &QTimer::timeout
, this, &Server::dropTimedOutConnection
);
83 dropConnectionTimer
->start(CONNECTIONS_SCAN_INTERVAL
* 1000);
86 void Server::incomingConnection(const qintptr socketDescriptor
)
88 if (m_connections
.size() >= CONNECTIONS_LIMIT
) return;
90 QTcpSocket
*serverSocket
;
92 serverSocket
= new QSslSocket(this);
94 serverSocket
= new QTcpSocket(this);
96 if (!serverSocket
->setSocketDescriptor(socketDescriptor
))
104 static_cast<QSslSocket
*>(serverSocket
)->setProtocol(QSsl::SecureProtocols
);
105 static_cast<QSslSocket
*>(serverSocket
)->setPrivateKey(m_key
);
106 static_cast<QSslSocket
*>(serverSocket
)->setLocalCertificateChain(m_certificates
);
107 static_cast<QSslSocket
*>(serverSocket
)->setPeerVerifyMode(QSslSocket::VerifyNone
);
108 static_cast<QSslSocket
*>(serverSocket
)->startServerEncryption();
111 auto *c
= new Connection(serverSocket
, m_requestHandler
, this);
112 m_connections
.insert(c
);
113 connect(serverSocket
, &QAbstractSocket::disconnected
, this, [c
, this]() { removeConnection(c
); });
116 void Server::removeConnection(Connection
*connection
)
118 m_connections
.remove(connection
);
119 connection
->deleteLater();
122 void Server::dropTimedOutConnection()
124 Algorithm::removeIf(m_connections
, [](Connection
*connection
)
126 if (!connection
->hasExpired(KEEP_ALIVE_DURATION
))
129 connection
->deleteLater();
134 bool Server::setupHttps(const QByteArray
&certificates
, const QByteArray
&privateKey
)
136 const QList
<QSslCertificate
> certs
{Utils::Net::loadSSLCertificate(certificates
)};
137 const QSslKey key
{Utils::Net::loadSSLKey(privateKey
)};
139 if (certs
.isEmpty() || key
.isNull())
146 m_certificates
= certs
;
151 void Server::disableHttps()
154 m_certificates
.clear();