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.
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
}
48 connect(Preferences::instance(), &Preferences::changed
, this, &WebUI::configure
);
51 void WebUI::configure()
53 m_isErrored
= false; // clear previous error state
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();
74 auto *portForwarder
= Net::PortForwarder::instance();
75 if (pref
->useUPnPForWebUIPort())
77 portForwarder
->setPorts(portForwardingProfile
, {port
});
81 portForwarder
->removePorts(portForwardingProfile
);
85 const QString serverAddressString
= pref
->getWebUIAddress();
86 const auto serverAddress
= ((serverAddressString
== u
"*") || serverAddressString
.isEmpty())
87 ? QHostAddress::Any
: QHostAddress(serverAddressString
);
91 m_webapp
= new WebApplication(app(), this);
92 m_httpServer
= new Http::Server(m_webapp
, this);
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
);
115 LogMsg(tr("WebUI: HTTPS setup successful"));
117 LogMsg(tr("WebUI: HTTPS setup failed, fallback to HTTP"), Log::CRITICAL
);
121 m_httpServer
->disableHttps();
124 if (!m_httpServer
->isListening())
126 const bool success
= m_httpServer
->listen(serverAddress
, port
);
129 LogMsg(tr("WebUI: Now listening on IP: %1, port: %2").arg(serverAddressString
).arg(port
));
133 setError(tr("Unable to bind to IP: %1, port: %2. Reason: %3")
134 .arg(serverAddressString
).arg(port
).arg(m_httpServer
->errorString()));
139 if (pref
->isDynDNSEnabled())
142 m_dnsUpdater
= new Net::DNSUpdater(this);
144 m_dnsUpdater
->updateCredentials();
153 Net::PortForwarder::instance()->removePorts(portForwardingProfile
);
161 void WebUI::setError(const QString
&message
)
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
178 bool WebUI::isErrored() const
183 QString
WebUI::errorMessage() const
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();