diff --git a/libnymea-remoteproxy/engine.cpp b/libnymea-remoteproxy/engine.cpp index a8a5249..19fd336 100644 --- a/libnymea-remoteproxy/engine.cpp +++ b/libnymea-remoteproxy/engine.cpp @@ -69,38 +69,43 @@ void Engine::start(ProxyConfiguration *configuration) m_configuration = configuration; qCDebug(dcEngine()) << "Using configuration" << m_configuration; - // Make sure an authenticator was registered - Q_ASSERT_X(m_authenticator != nullptr, "Engine", "There is no authenticator registerd."); + // TODO: the old proxy server is deprecated. Will be removed in future releases + if (configuration->proxyEnabled()) { + // Make sure an authenticator was registered + Q_ASSERT_X(m_authenticator != nullptr, "Engine", "There is no authenticator registerd."); - // Proxy - // ------------------------------------- - m_proxyServer = new ProxyServer(this); - m_webSocketServerProxy = new WebSocketServer(m_configuration->sslEnabled(), m_configuration->sslConfiguration(), this); - m_tcpSocketServerProxy = new TcpSocketServer(m_configuration->sslEnabled(), m_configuration->sslConfiguration(), this); - m_unixSocketServerProxy = new UnixSocketServer(m_configuration->unixSocketFileName(), this); + // Proxy + // ------------------------------------- + m_proxyServer = new ProxyServer(this); + m_webSocketServerProxy = new WebSocketServer(m_configuration->sslEnabled(), m_configuration->sslConfiguration(), this); + m_tcpSocketServerProxy = new TcpSocketServer(m_configuration->sslEnabled(), m_configuration->sslConfiguration(), this); + m_unixSocketServerProxy = new UnixSocketServer(m_configuration->unixSocketFileName(), this); - // Configure websocket server - QUrl websocketServerUrl; - websocketServerUrl.setScheme(m_configuration->sslEnabled() ? "wss" : "ws"); - websocketServerUrl.setHost(m_configuration->webSocketServerProxyHost().toString()); - websocketServerUrl.setPort(m_configuration->webSocketServerProxyPort()); - m_webSocketServerProxy->setServerUrl(websocketServerUrl); + // Configure websocket server + QUrl websocketServerUrl; + websocketServerUrl.setScheme(m_configuration->sslEnabled() ? "wss" : "ws"); + websocketServerUrl.setHost(m_configuration->webSocketServerProxyHost().toString()); + websocketServerUrl.setPort(m_configuration->webSocketServerProxyPort()); + m_webSocketServerProxy->setServerUrl(websocketServerUrl); - // Configure tcp socket server - QUrl tcpSocketServerProxyUrl; - tcpSocketServerProxyUrl.setScheme(m_configuration->sslEnabled() ? "ssl" : "tcp"); - tcpSocketServerProxyUrl.setHost(m_configuration->tcpServerHost().toString()); - tcpSocketServerProxyUrl.setPort(m_configuration->tcpServerPort()); - m_tcpSocketServerProxy->setServerUrl(tcpSocketServerProxyUrl); + // Configure tcp socket server + QUrl tcpSocketServerProxyUrl; + tcpSocketServerProxyUrl.setScheme(m_configuration->sslEnabled() ? "ssl" : "tcp"); + tcpSocketServerProxyUrl.setHost(m_configuration->tcpServerHost().toString()); + tcpSocketServerProxyUrl.setPort(m_configuration->tcpServerPort()); + m_tcpSocketServerProxy->setServerUrl(tcpSocketServerProxyUrl); - // Register the transport interfaces in the proxy server - m_proxyServer->registerTransportInterface(m_webSocketServerProxy); - m_proxyServer->registerTransportInterface(m_tcpSocketServerProxy); - m_proxyServer->registerTransportInterface(m_unixSocketServerProxy); + // Register the transport interfaces in the proxy server + m_proxyServer->registerTransportInterface(m_webSocketServerProxy); + m_proxyServer->registerTransportInterface(m_tcpSocketServerProxy); + m_proxyServer->registerTransportInterface(m_unixSocketServerProxy); - // Start the server - qCDebug(dcEngine()) << "Starting the proxy servers..."; - m_proxyServer->startServer(); + // Start the server + qCDebug(dcEngine()) << "Starting the proxy servers..."; + m_proxyServer->startServer(); + } else { + qCDebug(dcEngine()) << "Proxy server disabled. Not starting proxy server."; + } // Tunnel proxy // ------------------------------------- @@ -263,7 +268,9 @@ QVariantMap Engine::createServerStatistic() monitorData.insert("serverName", m_configuration->serverName()); monitorData.insert("serverVersion", SERVER_VERSION_STRING); monitorData.insert("apiVersion", API_VERSION_STRING); - monitorData.insert("proxyStatistic", proxyServer()->currentStatistics()); + if (m_proxyServer) { + monitorData.insert("proxyStatistic", m_proxyServer->currentStatistics()); + } monitorData.insert("tunnelProxyStatistic", tunnelProxyServer()->currentStatistics()); return monitorData; } @@ -277,7 +284,8 @@ void Engine::onTimerTick() m_currentTimeCounter += deltaTime; if (m_currentTimeCounter >= 1000) { // One second passed, do second tick - m_proxyServer->tick(); + if (m_proxyServer) + m_proxyServer->tick(); QVariantMap serverStatistics = createServerStatistic(); m_monitorServer->updateClients(serverStatistics); diff --git a/libnymea-remoteproxy/proxyconfiguration.cpp b/libnymea-remoteproxy/proxyconfiguration.cpp index 96b576b..7bd4534 100644 --- a/libnymea-remoteproxy/proxyconfiguration.cpp +++ b/libnymea-remoteproxy/proxyconfiguration.cpp @@ -54,6 +54,7 @@ bool ProxyConfiguration::loadConfiguration(const QString &fileName) settings.beginGroup("ProxyServer"); setServerName(settings.value("name", "nymea-remoteproxy").toString()); + setProxyEnabled(settings.value("proxyEnabled", true).toBool()); setWriteLogFile(settings.value("writeLogs", false).toBool()); setLogFileName(settings.value("logFile", "/var/log/nymea-remoteproxy.log").toString()); setLogEngineEnabled(settings.value("logEngineEnabled", false).toBool()); @@ -241,6 +242,16 @@ void ProxyConfiguration::setAloneTimeout(int timeout) m_aloneTimeout = timeout; } +bool ProxyConfiguration::proxyEnabled() const +{ + return m_proxyEnabled; +} + +void ProxyConfiguration::setProxyEnabled(bool proxyEnabled) +{ + m_proxyEnabled = proxyEnabled; +} + QString ProxyConfiguration::awsRegion() const { return m_awsRegion; @@ -419,10 +430,6 @@ QDebug operator<<(QDebug debug, ProxyConfiguration *configuration) debug.nospace() << " - Authentication timeout:" << configuration->authenticationTimeout() << " [ms]" << endl; debug.nospace() << " - Inactive timeout:" << configuration->inactiveTimeout() << " [ms]" << endl; debug.nospace() << " - Alone timeout:" << configuration->aloneTimeout() << " [ms]" << endl; - debug.nospace() << "AWS configuration" << endl; - debug.nospace() << " - Region:" << configuration->awsRegion() << endl; - debug.nospace() << " - Authorizer lambda function:" << configuration->awsAuthorizerLambdaFunctionName() << endl; - debug.nospace() << " - Credentials URL:" << configuration->awsCredentialsUrl().toString() << endl; debug.nospace() << "SSL configuration" << endl; debug.nospace() << " - Enabled:" << configuration->sslEnabled() << endl; debug.nospace() << " - Certificate:" << configuration->sslCertificateFileName() << endl; @@ -444,14 +451,22 @@ 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 Proxy" << endl; - debug.nospace() << " - Host:" << configuration->webSocketServerProxyHost().toString() << endl; - debug.nospace() << " - Port:" << configuration->webSocketServerProxyPort() << endl; - debug.nospace() << "TcpServer Proxy" << endl; - debug.nospace() << " - Host:" << configuration->tcpServerHost().toString() << endl; - debug.nospace() << " - Port:" << configuration->tcpServerPort() << endl; - debug.nospace() << "UnixSocketServer Proxy" << endl; - debug.nospace() << " - Filename:" << configuration->unixSocketFileName() << endl; + if (configuration->proxyEnabled()) { + debug.nospace() << "AWS configuration" << endl; + debug.nospace() << " - Region:" << configuration->awsRegion() << endl; + debug.nospace() << " - Authorizer lambda function:" << configuration->awsAuthorizerLambdaFunctionName() << endl; + debug.nospace() << " - Credentials URL:" << configuration->awsCredentialsUrl().toString() << endl; + debug.nospace() << "WebSocketServer Proxy" << endl; + debug.nospace() << " - Host:" << configuration->webSocketServerProxyHost().toString() << endl; + debug.nospace() << " - Port:" << configuration->webSocketServerProxyPort() << endl; + debug.nospace() << "TcpServer Proxy" << endl; + debug.nospace() << " - Host:" << configuration->tcpServerHost().toString() << endl; + debug.nospace() << " - Port:" << configuration->tcpServerPort() << endl; + debug.nospace() << "UnixSocketServer Proxy" << endl; + debug.nospace() << " - Filename:" << configuration->unixSocketFileName() << endl; + } else { + debug.nospace() << "Proxy Server: disabled" << endl; + } debug.nospace() << "WebSocketServer TunnelProxy" << endl; debug.nospace() << " - Host:" << configuration->webSocketServerTunnelProxyHost().toString() << endl; debug.nospace() << " - Port:" << configuration->webSocketServerTunnelProxyPort() << endl; diff --git a/libnymea-remoteproxy/proxyconfiguration.h b/libnymea-remoteproxy/proxyconfiguration.h index 0f8fef5..0a9e3a7 100644 --- a/libnymea-remoteproxy/proxyconfiguration.h +++ b/libnymea-remoteproxy/proxyconfiguration.h @@ -74,6 +74,9 @@ public: int aloneTimeout() const; void setAloneTimeout(int timeout); + bool proxyEnabled() const; + void setProxyEnabled(bool proxyEnabled); + // AWS QString awsRegion() const; void setAwsRegion(const QString ®ion); @@ -145,6 +148,8 @@ private: int m_inactiveTimeout = 8000; int m_aloneTimeout = 8000; + bool m_proxyEnabled = true; + // AWS QString m_awsRegion; QString m_awsAuthorizerLambdaFunctionName; diff --git a/libnymea-remoteproxy/server/tcpsocketserver.cpp b/libnymea-remoteproxy/server/tcpsocketserver.cpp index d164c00..be0b2ab 100644 --- a/libnymea-remoteproxy/server/tcpsocketserver.cpp +++ b/libnymea-remoteproxy/server/tcpsocketserver.cpp @@ -64,10 +64,8 @@ void TcpSocketServer::killClientConnection(const QUuid &clientId, const QString if (!client) return; - if (client->state() == QAbstractSocket::ConnectedState) { - qCWarning(dcTcpSocketServer()) << "Killing client connection" << clientId.toString() << "Reason:" << killReason; - client->close(); - } + qCDebug(dcTcpSocketServer()) << "Killing client connection" << clientId.toString() << "Reason:" << killReason; + client->close(); } bool TcpSocketServer::running() const diff --git a/server/main.cpp b/server/main.cpp index a7d863e..17374bb 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -207,13 +207,14 @@ int main(int argc, char *argv[]) if (s_loggingEnabled) qCDebug(dcApplication()) << "Logging enabled. Writing logs to" << s_logFile.fileName(); - Authenticator *authenticator = nullptr; if (parser.isSet(mockAuthenticatorOption)) { authenticator = qobject_cast(new DummyAuthenticator(nullptr)); } else { - // Create default authenticator - authenticator = qobject_cast(new AwsAuthenticator(configuration->awsCredentialsUrl(), nullptr)); + if (configuration->proxyEnabled()) { + // Create default authenticator + authenticator = qobject_cast(new AwsAuthenticator(configuration->awsCredentialsUrl(), nullptr)); + } } // Configure and start the engines