diff --git a/src/ui/log/logentryblocked.cpp b/src/ui/log/logentryblocked.cpp index 3fbb9cf6..29892261 100644 --- a/src/ui/log/logentryblocked.cpp +++ b/src/ui/log/logentryblocked.cpp @@ -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; diff --git a/src/ui/log/logentryblocked.h b/src/ui/log/logentryblocked.h index 1beb1674..ffe2e120 100644 --- a/src/ui/log/logentryblocked.h +++ b/src/ui/log/logentryblocked.h @@ -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); diff --git a/src/ui/model/appstatmodel.cpp b/src/ui/model/appstatmodel.cpp index 9b2e63a2..30962041 100644 --- a/src/ui/model/appstatmodel.cpp +++ b/src/ui/model/appstatmodel.cpp @@ -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); } diff --git a/src/ui/model/appstatmodel.h b/src/ui/model/appstatmodel.h index 7e2e9720..42fea0c5 100644 --- a/src/ui/model/appstatmodel.h +++ b/src/ui/model/appstatmodel.h @@ -42,6 +42,7 @@ private: private: QVector m_appIds; + QVector m_pathTypes; }; #endif // APPSTATMODEL_H diff --git a/src/ui/stat/migrations/1.sql b/src/ui/stat/migrations/1.sql index 8b0828e1..3487f7e1 100644 --- a/src/ui/stat/migrations/1.sql +++ b/src/ui/stat/migrations/1.sql @@ -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 ); diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 785f0dd6..6e706145 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -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 &appIds) +void StatManager::getStatAppList( + QStringList &list, QVector &appIds, QVector &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) { diff --git a/src/ui/stat/statmanager.h b/src/ui/stat/statmanager.h index 6096ebc8..1268e3fb 100644 --- a/src/ui/stat/statmanager.h +++ b/src/ui/stat/statmanager.h @@ -49,7 +49,7 @@ public: bool logBlockedIp(const LogEntryBlockedIp &entry, qint64 unixTime); - void getStatAppList(QStringList &list, QVector &appIds); + void getStatAppList(QStringList &list, QVector &appIds, QVector &pathTypes); virtual bool deleteStatApp(qint64 appId); @@ -169,7 +169,13 @@ private: SqliteDb *m_sqliteDb = nullptr; - QHash m_appPidPathMap; // pid -> appPath + struct AppPathInfo + { + quint8 pathType = 0; + QString path; + }; + + QHash m_appPidPathMap; // pid -> appPath QHash m_appPathIdCache; // appPath -> appId TriggerTimer m_connChangedTimer; diff --git a/src/ui/stat/statsql.cpp b/src/ui/stat/statsql.cpp index ab952e57..6e0bb4e4 100644 --- a/src/ui/stat/statsql.cpp +++ b/src/ui/stat/statsql.cpp @@ -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;";