From 1dda1b22601989089de8faf5b6b7933ff7898348 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 4 Sep 2017 00:41:03 +0500 Subject: [PATCH] Add DriverManager class. --- src/ui/FortFirewall.pro | 10 +++--- src/ui/drivermanager.cpp | 70 ++++++++++++++++++++++++++++++++++++++++ src/ui/drivermanager.h | 39 ++++++++++++++++++++++ src/ui/fortmanager.cpp | 35 ++++++++++++++++++-- src/ui/fortmanager.h | 7 ++++ src/ui/fortsettings.cpp | 35 ++++++++++++++++---- src/ui/fortsettings.h | 12 +++++-- src/ui/main.cpp | 5 +++ 8 files changed, 198 insertions(+), 15 deletions(-) create mode 100644 src/ui/drivermanager.cpp create mode 100644 src/ui/drivermanager.h diff --git a/src/ui/FortFirewall.pro b/src/ui/FortFirewall.pro index 76d52b25..6b6734da 100644 --- a/src/ui/FortFirewall.pro +++ b/src/ui/FortFirewall.pro @@ -7,11 +7,12 @@ TEMPLATE = app SOURCES += \ main.cpp \ + activityLog/logbuffer.cpp \ + activityLog/logentry.cpp \ conf/addressgroup.cpp \ conf/appgroup.cpp \ conf/firewallconf.cpp \ - activityLog/logbuffer.cpp \ - activityLog/logentry.cpp \ + drivermanager.cpp \ fortcommon.cpp \ fortmanager.cpp \ fortsettings.cpp \ @@ -23,11 +24,12 @@ SOURCES += \ util/processinfo.cpp HEADERS += \ + activityLog/logbuffer.h \ + activityLog/logentry.h \ conf/addressgroup.h \ conf/appgroup.h \ conf/firewallconf.h \ - activityLog/logbuffer.h \ - activityLog/logentry.h \ + drivermanager.h \ fortcommon.h \ fortmanager.h \ fortsettings.h \ diff --git a/src/ui/drivermanager.cpp b/src/ui/drivermanager.cpp new file mode 100644 index 00000000..64c36838 --- /dev/null +++ b/src/ui/drivermanager.cpp @@ -0,0 +1,70 @@ +#include "drivermanager.h" + +#include "fortcommon.h" +#include "conf/firewallconf.h" +#include "util/confutil.h" +#include "util/device.h" + +DriverManager::DriverManager(QObject *parent) : + QObject(parent), + m_device(new Device(this)) +{ +} + +void DriverManager::setErrorMessage(const QString &errorMessage) +{ + if (m_errorMessage != errorMessage) { + m_errorMessage = errorMessage; + emit errorMessageChanged(); + } +} + +bool DriverManager::openDevice() +{ + if (!m_device->open(FortCommon::deviceName())) { + setErrorMessage(m_device->getLastErrorMessage()); + return false; + } + + return true; +} + +bool DriverManager::writeConf(const FirewallConf &conf) +{ + ConfUtil confUtil; + QByteArray buf; + + const int confSize = confUtil.write(conf, buf); + if (!confSize) { + setErrorMessage(confUtil.errorMessage()); + return false; + } + + return writeData(FortCommon::ioctlSetConf(), + buf, confSize); +} + +bool DriverManager::writeConfFlags(const FirewallConf &conf) +{ + ConfUtil confUtil; + QByteArray buf; + + const int flagsSize = confUtil.writeFlags(conf, buf); + if (!flagsSize) { + setErrorMessage(confUtil.errorMessage()); + return false; + } + + return writeData(FortCommon::ioctlSetFlags(), + buf, flagsSize); +} + +bool DriverManager::writeData(int code, QByteArray &buf, int size) +{ + if (!m_device->ioctl(code, buf.data(), size)) { + setErrorMessage(m_device->getLastErrorMessage()); + return false; + } + + return true; +} diff --git a/src/ui/drivermanager.h b/src/ui/drivermanager.h new file mode 100644 index 00000000..79defc20 --- /dev/null +++ b/src/ui/drivermanager.h @@ -0,0 +1,39 @@ +#ifndef DRIVERMANAGER_H +#define DRIVERMANAGER_H + +#include + +class Device; +class FirewallConf; + +class DriverManager : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) + +public: + explicit DriverManager(QObject *parent = nullptr); + + QString errorMessage() const { return m_errorMessage; } + +signals: + void errorMessageChanged(); + +public slots: + bool openDevice(); + + bool writeConf(const FirewallConf &conf); + bool writeConfFlags(const FirewallConf &conf); + +private: + void setErrorMessage(const QString &errorMessage); + + bool writeData(int code, QByteArray &buf, int size); + +private: + Device *m_device; + + QString m_errorMessage; +}; + +#endif // DRIVERMANAGER_H diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index 1588c089..8c977740 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -10,6 +11,7 @@ #include "conf/addressgroup.h" #include "conf/appgroup.h" #include "conf/firewallconf.h" +#include "drivermanager.h" #include "fortsettings.h" FortManager::FortManager(QObject *parent) : @@ -18,7 +20,8 @@ FortManager::FortManager(QObject *parent) : m_engine(new QQmlApplicationEngine(this)), m_fortSettings(new FortSettings(qApp->arguments(), this)), m_firewallConf(new FirewallConf(this)), - m_firewallConfToEdit(nullConf()) + m_firewallConfToEdit(nullConf()), + m_driverManager(new DriverManager(this)) { m_fortSettings->readConf(*m_firewallConf); @@ -65,6 +68,16 @@ void FortManager::setupEngine() Q_ASSERT(m_appWindow); } +bool FortManager::setupDriver() +{ + if (!m_driverManager->openDevice()) { + showErrorBox(m_driverManager->errorMessage()); + return false; + } + + return true; +} + void FortManager::showTrayIcon() { m_trayIcon->show(); @@ -88,6 +101,11 @@ void FortManager::closeWindow() setFirewallConfToEdit(nullConf()); } +void FortManager::showErrorBox(const QString &text) +{ + QMessageBox::critical(&m_window, QString(), text); +} + bool FortManager::saveConf() { return saveSettings(m_firewallConfToEdit); @@ -112,14 +130,22 @@ void FortManager::setFirewallConfToEdit(FirewallConf *conf) bool FortManager::saveSettings(FirewallConf *newConf) { - if (!m_fortSettings->writeConf(*newConf)) + if (!m_fortSettings->writeConf(*newConf)) { + showErrorBox(m_fortSettings->errorMessage()); return false; + } m_firewallConf->deleteLater(); m_firewallConf = newConf; updateTrayMenu(); + // Update driver + if (!m_driverManager->writeConf(*m_firewallConf)) { + showErrorBox(m_driverManager->errorMessage()); + return false; + } + return true; } @@ -140,6 +166,11 @@ void FortManager::saveTrayFlags() } m_fortSettings->writeConfFlags(*m_firewallConf); + + // Update driver + if (!m_driverManager->writeConfFlags(*m_firewallConf)) { + showErrorBox(m_driverManager->errorMessage()); + } } FirewallConf *FortManager::cloneConf(const FirewallConf &conf) diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index d51de987..9d4c921e 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -7,6 +7,7 @@ class QQmlApplicationEngine; class QSystemTrayIcon; +class DriverManager; class FortSettings; class FirewallConf; @@ -29,11 +30,15 @@ signals: void firewallConfToEditChanged(); public slots: + bool setupDriver(); + void showTrayIcon(); void showWindow(); void closeWindow(); + void showErrorBox(const QString &text); + bool saveConf(); bool applyConf(); @@ -80,6 +85,8 @@ private: QAction *m_appBlockAllAction; QAction *m_appAllowAllAction; QMenu *m_appGroupsMenu; + + DriverManager *m_driverManager; }; #endif // FORTMANAGER_H diff --git a/src/ui/fortsettings.cpp b/src/ui/fortsettings.cpp index 9482b50a..169217bf 100644 --- a/src/ui/fortsettings.cpp +++ b/src/ui/fortsettings.cpp @@ -83,6 +83,14 @@ void FortSettings::setupIni() m_ini = new QSettings(iniPath, QSettings::IniFormat, this); } +void FortSettings::setErrorMessage(const QString &errorMessage) +{ + if (m_errorMessage != errorMessage) { + m_errorMessage = errorMessage; + emit errorMessageChanged(); + } +} + QString FortSettings::confFilePath() const { return m_profilePath + QLatin1String("FortFirewall.conf"); @@ -93,7 +101,7 @@ QString FortSettings::confBackupFilePath() const return confFilePath() + QLatin1String(".backup"); } -bool FortSettings::readConf(FirewallConf &conf) const +bool FortSettings::readConf(FirewallConf &conf) { const QString filePath = confFilePath(); const QString backupFilePath = confBackupFilePath(); @@ -102,15 +110,17 @@ bool FortSettings::readConf(FirewallConf &conf) const || tryToReadConf(conf, backupFilePath); } -bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath) const +bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath) { const QByteArray data = FileUtil::readFileData(filePath); QJsonParseError jsonParseError; const QJsonDocument jsonDoc = QJsonDocument::fromJson( data, &jsonParseError); - if (jsonParseError.error != QJsonParseError::NoError) + if (jsonParseError.error != QJsonParseError::NoError) { + setErrorMessage(jsonParseError.errorString()); return false; + } conf.fromVariant(jsonDoc.toVariant()); @@ -123,11 +133,20 @@ bool FortSettings::writeConf(const FirewallConf &conf) const QString backupFilePath = confBackupFilePath(); if (FileUtil::fileExists(backupFilePath) - && !FileUtil::renameFile(backupFilePath, filePath)) + && !FileUtil::renameFile(backupFilePath, filePath)) { + setErrorMessage(tr("Can't rename old backup conf. file")); + return false; + } + + if (!tryToWriteConf(conf, backupFilePath)) return false; - return tryToWriteConf(conf, backupFilePath) - && FileUtil::renameFile(backupFilePath, filePath); + if (!FileUtil::renameFile(backupFilePath, filePath)) { + setErrorMessage(tr("Can't rename backup conf. file")); + return false; + } + + return true; } bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &filePath) @@ -137,8 +156,10 @@ bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &fileP const QByteArray data = jsonDoc.toJson(QJsonDocument::Indented); - if (!FileUtil::writeFileData(filePath, data)) + if (!FileUtil::writeFileData(filePath, data)) { + setErrorMessage(tr("Can't write conf. file")); return false; + } return writeConfFlags(conf); } diff --git a/src/ui/fortsettings.h b/src/ui/fortsettings.h index 5b2fb892..791ce096 100644 --- a/src/ui/fortsettings.h +++ b/src/ui/fortsettings.h @@ -13,6 +13,7 @@ class FortSettings : public QObject Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY iniChanged) Q_PROPERTY(QString updatesUrl READ updatesUrl WRITE setUpdatesUrl NOTIFY iniChanged) Q_PROPERTY(bool startWithWindows READ startWithWindows WRITE setStartWithWindows NOTIFY startWithWindowsChanged) + Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) public: explicit FortSettings(const QStringList &args, @@ -32,15 +33,18 @@ public: bool startWithWindows() const; void setStartWithWindows(bool start); + QString errorMessage() const { return m_errorMessage; } + signals: void iniChanged(); void startWithWindowsChanged(); + void errorMessageChanged(); public slots: QString confFilePath() const; QString confBackupFilePath() const; - bool readConf(FirewallConf &conf) const; + bool readConf(FirewallConf &conf); bool writeConf(const FirewallConf &conf); bool readConfFlags(FirewallConf &conf) const; @@ -50,7 +54,9 @@ private: void processArguments(const QStringList &args); void setupIni(); - bool tryToReadConf(FirewallConf &conf, const QString &filePath) const; + void setErrorMessage(const QString &errorMessage); + + bool tryToReadConf(FirewallConf &conf, const QString &filePath); bool tryToWriteConf(const FirewallConf &conf, const QString &filePath); bool iniBool(const QString &key, bool defaultValue = false) const; @@ -72,6 +78,8 @@ private: QString m_profilePath; + QString m_errorMessage; + QSettings *m_ini; }; diff --git a/src/ui/main.cpp b/src/ui/main.cpp index bc23a207..338216f0 100644 --- a/src/ui/main.cpp +++ b/src/ui/main.cpp @@ -22,6 +22,11 @@ int main(int argc, char *argv[]) return FortCommon::provRegister(true); } + // Open the driver device + if (!fortManager.setupDriver()) { + return 1; + } + fortManager.showTrayIcon(); return app.exec();