Driver: Remove app group periods handling

This commit is contained in:
Nodir Temirkhodjaev 2024-10-13 13:11:02 +05:00
parent ca5933def1
commit 0c75c943b6
22 changed files with 133 additions and 338 deletions

View File

@ -13,8 +13,6 @@ static_assert(sizeof(FORT_CONF_FLAGS) == sizeof(UINT64), "FORT_CONF_FLAGS size m
static_assert(sizeof(FORT_CONF_RULE_EXPR) == sizeof(UINT32), "FORT_CONF_RULE_EXPR size mismatch");
static_assert(sizeof(FORT_CONF_RULE) == sizeof(UINT16), "FORT_CONF_RULE size mismatch");
static_assert(sizeof(FORT_TRAF) == sizeof(UINT64), "FORT_TRAF size mismatch");
static_assert(sizeof(FORT_TIME) == sizeof(UINT16), "FORT_TIME size mismatch");
static_assert(sizeof(FORT_PERIOD) == sizeof(UINT32), "FORT_PERIOD size mismatch");
static_assert(sizeof(FORT_APP_FLAGS) == sizeof(UINT16), "FORT_APP_FLAGS size mismatch");
static_assert(sizeof(FORT_APP_DATA) == 2 * sizeof(UINT32), "FORT_APP_DATA size mismatch");
@ -28,15 +26,6 @@ static int fort_memcmp(const void *p1, const void *p2, size_t len)
}
#endif
FORT_API BOOL is_time_in_period(FORT_TIME time, FORT_PERIOD period)
{
const int x = time.hour * 60 + time.minute;
const int from = period.from.hour * 60 + period.from.minute;
const int to = period.to.hour * 60 + period.to.minute;
return (from <= to ? (x >= from && x < (to - 1)) : (x >= from || x < (to - 1)));
}
static BOOL fort_conf_ip4_find(const UINT32 *iparr, UINT32 ip, UINT32 count, BOOL is_range)
{
if (count == 0)
@ -299,47 +288,12 @@ FORT_API FORT_APP_DATA fort_conf_app_find(const PFORT_CONF conf, const PVOID pat
return app_data;
}
FORT_API BOOL fort_conf_app_group_blocked(const PFORT_CONF conf, FORT_APP_DATA app_data)
FORT_API BOOL fort_conf_app_group_blocked(const FORT_CONF_FLAGS conf_flags, FORT_APP_DATA app_data)
{
const UINT16 app_group_bit = (1 << app_data.flags.group_index);
if ((app_group_bit & conf->active_group_bits) != 0)
if ((app_group_bit & conf_flags.group_bits) != 0)
return FALSE;
return conf->flags.group_blocked;
}
FORT_API UINT16 fort_conf_app_period_bits(const PFORT_CONF conf, FORT_TIME time, int *periods_n)
{
UINT8 count = conf->app_periods_n;
if (count == 0)
return 0;
const char *data = conf->data;
PFORT_PERIOD app_periods = (const PFORT_PERIOD)(data + conf->app_periods_off);
UINT16 period_bits = (UINT16) conf->flags.group_bits;
int n = 0;
for (int i = 0; i < FORT_CONF_GROUP_MAX; ++i) {
const UINT16 bit = (1 << i);
const FORT_PERIOD period = *app_periods++;
if ((period_bits & bit) != 0 && period.v != 0) {
if (!is_time_in_period(time, period)) {
period_bits ^= bit;
}
++n;
if (--count == 0)
break;
}
}
if (periods_n != NULL) {
*periods_n = n;
}
return period_bits;
return conf_flags.group_blocked;
}

View File

@ -204,32 +204,6 @@ typedef struct fort_traf
};
} FORT_TRAF, *PFORT_TRAF;
typedef struct fort_time
{
union {
UINT16 v;
struct
{
UCHAR hour;
UCHAR minute;
};
};
} FORT_TIME, *PFORT_TIME;
typedef struct fort_period
{
union {
UINT32 v;
struct
{
FORT_TIME from;
FORT_TIME to;
};
};
} FORT_PERIOD, *PFORT_PERIOD;
typedef struct fort_app_flags
{
UINT16 group_index : 5;
@ -301,19 +275,12 @@ typedef struct fort_conf
UCHAR proc_wild : 1; /* check also wildcard paths on process creation */
UCHAR app_periods_n;
UINT16 wild_apps_n;
UINT16 prefix_apps_n;
UINT16 exe_apps_n;
UINT16 active_group_bits;
UINT16 reserved; /* not used */
UINT32 addr_groups_off;
UINT32 app_periods_off;
UINT32 wild_apps_off;
UINT32 prefix_apps_off;
UINT32 exe_apps_off;
@ -359,8 +326,6 @@ typedef BOOL fort_conf_zones_ip_included_func(
extern "C" {
#endif
FORT_API BOOL is_time_in_period(FORT_TIME time, FORT_PERIOD period);
FORT_API BOOL fort_conf_ip_inlist(
const UINT32 *ip, const PFORT_CONF_ADDR4_LIST addr_list, BOOL isIPv6);
@ -392,9 +357,7 @@ FORT_API FORT_APP_DATA fort_conf_app_exe_find(
FORT_API FORT_APP_DATA fort_conf_app_find(const PFORT_CONF conf, const PVOID path, UINT32 path_len,
fort_conf_app_exe_find_func *exe_find_func, PVOID exe_context);
FORT_API BOOL fort_conf_app_group_blocked(const PFORT_CONF conf, FORT_APP_DATA app_data);
FORT_API UINT16 fort_conf_app_period_bits(const PFORT_CONF conf, FORT_TIME time, int *periods_n);
FORT_API BOOL fort_conf_app_group_blocked(const FORT_CONF_FLAGS conf_flags, FORT_APP_DATA app_data);
#ifdef __cplusplus
} // extern "C"

View File

@ -15,22 +15,6 @@ typedef struct fort_conf_exe_node
tommy_key_t path_hash; /* tommy_hashdyn_node::index */
} FORT_CONF_EXE_NODE, *PFORT_CONF_EXE_NODE;
static FORT_TIME fort_current_time(void)
{
TIME_FIELDS tf;
LARGE_INTEGER system_time, local_time;
KeQuerySystemTime(&system_time);
ExSystemTimeToLocalTime(&system_time, &local_time);
RtlTimeToTimeFields(&local_time, &tf);
FORT_TIME time;
time.hour = (UCHAR) tf.Hour;
time.minute = (UCHAR) tf.Minute;
return time;
}
static int bit_scan_forward(ULONG mask)
{
unsigned long index;
@ -390,8 +374,6 @@ FORT_API FORT_CONF_FLAGS fort_conf_ref_flags_set(
old_conf_flags = conf->flags;
conf->flags = conf_flags;
conf->active_group_bits = conf_flags.group_bits;
fort_device_flag_set(device_conf, FORT_DEVICE_BOOT_FILTER, conf_flags.boot_filter);
fort_device_flag_set(
device_conf, FORT_DEVICE_BOOT_FILTER_LOCALS, conf_flags.filter_locals);
@ -412,34 +394,6 @@ FORT_API FORT_CONF_FLAGS fort_conf_ref_flags_set(
return old_conf_flags;
}
FORT_API BOOL fort_conf_ref_period_update(PFORT_DEVICE_CONF device_conf, BOOL force, int *periods_n)
{
PFORT_CONF_REF conf_ref = fort_conf_ref_take(device_conf);
if (conf_ref == NULL)
return FALSE;
BOOL res = FALSE;
PFORT_CONF conf = &conf_ref->conf;
if (conf->app_periods_n != 0) {
const FORT_TIME time = fort_current_time();
const UINT16 period_bits = fort_conf_app_period_bits(conf, time, periods_n);
if (force || device_conf->conf_flags.group_bits != period_bits) {
device_conf->conf_flags.group_bits = period_bits;
conf->active_group_bits = period_bits;
res = TRUE;
}
}
fort_conf_ref_put(device_conf, conf_ref);
return res;
}
FORT_API PFORT_CONF_ZONES fort_conf_zones_new(PFORT_CONF_ZONES zones, ULONG len)
{
PFORT_CONF_ZONES conf_zones = fort_mem_alloc(len, FORT_ZONES_POOL_TAG);

View File

@ -74,9 +74,6 @@ FORT_API FORT_CONF_FLAGS fort_conf_ref_set(PFORT_DEVICE_CONF device_conf, PFORT_
FORT_API FORT_CONF_FLAGS fort_conf_ref_flags_set(
PFORT_DEVICE_CONF device_conf, const FORT_CONF_FLAGS conf_flags);
FORT_API BOOL fort_conf_ref_period_update(
PFORT_DEVICE_CONF device_conf, BOOL force, int *periods_n);
FORT_API PFORT_CONF_ZONES fort_conf_zones_new(PFORT_CONF_ZONES zones, ULONG len);
FORT_API void fort_conf_zones_set(PFORT_DEVICE_CONF device_conf, PFORT_CONF_ZONES zones);

View File

@ -215,9 +215,9 @@ inline static BOOL fort_callout_ale_ip_zone_check(
}
static BOOL fort_callout_ale_app_blocked(PCFORT_CALLOUT_ARG ca, PFORT_CALLOUT_ALE_EXTRA cx,
PFORT_CONF_REF conf_ref, FORT_APP_DATA app_data)
FORT_CONF_FLAGS conf_flags, FORT_APP_DATA app_data)
{
if (fort_conf_app_group_blocked(&conf_ref->conf, app_data)) {
if (fort_conf_app_group_blocked(conf_flags, app_data)) {
cx->block_reason = FORT_BLOCK_REASON_APP_GROUP_FOUND;
return TRUE; /* block Group */
}
@ -260,7 +260,7 @@ inline static BOOL fort_callout_ale_flags_allowed(
}
inline static BOOL fort_callout_ale_is_allowed(PCFORT_CALLOUT_ARG ca, PFORT_CALLOUT_ALE_EXTRA cx,
PFORT_CONF_REF conf_ref, FORT_CONF_FLAGS conf_flags, FORT_APP_DATA app_data)
FORT_CONF_FLAGS conf_flags, FORT_APP_DATA app_data)
{
/* Collect traffic, when Filter Disabled */
if (!cx->blocked)
@ -268,7 +268,7 @@ inline static BOOL fort_callout_ale_is_allowed(PCFORT_CALLOUT_ARG ca, PFORT_CALL
if (app_data.found != 0) {
/* Check app is blocked */
return !fort_callout_ale_app_blocked(ca, cx, conf_ref, app_data);
return !fort_callout_ale_app_blocked(ca, cx, conf_flags, app_data);
}
return fort_callout_ale_flags_allowed(cx, conf_flags);
@ -279,7 +279,7 @@ inline static void fort_callout_ale_check_app(PCFORT_CALLOUT_ARG ca, PFORT_CALLO
{
const FORT_APP_DATA app_data = fort_callout_ale_conf_app_data(cx, conf_ref);
if (fort_callout_ale_is_allowed(ca, cx, conf_ref, conf_flags, app_data)) {
if (fort_callout_ale_is_allowed(ca, cx, conf_flags, app_data)) {
if (fort_callout_ale_process_flow(ca, cx, conf_flags, app_data))
return;
@ -868,15 +868,6 @@ FORT_API NTSTATUS fort_callout_force_reauth(const FORT_CONF_FLAGS old_conf_flags
NTSTATUS status;
/* Check app group periods & update group_bits */
{
int periods_n = 0;
fort_conf_ref_period_update(&fort_device()->conf, /*force=*/TRUE, &periods_n);
fort_timer_set_running(&fort_device()->app_timer, /*run=*/(periods_n != 0));
}
const FORT_CONF_FLAGS conf_flags = fort_device()->conf.conf_flags;
/* Handle log_stat */

View File

@ -57,20 +57,6 @@ static void fort_device_reauth_queue(void)
fort_worker_queue(&fort_device()->worker, FORT_WORKER_REAUTH);
}
static void fort_app_period_timer(void)
{
if (fort_conf_ref_period_update(&fort_device()->conf, /*force=*/FALSE, /*periods_n=*/NULL)) {
fort_device_reauth_queue();
}
}
FORT_API void fort_device_on_system_time(void)
{
if (fort_timer_is_running(&fort_device()->app_timer)) {
fort_app_period_timer();
}
}
FORT_API NTSTATUS fort_device_create(PDEVICE_OBJECT device, PIRP irp)
{
UNUSED(device);
@ -464,8 +450,6 @@ FORT_API NTSTATUS fort_device_load(PVOID device_param)
fort_pending_open(&fort_device()->pending);
fort_shaper_open(&fort_device()->shaper);
fort_timer_open(&fort_device()->log_timer, 500, /*flags=*/0, &fort_callout_timer);
fort_timer_open(
&fort_device()->app_timer, 60000, FORT_TIMER_COALESCABLE, &fort_app_period_timer);
fort_pstree_open(&fort_device()->ps_tree);
/* Register filters provider */
@ -505,7 +489,6 @@ FORT_API void fort_device_unload(void)
fort_syscb_time_unregister();
/* Stop timers */
fort_timer_close(&fort_device()->app_timer);
fort_timer_close(&fort_device()->log_timer);
/* Stop worker threads */

View File

@ -30,7 +30,6 @@ typedef struct fort_device
FORT_SHAPER shaper;
FORT_PSTREE ps_tree;
FORT_TIMER log_timer;
FORT_TIMER app_timer;
FORT_WORKER worker;
} FORT_DEVICE, *PFORT_DEVICE;
@ -42,8 +41,6 @@ FORT_API PFORT_DEVICE fort_device(void);
FORT_API void fort_device_set(PFORT_DEVICE device);
FORT_API void fort_device_on_system_time(void);
FORT_API NTSTATUS fort_device_create(PDEVICE_OBJECT device, PIRP irp);
FORT_API NTSTATUS fort_device_close(PDEVICE_OBJECT device, PIRP irp);

View File

@ -76,8 +76,6 @@ static void NTAPI fort_syscb_time(PVOID context, PVOID event, PVOID specifics)
FORT_CHECK_STACK(FORT_SYSCB_TIME);
fort_stat_flags_set(&fort_device()->stat, FORT_STAT_SYSTEM_TIME_CHANGED, TRUE);
fort_device_on_system_time();
}
FORT_API NTSTATUS fort_syscb_time_register(void)

View File

@ -3,6 +3,7 @@ include(../Common/Common.pri)
HEADERS += \
tst_bitutil.h \
tst_confutil.h \
tst_dateutil.h \
tst_fileutil.h \
tst_ioccontainer.h \
tst_netutil.h \

View File

@ -109,32 +109,11 @@ TEST_F(ConfUtilTest, confWriteRead)
data, FileUtil::pathToKernelPath("C:\\Program Files\\Test.exe"))
.found);
ASSERT_EQ(DriverCommon::confAppPeriodBits(data, 0, 0), 0x01);
ASSERT_EQ(DriverCommon::confAppPeriodBits(data, 12, 0), 0);
const auto firefoxData = DriverCommon::confAppFind(
data, FileUtil::pathToKernelPath("C:\\Utils\\Firefox\\Bin\\firefox.exe"));
ASSERT_EQ(int(firefoxData.flags.group_index), 1);
}
TEST_F(ConfUtilTest, checkPeriod)
{
const quint8 h = 15, m = 35;
ASSERT_TRUE(DriverCommon::isTimeInPeriod(h, m, 0, 0, 24, 0));
ASSERT_TRUE(DriverCommon::isTimeInPeriod(h, m, 15, 0, 16, 0));
ASSERT_TRUE(DriverCommon::isTimeInPeriod(h, m, 15, 0, 10, 0));
ASSERT_FALSE(DriverCommon::isTimeInPeriod(h, m, 15, 0, 15, 0));
ASSERT_FALSE(DriverCommon::isTimeInPeriod(h, m, 0, 0, 15, 0));
ASSERT_FALSE(DriverCommon::isTimeInPeriod(h, m, 16, 0, 15, 0));
ASSERT_FALSE(DriverCommon::isTimeInPeriod(h, m, 24, 0, 0, 0));
ASSERT_FALSE(DriverCommon::isTimeInPeriod(h, m, 16, 0, 14, 0));
ASSERT_FALSE(DriverCommon::isTimeInPeriod(h, m, 16, 0, 24, 0));
ASSERT_TRUE(DriverCommon::isTimeInPeriod(h, m, 15, 35, 15, 37));
ASSERT_TRUE(!DriverCommon::isTimeInPeriod(h, m, 15, 35, 15, 36));
}
TEST_F(ConfUtilTest, checkEnvManager)
{
EnvManager envManager;

View File

@ -0,0 +1,37 @@
#pragma once
#include <QSignalSpy>
#include <googletest.h>
#include <util/dateutil.h>
class DateUtilTest : public Test
{
// Test interface
protected:
void SetUp();
void TearDown();
};
void DateUtilTest::SetUp() { }
void DateUtilTest::TearDown() { }
TEST_F(DateUtilTest, checkPeriod)
{
const QTime x(15, 35);
ASSERT_TRUE(DateUtil::isTimeInPeriod(x, { 0, 0 }, { 23, 0 }));
ASSERT_TRUE(DateUtil::isTimeInPeriod(x, { 15, 0 }, { 16, 0 }));
ASSERT_TRUE(DateUtil::isTimeInPeriod(x, { 15, 0 }, { 10, 0 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 15, 0 }, { 15, 0 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 0, 0 }, { 15, 0 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 16, 0 }, { 15, 0 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 24, 0 }, { 0, 0 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 16, 0 }, { 14, 0 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 16, 0 }, { 23, 0 }));
ASSERT_TRUE(DateUtil::isTimeInPeriod(x, { 15, 35 }, { 15, 36 }));
ASSERT_FALSE(DateUtil::isTimeInPeriod(x, { 15, 34 }, { 15, 35 }));
}

View File

@ -1,5 +1,6 @@
#include "tst_bitutil.h"
#include "tst_confutil.h"
#include "tst_dateutil.h"
#include "tst_fileutil.h"
#include "tst_ioccontainer.h"
#include "tst_netutil.h"

View File

@ -223,33 +223,6 @@ FORT_APP_DATA confAppFind(const void *drvConf, const QString &kernelPath)
return app_data;
}
quint16 confAppPeriodBits(const void *drvConf, quint8 hour, quint8 minute)
{
const PFORT_CONF conf = (const PFORT_CONF) drvConf;
FORT_TIME time;
time.hour = hour;
time.minute = minute;
return fort_conf_app_period_bits(conf, time, nullptr);
}
bool isTimeInPeriod(quint8 hour, quint8 minute, quint8 fromHour, quint8 fromMinute, quint8 toHour,
quint8 toMinute)
{
FORT_TIME time;
time.hour = hour;
time.minute = minute;
FORT_PERIOD period;
period.from.hour = fromHour;
period.from.minute = fromMinute;
period.to.hour = toHour;
period.to.minute = toMinute;
return is_time_in_period(time, period);
}
bool provRegister(bool bootFilter)
{
const FORT_PROV_BOOT_CONF boot_conf = {

View File

@ -69,10 +69,6 @@ bool confIp6InRange(
const void *drvConf, const ip6_addr_t &ip, bool included = false, int addrGroupIndex = 0);
FORT_APP_DATA confAppFind(const void *drvConf, const QString &kernelPath);
quint16 confAppPeriodBits(const void *drvConf, quint8 hour, quint8 minute);
bool isTimeInPeriod(quint8 hour, quint8 minute, quint8 fromHour, quint8 fromMinute, quint8 toHour,
quint8 toMinute);
bool provRegister(bool bootFilter);
void provUnregister();

View File

@ -24,8 +24,6 @@ const QLoggingCategory LC("stat");
constexpr int DATABASE_USER_VERSION = 7;
constexpr qint32 ACTIVE_PERIOD_CHECK_SECS = 60 * OS_TICKS_PER_SECOND;
constexpr qint64 INVALID_APP_ID = Q_INT64_C(-1);
bool migrateFunc(SqliteDb *db, int version, bool isNewDb, void *ctx)
@ -88,7 +86,7 @@ void StatManager::setupByConf()
logClear();
}
m_isActivePeriodSet = false;
m_tickSecs = 0;
if (conf()) {
setupActivePeriod();
@ -97,29 +95,26 @@ void StatManager::setupByConf()
void StatManager::setupActivePeriod()
{
DateUtil::parseTime(
conf()->activePeriodFrom(), m_activePeriodFromHour, m_activePeriodFromMinute);
m_activePeriodFrom = DateUtil::parseTime(conf()->activePeriodFrom());
DateUtil::parseTime(conf()->activePeriodTo(), m_activePeriodToHour, m_activePeriodToMinute);
m_activePeriodTo = DateUtil::parseTime(conf()->activePeriodTo());
}
void StatManager::updateActivePeriod()
void StatManager::updateActivePeriod(qint32 tickSecs)
{
const qint32 currentTick = OsUtil::getTickCount();
constexpr qint32 ACTIVE_PERIOD_CHECK_SECS = 60;
if (!m_isActivePeriodSet || qAbs(currentTick - m_tick) >= ACTIVE_PERIOD_CHECK_SECS) {
m_tick = currentTick;
if (qAbs(tickSecs - m_tickSecs) < ACTIVE_PERIOD_CHECK_SECS)
return;
m_tickSecs = tickSecs;
m_isActivePeriodSet = true;
m_isActivePeriod = true;
if (conf() && conf()->activePeriodEnabled()) {
const QTime now = QTime::currentTime();
const QTime now = DateUtil::currentTime();
m_isActivePeriod = DriverCommon::isTimeInPeriod(quint8(now.hour()),
quint8(now.minute()), m_activePeriodFromHour, m_activePeriodFromMinute,
m_activePeriodToHour, m_activePeriodToMinute);
}
m_isActivePeriod = DateUtil::isTimeInPeriod(now, m_activePeriodFrom, m_activePeriodTo);
}
}
@ -132,7 +127,9 @@ void StatManager::clearQuotas(bool isNewDay, bool isNewMonth)
void StatManager::checkQuotas(quint32 inBytes)
{
if (m_isActivePeriod) {
if (!m_isActivePeriod)
return;
auto quotaManager = IoC<QuotaManager>();
// Update quota traffic bytes
@ -141,7 +138,6 @@ void StatManager::checkQuotas(quint32 inBytes)
quotaManager->checkQuotaDay(m_trafDay);
quotaManager->checkQuotaMonth(m_trafMonth);
}
}
bool StatManager::updateTrafDay(qint64 unixTime)
{
@ -256,7 +252,7 @@ bool StatManager::logProcNew(const LogEntryProcNew &entry, qint64 unixTime)
bool StatManager::logStatTraf(const LogEntryStatTraf &entry, qint64 unixTime)
{
// Active period
updateActivePeriod();
updateActivePeriod(qint32(unixTime));
const bool logStat = conf() && conf()->logStat() && m_isActivePeriod;

View File

@ -4,6 +4,7 @@
#include <QHash>
#include <QObject>
#include <QStringList>
#include <QTime>
#include <QVector>
#include <sqlite/sqlite_types.h>
@ -70,7 +71,7 @@ private:
void setupByConf();
void setupActivePeriod();
void updateActivePeriod();
void updateActivePeriod(qint32 tickSecs);
void clearQuotas(bool isNewDay, bool isNewMonth);
void checkQuotas(quint32 inBytes);
@ -110,18 +111,16 @@ private:
void commitTransaction(bool &ok);
private:
bool m_isActivePeriodSet : 1 = false;
bool m_isActivePeriod : 1 = false;
quint8 m_activePeriodFromHour = 0;
quint8 m_activePeriodFromMinute = 0;
quint8 m_activePeriodToHour = 0;
quint8 m_activePeriodToMinute = 0;
qint32 m_trafHour = 0;
qint32 m_trafDay = 0;
qint32 m_trafMonth = 0;
qint32 m_tick = 0;
qint32 m_tickSecs = 0;
QTime m_activePeriodFrom;
QTime m_activePeriodTo;
const FirewallConf *m_conf = nullptr;

View File

@ -246,7 +246,7 @@ bool ConfUtil::write(
if (!parseExeApps(envManager, confAppsWalker, opt))
return false;
if (!parseAppGroups(envManager, conf.appGroups(), wca.gr, opt))
if (!parseAppGroups(envManager, conf.appGroups(), opt))
return false;
const quint32 appsSize = opt.wildAppsSize + opt.prefixAppsSize + opt.exeAppsSize;
@ -257,7 +257,6 @@ bool ConfUtil::write(
// Fill the buffer
const int confIoSize = int(FORT_CONF_IO_CONF_OFF + FORT_CONF_DATA_OFF + addressGroupsSize
+ FORT_CONF_STR_DATA_SIZE(conf.appGroups().size() * sizeof(FORT_PERIOD)) // appPeriods
+ FORT_CONF_STR_DATA_SIZE(opt.wildAppsSize)
+ FORT_CONF_STR_HEADER_SIZE(opt.prefixAppsMap.size())
+ FORT_CONF_STR_DATA_SIZE(opt.prefixAppsSize)
@ -522,8 +521,8 @@ bool ConfUtil::parseAddressGroups(const QList<AddressGroup *> &addressGroups,
return true;
}
bool ConfUtil::parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &appGroups,
ParseAppGroupsArgs &gr, AppParseOptions &opt)
bool ConfUtil::parseAppGroups(
EnvManager &envManager, const QList<AppGroup *> &appGroups, AppParseOptions &opt)
{
const int groupsCount = appGroups.size();
if (groupsCount < 1 || groupsCount > APP_GROUP_MAX) {
@ -565,9 +564,6 @@ bool ConfUtil::parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &a
app.blocked = false;
if (!parseAppsText(envManager, app, opt))
return false;
// Enabled Period
parseAppPeriod(appGroup, gr);
}
return true;
@ -696,36 +692,12 @@ QString ConfUtil::parseAppPath(const QStringView &line, bool &isWild, bool &isPr
return path.toString();
}
void ConfUtil::parseAppPeriod(const AppGroup *appGroup, ParseAppGroupsArgs &gr)
{
quint8 fromHour = 0, fromMinute = 0;
quint8 toHour = 0, toMinute = 0;
if (appGroup->periodEnabled()) {
DateUtil::parseTime(appGroup->periodFrom(), fromHour, fromMinute);
DateUtil::parseTime(appGroup->periodTo(), toHour, toMinute);
const bool fromIsEmpty = (fromHour == 0 && fromMinute == 0);
const bool toIsEmpty = (toHour == 0 && toMinute == 0);
if (!fromIsEmpty || !toIsEmpty) {
++gr.appPeriodsCount;
}
}
gr.appPeriods.append(qint8(fromHour));
gr.appPeriods.append(qint8(fromMinute));
gr.appPeriods.append(qint8(toHour));
gr.appPeriods.append(qint8(toMinute));
}
void ConfUtil::writeConf(char *output, const WriteConfArgs &wca, AppParseOptions &opt)
{
PFORT_CONF_IO drvConfIo = (PFORT_CONF_IO) output;
PFORT_CONF drvConf = &drvConfIo->conf;
char *data = drvConf->data;
quint32 addrGroupsOff;
quint32 appPeriodsOff;
quint32 wildAppsOff, prefixAppsOff, exeAppsOff;
#define CONF_DATA_OFFSET quint32(data - drvConf->data)
@ -733,9 +705,6 @@ void ConfUtil::writeConf(char *output, const WriteConfArgs &wca, AppParseOptions
writeLongs(&data, wca.ad.addressGroupOffsets);
writeAddressRanges(&data, wca.ad.addressRanges);
appPeriodsOff = CONF_DATA_OFFSET;
writeChars(&data, wca.gr.appPeriods);
wildAppsOff = CONF_DATA_OFFSET;
writeApps(&data, opt.wildAppsMap);
@ -756,18 +725,12 @@ void ConfUtil::writeConf(char *output, const WriteConfArgs &wca, AppParseOptions
drvConf->proc_wild = opt.procWild;
drvConf->app_periods_n = wca.gr.appPeriodsCount;
drvConf->wild_apps_n = quint16(opt.wildAppsMap.size());
drvConf->prefix_apps_n = quint16(opt.prefixAppsMap.size());
drvConf->exe_apps_n = quint16(opt.exeAppsMap.size());
drvConf->active_group_bits = conf_group->group_bits;
drvConf->addr_groups_off = addrGroupsOff;
drvConf->app_periods_off = appPeriodsOff;
drvConf->wild_apps_off = wildAppsOff;
drvConf->prefix_apps_off = prefixAppsOff;
drvConf->exe_apps_off = exeAppsOff;

View File

@ -77,18 +77,11 @@ private:
longs_arr_t addressGroupOffsets;
};
struct ParseAppGroupsArgs
{
chars_arr_t appPeriods;
quint8 appPeriodsCount = 0;
};
struct WriteConfArgs
{
const FirewallConf &conf;
ParseAddressGroupsArgs ad;
ParseAppGroupsArgs gr;
};
bool parseAddressGroups(const QList<AddressGroup *> &addressGroups, ParseAddressGroupsArgs &ad,
@ -96,7 +89,7 @@ private:
// Convert app. groups to plain lists
bool parseAppGroups(EnvManager &envManager, const QList<AppGroup *> &appGroups,
ParseAppGroupsArgs &gr, AppParseOptions &opt);
AppParseOptions &opt);
bool parseExeApps(
EnvManager &envManager, const ConfAppsWalker *confAppsWalker, AppParseOptions &opt);
@ -109,8 +102,6 @@ private:
static QString parseAppPath(const QStringView &line, bool &isWild, bool &isPrefix);
static void parseAppPeriod(const AppGroup *appGroup, ParseAppGroupsArgs &gr);
static void writeConf(char *output, const WriteConfArgs &wca, AppParseOptions &opt);
static void writeAddressRanges(char **data, const addrranges_arr_t &addressRanges);

View File

@ -3,6 +3,20 @@
#include <QLocale>
#include <QTimeZone>
namespace {
quint8 parseTimeHour(const QString &time)
{
return quint8(QStringView(time).left(2).toUInt());
}
quint8 parseTimeMinute(const QString &time)
{
return quint8(QStringView(time).right(2).toUInt());
}
}
QDateTime DateUtil::now()
{
return QDateTime::currentDateTime();
@ -111,23 +125,38 @@ QString DateUtil::reformatTime(const QString &time)
return formatTime(hour, minute);
}
void DateUtil::parseTime(const QString &time, quint8 &hour, quint8 &minute)
{
hour = parseTimeHour(time);
minute = parseTimeMinute(time);
}
quint8 DateUtil::parseTimeHour(const QString &period)
{
return quint8(QStringView(period).left(2).toUInt());
}
quint8 DateUtil::parseTimeMinute(const QString &period)
{
return quint8(QStringView(period).right(2).toUInt());
}
QString DateUtil::localeDateTime(const QDateTime &dateTime, QLocale::FormatType format)
{
return QLocale().toString(dateTime, format);
}
QTime DateUtil::currentTime()
{
return QTime::currentTime();
}
QTime DateUtil::midnightTime()
{
return QTime(23, 59, 59, 999);
}
QTime DateUtil::parseTime(const QString &time)
{
const quint8 hour = parseTimeHour(time);
if (hour > 23) {
return midnightTime();
}
const quint8 minute = parseTimeMinute(time);
return QTime(hour, minute);
}
bool DateUtil::isTimeInPeriod(QTime time, QTime periodFrom, QTime periodTo)
{
const int x = time.msecsSinceStartOfDay();
const int from = periodFrom.msecsSinceStartOfDay();
const int to = periodTo.msecsSinceStartOfDay();
return (from <= to) ? (x >= from && x < to) : (x >= from || x < to);
}

View File

@ -34,13 +34,15 @@ public:
static QString formatTime(quint8 hour, quint8 minute);
static QString reformatTime(const QString &time);
static void parseTime(const QString &time, quint8 &hour, quint8 &minute);
static quint8 parseTimeHour(const QString &period);
static quint8 parseTimeMinute(const QString &period);
static QString localeDateTime(
const QDateTime &dateTime, QLocale::FormatType format = QLocale::ShortFormat);
static QTime currentTime();
static QTime midnightTime();
static QTime parseTime(const QString &time);
static bool isTimeInPeriod(QTime time, QTime periodFrom, QTime periodTo);
};
#endif // DATEUTIL_H

View File

@ -93,11 +93,6 @@ QString OsUtil::errorMessage(quint32 errorCode)
return text;
}
qint32 OsUtil::getTickCount()
{
return qint32(GetTickCount());
}
QString OsUtil::userName()
{
wchar_t buf[UNLEN + 1];

View File

@ -15,8 +15,6 @@ enum SoundType {
SoundDefault = 0,
};
#define OS_TICKS_PER_SECOND 1000
class OsUtil
{
public:
@ -31,8 +29,6 @@ public:
static quint32 lastErrorCode();
static QString errorMessage(quint32 errorCode = lastErrorCode());
static qint32 getTickCount();
static QString userName();
static bool isUserAdmin();