Set metadata asynchronously
[qBittorrent.git] / src / webui / webui.cpp
blobdc1b435962998ba6e56b5b7f8b1f87ee136e5fcf
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/path.h"
38 #include "base/preferences.h"
39 #include "base/utils/net.h"
40 #include "webapplication.h"
42 WebUI::WebUI(IApplication *app)
43 : ApplicationComponent(app)
45 configure();
46 connect(Preferences::instance(), &Preferences::changed, this, &WebUI::configure);
49 void WebUI::configure()
51 m_isErrored = false; // clear previous error state
53 const QString portForwardingProfile = u"webui"_qs;
54 const Preferences *pref = Preferences::instance();
55 const quint16 port = pref->getWebUiPort();
57 if (pref->isWebUiEnabled())
59 // Port forwarding
60 auto *portForwarder = Net::PortForwarder::instance();
61 if (pref->useUPnPForWebUIPort())
63 portForwarder->setPorts(portForwardingProfile, {port});
65 else
67 portForwarder->removePorts(portForwardingProfile);
70 // http server
71 const QString serverAddressString = pref->getWebUiAddress();
72 if (!m_httpServer)
74 m_webapp = new WebApplication(app(), this);
75 m_httpServer = new Http::Server(m_webapp, this);
77 else
79 if ((m_httpServer->serverAddress().toString() != serverAddressString)
80 || (m_httpServer->serverPort() != port))
81 m_httpServer->close();
84 if (pref->isWebUiHttpsEnabled())
86 const auto readData = [](const Path &path) -> QByteArray
88 QFile file {path.data()};
89 if (!file.open(QIODevice::ReadOnly))
90 return {};
91 return file.read(Utils::Net::MAX_SSL_FILE_SIZE);
93 const QByteArray cert = readData(pref->getWebUIHttpsCertificatePath());
94 const QByteArray key = readData(pref->getWebUIHttpsKeyPath());
96 const bool success = m_httpServer->setupHttps(cert, key);
97 if (success)
98 LogMsg(tr("Web UI: HTTPS setup successful"));
99 else
100 LogMsg(tr("Web UI: HTTPS setup failed, fallback to HTTP"), Log::CRITICAL);
102 else
104 m_httpServer->disableHttps();
107 if (!m_httpServer->isListening())
109 const auto address = ((serverAddressString == u"*") || serverAddressString.isEmpty())
110 ? QHostAddress::Any : QHostAddress(serverAddressString);
111 bool success = m_httpServer->listen(address, port);
112 if (success)
114 LogMsg(tr("Web UI: Now listening on IP: %1, port: %2").arg(serverAddressString).arg(port));
116 else
118 const QString errorMsg = tr("Web UI: Unable to bind to IP: %1, port: %2. Reason: %3")
119 .arg(serverAddressString).arg(port).arg(m_httpServer->errorString());
120 LogMsg(errorMsg, Log::CRITICAL);
121 qCritical() << errorMsg;
123 m_isErrored = true;
124 emit fatalError();
128 // DynDNS
129 if (pref->isDynDNSEnabled())
131 if (!m_dnsUpdater)
132 m_dnsUpdater = new Net::DNSUpdater(this);
133 else
134 m_dnsUpdater->updateCredentials();
136 else
138 delete m_dnsUpdater;
141 else
143 Net::PortForwarder::instance()->removePorts(portForwardingProfile);
145 delete m_httpServer;
146 delete m_webapp;
147 delete m_dnsUpdater;
151 bool WebUI::isErrored() const
153 return m_isErrored;