From bffc750465de13a87143f59be8f468a8aecdcaf2 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 15 May 2021 13:18:48 +0300 Subject: [PATCH] UI: StatManagerRpc: Fix connections handling. --- src/ui/model/connlistmodel.cpp | 10 +--------- src/ui/model/connlistmodel.h | 2 -- src/ui/rpc/rpcmanager.cpp | 8 ++++++-- src/ui/rpc/statmanagerrpc.cpp | 15 ++++++++++++++- src/ui/rpc/statmanagerrpc.h | 3 +++ src/ui/stat/statmanager.cpp | 8 ++++++-- src/ui/stat/statmanager.h | 9 +++++++-- 7 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/ui/model/connlistmodel.cpp b/src/ui/model/connlistmodel.cpp index d82e36d7..47220aaf 100644 --- a/src/ui/model/connlistmodel.cpp +++ b/src/ui/model/connlistmodel.cpp @@ -61,17 +61,9 @@ void ConnListModel::handleLogBlockedIp(const LogEntryBlockedIp &entry, qint64 un doBeginInsertRows(row, row); - if (statManager()->logBlockedIp(entry, unixTime)) { - ++m_connBlockInc; - } + statManager()->logBlockedIp(entry, unixTime); doEndInsertRows(); - - constexpr int connBlockIncMax = 100; - if (m_connBlockInc >= connBlockIncMax) { - m_connBlockInc = 0; - statManager()->deleteOldConnBlock(); - } } int ConnListModel::columnCount(const QModelIndex &parent) const diff --git a/src/ui/model/connlistmodel.h b/src/ui/model/connlistmodel.h index d21531bc..3965d7b6 100644 --- a/src/ui/model/connlistmodel.h +++ b/src/ui/model/connlistmodel.h @@ -102,8 +102,6 @@ private: bool m_blockedMode = false; bool m_resolveAddress = false; - int m_connBlockInc = 999999999; // to trigger on first check - StatManager *m_statManager = nullptr; AppInfoCache *m_appInfoCache = nullptr; HostInfoCache *m_hostInfoCache = nullptr; diff --git a/src/ui/rpc/rpcmanager.cpp b/src/ui/rpc/rpcmanager.cpp index 1464f230..f14e4b80 100644 --- a/src/ui/rpc/rpcmanager.cpp +++ b/src/ui/rpc/rpcmanager.cpp @@ -459,10 +459,14 @@ bool RpcManager::processStatManagerRpc( args.value(0).toLongLong(), args.value(1).toUInt(), args.value(2).toUInt()); return true; case Control::Rpc_StatManager_connBlockAdded: - emit statManager()->connBlockAdded(); + if (auto sm = qobject_cast(statManager())) { + sm->onConnBlockAdded(); + } return true; case Control::Rpc_StatManager_connRemoved: - emit statManager()->connRemoved(); + if (auto sm = qobject_cast(statManager())) { + sm->onConnRemoved(); + } return true; case Control::Rpc_StatManager_appTrafTotalsResetted: emit statManager()->appTrafTotalsResetted(); diff --git a/src/ui/rpc/statmanagerrpc.cpp b/src/ui/rpc/statmanagerrpc.cpp index aa86d117..da4984cf 100644 --- a/src/ui/rpc/statmanagerrpc.cpp +++ b/src/ui/rpc/statmanagerrpc.cpp @@ -6,7 +6,8 @@ #include "../rpc/rpcmanager.h" StatManagerRpc::StatManagerRpc(const QString &filePath, FortManager *fortManager, QObject *parent) : - StatManager(filePath, fortManager->quotaManager(), parent, SqliteDb::OpenDefaultReadOnly) + StatManager(filePath, fortManager->quotaManager(), parent, SqliteDb::OpenDefaultReadOnly), + m_fortManager(fortManager) { } @@ -39,3 +40,15 @@ bool StatManagerRpc::clearTraffic() { return rpcManager()->doOnServer(Control::Rpc_StatManager_clearTraffic); } + +void StatManagerRpc::onConnBlockAdded() +{ + setupConnBlockId(); + emit connBlockAdded(); +} + +void StatManagerRpc::onConnRemoved() +{ + setupConnBlockId(); + emit connRemoved(); +} diff --git a/src/ui/rpc/statmanagerrpc.h b/src/ui/rpc/statmanagerrpc.h index b5379da3..157da1bc 100644 --- a/src/ui/rpc/statmanagerrpc.h +++ b/src/ui/rpc/statmanagerrpc.h @@ -29,6 +29,9 @@ public: public slots: bool clearTraffic() override; + void onConnBlockAdded(); + void onConnRemoved(); + private: FortManager *m_fortManager = nullptr; }; diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index f8d12e04..3f245e6a 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -342,6 +342,12 @@ bool StatManager::logBlockedIp(const LogEntryBlockedIp &entry, qint64 unixTime) ok = (appId != INVALID_APP_ID); if (ok) { ok = createConnBlock(entry, unixTime, appId); + + constexpr int connBlockIncMax = 100; + if (++m_connBlockInc >= connBlockIncMax) { + m_connBlockInc = 0; + deleteOldConnBlock(); + } } sqliteDb()->endTransaction(ok); @@ -380,8 +386,6 @@ bool StatManager::deleteOldConnBlock() deleteRangeConnBlock(m_connBlockIdMin, m_connBlockIdMin + oldCount); - emit connRemoved(); - return true; } diff --git a/src/ui/stat/statmanager.h b/src/ui/stat/statmanager.h index 653aa62d..58d3f585 100644 --- a/src/ui/stat/statmanager.h +++ b/src/ui/stat/statmanager.h @@ -51,7 +51,6 @@ public: virtual bool deleteStatApp(qint64 appId); - bool deleteOldConnBlock(); virtual bool deleteConn(qint64 rowIdTo, bool blocked); virtual bool deleteConnAll(); @@ -78,11 +77,13 @@ signals: public slots: virtual bool clearTraffic(); +protected: + void setupConnBlockId(); + private: using QStmtList = QList; void setupTrafDate(); - void setupConnBlockId(); void setupByConf(); @@ -103,6 +104,8 @@ private: void clearCachedAppId(const QString &appPath); void clearAppIdCache(); + bool deleteOldConnBlock(); + qint64 getAppId(const QString &appPath); qint64 createAppId(const QString &appPath, qint64 unixTime); qint64 getOrCreateAppId(const QString &appPath, qint64 unixTime = 0, bool blocked = false); @@ -142,6 +145,8 @@ private: quint8 m_activePeriodToHour = 0; quint8 m_activePeriodToMinute = 0; + int m_connBlockInc = 999999999; // to trigger on first check + qint32 m_trafHour = 0; qint32 m_trafDay = 0; qint32 m_trafMonth = 0;