mirror of
https://github.com/tnodir/fort
synced 2024-11-15 04:15:22 +00:00
Add FortSettings class.
This commit is contained in:
parent
63a42aa2a5
commit
dd882b5d7a
@ -14,6 +14,7 @@ SOURCES += \
|
||||
firewallLog/logbuffer.cpp \
|
||||
firewallLog/logentry.cpp \
|
||||
fortcommon.cpp \
|
||||
fortsettings.cpp \
|
||||
util/confutil.cpp \
|
||||
util/device.cpp \
|
||||
util/fileutil.cpp \
|
||||
@ -27,6 +28,7 @@ HEADERS += \
|
||||
firewallLog/logbuffer.h \
|
||||
firewallLog/logentry.h \
|
||||
fortcommon.h \
|
||||
fortsettings.h \
|
||||
util/confutil.h \
|
||||
util/device.h \
|
||||
util/fileutil.h \
|
||||
|
@ -61,6 +61,28 @@ void FirewallConf::setAppAllowAll(bool appAllowAll)
|
||||
}
|
||||
}
|
||||
|
||||
quint32 FirewallConf::appGroupBits() const
|
||||
{
|
||||
quint32 groupBits = 0;
|
||||
int i = 0;
|
||||
foreach (const AppGroup *appGroup, appGroupsList()) {
|
||||
if (appGroup->enabled()) {
|
||||
groupBits |= (1 << i);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return groupBits;
|
||||
}
|
||||
|
||||
void FirewallConf::setAppGroupBits(quint32 groupBits)
|
||||
{
|
||||
int i = 0;
|
||||
foreach (AppGroup *appGroup, appGroupsList()) {
|
||||
appGroup->setEnabled(groupBits & (1 << i));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void FirewallConf::setIpIncludeText(const QString &ipIncludeText)
|
||||
{
|
||||
if (m_ipIncludeText != ipIncludeText) {
|
||||
@ -113,11 +135,6 @@ QVariant FirewallConf::toVariant() const
|
||||
{
|
||||
QVariantMap map;
|
||||
|
||||
//map["filterDisabled"] = filterDisabled();
|
||||
map["ipIncludeAll"] = ipIncludeAll();
|
||||
map["ipExcludeAll"] = ipExcludeAll();
|
||||
map["appLogBlocked"] = appLogBlocked();
|
||||
map["appBlockAll"] = appBlockAll();
|
||||
map["ipIncludeText"] = ipIncludeText();
|
||||
map["ipExcludeText"] = ipExcludeText();
|
||||
|
||||
@ -134,11 +151,6 @@ void FirewallConf::fromVariant(const QVariant &v)
|
||||
{
|
||||
QVariantMap map = v.toMap();
|
||||
|
||||
//m_filterDisabled = map["filterDisabled"].toBool();
|
||||
m_ipIncludeAll = map["ipIncludeAll"].toBool();
|
||||
m_ipExcludeAll = map["ipExcludeAll"].toBool();
|
||||
m_appLogBlocked = map["appLogBlocked"].toBool();
|
||||
m_appBlockAll = map["appBlockAll"].toBool();
|
||||
m_ipIncludeText = map["ipIncludeText"].toString();
|
||||
m_ipExcludeText = map["ipExcludeText"].toString();
|
||||
|
||||
|
@ -42,6 +42,9 @@ public:
|
||||
bool appAllowAll() const { return m_appAllowAll; }
|
||||
void setAppAllowAll(bool appAllowAll);
|
||||
|
||||
quint32 appGroupBits() const;
|
||||
void setAppGroupBits(quint32 groupBits);
|
||||
|
||||
QString ipIncludeText() const { return m_ipIncludeText; }
|
||||
void setIpIncludeText(const QString &ipIncludeText);
|
||||
|
||||
|
187
src/ui/fortsettings.cpp
Normal file
187
src/ui/fortsettings.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
#include "fortsettings.h"
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QJsonDocument>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include "conf/firewallconf.h"
|
||||
#include "util/fileutil.h"
|
||||
|
||||
FortSettings::FortSettings(const QStringList &args,
|
||||
QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
processArguments(args);
|
||||
setupIni();
|
||||
}
|
||||
|
||||
void FortSettings::processArguments(const QStringList &args)
|
||||
{
|
||||
const QCommandLineOption profileOption("profile", "Directory to store settings.");
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.addOption(profileOption);
|
||||
parser.process(args);
|
||||
|
||||
m_profilePath = parser.value(profileOption);
|
||||
if (m_profilePath.isEmpty()) {
|
||||
m_profilePath = QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppConfigLocation);
|
||||
}
|
||||
m_profilePath = FileUtil::absolutePath(m_profilePath);
|
||||
|
||||
const QLatin1Char slash('/');
|
||||
if (!m_profilePath.endsWith(slash))
|
||||
m_profilePath += slash;
|
||||
}
|
||||
|
||||
void FortSettings::setupIni()
|
||||
{
|
||||
const QString qrcIniPath(":/FortFirewall.ini");
|
||||
const QString iniPath(m_profilePath + "FortFirewall.ini");
|
||||
|
||||
FileUtil::makePath(m_profilePath);
|
||||
|
||||
// Copy default .ini into writable location
|
||||
if (!FileUtil::fileExists(iniPath)) {
|
||||
const QString text = FileUtil::readFile(qrcIniPath);
|
||||
if (!FileUtil::writeFile(iniPath, text)) {
|
||||
FileUtil::removeFile(iniPath);
|
||||
}
|
||||
}
|
||||
|
||||
m_ini = new QSettings(iniPath, QSettings::IniFormat, this);
|
||||
}
|
||||
|
||||
QString FortSettings::confFilePath() const
|
||||
{
|
||||
return m_profilePath + QLatin1String("FortFirewall.conf");
|
||||
}
|
||||
|
||||
QString FortSettings::confBackupFilePath() const
|
||||
{
|
||||
return confFilePath() + QLatin1String(".backup");
|
||||
}
|
||||
|
||||
bool FortSettings::readConf(FirewallConf &conf) const
|
||||
{
|
||||
const QString filePath = confFilePath();
|
||||
const QString backupFilePath = confBackupFilePath();
|
||||
|
||||
return tryToReadConf(conf, filePath)
|
||||
|| tryToReadConf(conf, backupFilePath);
|
||||
}
|
||||
|
||||
bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath) const
|
||||
{
|
||||
const QString text = FileUtil::readFile(filePath);
|
||||
|
||||
QJsonParseError jsonParseError;
|
||||
const QJsonDocument jsonDoc = QJsonDocument::fromJson(
|
||||
text.toUtf8(), &jsonParseError);
|
||||
if (jsonParseError.error != QJsonParseError::NoError)
|
||||
return false;
|
||||
|
||||
conf.fromVariant(jsonDoc.toVariant());
|
||||
|
||||
return readConfFlags(conf);
|
||||
}
|
||||
|
||||
bool FortSettings::writeConf(const FirewallConf &conf)
|
||||
{
|
||||
const QString filePath = confFilePath();
|
||||
const QString backupFilePath = confBackupFilePath();
|
||||
|
||||
if (FileUtil::fileExists(backupFilePath)
|
||||
&& !FileUtil::renameFile(backupFilePath, filePath))
|
||||
return false;
|
||||
|
||||
return tryToWriteConf(conf, backupFilePath)
|
||||
&& FileUtil::renameFile(backupFilePath, filePath);
|
||||
}
|
||||
|
||||
bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &filePath)
|
||||
{
|
||||
const QJsonDocument jsonDoc = QJsonDocument::fromVariant(
|
||||
conf.toVariant());
|
||||
|
||||
const QByteArray data = jsonDoc.toJson(QJsonDocument::Indented);
|
||||
|
||||
if (FileUtil::writeFileData(filePath, data))
|
||||
return false;
|
||||
|
||||
return writeConfFlags(conf);
|
||||
}
|
||||
|
||||
bool FortSettings::readConfFlags(FirewallConf &conf) const
|
||||
{
|
||||
conf.setFilterDisabled(iniBool("filterDisabled"));
|
||||
conf.setIpIncludeAll(iniBool("ipIncludeAll"));
|
||||
conf.setIpExcludeAll(iniBool("ipExcludeAll"));
|
||||
conf.setAppLogBlocked(iniBool("appLogBlocked", true));
|
||||
conf.setAppBlockAll(iniBool("appBlockAll", true));
|
||||
conf.setAppAllowAll(iniBool("appAllowAll"));
|
||||
conf.setAppGroupBits(iniUInt("appGroupBits", 0xFFFF));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FortSettings::writeConfFlags(const FirewallConf &conf)
|
||||
{
|
||||
setIniValue("filterDisabled", conf.filterDisabled());
|
||||
setIniValue("ipIncludeAll", conf.ipIncludeAll());
|
||||
setIniValue("ipExcludeAll", conf.ipExcludeAll());
|
||||
setIniValue("appLogBlocked", conf.appLogBlocked());
|
||||
setIniValue("appBlockAll", conf.appBlockAll());
|
||||
setIniValue("appAllowAll", conf.appAllowAll());
|
||||
setIniValue("appGroupBits", conf.appGroupBits());
|
||||
|
||||
m_ini->sync();
|
||||
|
||||
return m_ini->status() != QSettings::NoError;
|
||||
}
|
||||
|
||||
bool FortSettings::iniBool(const QString &key, bool defaultValue) const
|
||||
{
|
||||
return iniValue(key, defaultValue).toBool();
|
||||
}
|
||||
|
||||
int FortSettings::iniInt(const QString &key, int defaultValue) const
|
||||
{
|
||||
return iniValue(key, defaultValue).toInt();
|
||||
}
|
||||
|
||||
int FortSettings::iniUInt(const QString &key, int defaultValue) const
|
||||
{
|
||||
return iniValue(key, defaultValue).toUInt();
|
||||
}
|
||||
|
||||
int FortSettings::iniReal(const QString &key, qreal defaultValue) const
|
||||
{
|
||||
return iniValue(key, defaultValue).toReal();
|
||||
}
|
||||
|
||||
QString FortSettings::iniText(const QString &key, const QString &defaultValue) const
|
||||
{
|
||||
return iniValue(key, defaultValue).toString();
|
||||
}
|
||||
|
||||
QStringList FortSettings::iniList(const QString &key) const
|
||||
{
|
||||
return iniValue(key).toStringList();
|
||||
}
|
||||
|
||||
QVariant FortSettings::iniValue(const QString &key,
|
||||
const QVariant &defaultValue) const
|
||||
{
|
||||
if (key.isEmpty())
|
||||
return QVariant();
|
||||
|
||||
return m_ini->value(key, defaultValue);
|
||||
}
|
||||
|
||||
void FortSettings::setIniValue(const QString &key, const QVariant &value)
|
||||
{
|
||||
m_ini->setValue(key, value);
|
||||
}
|
53
src/ui/fortsettings.h
Normal file
53
src/ui/fortsettings.h
Normal file
@ -0,0 +1,53 @@
|
||||
#ifndef FORTSETTINGS_H
|
||||
#define FORTSETTINGS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
|
||||
class FirewallConf;
|
||||
|
||||
class FortSettings : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FortSettings(const QStringList &args,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
QString confFilePath() const;
|
||||
QString confBackupFilePath() const;
|
||||
|
||||
bool readConf(FirewallConf &conf) const;
|
||||
bool writeConf(const FirewallConf &conf);
|
||||
|
||||
bool readConfFlags(FirewallConf &conf) const;
|
||||
bool writeConfFlags(const FirewallConf &conf);
|
||||
|
||||
private:
|
||||
void processArguments(const QStringList &args);
|
||||
void setupIni();
|
||||
|
||||
bool tryToReadConf(FirewallConf &conf, const QString &filePath) const;
|
||||
bool tryToWriteConf(const FirewallConf &conf, const QString &filePath);
|
||||
|
||||
bool iniBool(const QString &key, bool defaultValue = false) const;
|
||||
int iniInt(const QString &key, int defaultValue = 0) const;
|
||||
int iniUInt(const QString &key, int defaultValue = 0) const;
|
||||
int iniReal(const QString &key, qreal defaultValue = 0) const;
|
||||
QString iniText(const QString &key, const QString &defaultValue = QString()) const;
|
||||
QStringList iniList(const QString &key) const;
|
||||
|
||||
QVariant iniValue(const QString &key,
|
||||
const QVariant &defaultValue = QVariant()) const;
|
||||
void setIniValue(const QString &key, const QVariant &value);
|
||||
|
||||
private:
|
||||
QString m_profilePath;
|
||||
|
||||
QSettings *m_ini;
|
||||
};
|
||||
|
||||
#endif // FORTSETTINGS_H
|
@ -231,7 +231,7 @@ void ConfUtil::writeData(char *output, const FirewallConf &conf,
|
||||
drvConf->flags.app_block_all = conf.appBlockAll();
|
||||
drvConf->flags.app_allow_all = conf.appAllowAll();
|
||||
|
||||
drvConf->flags.group_bits = appGroupBits(conf);
|
||||
drvConf->flags.group_bits = conf.appGroupBits();
|
||||
|
||||
FortCommon::confAppPermsMaskInit(drvConf);
|
||||
|
||||
@ -284,16 +284,3 @@ void ConfUtil::writeStrings(char **data, const QStringList &list)
|
||||
|
||||
*data += offTableSize + FORT_CONF_STR_DATA_SIZE(off);
|
||||
}
|
||||
|
||||
quint32 ConfUtil::appGroupBits(const FirewallConf &conf)
|
||||
{
|
||||
quint32 groupBits = 0;
|
||||
int i = 0;
|
||||
foreach (const AppGroup *appGroup, conf.appGroupsList()) {
|
||||
if (appGroup->enabled()) {
|
||||
groupBits |= (1 << i);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return groupBits;
|
||||
}
|
||||
|
@ -53,8 +53,6 @@ private:
|
||||
static void writeNumbers(char **data, const QVector<quint32> &array);
|
||||
static void writeStrings(char **data, const QStringList &list);
|
||||
|
||||
static quint32 appGroupBits(const FirewallConf &conf);
|
||||
|
||||
private:
|
||||
QString m_errorMessage;
|
||||
};
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include "fileutil.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QTextStream>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
@ -68,3 +70,61 @@ QString FileUtil::pathToDosPath(const QString &path)
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
QString FileUtil::absolutePath(const QString &path)
|
||||
{
|
||||
return QDir(path).absolutePath();
|
||||
}
|
||||
|
||||
bool FileUtil::makePath(const QString &path)
|
||||
{
|
||||
return QDir().mkpath(path);
|
||||
}
|
||||
|
||||
bool FileUtil::fileExists(const QString &filePath)
|
||||
{
|
||||
return QFileInfo::exists(filePath);
|
||||
}
|
||||
|
||||
bool FileUtil::removeFile(const QString &filePath)
|
||||
{
|
||||
return QFile::remove(filePath);
|
||||
}
|
||||
|
||||
bool FileUtil::renameFile(const QString &oldFilePath, const QString &newFilePath)
|
||||
{
|
||||
removeFile(newFilePath);
|
||||
|
||||
return QFile::rename(oldFilePath, newFilePath);
|
||||
}
|
||||
|
||||
bool FileUtil::copyFile(const QString &filePath, const QString &newFilePath)
|
||||
{
|
||||
return QFile::copy(filePath, newFilePath);
|
||||
}
|
||||
|
||||
QString FileUtil::readFile(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return QString();
|
||||
|
||||
QTextStream in(&file);
|
||||
const QString data = in.readAll();
|
||||
return data;
|
||||
}
|
||||
|
||||
bool FileUtil::writeFile(const QString &filePath, const QString &text)
|
||||
{
|
||||
return writeFileData(filePath, text.toUtf8());
|
||||
}
|
||||
|
||||
bool FileUtil::writeFileData(const QString &filePath, const QByteArray &data)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
||||
return false;
|
||||
|
||||
return file.write(data) == data.size()
|
||||
&& file.flush();
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
#define FILEUTIL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QHash>
|
||||
|
||||
class FileUtil : public QObject
|
||||
{
|
||||
@ -22,6 +21,19 @@ public:
|
||||
|
||||
// Convert Win32 path to Native path
|
||||
static QString pathToDosPath(const QString &path);
|
||||
|
||||
static QString absolutePath(const QString &path);
|
||||
|
||||
static bool makePath(const QString &path);
|
||||
static bool fileExists(const QString &filePath);
|
||||
static bool removeFile(const QString &filePath);
|
||||
static bool renameFile(const QString &oldFilePath, const QString &newFilePath);
|
||||
static bool copyFile(const QString &filePath, const QString &newFilePath);
|
||||
|
||||
static QString readFile(const QString &filePath);
|
||||
|
||||
static bool writeFile(const QString &filePath, const QString &text);
|
||||
static bool writeFileData(const QString &filePath, const QByteArray &data);
|
||||
};
|
||||
|
||||
#endif // FILEUTIL_H
|
||||
|
Loading…
Reference in New Issue
Block a user