mirror of
https://github.com/tnodir/fort
synced 2024-11-15 07:25:18 +00:00
UI: Refactor ConfUtil
This commit is contained in:
parent
e0957ab1bb
commit
f26458c49a
@ -39,12 +39,11 @@ namespace {
|
||||
void validateDriver(Device &device)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int verSize = confUtil.writeVersion(buf);
|
||||
const int verSize = confUtil.writeVersion();
|
||||
ASSERT_NE(verSize, 0);
|
||||
|
||||
ASSERT_TRUE(device.ioctl(DriverCommon::ioctlValidate(), buf.data(), verSize));
|
||||
ASSERT_TRUE(device.ioctl(DriverCommon::ioctlValidate(), confUtil.data(), verSize));
|
||||
}
|
||||
|
||||
void setConf(Device &device)
|
||||
@ -71,11 +70,10 @@ void setConf(Device &device)
|
||||
|
||||
ConfUtil confUtil;
|
||||
|
||||
QByteArray buf;
|
||||
const int confIoSize = confUtil.write(conf, nullptr, envManager, buf);
|
||||
const int confIoSize = confUtil.write(conf, nullptr, envManager);
|
||||
ASSERT_NE(confIoSize, 0);
|
||||
|
||||
ASSERT_TRUE(device.ioctl(DriverCommon::ioctlSetConf(), buf.data(), confIoSize));
|
||||
ASSERT_TRUE(device.ioctl(DriverCommon::ioctlSetConf(), confUtil.data(), confIoSize));
|
||||
}
|
||||
|
||||
void printLogs(LogBuffer &buf)
|
||||
|
@ -69,15 +69,14 @@ TEST_F(ConfUtilTest, confWriteRead)
|
||||
|
||||
ConfUtil confUtil;
|
||||
|
||||
QByteArray buf;
|
||||
const int confIoSize = confUtil.write(conf, nullptr, envManager, buf);
|
||||
const int confIoSize = confUtil.write(conf, nullptr, envManager);
|
||||
if (confIoSize == 0) {
|
||||
qCritical() << "Error:" << confUtil.errorMessage();
|
||||
ASSERT_NE(confIoSize, 0);
|
||||
}
|
||||
|
||||
// Check the buffer
|
||||
const char *data = buf.constData() + DriverCommon::confIoConfOff();
|
||||
const char *data = confUtil.data() + DriverCommon::confIoConfOff();
|
||||
|
||||
ASSERT_FALSE(DriverCommon::confIp4InRange(data, 0, true));
|
||||
ASSERT_FALSE(DriverCommon::confIp4InRange(data, NetUtil::textToIp4("9.255.255.255")));
|
||||
|
@ -571,10 +571,9 @@ qint64 ConfAppManager::getAlertAppId()
|
||||
bool ConfAppManager::updateDriverConf(bool onlyFlags)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int confSize = onlyFlags ? confUtil.writeFlags(*conf(), buf)
|
||||
: confUtil.write(*conf(), this, *IoC<EnvManager>(), buf);
|
||||
const int confSize = onlyFlags ? confUtil.writeFlags(*conf())
|
||||
: confUtil.write(*conf(), this, *IoC<EnvManager>());
|
||||
|
||||
if (confSize == 0) {
|
||||
qCWarning(LC) << "Driver config error:" << confUtil.errorMessage();
|
||||
@ -582,7 +581,7 @@ bool ConfAppManager::updateDriverConf(bool onlyFlags)
|
||||
}
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
if (!driverManager->writeConf(buf, confSize, onlyFlags)) {
|
||||
if (!driverManager->writeConf(confUtil.buffer(), confSize, onlyFlags)) {
|
||||
qCWarning(LC) << "Update driver error:" << driverManager->errorMessage();
|
||||
return false;
|
||||
}
|
||||
@ -644,9 +643,8 @@ bool ConfAppManager::updateDriverDeleteApp(const QString &appPath)
|
||||
bool ConfAppManager::updateDriverUpdateApp(const App &app, bool remove)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int entrySize = confUtil.writeAppEntry(app, /*isNew=*/false, buf);
|
||||
const int entrySize = confUtil.writeAppEntry(app);
|
||||
|
||||
if (entrySize == 0) {
|
||||
qCWarning(LC) << "Driver config error:" << confUtil.errorMessage();
|
||||
@ -654,7 +652,7 @@ bool ConfAppManager::updateDriverUpdateApp(const App &app, bool remove)
|
||||
}
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
if (!driverManager->writeApp(buf, entrySize, remove)) {
|
||||
if (!driverManager->writeApp(confUtil.buffer(), entrySize, remove)) {
|
||||
qCWarning(LC) << "Update driver error:" << driverManager->errorMessage();
|
||||
return false;
|
||||
}
|
||||
|
@ -812,9 +812,8 @@ bool ConfManager::validateConf(const FirewallConf &newConf)
|
||||
return true;
|
||||
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int confSize = confUtil.write(newConf, IoC<ConfAppManager>(), *IoC<EnvManager>(), buf);
|
||||
const int confSize = confUtil.write(newConf, IoC<ConfAppManager>(), *IoC<EnvManager>());
|
||||
if (confSize == 0) {
|
||||
qCCritical(LC) << "Conf save error:" << confUtil.errorMessage();
|
||||
return false;
|
||||
@ -826,12 +825,11 @@ bool ConfManager::validateConf(const FirewallConf &newConf)
|
||||
bool ConfManager::validateDriver()
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int verSize = confUtil.writeVersion(buf);
|
||||
const int verSize = confUtil.writeVersion();
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
return driverManager->validate(buf, verSize);
|
||||
return driverManager->validate(confUtil.buffer(), verSize);
|
||||
}
|
||||
|
||||
void ConfManager::updateServices()
|
||||
@ -854,12 +852,11 @@ void ConfManager::updateDriverServices(
|
||||
const QVector<ServiceInfo> &services, int runningServicesCount)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int outSize = confUtil.writeServices(services, runningServicesCount, buf);
|
||||
const int outSize = confUtil.writeServices(services, runningServicesCount);
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
driverManager->writeServices(buf, outSize);
|
||||
driverManager->writeServices(confUtil.buffer(), outSize);
|
||||
}
|
||||
|
||||
bool ConfManager::loadFromDb(FirewallConf &conf, bool &isNew)
|
||||
|
@ -300,24 +300,22 @@ void ConfRuleManager::updateDriverRules(quint32 rulesMask, quint32 enabledMask,
|
||||
const QList<QByteArray> &rulesData)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
#if 0
|
||||
const int entrySize = confUtil.writeRules(rulesMask, enabledMask, dataSize, rulesData, buf);
|
||||
const int entrySize = confUtil.writeRules(rulesMask, enabledMask, dataSize, rulesData);
|
||||
|
||||
driverWriteRules(confUtil, buf, entrySize);
|
||||
driverWriteRules(confUtil, confUtil.buffer(), entrySize);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ConfRuleManager::updateDriverRuleFlag(int ruleId, bool enabled)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
#if 0
|
||||
const int entrySize = confUtil.writeRuleFlag(ruleId, enabled, buf);
|
||||
const int entrySize = confUtil.writeRuleFlag(ruleId, enabled);
|
||||
|
||||
return driverWriteRules(confUtil, buf, entrySize, /*onlyFlags=*/true);
|
||||
return driverWriteRules(confUtil, confUtil.buffer(), entrySize, /*onlyFlags=*/true);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ const char *const sqlUpdateZoneResult =
|
||||
" source_modtime = ?5, last_run = ?6, last_success = ?7"
|
||||
" WHERE zone_id = ?1;";
|
||||
|
||||
bool driverWriteZones(ConfUtil &confUtil, QByteArray &buf, int entrySize, bool onlyFlags = false)
|
||||
bool driverWriteZones(ConfUtil &confUtil, int entrySize, bool onlyFlags = false)
|
||||
{
|
||||
if (entrySize == 0) {
|
||||
qCWarning(LC) << "Driver config error:" << confUtil.errorMessage();
|
||||
@ -68,7 +68,7 @@ bool driverWriteZones(ConfUtil &confUtil, QByteArray &buf, int entrySize, bool o
|
||||
}
|
||||
|
||||
auto driverManager = IoC<DriverManager>();
|
||||
if (!driverManager->writeZones(buf, entrySize, onlyFlags)) {
|
||||
if (!driverManager->writeZones(confUtil.buffer(), entrySize, onlyFlags)) {
|
||||
qCWarning(LC) << "Update driver error:" << driverManager->errorMessage();
|
||||
return false;
|
||||
}
|
||||
@ -240,21 +240,19 @@ void ConfZoneManager::updateDriverZones(quint32 zonesMask, quint32 enabledMask,
|
||||
const QList<QByteArray> &zonesData)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int entrySize = confUtil.writeZones(zonesMask, enabledMask, dataSize, zonesData, buf);
|
||||
const int entrySize = confUtil.writeZones(zonesMask, enabledMask, dataSize, zonesData);
|
||||
|
||||
driverWriteZones(confUtil, buf, entrySize);
|
||||
driverWriteZones(confUtil, entrySize);
|
||||
}
|
||||
|
||||
bool ConfZoneManager::updateDriverZoneFlag(int zoneId, bool enabled)
|
||||
{
|
||||
ConfUtil confUtil;
|
||||
QByteArray buf;
|
||||
|
||||
const int entrySize = confUtil.writeZoneFlag(zoneId, enabled, buf);
|
||||
const int entrySize = confUtil.writeZoneFlag(zoneId, enabled);
|
||||
|
||||
return driverWriteZones(confUtil, buf, entrySize, /*onlyFlags=*/true);
|
||||
return driverWriteZones(confUtil, entrySize, /*onlyFlags=*/true);
|
||||
}
|
||||
|
||||
bool ConfZoneManager::beginTransaction()
|
||||
|
@ -122,11 +122,11 @@ bool TaskZoneDownloader::storeAddresses(const StringViewList &list)
|
||||
|
||||
// Store binary file
|
||||
ConfUtil confUtil;
|
||||
const int bufSize = confUtil.writeZone(ipRange, m_zoneData);
|
||||
const int bufSize = confUtil.writeZone(ipRange);
|
||||
if (bufSize == 0)
|
||||
return false;
|
||||
|
||||
m_zoneData.resize(bufSize);
|
||||
m_zoneData = confUtil.buffer();
|
||||
|
||||
const auto binData = qCompress(m_zoneData);
|
||||
|
||||
@ -160,9 +160,10 @@ bool TaskZoneDownloader::saveAddressesAsText(const QString &filePath)
|
||||
QString text;
|
||||
|
||||
if (loadAddresses() && !zoneData().isEmpty()) {
|
||||
ConfUtil confUtil;
|
||||
ConfUtil confUtil(zoneData());
|
||||
|
||||
IpRange ipRange;
|
||||
if (!confUtil.loadZone(zoneData(), ipRange))
|
||||
if (!confUtil.loadZone(ipRange))
|
||||
return false;
|
||||
|
||||
text = ipRange.toText();
|
||||
|
@ -77,7 +77,8 @@ void writeConfFlags(const FirewallConf &conf, PFORT_CONF_FLAGS confFlags)
|
||||
|
||||
}
|
||||
|
||||
ConfUtil::ConfUtil(QObject *parent) : QObject(parent) { }
|
||||
ConfUtil::ConfUtil(const QByteArray &buffer, QObject *parent) :
|
||||
QObject(parent), m_buffer(buffer) { }
|
||||
|
||||
int ConfUtil::ruleMaxCount()
|
||||
{
|
||||
@ -104,41 +105,40 @@ int ConfUtil::zoneMaxCount()
|
||||
return FORT_CONF_ZONE_MAX;
|
||||
}
|
||||
|
||||
int ConfUtil::writeVersion(QByteArray &buf)
|
||||
int ConfUtil::writeVersion()
|
||||
{
|
||||
const int verSize = sizeof(FORT_CONF_VERSION);
|
||||
|
||||
buf.reserve(verSize);
|
||||
buffer().reserve(verSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_VERSION confVer = (PFORT_CONF_VERSION) buf.data();
|
||||
PFORT_CONF_VERSION confVer = (PFORT_CONF_VERSION) buffer().data();
|
||||
|
||||
confVer->driver_version = DRIVER_VERSION;
|
||||
|
||||
return verSize;
|
||||
}
|
||||
|
||||
int ConfUtil::writeServices(
|
||||
const QVector<ServiceInfo> &services, int runningServicesCount, QByteArray &buf)
|
||||
int ConfUtil::writeServices(const QVector<ServiceInfo> &services, int runningServicesCount)
|
||||
{
|
||||
buf.reserve(FORT_SERVICE_INFO_LIST_MIN_SIZE);
|
||||
buffer().reserve(FORT_SERVICE_INFO_LIST_MIN_SIZE);
|
||||
|
||||
int outSize = writeServicesHeader(buf.data(), runningServicesCount);
|
||||
int outSize = writeServicesHeader(buffer().data(), runningServicesCount);
|
||||
|
||||
for (const ServiceInfo &info : services) {
|
||||
if (!info.isRunning)
|
||||
continue;
|
||||
|
||||
buf.reserve(outSize + FORT_SERVICE_INFO_MAX_SIZE);
|
||||
buffer().reserve(outSize + FORT_SERVICE_INFO_MAX_SIZE);
|
||||
|
||||
outSize += writeServiceInfo(buf.data() + outSize, info);
|
||||
outSize += writeServiceInfo(buffer().data() + outSize, info);
|
||||
}
|
||||
|
||||
return outSize;
|
||||
}
|
||||
|
||||
int ConfUtil::write(const FirewallConf &conf, ConfAppsWalker *confAppsWalker,
|
||||
EnvManager &envManager, QByteArray &buf)
|
||||
int ConfUtil::write(
|
||||
const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager)
|
||||
{
|
||||
quint32 addressGroupsSize = 0;
|
||||
longs_arr_t addressGroupOffsets;
|
||||
@ -173,29 +173,29 @@ int ConfUtil::write(const FirewallConf &conf, ConfAppsWalker *confAppsWalker,
|
||||
+ FORT_CONF_STR_DATA_SIZE(opt.prefixAppsSize)
|
||||
+ FORT_CONF_STR_DATA_SIZE(opt.exeAppsSize));
|
||||
|
||||
buf.reserve(confIoSize);
|
||||
buffer().reserve(confIoSize);
|
||||
|
||||
writeConf(
|
||||
buf.data(), conf, addressRanges, addressGroupOffsets, appPeriods, appPeriodsCount, opt);
|
||||
writeConf(buffer().data(), conf, addressRanges, addressGroupOffsets, appPeriods,
|
||||
appPeriodsCount, opt);
|
||||
|
||||
return confIoSize;
|
||||
}
|
||||
|
||||
int ConfUtil::writeFlags(const FirewallConf &conf, QByteArray &buf)
|
||||
int ConfUtil::writeFlags(const FirewallConf &conf)
|
||||
{
|
||||
const int flagsSize = sizeof(FORT_CONF_FLAGS);
|
||||
|
||||
buf.reserve(flagsSize);
|
||||
buffer().reserve(flagsSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_FLAGS confFlags = (PFORT_CONF_FLAGS) buf.data();
|
||||
PFORT_CONF_FLAGS confFlags = (PFORT_CONF_FLAGS) buffer().data();
|
||||
|
||||
writeConfFlags(conf, confFlags);
|
||||
|
||||
return flagsSize;
|
||||
}
|
||||
|
||||
int ConfUtil::writeAppEntry(const App &app, bool isNew, QByteArray &buf)
|
||||
int ConfUtil::writeAppEntry(const App &app, bool isNew)
|
||||
{
|
||||
appdata_map_t appsMap;
|
||||
quint32 appsSize = 0;
|
||||
@ -203,25 +203,25 @@ int ConfUtil::writeAppEntry(const App &app, bool isNew, QByteArray &buf)
|
||||
if (!addApp(app, isNew, appsMap, appsSize))
|
||||
return 0;
|
||||
|
||||
buf.reserve(appsSize);
|
||||
buffer().reserve(appsSize);
|
||||
|
||||
// Fill the buffer
|
||||
char *data = (char *) buf.data();
|
||||
char *data = (char *) buffer().data();
|
||||
|
||||
writeApps(&data, appsMap);
|
||||
|
||||
return int(appsSize);
|
||||
}
|
||||
|
||||
int ConfUtil::writeZone(const IpRange &ipRange, QByteArray &buf)
|
||||
int ConfUtil::writeZone(const IpRange &ipRange)
|
||||
{
|
||||
const int addrSize = FORT_CONF_ADDR_LIST_SIZE(
|
||||
ipRange.ip4Size(), ipRange.pair4Size(), ipRange.ip6Size(), ipRange.pair6Size());
|
||||
|
||||
buf.reserve(addrSize);
|
||||
buffer().reserve(addrSize);
|
||||
|
||||
// Fill the buffer
|
||||
char *data = (char *) buf.data();
|
||||
char *data = (char *) buffer().data();
|
||||
|
||||
writeAddressList(&data, ipRange);
|
||||
|
||||
@ -229,14 +229,14 @@ int ConfUtil::writeZone(const IpRange &ipRange, QByteArray &buf)
|
||||
}
|
||||
|
||||
int ConfUtil::writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize,
|
||||
const QList<QByteArray> &zonesData, QByteArray &buf)
|
||||
const QList<QByteArray> &zonesData)
|
||||
{
|
||||
const int zonesSize = FORT_CONF_ZONES_DATA_OFF + dataSize;
|
||||
|
||||
buf.reserve(zonesSize);
|
||||
buffer().reserve(zonesSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_ZONES confZones = (PFORT_CONF_ZONES) buf.data();
|
||||
PFORT_CONF_ZONES confZones = (PFORT_CONF_ZONES) buffer().data();
|
||||
char *data = confZones->data;
|
||||
|
||||
memset(confZones, 0, sizeof(FORT_CONF_ZONES_DATA_OFF));
|
||||
@ -272,14 +272,14 @@ void ConfUtil::migrateZoneData(char **data, const QByteArray &zoneData)
|
||||
}
|
||||
}
|
||||
|
||||
int ConfUtil::writeZoneFlag(int zoneId, bool enabled, QByteArray &buf)
|
||||
int ConfUtil::writeZoneFlag(int zoneId, bool enabled)
|
||||
{
|
||||
const int flagSize = sizeof(FORT_CONF_ZONE_FLAG);
|
||||
|
||||
buf.reserve(flagSize);
|
||||
buffer().reserve(flagSize);
|
||||
|
||||
// Fill the buffer
|
||||
PFORT_CONF_ZONE_FLAG confZoneFlag = (PFORT_CONF_ZONE_FLAG) buf.data();
|
||||
PFORT_CONF_ZONE_FLAG confZoneFlag = (PFORT_CONF_ZONE_FLAG) buffer().data();
|
||||
|
||||
confZoneFlag->zone_id = zoneId;
|
||||
confZoneFlag->enabled = enabled;
|
||||
@ -287,10 +287,10 @@ int ConfUtil::writeZoneFlag(int zoneId, bool enabled, QByteArray &buf)
|
||||
return flagSize;
|
||||
}
|
||||
|
||||
bool ConfUtil::loadZone(const QByteArray &buf, IpRange &ipRange)
|
||||
bool ConfUtil::loadZone(IpRange &ipRange)
|
||||
{
|
||||
const char *data = buf.data();
|
||||
uint bufSize = buf.size();
|
||||
const char *data = buffer().data();
|
||||
uint bufSize = buffer().size();
|
||||
|
||||
return loadAddressList(&data, ipRange, bufSize);
|
||||
}
|
||||
|
@ -26,12 +26,18 @@ class ConfUtil : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ConfUtil(QObject *parent = nullptr);
|
||||
explicit ConfUtil(const QByteArray &buffer = {}, QObject *parent = nullptr);
|
||||
|
||||
quint32 driveMask() const { return m_driveMask; }
|
||||
|
||||
QString errorMessage() const { return m_errorMessage; }
|
||||
|
||||
const QByteArray &buffer() const { return m_buffer; }
|
||||
QByteArray &buffer() { return m_buffer; }
|
||||
|
||||
const char *data() const { return buffer().constData(); }
|
||||
char *data() { return m_buffer.data(); }
|
||||
|
||||
static int ruleMaxCount();
|
||||
static int ruleSetMaxCount();
|
||||
static int ruleDepthMaxCount();
|
||||
@ -39,20 +45,18 @@ public:
|
||||
static int zoneMaxCount();
|
||||
|
||||
public slots:
|
||||
int writeVersion(QByteArray &buf);
|
||||
int writeServices(
|
||||
const QVector<ServiceInfo> &services, int runningServicesCount, QByteArray &buf);
|
||||
int write(const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager,
|
||||
QByteArray &buf);
|
||||
int writeFlags(const FirewallConf &conf, QByteArray &buf);
|
||||
int writeAppEntry(const App &app, bool isNew, QByteArray &buf);
|
||||
int writeZone(const IpRange &ipRange, QByteArray &buf);
|
||||
int writeVersion();
|
||||
int writeServices(const QVector<ServiceInfo> &services, int runningServicesCount);
|
||||
int write(const FirewallConf &conf, ConfAppsWalker *confAppsWalker, EnvManager &envManager);
|
||||
int writeFlags(const FirewallConf &conf);
|
||||
int writeAppEntry(const App &app, bool isNew = false);
|
||||
int writeZone(const IpRange &ipRange);
|
||||
int writeZones(quint32 zonesMask, quint32 enabledMask, quint32 dataSize,
|
||||
const QList<QByteArray> &zonesData, QByteArray &buf);
|
||||
const QList<QByteArray> &zonesData);
|
||||
void migrateZoneData(char **data, const QByteArray &zoneData);
|
||||
int writeZoneFlag(int zoneId, bool enabled, QByteArray &buf);
|
||||
int writeZoneFlag(int zoneId, bool enabled);
|
||||
|
||||
bool loadZone(const QByteArray &buf, IpRange &ipRange);
|
||||
bool loadZone(IpRange &ipRange);
|
||||
|
||||
private:
|
||||
void setErrorMessage(const QString &errorMessage) { m_errorMessage = errorMessage; }
|
||||
@ -118,6 +122,8 @@ private:
|
||||
quint32 m_driveMask = 0;
|
||||
|
||||
QString m_errorMessage;
|
||||
|
||||
QByteArray m_buffer;
|
||||
};
|
||||
|
||||
#endif // CONFUTIL_H
|
||||
|
Loading…
Reference in New Issue
Block a user