UI: Prepare ConfManager.

This commit is contained in:
Nodir Temirkhodjaev 2019-10-23 17:03:59 +05:00
parent 1bb4965f17
commit 6f1c42a02e
18 changed files with 310 additions and 35 deletions

View File

@ -10,9 +10,9 @@ OBJECTS_DIR = .obj
RCC_DIR = .rcc
SOURCES += \
main.cpp \
conf/addressgroup.cpp \
conf/appgroup.cpp \
conf/confmanager.cpp \
conf/firewallconf.cpp \
control/controlmanager.cpp \
control/controlworker.cpp \
@ -36,6 +36,7 @@ SOURCES += \
log/model/iplistmodel.cpp \
log/model/stringlistmodel.cpp \
log/model/traflistmodel.cpp \
main.cpp \
mainwindow.cpp \
stat/quotamanager.cpp \
stat/statmanager.cpp \
@ -84,6 +85,7 @@ SOURCES += \
HEADERS += \
conf/addressgroup.h \
conf/appgroup.h \
conf/confmanager.h \
conf/firewallconf.h \
control/controlmanager.h \
control/controlworker.h \
@ -175,10 +177,12 @@ RESOURCES += fort_images.qrc
# Database Migrations
OTHER_FILES += \
conf/migrations/*.sql \
stat/migrations/*.sql \
util/app/migrations/*.sql
RESOURCES += \
conf/conf-migrations.qrc \
stat/stat-migrations.qrc \
util/app/app-migrations.qrc

View File

@ -2,8 +2,10 @@
AddressGroup::AddressGroup(QObject *parent) :
QObject(parent),
m_edited(false),
m_includeAll(true),
m_excludeAll(false)
m_excludeAll(false),
m_id(0)
{
}
@ -12,6 +14,8 @@ void AddressGroup::setIncludeAll(bool includeAll)
if (m_includeAll != includeAll) {
m_includeAll = includeAll;
emit includeAllChanged();
setEdited(true);
}
}
@ -20,6 +24,8 @@ void AddressGroup::setExcludeAll(bool excludeAll)
if (m_excludeAll != excludeAll) {
m_excludeAll = excludeAll;
emit excludeAllChanged();
setEdited(true);
}
}
@ -28,6 +34,8 @@ void AddressGroup::setIncludeText(const QString &includeText)
if (m_includeText != includeText) {
m_includeText = includeText;
emit includeTextChanged();
setEdited(true);
}
}
@ -36,9 +44,24 @@ void AddressGroup::setExcludeText(const QString &excludeText)
if (m_excludeText != excludeText) {
m_excludeText = excludeText;
emit excludeTextChanged();
setEdited(true);
}
}
void AddressGroup::copy(const AddressGroup &o)
{
m_edited = false;
m_includeAll = o.includeAll();
m_excludeAll = o.excludeAll();
m_id = 0;
m_includeText = o.includeText();
m_excludeText = o.excludeText();
}
QVariant AddressGroup::toVariant() const
{
QVariantMap map;

View File

@ -15,18 +15,26 @@ class AddressGroup : public QObject
public:
explicit AddressGroup(QObject *parent = nullptr);
bool edited() const { return m_edited; }
void setEdited(bool edited) { m_edited = edited; }
bool includeAll() const { return m_includeAll; }
void setIncludeAll(bool includeAll);
bool excludeAll() const { return m_excludeAll; }
void setExcludeAll(bool excludeAll);
qint64 id() const { return m_id; }
void setId(qint64 id) { m_id = id; }
QString includeText() const { return m_includeText; }
void setIncludeText(const QString &includeText);
QString excludeText() const { return m_excludeText; }
void setExcludeText(const QString &excludeText);
void copy(const AddressGroup &o);
QVariant toVariant() const;
void fromVariant(const QVariant &v);
@ -39,9 +47,13 @@ signals:
public slots:
private:
uint m_edited : 1;
uint m_includeAll : 1;
uint m_excludeAll : 1;
qint64 m_id;
QString m_includeText;
QString m_excludeText;
};

View File

@ -5,13 +5,15 @@
AppGroup::AppGroup(QObject *parent) :
QObject(parent),
m_edited(false),
m_enabled(true),
m_fragmentPacket(false),
m_periodEnabled(false),
m_limitInEnabled(false),
m_limitOutEnabled(false),
m_speedLimitIn(0),
m_speedLimitOut(0)
m_speedLimitOut(0),
m_id(0)
{
}
@ -20,6 +22,8 @@ void AppGroup::setEnabled(bool enabled)
if (bool(m_enabled) != enabled) {
m_enabled = enabled;
emit enabledChanged();
setEdited(true);
}
}
@ -28,14 +32,18 @@ void AppGroup::setFragmentPacket(bool enabled)
if (bool(m_fragmentPacket) != enabled) {
m_fragmentPacket = enabled;
emit fragmentPacketChanged();
setEdited(true);
}
}
void AppGroup::setPeriodEnabled(bool periodEnabled)
void AppGroup::setPeriodEnabled(bool enabled)
{
if (bool(m_periodEnabled) != periodEnabled) {
m_periodEnabled = periodEnabled;
if (bool(m_periodEnabled) != enabled) {
m_periodEnabled = enabled;
emit periodEnabledChanged();
setEdited(true);
}
}
@ -44,6 +52,8 @@ void AppGroup::setLimitInEnabled(bool enabled)
if (bool(m_limitInEnabled) != enabled) {
m_limitInEnabled = enabled;
emit limitInEnabledChanged();
setEdited(true);
}
}
@ -52,6 +62,8 @@ void AppGroup::setLimitOutEnabled(bool enabled)
if (bool(m_limitOutEnabled) != enabled) {
m_limitOutEnabled = enabled;
emit limitOutEnabledChanged();
setEdited(true);
}
}
@ -60,6 +72,8 @@ void AppGroup::setSpeedLimitIn(quint32 limit)
if (m_speedLimitIn != limit) {
m_speedLimitIn = limit;
emit speedLimitInChanged();
setEdited(true);
}
}
@ -68,6 +82,8 @@ void AppGroup::setSpeedLimitOut(quint32 limit)
if (m_speedLimitOut != limit) {
m_speedLimitOut = limit;
emit speedLimitOutChanged();
setEdited(true);
}
}
@ -76,6 +92,8 @@ void AppGroup::setName(const QString &name)
if (m_name != name) {
m_name = name;
emit nameChanged();
setEdited(true);
}
}
@ -84,6 +102,8 @@ void AppGroup::setBlockText(const QString &blockText)
if (m_blockText != blockText) {
m_blockText = blockText;
emit blockTextChanged();
setEdited(true);
}
}
@ -92,6 +112,8 @@ void AppGroup::setAllowText(const QString &allowText)
if (m_allowText != allowText) {
m_allowText = allowText;
emit allowTextChanged();
setEdited(true);
}
}
@ -100,6 +122,8 @@ void AppGroup::setPeriodFrom(const QString &periodFrom)
if (m_periodFrom != periodFrom) {
m_periodFrom = periodFrom;
emit periodFromChanged();
setEdited(true);
}
}
@ -108,6 +132,8 @@ void AppGroup::setPeriodTo(const QString &periodTo)
if (m_periodTo != periodTo) {
m_periodTo = periodTo;
emit periodToChanged();
setEdited(true);
}
}
@ -140,6 +166,29 @@ QString AppGroup::menuLabel() const
return text;
}
void AppGroup::copy(const AppGroup &o)
{
m_edited = false;
m_enabled = o.enabled();
m_fragmentPacket = o.fragmentPacket();
m_periodEnabled = o.periodEnabled();
m_periodFrom = o.periodFrom();
m_periodTo = o.periodTo();
m_limitInEnabled = o.limitInEnabled();
m_limitOutEnabled = o.limitOutEnabled();
m_speedLimitIn = o.speedLimitIn();
m_speedLimitOut = o.speedLimitOut();
m_id = 0;
m_name = o.name();
m_blockText = o.blockText();
m_allowText = o.allowText();
}
QVariant AppGroup::toVariant() const
{
QVariantMap map;

View File

@ -23,6 +23,9 @@ class AppGroup : public QObject
public:
explicit AppGroup(QObject *parent = nullptr);
bool edited() const { return m_edited; }
void setEdited(bool edited) { m_edited = edited; }
bool enabled() const { return m_enabled; }
void setEnabled(bool enabled);
@ -30,7 +33,7 @@ public:
void setFragmentPacket(bool enabled);
bool periodEnabled() const { return m_periodEnabled; }
void setPeriodEnabled(bool periodEnabled);
void setPeriodEnabled(bool enabled);
bool limitInEnabled() const { return m_limitInEnabled; }
void setLimitInEnabled(bool enabled);
@ -51,6 +54,9 @@ public:
return limitOutEnabled() ? speedLimitOut() : 0;
}
qint64 id() const { return m_id; }
void setId(qint64 id) { m_id = id; }
QString name() const { return m_name; }
void setName(const QString &name);
@ -68,6 +74,8 @@ public:
QString menuLabel() const;
void copy(const AppGroup &o);
QVariant toVariant() const;
void fromVariant(const QVariant &v);
@ -88,6 +96,7 @@ signals:
public slots:
private:
uint m_edited : 1;
uint m_enabled : 1;
uint m_fragmentPacket : 1;
@ -101,6 +110,8 @@ private:
quint32 m_speedLimitIn;
quint32 m_speedLimitOut;
qint64 m_id;
QString m_name;
QString m_blockText;

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/stat">
<file>migrations/1.sql</file>
</qresource>
</RCC>

View File

@ -0,0 +1,56 @@
#include "confmanager.h"
#include <QLoggingCategory>
#include <sqlite/sqlitedb.h>
#include <sqlite/sqlitestmt.h>
#include "../fortcommon.h"
#include "../util/dateutil.h"
#include "../util/fileutil.h"
#include "../util/osutil.h"
#include "firewallconf.h"
Q_DECLARE_LOGGING_CATEGORY(CLOG_CONF_MANAGER)
Q_LOGGING_CATEGORY(CLOG_CONF_MANAGER, "fort.confManager")
#define logWarning() qCWarning(CLOG_CONF_MANAGER,)
#define logCritical() qCCritical(CLOG_CONF_MANAGER,)
#define DATABASE_USER_VERSION 1
namespace {
const char * const sqlPragmas =
"PRAGMA locking_mode = EXCLUSIVE;"
"PRAGMA synchronous = NORMAL;"
;
}
ConfManager::ConfManager(const QString &filePath,
QObject *parent) :
QObject(parent),
m_sqliteDb(new SqliteDb(filePath))
{
}
bool ConfManager::initialize()
{
if (!m_sqliteDb->open()) {
logCritical() << "File open error:"
<< m_sqliteDb->filePath()
<< m_sqliteDb->errorMessage();
return false;
}
m_sqliteDb->execute(sqlPragmas);
if (!m_sqliteDb->migrate(":/conf/migrations", DATABASE_USER_VERSION)) {
logCritical() << "Migration error"
<< m_sqliteDb->filePath();
return false;
}
return true;
}

29
src/ui/conf/confmanager.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef CONFMANAGER_H
#define CONFMANAGER_H
#include <QObject>
#include "../util/classhelpers.h"
QT_FORWARD_DECLARE_CLASS(FirewallConf)
QT_FORWARD_DECLARE_CLASS(SqliteDb)
QT_FORWARD_DECLARE_CLASS(SqliteStmt)
class ConfManager : public QObject
{
Q_OBJECT
public:
explicit ConfManager(const QString &filePath,
QObject *parent = nullptr);
CLASS_DELETE_COPY_MOVE(ConfManager)
bool initialize();
signals:
private:
SqliteDb *m_sqliteDb;
};
#endif // CONFMANAGER_H

View File

@ -301,6 +301,23 @@ void FirewallConf::copyImmediateFlags(const FirewallConf &o)
setTrafUnit(o.trafUnit());
}
void FirewallConf::copy(const FirewallConf &o)
{
copyFlags(o);
int addrGroupIndex = 0;
for (const AddressGroup *ag : o.addressGroupsList()) {
AddressGroup *addressGroup = m_addressGroups.at(addrGroupIndex++);
addressGroup->copy(*ag);
}
for (const AppGroup *ag : o.appGroupsList()) {
auto appGroup = new AppGroup();
appGroup->copy(*ag);
addAppGroup(appGroup);
}
}
QVariant FirewallConf::toVariant() const
{
QVariantMap map;

View File

@ -131,6 +131,8 @@ public:
void copyFlags(const FirewallConf &o);
void copyImmediateFlags(const FirewallConf &o);
void copy(const FirewallConf &o);
QVariant toVariant() const;
void fromVariant(const QVariant &v);

View File

@ -0,0 +1,43 @@
PRAGMA user_version = 1;
PRAGMA journal_mode = WAL;
CREATE TABLE IF NOT EXISTS address_group(
addr_group_id INTEGER PRIMARY KEY,
order INTEGER NOT NULL,
include_all BOOLEAN NOT NULL,
exclude_all BOOLEAN NOT NULL,
include_text TEXT NOT NULL,
exclude_text TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS app_group(
app_group_id INTEGER PRIMARY KEY,
order INTEGER NOT NULL,
enabled BOOLEAN NOT NULL,
fragment_packet BOOLEAN NOT NULL,
period_enabled BOOLEAN NOT NULL,
limit_in_enabled BOOLEAN NOT NULL,
limit_out_enabled BOOLEAN NOT NULL,
speed_limit_in INTEGER NOT NULL,
speed_limit_out INTEGER NOT NULL,
name TEXT NOT NULL,
block_text TEXT NOT NULL,
allow_text TEXT NOT NULL,
period_from TEXT NOT NULL,
period_to TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS app(
app_id INTEGER PRIMARY KEY,
path TEXT UNIQUE NOT NULL,
blocked BOOLEAN NOT NULL,
creat_time INTEGER NOT NULL,
end_time INTEGER
);
CREATE INDEX idx_app_end_time ON app(end_time);
CREATE TABLE IF NOT EXISTS app_alert(
app_id INTEGER PRIMARY KEY
);

View File

@ -12,6 +12,9 @@
Q_DECLARE_LOGGING_CATEGORY(CLOG_CONTROL_MANAGER)
Q_LOGGING_CATEGORY(CLOG_CONTROL_MANAGER, "fort.controlManager")
#define logWarning() qCWarning(CLOG_CONTROL_MANAGER,)
#define logCritical() qCCritical(CLOG_CONTROL_MANAGER,)
ControlManager::ControlManager(const QString &globalName,
const QString &scriptPath,
QObject *parent) :
@ -37,8 +40,8 @@ bool ControlManager::listen(FortManager *fortManager)
return true;
if (!m_sharedMemory.create(4096)) {
qWarning(CLOG_CONTROL_MANAGER()) << "Shared Memory create error:"
<< m_sharedMemory.errorString();
logWarning() << "Shared Memory create error:"
<< m_sharedMemory.errorString();
return false;
}
@ -54,8 +57,8 @@ bool ControlManager::listen(FortManager *fortManager)
bool ControlManager::post(const QStringList &args)
{
if (!m_sharedMemory.attach()) {
qWarning(CLOG_CONTROL_MANAGER()) << "Shared Memory attach error:"
<< m_sharedMemory.errorString();
logWarning() << "Shared Memory attach error:"
<< m_sharedMemory.errorString();
return false;
}
@ -69,8 +72,8 @@ void ControlManager::processRequest(const QString &scriptPath,
{
const QString script = FileUtil::readFile(scriptPath);
if (script.isEmpty()) {
qWarning(CLOG_CONTROL_MANAGER()) << "Script is empty:"
<< scriptPath;
logWarning() << "Script is empty:"
<< scriptPath;
return;
}
@ -104,10 +107,10 @@ void ControlManager::processRequest(const QString &scriptPath,
// Run the script
const QJSValue res = engine.evaluate(script, scriptPath);
if (res.isError()) {
qWarning(CLOG_CONTROL_MANAGER()) << "Script error:"
<< scriptPath << "line"
<< res.property("lineNumber").toInt()
<< ":" << res.toString();
logWarning() << "Script error:"
<< scriptPath << "line"
<< res.property("lineNumber").toInt()
<< ":" << res.toString();
return;
}

View File

@ -15,6 +15,7 @@
#include "conf/addressgroup.h"
#include "conf/appgroup.h"
#include "conf/confmanager.h"
#include "conf/firewallconf.h"
#include "driver/drivermanager.h"
#include "fortsettings.h"
@ -64,6 +65,7 @@ FortManager::FortManager(FortSettings *fortSettings,
m_quotaManager(new QuotaManager(fortSettings, this)),
m_statManager(new StatManager(fortSettings->statFilePath(),
m_quotaManager, this)),
m_confManager(new ConfManager(fortSettings->confDbFilePath(), this)),
m_driverManager(new DriverManager(this)),
m_logManager(new LogManager(m_statManager,
m_driverManager->driverWorker(), this)),
@ -77,6 +79,7 @@ FortManager::FortManager(FortSettings *fortSettings,
setupLogger();
setupAppInfoCache();
setupStatManager();
setupConfManager();
setupLogManager();
setupDriver();
@ -204,6 +207,11 @@ void FortManager::setupStatManager()
this, &FortManager::showInfoBox);
}
void FortManager::setupConfManager()
{
m_confManager->initialize();
}
void FortManager::setupLogger()
{
Logger *logger = Logger::instance();
@ -619,10 +627,7 @@ FirewallConf *FortManager::cloneConf(const FirewallConf &conf)
{
auto newConf = new FirewallConf(this);
const QVariant data = conf.toVariant();
newConf->fromVariant(data);
newConf->copyFlags(conf);
newConf->copy(conf);
return newConf;
}

View File

@ -10,6 +10,7 @@ QT_FORWARD_DECLARE_CLASS(QQmlApplicationEngine)
QT_FORWARD_DECLARE_CLASS(QSystemTrayIcon)
QT_FORWARD_DECLARE_CLASS(AppInfoCache)
QT_FORWARD_DECLARE_CLASS(ConfManager)
QT_FORWARD_DECLARE_CLASS(DriverManager)
QT_FORWARD_DECLARE_CLASS(FirewallConf)
QT_FORWARD_DECLARE_CLASS(FortSettings)
@ -108,6 +109,7 @@ private:
void closeLogManager();
void setupStatManager();
void setupConfManager();
void setupLogger();
@ -177,6 +179,7 @@ private:
QuotaManager *m_quotaManager;
StatManager *m_statManager;
ConfManager *m_confManager;
DriverManager *m_driverManager;
LogManager *m_logManager;
NativeEventFilter *m_nativeEventFilter;

View File

@ -160,6 +160,11 @@ QString FortSettings::statFilePath() const
return statPath() + QLatin1String("FortFirewall.stat");
}
QString FortSettings::confDbFilePath() const
{
return profilePath() + QLatin1String("FortFirewall.config");
}
QString FortSettings::confFilePath() const
{
return profilePath() + QLatin1String("FortFirewall.conf");

View File

@ -162,6 +162,8 @@ public:
QString statPath() const { return m_statPath; }
QString statFilePath() const;
QString confDbFilePath() const;
QString controlPath() const { return m_controlPath; }
QStringList args() const { return m_args; }

View File

@ -16,6 +16,9 @@
Q_DECLARE_LOGGING_CATEGORY(CLOG_STAT_MANAGER)
Q_LOGGING_CATEGORY(CLOG_STAT_MANAGER, "fort.statManager")
#define logWarning() qCWarning(CLOG_STAT_MANAGER,)
#define logCritical() qCCritical(CLOG_STAT_MANAGER,)
#define DATABASE_USER_VERSION 2
#define INVALID_APP_INDEX qint16(-1)
@ -97,9 +100,9 @@ bool StatManager::initialize()
m_lastTrafHour = m_lastTrafDay = m_lastTrafMonth = 0;
if (!m_sqliteDb->open()) {
qCritical(CLOG_STAT_MANAGER()) << "File open error:"
<< m_sqliteDb->filePath()
<< m_sqliteDb->errorMessage();
logCritical() << "File open error:"
<< m_sqliteDb->filePath()
<< m_sqliteDb->errorMessage();
return false;
}
@ -107,8 +110,8 @@ bool StatManager::initialize()
if (!m_sqliteDb->migrate(":/stat/migrations", DATABASE_USER_VERSION,
false, &migrateFunc)) {
qCritical(CLOG_STAT_MANAGER()) << "Migration error"
<< m_sqliteDb->filePath();
logCritical() << "Migration error"
<< m_sqliteDb->filePath();
return false;
}
@ -346,9 +349,9 @@ void StatManager::logStatTraf(quint16 procCount, const quint32 *procTrafBytes)
const int procIndex = m_appIndexes.value(pid, INVALID_APP_INDEX);
if (Q_UNLIKELY(procIndex == INVALID_APP_INDEX)) {
qCritical(CLOG_STAT_MANAGER()) << "UI & Driver's states mismatch! Expected processes:"
<< m_appIndexes.keys() << "Got:" << procCount
<< "(" << i << pid << inactive << ")";
logCritical() << "UI & Driver's states mismatch! Expected processes:"
<< m_appIndexes.keys() << "Got:" << procCount
<< "(" << i << pid << inactive << ")";
abort();
}
@ -521,8 +524,8 @@ void StatManager::updateTrafficList(const QStmtList &insertStmtList,
if (!updateTraffic(stmtUpdate, inBytes, outBytes, appId)) {
SqliteStmt *stmtInsert = insertStmtList.at(i);
if (!updateTraffic(stmtInsert, inBytes, outBytes, appId)) {
qCritical(CLOG_STAT_MANAGER()) << "Update traffic error:"
<< m_sqliteDb->errorMessage();
logCritical() << "Update traffic error:"
<< m_sqliteDb->errorMessage();
}
}
++i;

View File

@ -13,6 +13,9 @@
Q_DECLARE_LOGGING_CATEGORY(CLOG_APPINFOCACHE)
Q_LOGGING_CATEGORY(CLOG_APPINFOCACHE, "fort.appInfoWorker")
#define logWarning() qCWarning(CLOG_APPINFOCACHE,)
#define logCritical() qCCritical(CLOG_APPINFOCACHE,)
#define DATABASE_USER_VERSION 3
#define APP_CACHE_MAX_COUNT 2000
@ -94,15 +97,15 @@ AppInfoManager::~AppInfoManager()
void AppInfoManager::setupDb(const QString &filePath)
{
if (!m_sqliteDb->open(filePath)) {
qCritical(CLOG_APPINFOCACHE()) << "File open error:"
<< filePath
<< m_sqliteDb->errorMessage();
logCritical() << "File open error:"
<< filePath
<< m_sqliteDb->errorMessage();
return;
}
if (!m_sqliteDb->migrate(":/appinfocache/migrations",
DATABASE_USER_VERSION, true)) {
qCritical(CLOG_APPINFOCACHE()) << "Migration error" << filePath;
logCritical() << "Migration error" << filePath;
return;
}
}