From 481cddb8693bf7c3dc8664f00bf396626bd50a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 16 Aug 2018 19:20:48 +0200 Subject: [PATCH] Update configurations and add monitor socket --- libnymea-remoteproxy/engine.cpp | 4 +- libnymea-remoteproxy/monitorserver.cpp | 2 +- libnymea-remoteproxy/proxyconfiguration.cpp | 58 +++++++++++++------ libnymea-remoteproxy/proxyconfiguration.h | 10 +++- monitor/main.cpp | 2 +- nymea-remoteproxy.conf | 5 +- tests/resources/test-configuration.conf | 5 ++ .../nymea-remoteproxy-tests-offline.cpp | 8 ++- 8 files changed, 67 insertions(+), 27 deletions(-) diff --git a/libnymea-remoteproxy/engine.cpp b/libnymea-remoteproxy/engine.cpp index db02b1f..5bee31e 100644 --- a/libnymea-remoteproxy/engine.cpp +++ b/libnymea-remoteproxy/engine.cpp @@ -59,7 +59,7 @@ void Engine::start(ProxyConfiguration *configuration) qCDebug(dcEngine()) << "Starting proxy server"; m_proxyServer->startServer(); - m_monitorServer = new MonitorServer("/tmp/nymea-remoteproxy-monitor.socket", this); + m_monitorServer = new MonitorServer(configuration->monitorSocketFileName(), this); m_monitorServer->startServer(); // Set tunning true in the next event loop @@ -169,7 +169,9 @@ void Engine::setRunning(bool running) if (m_running == running) return; + qCDebug(dcEngine()) << "----------------------------------------------------------"; qCDebug(dcEngine()) << "Engine is" << (running ? "now running." : "not running any more."); + qCDebug(dcEngine()) << "----------------------------------------------------------"; m_running = running; emit runningChanged(m_running); } diff --git a/libnymea-remoteproxy/monitorserver.cpp b/libnymea-remoteproxy/monitorserver.cpp index 1f042fc..20d99ea 100644 --- a/libnymea-remoteproxy/monitorserver.cpp +++ b/libnymea-remoteproxy/monitorserver.cpp @@ -103,7 +103,7 @@ void MonitorServer::stopServer() } m_server->close(); - m_server->deleteLater(); + delete m_server; m_server = nullptr; } diff --git a/libnymea-remoteproxy/proxyconfiguration.cpp b/libnymea-remoteproxy/proxyconfiguration.cpp index 217272b..87f8dfe 100644 --- a/libnymea-remoteproxy/proxyconfiguration.cpp +++ b/libnymea-remoteproxy/proxyconfiguration.cpp @@ -16,26 +16,32 @@ ProxyConfiguration::ProxyConfiguration(QObject *parent) : bool ProxyConfiguration::loadConfiguration(const QString &fileName) { - QFileInfo fileInfo(fileName); + m_fileName = fileName; + QFileInfo fileInfo(m_fileName); if (!fileInfo.exists()) { - qCWarning(dcApplication()) << "Configuration: Could not find configuration file" << fileName; + qCWarning(dcApplication()) << "Configuration: Could not find configuration file" << m_fileName; return false; } if (!fileInfo.isReadable()) { - qCWarning(dcApplication()) << "Configuration: Cannot read configuration file" << fileName; + qCWarning(dcApplication()) << "Configuration: Cannot read configuration file" << m_fileName; return false; } - QSettings settings(fileName, QSettings::IniFormat); + QSettings settings(m_fileName, QSettings::IniFormat); + settings.beginGroup("ProxyServer"); setServerName(settings.value("name", "nymea-remoteproxy").toString()); setWriteLogFile(settings.value("writeLogs", false).toBool()); setLogFileName(settings.value("logFile", "/var/log/nymea-remoteproxy.log").toString()); setMonitorSocketFileName(settings.value("monitorSocket", "/tmp/nymea-remoteproxy.monitor").toString()); + settings.endGroup(); + + settings.beginGroup("SSL"); setSslCertificateFileName(settings.value("certificate", "/etc/ssl/certs/ssl-cert-snakeoil.pem").toString()); setSslCertificateKeyFileName(settings.value("certificateKey", "/etc/ssl/private/ssl-cert-snakeoil.key").toString()); setSslCertificateChainFileName(settings.value("certificateChain", "").toString()); + settings.endGroup(); settings.beginGroup("WebSocketServer"); setWebSocketServerHost(QHostAddress(settings.value("host", "127.0.0.1").toString())); @@ -92,6 +98,11 @@ bool ProxyConfiguration::loadConfiguration(const QString &fileName) return true; } +QString ProxyConfiguration::fileName() const +{ + return m_fileName; +} + QString ProxyConfiguration::serverName() const { return m_serverName; @@ -210,14 +221,24 @@ void ProxyConfiguration::setTcpServerPort(quint16 port) QDebug operator<<(QDebug debug, ProxyConfiguration *configuration) { debug.nospace() << endl << "========== ProxyConfiguration ==========" << endl; - debug.nospace() << "General" << endl; - debug.nospace() << " - name:" << configuration->serverName() << endl; - debug.nospace() << " - write logfile:" << configuration->writeLogFile() << endl; - debug.nospace() << " - logfile:" << configuration->logFileName() << endl; - debug.nospace() << " - certificate:" << configuration->sslCertificateFileName() << endl; - debug.nospace() << " - certificate key:" << configuration->sslCertificateKeyFileName() << endl; - debug.nospace() << " - certificate chain:" << configuration->sslCertificateChainFileName() << endl; - debug.nospace() << " - SSL certificate information:"; + debug.nospace() << "Configuration file:" << configuration->fileName() << endl; + debug.nospace() << "RemoteProxy configuration" << endl; + debug.nospace() << " - Server name:" << configuration->serverName() << endl; + debug.nospace() << " - Write logfile:" << configuration->writeLogFile() << endl; + debug.nospace() << " - Logfile:" << configuration->logFileName() << endl; + debug.nospace() << "SSL configuration" << endl; + debug.nospace() << " - Certificate:" << configuration->sslCertificateFileName() << endl; + debug.nospace() << " - Certificate key:" << configuration->sslCertificateKeyFileName() << endl; + debug.nospace() << " - Certificate chain:" << configuration->sslCertificateChainFileName() << endl; + debug.nospace() << " - SSL certificate information:" << endl; + debug.nospace() << " Common name:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::CommonName) << endl; + debug.nospace() << " Organisation:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::Organization) << endl; + debug.nospace() << " Organisation unit name:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::OrganizationalUnitName) << endl; + debug.nospace() << " Country name:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::CountryName) << endl; + debug.nospace() << " Locality name:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::LocalityName) << endl; + debug.nospace() << " State/Province:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::StateOrProvinceName) << endl; + debug.nospace() << " Email address:" << configuration->sslConfiguration().localCertificate().subjectInfo(QSslCertificate::EmailAddress) << endl; + debug.nospace() << " - SSL certificate issuer information:" << endl; debug.nospace() << " Common name:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::CommonName) << endl; debug.nospace() << " Organisation:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::Organization) << endl; debug.nospace() << " Organisation unit name:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::OrganizationalUnitName) << endl; @@ -225,14 +246,13 @@ QDebug operator<<(QDebug debug, ProxyConfiguration *configuration) debug.nospace() << " Locality name:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::LocalityName) << endl; debug.nospace() << " State/Province:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::StateOrProvinceName) << endl; debug.nospace() << " Email address:" << configuration->sslConfiguration().localCertificate().issuerInfo(QSslCertificate::EmailAddress) << endl; - debug.nospace() << "WebSocketServer" << endl; - debug.nospace() << " - host:" << configuration->webSocketServerHost().toString() << endl; - debug.nospace() << " - port:" << configuration->webSocketServerPort() << endl; + debug.nospace() << "WebSocketServer configuration" << endl; + debug.nospace() << " - Host:" << configuration->webSocketServerHost().toString() << endl; + debug.nospace() << " - Port:" << configuration->webSocketServerPort() << endl; debug.nospace() << "TcpServer" << endl; - debug.nospace() << " - host:" << configuration->tcpServerHost().toString() << endl; - debug.nospace() << " - port:" << configuration->tcpServerPort() << endl; - debug.nospace() << "========== ProxyConfiguration ==========" << endl; - + debug.nospace() << " - Host:" << configuration->tcpServerHost().toString() << endl; + debug.nospace() << " - Port:" << configuration->tcpServerPort() << endl; + debug.nospace() << "========== ProxyConfiguration =========="; return debug; } diff --git a/libnymea-remoteproxy/proxyconfiguration.h b/libnymea-remoteproxy/proxyconfiguration.h index f8cb629..98311ff 100644 --- a/libnymea-remoteproxy/proxyconfiguration.h +++ b/libnymea-remoteproxy/proxyconfiguration.h @@ -16,7 +16,9 @@ public: bool loadConfiguration(const QString &fileName); - // General + QString fileName() const; + + // ProxyServer QString serverName() const; void setServerName(const QString &serverName); @@ -29,6 +31,7 @@ public: QString monitorSocketFileName() const; void setMonitorSocketFileName(const QString &fileName); + // Ssl QString sslCertificateFileName() const; void setSslCertificateFileName(const QString &fileName); @@ -55,11 +58,14 @@ public: void setTcpServerPort(quint16 port); private: - // General + // ProxyServer + QString m_fileName; QString m_serverName; bool m_writeLogFile = false; QString m_logFileName = "/var/log/nymea-remoteproxy.log"; QString m_monitorSocketFileName; + + // Ssl QString m_sslCertificateFileName = "/etc/ssl/certs/ssl-cert-snakeoil.pem"; QString m_sslCertificateKeyFileName = "/etc/ssl/private/ssl-cert-snakeoil.key"; QString m_sslCertificateChainFileName; diff --git a/monitor/main.cpp b/monitor/main.cpp index 4012f9b..f87f754 100644 --- a/monitor/main.cpp +++ b/monitor/main.cpp @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) - QCommandLineOption tokenOption(QStringList() << "s" << "socket", "The AWS token for authentication.", "socket"); + QCommandLineOption tokenOption(QStringList() << "s" << "socket", "The AWS token for authentication. Default /tmp/", "socket"); parser.addOption(tokenOption); parser.process(application); diff --git a/nymea-remoteproxy.conf b/nymea-remoteproxy.conf index 15ee723..8a29ce7 100644 --- a/nymea-remoteproxy.conf +++ b/nymea-remoteproxy.conf @@ -1,7 +1,10 @@ +[ProxyServer] name=nymea-remoteproxy writeLogs=false logFile=/var/log/nymea-remoteproxy.log -monitorSocket=/tmp/nymea-remoteproxy.monitor +monitorSocket=/tmp/nymea-remoteproxy-monitor.sock + +[SSL] certificate=/etc/ssl/certs/ssl-cert-snakeoil.pem certificateKey=/etc/ssl/private/ssl-cert-snakeoil.key certificateChain= diff --git a/tests/resources/test-configuration.conf b/tests/resources/test-configuration.conf index d6d4888..9351534 100644 --- a/tests/resources/test-configuration.conf +++ b/tests/resources/test-configuration.conf @@ -1,8 +1,13 @@ +[ProxyServer] name=test-nymea-remoteproxy writeLogs=false logFile=/var/log/nymea-remoteproxy.log +monitorSocket=/tmp/nymea-remoteproxy-test.sock + +[SSL] certificate=:/test-certificate.crt certificateKey=:/test-certificate.key +certificateChain= [WebSocketServer] host=127.0.0.1 diff --git a/tests/test-offline/nymea-remoteproxy-tests-offline.cpp b/tests/test-offline/nymea-remoteproxy-tests-offline.cpp index 481d122..6df34c2 100644 --- a/tests/test-offline/nymea-remoteproxy-tests-offline.cpp +++ b/tests/test-offline/nymea-remoteproxy-tests-offline.cpp @@ -55,12 +55,16 @@ void RemoteProxyOfflineTests::dummyAuthenticator() void RemoteProxyOfflineTests::monitorServer() { startServer(); - QVERIFY(Engine::instance()->monitorServer()->running()); + + + stopServer(); } void RemoteProxyOfflineTests::webserverConnectionBlocked() { + cleanUpEngine(); + // Create a dummy server which blocks the port QWebSocketServer dummyServer("dummy-server", QWebSocketServer::NonSecureMode); dummyServer.listen(QHostAddress::LocalHost, 1212); @@ -70,7 +74,7 @@ void RemoteProxyOfflineTests::webserverConnectionBlocked() Engine::instance()->setAuthenticator(m_authenticator); Engine::instance()->start(m_configuration); runningSpy.wait(); - + qDebug() << runningSpy.count(); QVERIFY(runningSpy.count() == 1); // Make sure the server is not running