From ecf58505d1fd4ff5fdf7908bce4036d2e74c2fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 13 Feb 2023 02:21:11 +0100 Subject: [PATCH] Introduce inactive timeout for tunnel proxy clients --- libnymea-remoteproxy/engine.cpp | 2 +- .../tunnelproxy/tunnelproxyclient.cpp | 16 +++++++++++++++- .../tunnelproxy/tunnelproxyclient.h | 4 ++++ .../tunnelproxy/tunnelproxyserver.cpp | 8 ++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/libnymea-remoteproxy/engine.cpp b/libnymea-remoteproxy/engine.cpp index cea1334..7d6ca85 100644 --- a/libnymea-remoteproxy/engine.cpp +++ b/libnymea-remoteproxy/engine.cpp @@ -289,13 +289,13 @@ void Engine::onTimerTick() QVariantMap serverStatistics = createServerStatistic(); m_monitorServer->updateClients(serverStatistics); + m_logEngine->logStatistics(serverStatistics.value("proxyStatistic").toMap().value("tunnelCount").toInt(), serverStatistics.value("proxyStatistic").toMap().value("clientCount").toInt(), serverStatistics.value("proxyStatistic").toMap().value("troughput").toInt()); m_currentTimeCounter = 0; - if (m_tunnelProxyServer) { m_tunnelProxyServer->tick(); } diff --git a/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.cpp b/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.cpp index f9bb7fb..6663c0b 100644 --- a/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.cpp +++ b/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.cpp @@ -1,6 +1,7 @@ #include "tunnelproxyclient.h" #include "loggingcategories.h" #include "server/transportinterface.h" +#include "../engine.h" #include "../common/slipdataprocessor.h" namespace remoteproxy { @@ -8,7 +9,15 @@ namespace remoteproxy { TunnelProxyClient::TunnelProxyClient(TransportInterface *interface, const QUuid &clientId, const QHostAddress &address, QObject *parent) : TransportClient(interface, clientId, address, parent) { - + // Note: a client is not inactive any more once registered successfully as client or server. + // This makes sure we have not any inactive sockets connected to the proxy blocking resources. + // The tunnelproxy server will call makeClientActive once registered successfuly to stop this timer. + m_inactiveTimer.setInterval(Engine::instance()->configuration()->inactiveTimeout()); + connect(&m_inactiveTimer, &QTimer::timeout, this, [this](){ + m_interface->killClientConnection(m_clientId, "Tunnelproxy timeout occurred. The socket was inactive."); + }); + m_inactiveTimer.setSingleShot(true); + m_inactiveTimer.start(); } TunnelProxyClient::Type TunnelProxyClient::type() const @@ -69,6 +78,11 @@ QList TunnelProxyClient::processData(const QByteArray &data) return packets; } +void TunnelProxyClient::makeClientActive() +{ + m_inactiveTimer.stop(); +} + QDebug operator<<(QDebug debug, TunnelProxyClient *tunnelProxyClient) { debug.nospace() << "TunnelProxyClient("; diff --git a/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.h b/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.h index 832be0a..b772b3c 100644 --- a/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.h +++ b/libnymea-remoteproxy/tunnelproxy/tunnelproxyclient.h @@ -2,6 +2,7 @@ #define TUNNELPROXYCLIENT_H #include +#include #include "server/transportclient.h" @@ -26,10 +27,13 @@ public: // Json server methods QList processData(const QByteArray &data) override; + void makeClientActive(); + signals: void typeChanged(Type type); private: + QTimer m_inactiveTimer; Type m_type = TypeNone; }; diff --git a/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp b/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp index 735942b..dedef2f 100644 --- a/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp +++ b/libnymea-remoteproxy/tunnelproxy/tunnelproxyserver.cpp @@ -98,6 +98,10 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerServer(const QUui return TunnelProxyServer::TunnelProxyErrorAlreadyRegistered; } + // This client has been registered successfully. + // Make sure it does not get disconnected any more because due to inactivity. + tunnelProxyClient->makeClientActive(); + tunnelProxyClient->setType(TunnelProxyClient::TypeServer); tunnelProxyClient->setUuid(serverUuid); tunnelProxyClient->setName(serverName); @@ -140,6 +144,10 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerClient(const QUui return TunnelProxyServer::TunnelProxyErrorServerNotFound; } + // This client has been registered successfully. + // Make sure it does not get disconnected any more because due to inactivity. + tunnelProxyClient->makeClientActive(); + // Not registered yet, we have a connected server for the requested server uuid tunnelProxyClient->setType(TunnelProxyClient::TypeClient); tunnelProxyClient->setUuid(clientUuid);