|
|
|
@ -45,78 +45,79 @@ WebUI::WebUI(QObject *parent)
@@ -45,78 +45,79 @@ WebUI::WebUI(QObject *parent)
|
|
|
|
|
|
|
|
|
|
void WebUI::init() |
|
|
|
|
{ |
|
|
|
|
Preferences* const pref = Preferences::instance(); |
|
|
|
|
Logger* const logger = Logger::instance(); |
|
|
|
|
Preferences* const pref = Preferences::instance(); |
|
|
|
|
|
|
|
|
|
const quint16 oldPort = m_port; |
|
|
|
|
m_port = pref->getWebUiPort(); |
|
|
|
|
|
|
|
|
|
if (pref->isWebUiEnabled()) { |
|
|
|
|
const quint16 port = pref->getWebUiPort(); |
|
|
|
|
if (m_port != port) { |
|
|
|
|
Net::PortForwarder::instance()->deletePort(port); |
|
|
|
|
m_port = port; |
|
|
|
|
// UPnP/NAT-PMP
|
|
|
|
|
if (pref->useUPnPForWebUIPort()) { |
|
|
|
|
if (m_port != oldPort) { |
|
|
|
|
Net::PortForwarder::instance()->deletePort(oldPort); |
|
|
|
|
Net::PortForwarder::instance()->addPort(m_port); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
Net::PortForwarder::instance()->deletePort(oldPort); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (httpServer_) { |
|
|
|
|
if (httpServer_->serverPort() != m_port) |
|
|
|
|
httpServer_->close(); |
|
|
|
|
// http server
|
|
|
|
|
if (!m_httpServer) { |
|
|
|
|
m_webapp = new WebApplication(this); |
|
|
|
|
m_httpServer = new Http::Server(m_webapp, this); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
webapp_ = new WebApplication(this); |
|
|
|
|
httpServer_ = new Http::Server(webapp_, this); |
|
|
|
|
if (m_httpServer->serverPort() != m_port) |
|
|
|
|
m_httpServer->close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifndef QT_NO_OPENSSL |
|
|
|
|
if (pref->isWebUiHttpsEnabled()) { |
|
|
|
|
const QByteArray keyRaw = pref->getWebUiHttpsKey(); |
|
|
|
|
QSslKey key(keyRaw, QSsl::Rsa); |
|
|
|
|
if (key.isNull()) |
|
|
|
|
key = QSslKey(keyRaw, QSsl::Ec); |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
const QByteArray certs = pref->getWebUiHttpsCertificate(); |
|
|
|
|
const QByteArray key = pref->getWebUiHttpsKey(); |
|
|
|
|
bool success = m_httpServer->setupHttps(certs, key); |
|
|
|
|
if (success) |
|
|
|
|
logger->addMessage(tr("Web UI: https setup successful")); |
|
|
|
|
else |
|
|
|
|
httpServer_->disableHttps(); |
|
|
|
|
logger->addMessage(tr("Web UI: https setup failed, fallback to http"), Log::CRITICAL); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
httpServer_->disableHttps(); |
|
|
|
|
m_httpServer->disableHttps(); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (!httpServer_->isListening()) { |
|
|
|
|
bool success = httpServer_->listen(QHostAddress::Any, m_port); |
|
|
|
|
if (!m_httpServer->isListening()) { |
|
|
|
|
bool success = m_httpServer->listen(QHostAddress::Any, m_port); |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
|
if (pref->isDynDNSEnabled()) { |
|
|
|
|
if (!dynDNSUpdater_) |
|
|
|
|
dynDNSUpdater_ = new Net::DNSUpdater(this); |
|
|
|
|
if (!m_dnsUpdater) |
|
|
|
|
m_dnsUpdater = new Net::DNSUpdater(this); |
|
|
|
|
else |
|
|
|
|
dynDNSUpdater_->updateCredentials(); |
|
|
|
|
m_dnsUpdater->updateCredentials(); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
if (dynDNSUpdater_) |
|
|
|
|
delete dynDNSUpdater_; |
|
|
|
|
if (m_dnsUpdater) |
|
|
|
|
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 { |
|
|
|
|
if (httpServer_) |
|
|
|
|
delete httpServer_; |
|
|
|
|
if (webapp_) |
|
|
|
|
delete webapp_; |
|
|
|
|
if (dynDNSUpdater_) |
|
|
|
|
delete dynDNSUpdater_; |
|
|
|
|
Net::PortForwarder::instance()->deletePort(m_port); |
|
|
|
|
Net::PortForwarder::instance()->deletePort(oldPort); |
|
|
|
|
|
|
|
|
|
if (m_httpServer) |
|
|
|
|
delete m_httpServer; |
|
|
|
|
|
|
|
|
|
if (m_webapp) |
|
|
|
|
delete m_webapp; |
|
|
|
|
|
|
|
|
|
if (m_dnsUpdater) |
|
|
|
|
delete m_dnsUpdater; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|