2017-08-30 16:20:31 +00:00
|
|
|
#include "fortsettings.h"
|
|
|
|
|
2017-09-03 07:20:07 +00:00
|
|
|
#include <QApplication>
|
2017-08-30 16:20:31 +00:00
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QSettings>
|
|
|
|
|
2017-09-02 10:17:51 +00:00
|
|
|
#include "conf/addressgroup.h"
|
2017-08-30 16:20:31 +00:00
|
|
|
#include "conf/firewallconf.h"
|
|
|
|
#include "util/fileutil.h"
|
|
|
|
|
|
|
|
FortSettings::FortSettings(const QStringList &args,
|
|
|
|
QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{
|
|
|
|
processArguments(args);
|
|
|
|
setupIni();
|
|
|
|
}
|
|
|
|
|
2017-09-03 07:20:07 +00:00
|
|
|
bool FortSettings::startWithWindows() const
|
|
|
|
{
|
|
|
|
return FileUtil::fileExists(startupShortcutPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FortSettings::setStartWithWindows(bool start)
|
|
|
|
{
|
|
|
|
const QString linkPath = startupShortcutPath();
|
|
|
|
if (start) {
|
|
|
|
FileUtil::linkFile(qApp->applicationFilePath(), linkPath);
|
|
|
|
} else {
|
|
|
|
FileUtil::removeFile(linkPath);
|
|
|
|
}
|
|
|
|
emit startWithWindowsChanged();
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:20:31 +00:00
|
|
|
void FortSettings::processArguments(const QStringList &args)
|
|
|
|
{
|
|
|
|
QCommandLineParser parser;
|
2017-09-03 09:20:37 +00:00
|
|
|
|
|
|
|
const QCommandLineOption bootOption(
|
|
|
|
"boot", "Block access to network when Fort Firewall is not running.");
|
|
|
|
parser.addOption(bootOption);
|
|
|
|
|
|
|
|
const QCommandLineOption profileOption(
|
|
|
|
QStringList() << "p" << "profile",
|
|
|
|
"Directory to store settings.", "profile");
|
2017-08-30 16:20:31 +00:00
|
|
|
parser.addOption(profileOption);
|
2017-09-03 09:20:37 +00:00
|
|
|
|
2017-09-03 10:16:51 +00:00
|
|
|
parser.addVersionOption();
|
|
|
|
parser.addHelpOption();
|
|
|
|
|
2017-08-30 16:20:31 +00:00
|
|
|
parser.process(args);
|
|
|
|
|
2017-09-03 09:20:37 +00:00
|
|
|
m_boot = parser.isSet(bootOption);
|
|
|
|
|
2017-08-30 16:20:31 +00:00
|
|
|
m_profilePath = parser.value(profileOption);
|
|
|
|
if (m_profilePath.isEmpty()) {
|
2017-09-03 06:49:58 +00:00
|
|
|
m_profilePath = FileUtil::appConfigLocation();
|
2017-08-30 16:20:31 +00:00
|
|
|
}
|
|
|
|
m_profilePath = FileUtil::absolutePath(m_profilePath);
|
|
|
|
|
|
|
|
const QLatin1Char slash('/');
|
2017-09-03 09:20:37 +00:00
|
|
|
if (!m_profilePath.endsWith(slash)) {
|
2017-08-30 16:20:31 +00:00
|
|
|
m_profilePath += slash;
|
2017-09-03 09:20:37 +00:00
|
|
|
}
|
2017-08-30 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FortSettings::setupIni()
|
|
|
|
{
|
|
|
|
const QString iniPath(m_profilePath + "FortFirewall.ini");
|
|
|
|
|
|
|
|
FileUtil::makePath(m_profilePath);
|
|
|
|
|
|
|
|
m_ini = new QSettings(iniPath, QSettings::IniFormat, this);
|
|
|
|
}
|
|
|
|
|
2017-09-03 19:41:03 +00:00
|
|
|
void FortSettings::setErrorMessage(const QString &errorMessage)
|
|
|
|
{
|
|
|
|
if (m_errorMessage != errorMessage) {
|
|
|
|
m_errorMessage = errorMessage;
|
|
|
|
emit errorMessageChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:20:31 +00:00
|
|
|
QString FortSettings::confFilePath() const
|
|
|
|
{
|
|
|
|
return m_profilePath + QLatin1String("FortFirewall.conf");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FortSettings::confBackupFilePath() const
|
|
|
|
{
|
|
|
|
return confFilePath() + QLatin1String(".backup");
|
|
|
|
}
|
|
|
|
|
2017-09-03 19:41:03 +00:00
|
|
|
bool FortSettings::readConf(FirewallConf &conf)
|
2017-08-30 16:20:31 +00:00
|
|
|
{
|
|
|
|
const QString filePath = confFilePath();
|
|
|
|
const QString backupFilePath = confBackupFilePath();
|
|
|
|
|
2017-09-11 03:54:19 +00:00
|
|
|
if (!(FileUtil::fileExists(filePath)
|
|
|
|
|| FileUtil::fileExists(backupFilePath)))
|
|
|
|
return true;
|
|
|
|
|
2017-08-30 16:20:31 +00:00
|
|
|
return tryToReadConf(conf, filePath)
|
|
|
|
|| tryToReadConf(conf, backupFilePath);
|
|
|
|
}
|
|
|
|
|
2017-09-03 19:41:03 +00:00
|
|
|
bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath)
|
2017-08-30 16:20:31 +00:00
|
|
|
{
|
2017-09-02 12:29:07 +00:00
|
|
|
const QByteArray data = FileUtil::readFileData(filePath);
|
2017-08-30 16:20:31 +00:00
|
|
|
|
|
|
|
QJsonParseError jsonParseError;
|
|
|
|
const QJsonDocument jsonDoc = QJsonDocument::fromJson(
|
2017-09-02 12:29:07 +00:00
|
|
|
data, &jsonParseError);
|
2017-09-03 19:41:03 +00:00
|
|
|
if (jsonParseError.error != QJsonParseError::NoError) {
|
|
|
|
setErrorMessage(jsonParseError.errorString());
|
2017-08-30 16:20:31 +00:00
|
|
|
return false;
|
2017-09-03 19:41:03 +00:00
|
|
|
}
|
2017-08-30 16:20:31 +00:00
|
|
|
|
|
|
|
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)
|
2017-09-03 19:41:03 +00:00
|
|
|
&& !FileUtil::renameFile(backupFilePath, filePath)) {
|
|
|
|
setErrorMessage(tr("Can't rename old backup conf. file"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tryToWriteConf(conf, backupFilePath))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!FileUtil::renameFile(backupFilePath, filePath)) {
|
|
|
|
setErrorMessage(tr("Can't rename backup conf. file"));
|
2017-08-30 16:20:31 +00:00
|
|
|
return false;
|
2017-09-03 19:41:03 +00:00
|
|
|
}
|
2017-08-30 16:20:31 +00:00
|
|
|
|
2017-09-03 19:41:03 +00:00
|
|
|
return true;
|
2017-08-30 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &filePath)
|
|
|
|
{
|
|
|
|
const QJsonDocument jsonDoc = QJsonDocument::fromVariant(
|
|
|
|
conf.toVariant());
|
|
|
|
|
|
|
|
const QByteArray data = jsonDoc.toJson(QJsonDocument::Indented);
|
|
|
|
|
2017-09-03 19:41:03 +00:00
|
|
|
if (!FileUtil::writeFileData(filePath, data)) {
|
|
|
|
setErrorMessage(tr("Can't write conf. file"));
|
2017-08-30 16:20:31 +00:00
|
|
|
return false;
|
2017-09-03 19:41:03 +00:00
|
|
|
}
|
2017-08-30 16:20:31 +00:00
|
|
|
|
|
|
|
return writeConfFlags(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FortSettings::readConfFlags(FirewallConf &conf) const
|
|
|
|
{
|
2017-08-30 16:31:52 +00:00
|
|
|
m_ini->beginGroup("confFlags");
|
2017-09-01 10:05:30 +00:00
|
|
|
conf.setFilterEnabled(iniBool("filterEnabled", true));
|
2017-09-02 10:17:51 +00:00
|
|
|
conf.ipInclude()->setUseAll(iniBool("ipIncludeAll"));
|
|
|
|
conf.ipExclude()->setUseAll(iniBool("ipExcludeAll"));
|
2017-08-30 16:20:31 +00:00
|
|
|
conf.setAppBlockAll(iniBool("appBlockAll", true));
|
|
|
|
conf.setAppAllowAll(iniBool("appAllowAll"));
|
|
|
|
conf.setAppGroupBits(iniUInt("appGroupBits", 0xFFFF));
|
2017-08-30 16:31:52 +00:00
|
|
|
m_ini->endGroup();
|
2017-08-30 16:20:31 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FortSettings::writeConfFlags(const FirewallConf &conf)
|
|
|
|
{
|
2017-08-30 16:31:52 +00:00
|
|
|
m_ini->beginGroup("confFlags");
|
2017-09-01 10:05:30 +00:00
|
|
|
setIniValue("filterEnabled", conf.filterEnabled());
|
2017-09-02 10:17:51 +00:00
|
|
|
setIniValue("ipIncludeAll", conf.ipInclude()->useAll());
|
|
|
|
setIniValue("ipExcludeAll", conf.ipExclude()->useAll());
|
2017-08-30 16:20:31 +00:00
|
|
|
setIniValue("appBlockAll", conf.appBlockAll());
|
|
|
|
setIniValue("appAllowAll", conf.appAllowAll());
|
|
|
|
setIniValue("appGroupBits", conf.appGroupBits());
|
2017-08-30 16:31:52 +00:00
|
|
|
m_ini->endGroup();
|
2017-08-30 16:20:31 +00:00
|
|
|
|
|
|
|
m_ini->sync();
|
|
|
|
|
2017-09-01 13:13:33 +00:00
|
|
|
return m_ini->status() == QSettings::NoError;
|
2017-08-30 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:31:52 +00:00
|
|
|
void FortSettings::setIniValue(const QString &key, const QVariant &value,
|
|
|
|
const QVariant &defaultValue)
|
2017-08-30 16:20:31 +00:00
|
|
|
{
|
2017-08-30 16:31:52 +00:00
|
|
|
if (m_ini->value(key, defaultValue) == value)
|
|
|
|
return;
|
|
|
|
|
2017-08-30 16:20:31 +00:00
|
|
|
m_ini->setValue(key, value);
|
2017-08-30 16:31:52 +00:00
|
|
|
emit iniChanged();
|
2017-08-30 16:20:31 +00:00
|
|
|
}
|
2017-09-03 07:20:07 +00:00
|
|
|
|
|
|
|
QString FortSettings::startupShortcutPath()
|
|
|
|
{
|
|
|
|
return FileUtil::applicationsLocation() + QLatin1Char('\\')
|
|
|
|
+ "Startup" + QLatin1Char('\\')
|
|
|
|
+ qApp->applicationName() + ".lnk";
|
|
|
|
}
|