Enable customizing the save statistics time interval
[qBittorrent.git] / src / webui / webui.cpp
blob20036c156e85dd46ae94f4a2a87a29ee43e7bca2
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
54 m_errorMsg.clear();
56 const Preferences *pref = Preferences::instance();
57 m_isEnabled = pref->isWebUIEnabled();
58 const QString username = pref->getWebUIUsername();
59 if (const QByteArray passwordHash = pref->getWebUIPassword(); !passwordHash.isEmpty())
60 m_passwordHash = passwordHash;
62 if (m_isEnabled && (username.isEmpty() || m_passwordHash.isEmpty()))
64 setError(tr("Credentials are not set"));
67 const QString portForwardingProfile = u"webui"_s;
69 if (m_isEnabled && !m_isErrored)
71 const quint16 port = pref->getWebUIPort();
73 // Port forwarding
74 auto *portForwarder = Net::PortForwarder::instance();
75 if (pref->useUPnPForWebUIPort())
77 portForwarder->setPorts(portForwardingProfile, {port});
79 else
81 portForwarder->removePorts(portForwardingProfile);
84 // http server
85 const QString serverAddressString = pref->getWebUIAddress();
86 const auto serverAddress = ((serverAddressString == u"*") || serverAddressString.isEmpty())
87 ? QHostAddress::Any : QHostAddress(serverAddressString);
89 if (!m_httpServer)
91 m_webapp = new WebApplication(app(), this);
92 m_httpServer = new Http::Server(m_webapp, this);
94 else
96 if ((m_httpServer->serverAddress() != serverAddress) || (m_httpServer->serverPort() != port))
97 m_httpServer->close();
100 m_webapp->setUsername(username);
101 m_webapp->setPasswordHash(m_passwordHash);
103 if (pref->isWebUIHttpsEnabled())
105 const auto readData = [](const Path &path) -> QByteArray
107 const auto readResult = Utils::IO::readFile(path, Utils::Net::MAX_SSL_FILE_SIZE);
108 return readResult.value_or(QByteArray());
110 const QByteArray cert = readData(pref->getWebUIHttpsCertificatePath());
111 const QByteArray key = readData(pref->getWebUIHttpsKeyPath());
113 const bool success = m_httpServer->setupHttps(cert, key);
114 if (success)
115 LogMsg(tr("WebUI: HTTPS setup successful"));
116 else
117 LogMsg(tr("WebUI: HTTPS setup failed, fallback to HTTP"), Log::CRITICAL);
119 else
121 m_httpServer->disableHttps();
124 if (!m_httpServer->isListening())
126 const bool success = m_httpServer->listen(serverAddress, port);
127 if (success)
129 LogMsg(tr("WebUI: Now listening on IP: %1, port: %2").arg(serverAddressString).arg(port));
131 else
133 setError(tr("Unable to bind to IP: %1, port: %2. Reason: %3")
134 .arg(serverAddressString).arg(port).arg(m_httpServer->errorString()));
138 // DynDNS
139 if (pref->isDynDNSEnabled())
141 if (!m_dnsUpdater)
142 m_dnsUpdater = new Net::DNSUpdater(this);
143 else
144 m_dnsUpdater->updateCredentials();
146 else
148 delete m_dnsUpdater;
151 else
153 Net::PortForwarder::instance()->removePorts(portForwardingProfile);
155 delete m_httpServer;
156 delete m_webapp;
157 delete m_dnsUpdater;
161 void WebUI::setError(const QString &message)
163 m_isErrored = true;
164 m_errorMsg = message;
166 const QString logMessage = u"WebUI: " + m_errorMsg;
167 LogMsg(logMessage, Log::CRITICAL);
168 qCritical() << logMessage;
170 emit error(m_errorMsg);
173 bool WebUI::isEnabled() const
175 return m_isEnabled;
178 bool WebUI::isErrored() const
180 return m_isErrored;
183 QString WebUI::errorMessage() const
185 return m_errorMsg;
188 bool WebUI::isHttps() const
190 if (!m_httpServer) return false;
191 return m_httpServer->isHttps();
194 QHostAddress WebUI::hostAddress() const
196 if (!m_httpServer) return {};
197 return m_httpServer->serverAddress();
200 quint16 WebUI::port() const
202 if (!m_httpServer) return 0;
203 return m_httpServer->serverPort();