UI: StatManager: Prepare "path_type"

This commit is contained in:
Nodir Temirkhodjaev 2022-01-09 00:03:27 +03:00
parent 1754c101d6
commit 249658688b
8 changed files with 44 additions and 18 deletions

View File

@ -5,6 +5,11 @@ LogEntryBlocked::LogEntryBlocked(quint32 pid, const QString &kernelPath) :
{
}
FortLogType LogEntryBlocked::type() const
{
return blocked() ? FORT_LOG_TYPE_BLOCKED : FORT_LOG_TYPE_ALLOWED;
}
void LogEntryBlocked::setBlocked(bool blocked)
{
m_blocked = blocked;

View File

@ -8,7 +8,7 @@ class LogEntryBlocked : public LogEntry
public:
explicit LogEntryBlocked(quint32 pid = 0, const QString &kernelPath = QString());
FortLogType type() const override { return FORT_LOG_TYPE_BLOCKED; }
FortLogType type() const override;
bool blocked() const { return m_blocked; }
void setBlocked(bool blocked);

View File

@ -83,7 +83,7 @@ void AppStatModel::updateList()
m_appIds.clear();
m_appIds.append(0); // All
statManager()->getStatAppList(list, m_appIds);
statManager()->getStatAppList(list, m_appIds, m_pathTypes);
setList(list);
}

View File

@ -42,6 +42,7 @@ private:
private:
QVector<qint64> m_appIds;
QVector<quint8> m_pathTypes;
};
#endif // APPSTATMODEL_H

View File

@ -1,6 +1,7 @@
CREATE TABLE app(
app_id INTEGER PRIMARY KEY,
path TEXT NOT NULL,
path_type INTEGER NOT NULL DEFAULT 0,
creat_time INTEGER NOT NULL
);

View File

@ -24,7 +24,7 @@ Q_LOGGING_CATEGORY(CLOG_STAT_MANAGER, "stat")
#define logWarning() qCWarning(CLOG_STAT_MANAGER, )
#define logCritical() qCCritical(CLOG_STAT_MANAGER, )
#define DATABASE_USER_VERSION 4
#define DATABASE_USER_VERSION 5
#define ACTIVE_PERIOD_CHECK_SECS (60 * OS_TICKS_PER_SECOND)
@ -36,7 +36,11 @@ bool migrateFunc(SqliteDb *db, int version, bool isNewDb, void *ctx)
{
Q_UNUSED(ctx);
if (version == 3 && !isNewDb) {
if (isNewDb)
return true;
switch (version) {
case 3: {
// Move apps' total traffic to separate table
const QString srcSchema = SqliteDb::migrationOldSchemaName();
const QString dstSchema = SqliteDb::migrationNewSchemaName();
@ -46,6 +50,11 @@ bool migrateFunc(SqliteDb *db, int version, bool isNewDb, void *ctx)
SqliteDb::entityName(srcSchema, "app"),
"app_id, traf_time, in_bytes, out_bytes");
db->executeStr(sql);
} break;
case 5: {
// COMPAT: app.flags
db->execute("UPDATE app SET path_type = 1 WHERE path = 'System';");
} break;
}
return true;
@ -280,7 +289,7 @@ bool StatManager::logProcNew(const LogEntryProcNew &entry, qint64 unixTime)
const QString &appPath = entry.path();
Q_ASSERT(!m_appPidPathMap.contains(pid));
m_appPidPathMap.insert(pid, appPath);
m_appPidPathMap.insert(pid, { pathType, appPath });
return getOrCreateAppId(appPath, pathType, unixTime) != INVALID_APP_ID;
}
@ -366,7 +375,7 @@ bool StatManager::logBlockedIp(const LogEntryBlockedIp &entry, qint64 unixTime)
bool ok = false;
sqliteDb()->beginTransaction();
const qint64 appId = getOrCreateAppId(entry.path(), unixTime);
const qint64 appId = getOrCreateAppId(entry.path(), entry.pathType(), unixTime);
if (appId != INVALID_APP_ID) {
ok = createConnBlock(entry, unixTime, appId);
}
@ -571,13 +580,15 @@ void StatManager::deleteOldTraffic(qint32 trafHour)
doStmtList(deleteTrafStmts);
}
void StatManager::getStatAppList(QStringList &list, QVector<qint64> &appIds)
void StatManager::getStatAppList(
QStringList &list, QVector<qint64> &appIds, QVector<quint8> &pathTypes)
{
SqliteStmt *stmt = sqliteDb()->stmt(StatSql::sqlSelectStatAppList);
while (stmt->step() == SqliteStmt::StepRow) {
appIds.append(stmt->columnInt64(0));
list.append(stmt->columnText(1));
pathTypes.append(stmt->columnInt(1));
list.append(stmt->columnText(2));
}
stmt->reset();
}
@ -589,7 +600,9 @@ void StatManager::logTrafBytes(const QStmtList &insertStmtList, const QStmtList
const bool inactive = (pidFlag & 1) != 0;
const quint32 pid = pidFlag & ~quint32(1);
const QString appPath = m_appPidPathMap.value(pid);
const AppPathInfo appPathInfo = m_appPidPathMap.value(pid);
const quint8 pathType = appPathInfo.pathType;
const QString appPath = appPathInfo.path;
if (Q_UNLIKELY(appPath.isEmpty())) {
logCritical() << "UI & Driver's states mismatch! Expected processes:"
@ -604,7 +617,7 @@ void StatManager::logTrafBytes(const QStmtList &insertStmtList, const QStmtList
if (inBytes == 0 && outBytes == 0)
return;
const qint64 appId = getOrCreateAppId(appPath, unixTime);
const qint64 appId = getOrCreateAppId(appPath, pathType, unixTime);
Q_ASSERT(appId != INVALID_APP_ID);
if (m_isActivePeriod) {

View File

@ -49,7 +49,7 @@ public:
bool logBlockedIp(const LogEntryBlockedIp &entry, qint64 unixTime);
void getStatAppList(QStringList &list, QVector<qint64> &appIds);
void getStatAppList(QStringList &list, QVector<qint64> &appIds, QVector<quint8> &pathTypes);
virtual bool deleteStatApp(qint64 appId);
@ -169,7 +169,13 @@ private:
SqliteDb *m_sqliteDb = nullptr;
QHash<quint32, QString> m_appPidPathMap; // pid -> appPath
struct AppPathInfo
{
quint8 pathType = 0;
QString path;
};
QHash<quint32, AppPathInfo> m_appPidPathMap; // pid -> appPath
QHash<QString, qint64> m_appPathIdCache; // appPath -> appId
TriggerTimer m_connChangedTimer;

View File

@ -2,13 +2,14 @@
const char *const StatSql::sqlSelectAppId = "SELECT app_id FROM app WHERE path = ?1;";
const char *const StatSql::sqlInsertAppId = "INSERT INTO app(path, creat_time) VALUES(?1, ?2);";
const char *const StatSql::sqlInsertAppId = "INSERT INTO app(path, path_type, creat_time)"
" VALUES(?1, ?2, ?3);";
const char *const StatSql::sqlDeleteAppId = "DELETE FROM app WHERE app_id = ?1;";
const char *const StatSql::sqlSelectStatAppExists = "SELECT 1 FROM traffic_app WHERE app_id = ?1;";
const char *const StatSql::sqlSelectStatAppList = "SELECT t.app_id, t.path FROM app t"
const char *const StatSql::sqlSelectStatAppList = "SELECT t.app_id, t.path_type, t.path FROM app t"
" JOIN traffic_app ta ON ta.app_id = t.app_id"
" ORDER BY t.app_id;";
@ -179,10 +180,9 @@ const char *const StatSql::sqlInsertConnBlock = "INSERT INTO conn_block(conn_id,
const char *const StatSql::sqlSelectMinMaxConnBlockId = "SELECT MIN(id), MAX(id) FROM conn_block;";
const char *const StatSql::sqlDeleteConnForBlock =
"DELETE FROM conn WHERE conn_id IN ("
" SELECT conn_id FROM conn_block WHERE id <= ?1"
");";
const char *const StatSql::sqlDeleteConnForBlock = "DELETE FROM conn WHERE conn_id IN ("
" SELECT conn_id FROM conn_block WHERE id <= ?1"
");";
const char *const StatSql::sqlDeleteConnBlock = "DELETE FROM conn_block WHERE id <= ?1;";