Display External IP Address in status bar
[qBittorrent.git] / src / webui / webui.cpp
blobfc177d56bfec06c3da5d07f39d244f9b2cd48306
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_tempPasswordHash {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();
58 m_isEnabled = pref->isWebUIEnabled();
59 const QString username = pref->getWebUIUsername();
60 QByteArray passwordHash = m_tempPasswordHash;
61 if (const QByteArray prefPasswordHash = pref->getWebUIPassword(); !prefPasswordHash.isEmpty())
63 passwordHash = prefPasswordHash;
64 m_tempPasswordHash.clear();
67 if (m_isEnabled && (username.isEmpty() || passwordHash.isEmpty()))
69 setError(tr("Credentials are not set"));
72 const QString portForwardingProfile = u"webui"_s;
74 if (m_isEnabled && !m_isErrored)
76 const quint16 port = pref->getWebUIPort();
78 // Port forwarding
79 auto *portForwarder = Net::PortForwarder::instance();
80 if (pref->useUPnPForWebUIPort())
82 portForwarder->setPorts(portForwardingProfile, {port});
84 else
86 portForwarder->removePorts(portForwardingProfile);
89 // http server
90 const QString serverAddressString = pref->getWebUIAddress();
91 const auto serverAddress = ((serverAddressString == u"*") || serverAddressString.isEmpty())
92 ? QHostAddress::Any : QHostAddress(serverAddressString);
94 if (!m_httpServer)
96 m_webapp = new WebApplication(app(), this);
97 m_httpServer = new Http::Server(m_webapp, this);
99 else
101 if ((m_httpServer->serverAddress() != serverAddress) || (m_httpServer->serverPort() != port))
102 m_httpServer->close();
105 m_webapp->setUsername(username);
106 m_webapp->setPasswordHash(passwordHash);
108 if (pref->isWebUIHttpsEnabled())
110 const auto readData = [](const Path &path) -> QByteArray
112 const auto readResult = Utils::IO::readFile(path, Utils::Net::MAX_SSL_FILE_SIZE);
113 return readResult.value_or(QByteArray());
115 const QByteArray cert = readData(pref->getWebUIHttpsCertificatePath());
116 const QByteArray key = readData(pref->getWebUIHttpsKeyPath());
118 const bool success = m_httpServer->setupHttps(cert, key);
119 if (success)
120 LogMsg(tr("WebUI: HTTPS setup successful"));
121 else
122 LogMsg(tr("WebUI: HTTPS setup failed, fallback to HTTP"), Log::CRITICAL);
124 else
126 m_httpServer->disableHttps();
129 if (!m_httpServer->isListening())
131 const bool success = m_httpServer->listen(serverAddress, port);
132 if (success)
134 LogMsg(tr("WebUI: Now listening on IP: %1, port: %2").arg(serverAddressString).arg(port));
136 else
138 setError(tr("Unable to bind to IP: %1, port: %2. Reason: %3")
139 .arg(serverAddressString).arg(port).arg(m_httpServer->errorString()));
143 // DynDNS
144 if (pref->isDynDNSEnabled())
146 if (!m_dnsUpdater)
147 m_dnsUpdater = new Net::DNSUpdater(this);
148 else
149 m_dnsUpdater->updateCredentials();
151 else
153 delete m_dnsUpdater;
156 else
158 Net::PortForwarder::instance()->removePorts(portForwardingProfile);
160 delete m_httpServer;
161 delete m_webapp;
162 delete m_dnsUpdater;
166 void WebUI::setError(const QString &message)
168 m_isErrored = true;
169 m_errorMsg = message;
171 const QString logMessage = u"WebUI: " + m_errorMsg;
172 LogMsg(logMessage, Log::CRITICAL);
173 qCritical() << logMessage;
175 emit error(m_errorMsg);
178 bool WebUI::isEnabled() const
180 return m_isEnabled;
183 bool WebUI::isErrored() const
185 return m_isErrored;
188 QString WebUI::errorMessage() const
190 return m_errorMsg;
193 bool WebUI::isHttps() const
195 if (!m_httpServer) return false;
196 return m_httpServer->isHttps();
199 QHostAddress WebUI::hostAddress() const
201 if (!m_httpServer) return {};
202 return m_httpServer->serverAddress();
205 quint16 WebUI::port() const
207 if (!m_httpServer) return 0;
208 return m_httpServer->serverPort();