Browse Source

Merge pull request #6621 from Chocobo1/webui

Webui: refactor
adaptive-webui-19844
sledgehammer999 8 years ago committed by GitHub
parent
commit
48b0cf8a48
  1. 46
      src/base/http/server.cpp
  2. 8
      src/base/http/server.h
  3. 89
      src/webui/webui.cpp
  4. 6
      src/webui/webui.h

46
src/base/http/server.cpp

@ -60,22 +60,6 @@ Server::~Server()
{ {
} }
#ifndef QT_NO_OPENSSL
void Server::enableHttps(const QList<QSslCertificate> &certificates, const QSslKey &key)
{
m_certificates = certificates;
m_key = key;
m_https = true;
}
void Server::disableHttps()
{
m_https = false;
m_certificates.clear();
m_key.clear();
}
#endif
void Server::incomingConnection(qintptr socketDescriptor) void Server::incomingConnection(qintptr socketDescriptor)
{ {
QTcpSocket *serverSocket; QTcpSocket *serverSocket;
@ -104,6 +88,36 @@ void Server::incomingConnection(qintptr socketDescriptor)
} }
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
bool Server::setupHttps(const QByteArray &certificates, const QByteArray &key)
{
QSslKey sslKey(key, QSsl::Rsa);
if (sslKey.isNull())
sslKey = QSslKey(key, QSsl::Ec);
const QList<QSslCertificate> certs = QSslCertificate::fromData(certificates);
const bool areCertsValid = !certs.empty() && std::all_of(certs.begin(), certs.end(), [](const QSslCertificate &c) { return !c.isNull(); });
if (!sslKey.isNull() && areCertsValid)
{
m_key = sslKey;
m_certificates = certs;
m_https = true;
return true;
}
else
{
disableHttps();
return false;
}
}
void Server::disableHttps()
{
m_https = false;
m_certificates.clear();
m_key.clear();
}
QList<QSslCipher> Server::safeCipherList() const QList<QSslCipher> Server::safeCipherList() const
{ {
const QStringList badCiphers = {"idea", "rc4"}; const QStringList badCiphers = {"idea", "rc4"};

8
src/base/http/server.h

@ -34,6 +34,7 @@
#define HTTP_SERVER_H #define HTTP_SERVER_H
#include <QTcpServer> #include <QTcpServer>
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
#include <QSslCertificate> #include <QSslCertificate>
#include <QSslCipher> #include <QSslCipher>
@ -55,17 +56,18 @@ namespace Http
~Server(); ~Server();
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void enableHttps(const QList<QSslCertificate> &certificates, const QSslKey &key); bool setupHttps(const QByteArray &certificates, const QByteArray &key);
void disableHttps(); void disableHttps();
#endif #endif
private: private:
IRequestHandler *m_requestHandler;
void incomingConnection(qintptr socketDescriptor); void incomingConnection(qintptr socketDescriptor);
IRequestHandler *m_requestHandler;
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
QList<QSslCipher> safeCipherList() const; QList<QSslCipher> safeCipherList() const;
bool m_https; bool m_https;
QList<QSslCertificate> m_certificates; QList<QSslCertificate> m_certificates;
QSslKey m_key; QSslKey m_key;

89
src/webui/webui.cpp

@ -45,78 +45,79 @@ WebUI::WebUI(QObject *parent)
void WebUI::init() void WebUI::init()
{ {
Preferences* const pref = Preferences::instance();
Logger* const logger = Logger::instance(); Logger* const logger = Logger::instance();
Preferences* const pref = Preferences::instance();
const quint16 oldPort = m_port;
m_port = pref->getWebUiPort();
if (pref->isWebUiEnabled()) { if (pref->isWebUiEnabled()) {
const quint16 port = pref->getWebUiPort(); // UPnP/NAT-PMP
if (m_port != port) { if (pref->useUPnPForWebUIPort()) {
Net::PortForwarder::instance()->deletePort(port); if (m_port != oldPort) {
m_port = port; Net::PortForwarder::instance()->deletePort(oldPort);
Net::PortForwarder::instance()->addPort(m_port);
}
}
else {
Net::PortForwarder::instance()->deletePort(oldPort);
} }
if (httpServer_) { // http server
if (httpServer_->serverPort() != m_port) if (!m_httpServer) {
httpServer_->close(); m_webapp = new WebApplication(this);
m_httpServer = new Http::Server(m_webapp, this);
} }
else { else {
webapp_ = new WebApplication(this); if (m_httpServer->serverPort() != m_port)
httpServer_ = new Http::Server(webapp_, this); m_httpServer->close();
} }
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
if (pref->isWebUiHttpsEnabled()) { if (pref->isWebUiHttpsEnabled()) {
const QByteArray keyRaw = pref->getWebUiHttpsKey(); const QByteArray certs = pref->getWebUiHttpsCertificate();
QSslKey key(keyRaw, QSsl::Rsa); const QByteArray key = pref->getWebUiHttpsKey();
if (key.isNull()) bool success = m_httpServer->setupHttps(certs, key);
key = QSslKey(keyRaw, QSsl::Ec); if (success)
logger->addMessage(tr("Web UI: https setup successful"));
const QList<QSslCertificate> certs = QSslCertificate::fromData(pref->getWebUiHttpsCertificate());
const bool areCertsValid = !certs.empty() && std::all_of(certs.begin(), certs.end(), [](QSslCertificate c) { return !c.isNull(); });
if (!key.isNull() && areCertsValid)
httpServer_->enableHttps(certs, key);
else else
httpServer_->disableHttps(); logger->addMessage(tr("Web UI: https setup failed, fallback to http"), Log::CRITICAL);
} }
else { else {
httpServer_->disableHttps(); m_httpServer->disableHttps();
} }
#endif #endif
if (!httpServer_->isListening()) { if (!m_httpServer->isListening()) {
bool success = httpServer_->listen(QHostAddress::Any, m_port); bool success = m_httpServer->listen(QHostAddress::Any, m_port);
if (success) if (success)
logger->addMessage(tr("The Web UI is listening on port %1").arg(m_port)); logger->addMessage(tr("Web UI: Now listening on port %1").arg(m_port));
else else
logger->addMessage(tr("Web UI Error - Unable to bind Web UI to port %1").arg(m_port), Log::CRITICAL); logger->addMessage(tr("Web UI: Unable to bind to port %1").arg(m_port), Log::CRITICAL);
} }
// DynDNS // DynDNS
if (pref->isDynDNSEnabled()) { if (pref->isDynDNSEnabled()) {
if (!dynDNSUpdater_) if (!m_dnsUpdater)
dynDNSUpdater_ = new Net::DNSUpdater(this); m_dnsUpdater = new Net::DNSUpdater(this);
else else
dynDNSUpdater_->updateCredentials(); m_dnsUpdater->updateCredentials();
} }
else { else {
if (dynDNSUpdater_) if (m_dnsUpdater)
delete dynDNSUpdater_; delete m_dnsUpdater;
} }
// Use UPnP/NAT-PMP for Web UI
if (pref->useUPnPForWebUIPort())
Net::PortForwarder::instance()->addPort(m_port);
else
Net::PortForwarder::instance()->deletePort(m_port);
} }
else { else {
if (httpServer_) Net::PortForwarder::instance()->deletePort(oldPort);
delete httpServer_;
if (webapp_) if (m_httpServer)
delete webapp_; delete m_httpServer;
if (dynDNSUpdater_)
delete dynDNSUpdater_; if (m_webapp)
Net::PortForwarder::instance()->deletePort(m_port); delete m_webapp;
if (m_dnsUpdater)
delete m_dnsUpdater;
} }
} }

6
src/webui/webui.h

@ -55,9 +55,9 @@ private slots:
void init(); void init();
private: private:
QPointer<Http::Server> httpServer_; QPointer<Http::Server> m_httpServer;
QPointer<Net::DNSUpdater> dynDNSUpdater_; QPointer<Net::DNSUpdater> m_dnsUpdater;
QPointer<AbstractWebApplication> webapp_; QPointer<AbstractWebApplication> m_webapp;
quint16 m_port; quint16 m_port;
}; };

Loading…
Cancel
Save