Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / webui / webui.cpp
blobab2e9b96e3d2cdc3a0637e8d8ce5083be9f2ff4e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015, 2023 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 "base/global.h"
32 #include "base/http/server.h"
33 #include "base/logger.h"
34 #include "base/net/dnsupdater.h"
35 #include "base/net/portforwarder.h"
36 #include "base/path.h"
37 #include "base/preferences.h"
38 #include "base/utils/io.h"
39 #include "base/utils/net.h"
40 #include "base/utils/password.h"
41 #include "webapplication.h"
43 WebUI::WebUI(IApplication *app, const QByteArray &tempPasswordHash)
44 : ApplicationComponent(app)
45 , m_passwordHash {tempPasswordHash}
47 configure();
48 connect(Preferences::instance(), &Preferences::changed, this, &WebUI::configure);
51 void WebUI::configure()
53 m_isErrored = false; // clear previous error state
55 const Preferences *pref = Preferences::instance();
56 const bool isEnabled = pref->isWebUIEnabled();
57 const QString username = pref->getWebUIUsername();
58 if (const QByteArray passwordHash = pref->getWebUIPassword(); !passwordHash.isEmpty())
59 m_passwordHash = passwordHash;
61 if (isEnabled && (username.isEmpty() || m_passwordHash.isEmpty()))
63 setError(tr("Credentials are not set"));
66 const QString portForwardingProfile = u"webui"_s;
68 if (isEnabled && !m_isErrored)
70 const quint16 port = pref->getWebUIPort();
72 // Port forwarding
73 auto *portForwarder = Net::PortForwarder::instance();
74 if (pref->useUPnPForWebUIPort())
76 portForwarder->setPorts(portForwardingProfile, {port});
78 else
80 portForwarder->removePorts(portForwardingProfile);
83 // http server
84 const QString serverAddressString = pref->getWebUIAddress();
85 const auto serverAddress = ((serverAddressString == u"*") || serverAddressString.isEmpty())
86 ? QHostAddress::Any : QHostAddress(serverAddressString);
88 if (!m_httpServer)
90 m_webapp = new WebApplication(app(), this);
91 m_httpServer = new Http::Server(m_webapp, this);
93 else
95 if ((m_httpServer->serverAddress() != serverAddress) || (m_httpServer->serverPort() != port))
96 m_httpServer->close();
99 m_webapp->setUsername(username);
100 m_webapp->setPasswordHash(m_passwordHash);
102 if (pref->isWebUIHttpsEnabled())
104 const auto readData = [](const Path &path) -> QByteArray
106 const auto readResult = Utils::IO::readFile(path, Utils::Net::MAX_SSL_FILE_SIZE);
107 return readResult.value_or(QByteArray());
109 const QByteArray cert = readData(pref->getWebUIHttpsCertificatePath());
110 const QByteArray key = readData(pref->getWebUIHttpsKeyPath());
112 const bool success = m_httpServer->setupHttps(cert, key);
113 if (success)
114 LogMsg(tr("WebUI: HTTPS setup successful"));
115 else
116 LogMsg(tr("WebUI: HTTPS setup failed, fallback to HTTP"), Log::CRITICAL);
118 else
120 m_httpServer->disableHttps();
123 if (!m_httpServer->isListening())
125 const bool success = m_httpServer->listen(serverAddress, port);
126 if (success)
128 LogMsg(tr("WebUI: Now listening on IP: %1, port: %2").arg(serverAddressString).arg(port));
130 else
132 setError(tr("Unable to bind to IP: %1, port: %2. Reason: %3")
133 .arg(serverAddressString).arg(port).arg(m_httpServer->errorString()));
137 // DynDNS
138 if (pref->isDynDNSEnabled())
140 if (!m_dnsUpdater)
141 m_dnsUpdater = new Net::DNSUpdater(this);
142 else
143 m_dnsUpdater->updateCredentials();
145 else
147 delete m_dnsUpdater;
150 else
152 Net::PortForwarder::instance()->removePorts(portForwardingProfile);
154 delete m_httpServer;
155 delete m_webapp;
156 delete m_dnsUpdater;
160 void WebUI::setError(const QString &message)
162 m_isErrored = true;
163 m_errorMsg = message;
165 const QString logMessage = u"WebUI: " + m_errorMsg;
166 LogMsg(logMessage, Log::CRITICAL);
167 qCritical() << logMessage;
169 emit fatalError();
172 bool WebUI::isErrored() const
174 return m_isErrored;
177 QString WebUI::errorMessage() const
179 return m_errorMsg;