From d6623703a4a45f8781bb05ecb62f48e4e6aa79b3 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 28 Jan 2020 14:36:47 +0500 Subject: [PATCH] UI: Prepare Zones conf. --- src/ui/FortFirewall.pro | 5 +++++ src/ui/conf/confmanager.cpp | 32 +++++++++++++++++++++++++---- src/ui/conf/migrations/1.sql | 17 +++++++++++++++ src/ui/util/net/ip4range.cpp | 8 ++++---- src/ui/util/net/ip4range.h | 4 ++-- src/ui/zone/data/fort_zone_data.qrc | 6 ++++++ src/ui/zone/data/zone_types.json | 16 +++++++++++++++ src/ui/zone/data/zone_urls.json | 7 +++++++ 8 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/ui/zone/data/fort_zone_data.qrc create mode 100644 src/ui/zone/data/zone_types.json create mode 100644 src/ui/zone/data/zone_urls.json diff --git a/src/ui/FortFirewall.pro b/src/ui/FortFirewall.pro index 8fc3d46c..059edae4 100644 --- a/src/ui/FortFirewall.pro +++ b/src/ui/FortFirewall.pro @@ -229,6 +229,11 @@ RESOURCES += \ stat/stat-migrations.qrc \ util/app/app-migrations.qrc +# Zone +OTHER_FILES += zone/data/*.json + +RESOURCES += zone/data/fort_zone_data.qrc + # Shadow Build: Copy i18n/ to build path !equals(PWD, $${OUT_PWD}) { i18n.files = $$files(i18n/*.qm) diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index e5724a6e..353295d1 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -24,7 +24,7 @@ Q_LOGGING_CATEGORY(CLOG_CONF_MANAGER, "fort.confManager") #define logWarning() qCWarning(CLOG_CONF_MANAGER,) #define logCritical() qCCritical(CLOG_CONF_MANAGER,) -#define DATABASE_USER_VERSION 5 +#define DATABASE_USER_VERSION 6 namespace { @@ -174,6 +174,30 @@ const char * const sqlUpdateAppResetGroup = " WHERE app_group_id = ?1;" ; +bool migrateFunc(SqliteDb *db, int version, void *ctx) +{ + if (version != DATABASE_USER_VERSION) + return true; + + Q_UNUSED(ctx) + + if (version == 6) { + // Add TAS-IX zone + db->execute( + "INSERT INTO zone(name, enabled, zone_type, url, form_data) VALUES" + " ('Local Addresses', 1, 'local', NULL, NULL)," + " ('TAS-IX Addresses', 1, 'bgp'," + " 'http://mrlg.tas-ix.uz/index.cgi'," + " 'router=cisco&pass1=&query=1&arg=');" + + "INSERT INTO address_group_zone(addr_group_id, zone_id, include) VALUES" + " (1, 1, 0), (1, 2, 0);" + ); + } + + return true; +} + } ConfManager::ConfManager(const QString &filePath, @@ -224,8 +248,8 @@ bool ConfManager::initialize() m_sqliteDb->execute(sqlPragmas); - if (!m_sqliteDb->migrate(":/conf/migrations", - DATABASE_USER_VERSION, true, true)) { + if (!m_sqliteDb->migrate(":/conf/migrations", DATABASE_USER_VERSION, + true, true, &migrateFunc)) { logCritical() << "Migration error" << m_sqliteDb->filePath(); return false; @@ -675,7 +699,7 @@ bool ConfManager::saveToDb(const FirewallConf &conf) const auto vars = QVariantList() << (rowExists ? addrGroup->id() : QVariant()) - << orderIndex++ + << orderIndex << addrGroup->includeAll() << addrGroup->excludeAll() << addrGroup->includeText() diff --git a/src/ui/conf/migrations/1.sql b/src/ui/conf/migrations/1.sql index 08f69808..e32f93c1 100644 --- a/src/ui/conf/migrations/1.sql +++ b/src/ui/conf/migrations/1.sql @@ -1,3 +1,14 @@ +CREATE TABLE zone( + zone_id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + enabled BOOLEAN NOT NULL, + zone_type TEXT NOT NULL, + url TEXT, + form_data TEXT, + last_run INTEGER, + last_success INTEGER +); + CREATE TABLE address_group( addr_group_id INTEGER PRIMARY KEY, order_index INTEGER NOT NULL, @@ -7,6 +18,12 @@ CREATE TABLE address_group( exclude_text TEXT NOT NULL ); +CREATE TABLE address_group_zone( + addr_group_id INTEGER NOT NULL, + zone_id INTEGER NOT NULL, + include BOOLEAN NOT NULL +); + CREATE TABLE app_group( app_group_id INTEGER PRIMARY KEY, order_index INTEGER NOT NULL, diff --git a/src/ui/util/net/ip4range.cpp b/src/ui/util/net/ip4range.cpp index e17be9da..a46e7d0d 100644 --- a/src/ui/util/net/ip4range.cpp +++ b/src/ui/util/net/ip4range.cpp @@ -65,7 +65,7 @@ QString Ip4Range::toText() const return text; } -bool Ip4Range::fromText(const QString &text, int emptyMask) +bool Ip4Range::fromText(const QString &text, int emptyNetMask) { clear(); @@ -83,7 +83,7 @@ bool Ip4Range::fromText(const QString &text, int emptyMask) continue; quint32 from, to; - if (!parseAddressMask(lineTrimmed, from, to, emptyMask)) { + if (!parseAddressMask(lineTrimmed, from, to, emptyNetMask)) { setErrorLineNo(lineNo); return false; } @@ -105,7 +105,7 @@ bool Ip4Range::fromText(const QString &text, int emptyMask) // Parse "127.0.0.0-127.255.255.255" or "127.0.0.0/24" or "127.0.0.0" bool Ip4Range::parseAddressMask(const QStringRef &line, quint32 &from, quint32 &to, - int emptyMask) + int emptyNetMask) { const QRegularExpression re(R"(([\d.]+)\s*([/-]?)\s*(\S*))"); const QRegularExpressionMatch match = re.match(line); @@ -145,7 +145,7 @@ bool Ip4Range::parseAddressMask(const QStringRef &line, } } else if (sep == QLatin1Char('/')) { // e.g. "127.0.0.0/24", "127.0.0.0" bool ok = true; - const int nbits = mask.isEmpty() ? emptyMask : mask.toInt(&ok); + const int nbits = mask.isEmpty() ? emptyNetMask : mask.toInt(&ok); if (!ok || nbits < 0 || nbits > 32) { setErrorMessage(tr("Bad mask")); diff --git a/src/ui/util/net/ip4range.h b/src/ui/util/net/ip4range.h index 208c0af2..45bea959 100644 --- a/src/ui/util/net/ip4range.h +++ b/src/ui/util/net/ip4range.h @@ -50,7 +50,7 @@ public slots: QString toText() const; // Parse IPv4 ranges from text - bool fromText(const QString &text, int emptyMask = 32); + bool fromText(const QString &text, int emptyNetMask = 32); private: void setErrorLineNo(int lineNo); @@ -58,7 +58,7 @@ private: bool parseAddressMask(const QStringRef &line, quint32 &from, quint32 &to, - int emptyMask = 32); + int emptyNetMask = 32); void fillRange(const ip4range_map_t &ipRangeMap, int pairSize); diff --git a/src/ui/zone/data/fort_zone_data.qrc b/src/ui/zone/data/fort_zone_data.qrc new file mode 100644 index 00000000..39ce87c9 --- /dev/null +++ b/src/ui/zone/data/fort_zone_data.qrc @@ -0,0 +1,6 @@ + + + zone_types.json + zone_urls.json + + diff --git a/src/ui/zone/data/zone_types.json b/src/ui/zone/data/zone_types.json new file mode 100644 index 00000000..cfd1141d --- /dev/null +++ b/src/ui/zone/data/zone_types.json @@ -0,0 +1,16 @@ +[ + { + "id": "local", + "title": "Local", + "description": "Local addresses", + "local": true + }, + { + "id": "bgp", + "title": "BGP", + "description": "BGP table", + "sort": true, + "regex": "^*??([^\\s]+)", + "emptyNetMask": 24 + } +] diff --git a/src/ui/zone/data/zone_urls.json b/src/ui/zone/data/zone_urls.json new file mode 100644 index 00000000..e1b35c65 --- /dev/null +++ b/src/ui/zone/data/zone_urls.json @@ -0,0 +1,7 @@ +[ + { + "title": "TAS-IX Addresses", + "url": "http://mrlg.tas-ix.uz/index.cgi", + "formData": "router=cisco&pass1=&query=1&arg=" + } +]