|
|
@ -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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|