Fix incorrect function being used
[qBittorrent.git] / src / webui / webui.cpp
blob038c975d6307f61f7acb54759ccae4d925300199
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "webui.h"
31 #include <QFile>
33 #include "base/http/server.h"
34 #include "base/logger.h"
35 #include "base/net/dnsupdater.h"
36 #include "base/net/portforwarder.h"
37 #include "base/preferences.h"
38 #include "base/utils/net.h"
39 #include "webapplication.h"
41 WebUI::WebUI()
42 : m_isErrored(false)
43 , m_port(0)
45 configure();
46 connect(Preferences::instance(), &Preferences::changed, this, &WebUI::configure);
49 void WebUI::configure()
51 m_isErrored = false; // clear previous error state
53 Logger *const logger = Logger::instance();
54 Preferences *const pref = Preferences::instance();
56 const quint16 oldPort = m_port;
57 m_port = pref->getWebUiPort();
59 if (pref->isWebUiEnabled()) {
60 // UPnP/NAT-PMP
61 if (pref->useUPnPForWebUIPort()) {
62 if (m_port != oldPort) {
63 Net::PortForwarder::instance()->deletePort(oldPort);
64 Net::PortForwarder::instance()->addPort(m_port);
67 else {
68 Net::PortForwarder::instance()->deletePort(oldPort);
71 // http server
72 const QString serverAddressString = pref->getWebUiAddress();
73 if (!m_httpServer) {
74 m_webapp = new WebApplication(this);
75 m_httpServer = new Http::Server(m_webapp, this);
77 else {
78 if ((m_httpServer->serverAddress().toString() != serverAddressString)
79 || (m_httpServer->serverPort() != m_port))
80 m_httpServer->close();
83 if (pref->isWebUiHttpsEnabled()) {
84 const auto readData = [](const QString &path) -> QByteArray
86 QFile file(path);
87 if (!file.open(QIODevice::ReadOnly))
88 return {};
89 return file.read(Utils::Net::MAX_SSL_FILE_SIZE);
91 const QByteArray cert = readData(pref->getWebUIHttpsCertificatePath());
92 const QByteArray key = readData(pref->getWebUIHttpsKeyPath());
94 const bool success = m_httpServer->setupHttps(cert, key);
95 if (success)
96 logger->addMessage(tr("Web UI: HTTPS setup successful"));
97 else
98 logger->addMessage(tr("Web UI: HTTPS setup failed, fallback to HTTP"), Log::CRITICAL);
100 else {
101 m_httpServer->disableHttps();
104 if (!m_httpServer->isListening()) {
105 const auto address = (serverAddressString == "*" || serverAddressString.isEmpty())
106 ? QHostAddress::Any : QHostAddress(serverAddressString);
107 bool success = m_httpServer->listen(address, m_port);
108 if (success) {
109 logger->addMessage(tr("Web UI: Now listening on IP: %1, port: %2").arg(serverAddressString).arg(m_port));
111 else {
112 const QString errorMsg = tr("Web UI: Unable to bind to IP: %1, port: %2. Reason: %3")
113 .arg(serverAddressString).arg(m_port).arg(m_httpServer->errorString());
114 logger->addMessage(errorMsg, Log::CRITICAL);
115 qCritical() << errorMsg;
117 m_isErrored = true;
118 emit fatalError();
122 // DynDNS
123 if (pref->isDynDNSEnabled()) {
124 if (!m_dnsUpdater)
125 m_dnsUpdater = new Net::DNSUpdater(this);
126 else
127 m_dnsUpdater->updateCredentials();
129 else {
130 if (m_dnsUpdater)
131 delete m_dnsUpdater;
134 else {
135 Net::PortForwarder::instance()->deletePort(oldPort);
137 if (m_httpServer)
138 delete m_httpServer;
140 if (m_webapp)
141 delete m_webapp;
143 if (m_dnsUpdater)
144 delete m_dnsUpdater;
148 bool WebUI::isErrored() const
150 return m_isErrored;