reject IPs from statistics feature and top lists size as parameter

This commit is contained in:
acetone
2022-11-21 10:17:45 +03:00
parent 2e39d2f29f
commit a01bbaf540
6 changed files with 78 additions and 23 deletions
+41 -12
View File
@@ -21,11 +21,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "dbmanager.h"
#include "g.h"
#include <QRegularExpression>
#include <QDateTime>
#include <QDebug>
#include <QMutex>
#include <QFile>
QMutex Statistics::m_lastDestinationsMtx;
QStringList Statistics::m_lastDestinations;
std::atomic<quint64> Statistics::m_dailyUpload {0};
@@ -33,6 +35,7 @@ std::atomic<quint64> Statistics::m_dailyDownload {0};
std::atomic<quint64> Statistics::m_totalUpload {0};
std::atomic<quint64> Statistics::m_totalDownload {0};
QMutex Statistics::m_topDestinationsMtx;
TopDestinationList Statistics::m_dailyTopDestinations;
TopDestinationList Statistics::m_totalTopDestinations;
@@ -47,6 +50,23 @@ void Statistics::report(const LogEvent& event)
return;
}
m_lastDestinationsMtx.lock();
if (not lastDestinations().contains(event.dest))
{
if (lastDestinations().size() >= static_cast<int>(g::p::LAST_AND_TOP_LIST_SIZE))
{
m_lastDestinations.pop_back();
}
m_lastDestinations.push_front(event.dest);
}
m_lastDestinationsMtx.unlock();
if (g::p::IGNORE_IP_ADDRS_IN_STATISTCS and isIpAddress(event.dest))
{
qDebug() << event.dest << "rejected from statistics";
return;
}
DBManager db;
m_dailyDownload = db.incrementDailyDownloadTraffic(event.from);
@@ -57,21 +77,12 @@ void Statistics::report(const LogEvent& event)
db.incrementTotalTopCount(event.dest);
db.incrementDailyTopCount(event.dest);
// editing without mutex because DBManager() already lock this place
if (not lastDestinations().contains(event.dest))
{
if (lastDestinations().size() >= g::c::LAST_AND_TOP_LIST_SIZE)
{
m_lastDestinations.pop_back();
}
m_lastDestinations.push_front(event.dest);
}
m_cacheUpdatedAfterLastReport = false;
}
const QStringList Statistics::lastDestinations()
{
QMutexLocker lock (&m_lastDestinationsMtx);
return m_lastDestinations;
}
@@ -81,18 +92,19 @@ const TopDestinationList Statistics::dailyTopDestinations()
{
actualizeTopLists();
}
QMutexLocker lock (&m_topDestinationsMtx);
return m_dailyTopDestinations;
}
const TopDestinationList Statistics::totalTopDestinations()
{
QMutexLocker lock (&m_topDestinationsMtx);
return m_totalTopDestinations;
}
void Statistics::actualizeTopLists()
{
static QMutex mutex;
QMutexLocker lock (&mutex);
QMutexLocker lock (&m_topDestinationsMtx);
qint64 started = QDateTime::currentMSecsSinceEpoch();
if (started - m_actualizeTopListsLastTimestamp < g::c::CACHE_ACTUALIZE_TOP_LISTS_FROM_DB_MINIMAL_INTERVAL_MS)
@@ -109,3 +121,20 @@ void Statistics::actualizeTopLists()
m_actualizeTopListsLastTimestamp = QDateTime::currentMSecsSinceEpoch();
qDebug() << "Cache actualized in" << m_actualizeTopListsLastTimestamp-started << "ms";
}
bool Statistics::isIpAddress(const QString& dest)
{
static QRegularExpression ipv4("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$");
if (ipv4.match(dest).hasMatch())
{
return true;
}
static QRegularExpression ipv6("^\\[[a-f0-9:]*\\]$");
if (ipv6.match(dest).hasMatch())
{
return true;
}
return false;
}