Display External IP Address in status bar
[qBittorrent.git] / src / base / http / server.cpp
blob7c99d8ecd80282b6ee400d8fe2cd0b88b35c07a9
1 /*
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.
31 #include "server.h"
33 #include <algorithm>
34 #include <chrono>
35 #include <memory>
36 #include <new>
38 #include <QtLogging>
39 #include <QNetworkProxy>
40 #include <QSslCertificate>
41 #include <QSslCipher>
42 #include <QSslKey>
43 #include <QSslSocket>
44 #include <QStringList>
45 #include <QTimer>
47 #include "base/global.h"
48 #include "base/utils/net.h"
49 #include "base/utils/sslkey.h"
50 #include "connection.h"
52 using namespace std::chrono_literals;
54 namespace
56 const int KEEP_ALIVE_DURATION = std::chrono::milliseconds(7s).count();
57 const int CONNECTIONS_LIMIT = 500;
58 const std::chrono::seconds CONNECTIONS_SCAN_INTERVAL {2};
60 QList<QSslCipher> safeCipherList()
62 const QStringList badCiphers {u"idea"_s, u"rc4"_s};
63 // Contains Ciphersuites that use RSA for the Key Exchange but they don't mention it in their name
64 const QStringList badRSAShorthandSuites {
65 u"AES256-GCM-SHA384"_s, u"AES128-GCM-SHA256"_s, u"AES256-SHA256"_s,
66 u"AES128-SHA256"_s, u"AES256-SHA"_s, u"AES128-SHA"_s};
67 // Contains Ciphersuites that use AES CBC mode but they don't mention it in their name
68 const QStringList badAESShorthandSuites {
69 u"ECDHE-ECDSA-AES256-SHA384"_s, u"ECDHE-RSA-AES256-SHA384"_s, u"DHE-RSA-AES256-SHA256"_s,
70 u"ECDHE-ECDSA-AES128-SHA256"_s, u"ECDHE-RSA-AES128-SHA256"_s, u"DHE-RSA-AES128-SHA256"_s,
71 u"ECDHE-ECDSA-AES256-SHA"_s, u"ECDHE-RSA-AES256-SHA"_s, u"DHE-RSA-AES256-SHA"_s,
72 u"ECDHE-ECDSA-AES128-SHA"_s, u"ECDHE-RSA-AES128-SHA"_s, u"DHE-RSA-AES128-SHA"_s};
73 const QList<QSslCipher> allCiphers {QSslConfiguration::supportedCiphers()};
74 QList<QSslCipher> safeCiphers;
75 std::copy_if(allCiphers.cbegin(), allCiphers.cend(), std::back_inserter(safeCiphers),
76 [&badCiphers, &badRSAShorthandSuites, &badAESShorthandSuites](const QSslCipher &cipher)
78 const QString name = cipher.name();
79 if (name.contains(u"-cbc-"_s, Qt::CaseInsensitive) // AES CBC mode is considered vulnerable to BEAST attack
80 || name.startsWith(u"adh-"_s, Qt::CaseInsensitive) // Key Exchange: Diffie-Hellman, doesn't support Perfect Forward Secrecy
81 || name.startsWith(u"aecdh-"_s, Qt::CaseInsensitive) // Key Exchange: Elliptic Curve Diffie-Hellman, doesn't support Perfect Forward Secrecy
82 || name.startsWith(u"psk-"_s, Qt::CaseInsensitive) // Key Exchange: Pre-Shared Key, doesn't support Perfect Forward Secrecy
83 || name.startsWith(u"rsa-"_s, Qt::CaseInsensitive) // Key Exchange: Rivest Shamir Adleman (RSA), doesn't support Perfect Forward Secrecy
84 || badRSAShorthandSuites.contains(name, Qt::CaseInsensitive)
85 || badAESShorthandSuites.contains(name, Qt::CaseInsensitive))
87 return false;
90 return std::none_of(badCiphers.cbegin(), badCiphers.cend(), [&cipher](const QString &badCipher)
92 return cipher.name().contains(badCipher, Qt::CaseInsensitive);
93 });
94 });
95 return safeCiphers;
99 using namespace Http;
101 Server::Server(IRequestHandler *requestHandler, QObject *parent)
102 : QTcpServer(parent)
103 , m_requestHandler(requestHandler)
104 , m_sslConfig {QSslConfiguration::defaultConfiguration()}
106 setProxy(QNetworkProxy::NoProxy);
108 m_sslConfig.setCiphers(safeCipherList());
109 m_sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
111 auto *dropConnectionTimer = new QTimer(this);
112 connect(dropConnectionTimer, &QTimer::timeout, this, &Server::dropTimedOutConnection);
113 dropConnectionTimer->start(CONNECTIONS_SCAN_INTERVAL);
116 void Server::incomingConnection(const qintptr socketDescriptor)
118 std::unique_ptr<QTcpSocket> serverSocket = isHttps() ? std::make_unique<QSslSocket>(this) : std::make_unique<QTcpSocket>(this);
119 if (!serverSocket->setSocketDescriptor(socketDescriptor))
120 return;
122 if (m_connections.size() >= CONNECTIONS_LIMIT)
124 qWarning("Too many connections. Exceeded CONNECTIONS_LIMIT (%d). Connection closed.", CONNECTIONS_LIMIT);
125 return;
130 if (isHttps())
132 auto *sslSocket = static_cast<QSslSocket *>(serverSocket.get());
133 sslSocket->setSslConfiguration(m_sslConfig);
134 sslSocket->startServerEncryption();
137 auto *connection = new Connection(serverSocket.release(), m_requestHandler, this);
138 m_connections.insert(connection);
139 connect(connection, &Connection::closed, this, [this, connection] { removeConnection(connection); });
141 catch (const std::bad_alloc &exception)
143 // drop the connection instead of throwing exception and crash
144 qWarning("Failed to allocate memory for HTTP connection. Connection closed.");
145 return;
149 void Server::removeConnection(Connection *connection)
151 m_connections.remove(connection);
152 connection->deleteLater();
155 void Server::dropTimedOutConnection()
157 m_connections.removeIf([](Connection *connection)
159 if (!connection->hasExpired(KEEP_ALIVE_DURATION))
160 return false;
162 connection->deleteLater();
163 return true;
167 bool Server::setupHttps(const QByteArray &certificates, const QByteArray &privateKey)
169 const QList<QSslCertificate> certs {Utils::Net::loadSSLCertificate(certificates)};
170 const QSslKey key {Utils::SSLKey::load(privateKey)};
172 if (certs.isEmpty() || key.isNull())
174 disableHttps();
175 return false;
178 m_sslConfig.setLocalCertificateChain(certs);
179 m_sslConfig.setPrivateKey(key);
180 m_https = true;
181 return true;
184 void Server::disableHttps()
186 m_sslConfig.setLocalCertificateChain({});
187 m_sslConfig.setPrivateKey({});
188 m_https = false;
191 bool Server::isHttps() const
193 return m_https;