From 08157c342c13a9314d94d3e2d0f93148c0248cb5 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 17 Nov 2018 16:37:11 +0000 Subject: [PATCH] Add config options for REGEX support. Also add README.REGEX --- Conf.cpp | 18 +++++++++++++++++- Conf.h | 4 ++++ DAPNETGateway.cpp | 16 ++++++++-------- DAPNETGateway.ini | 2 ++ README.REGEX | 21 +++++++++++++++++++++ REGEX.cpp | 2 +- 6 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 README.REGEX diff --git a/Conf.cpp b/Conf.cpp index 1ea9f82..d532d5c 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -37,6 +37,8 @@ CConf::CConf(const std::string& file) : m_file(file), m_callsign(), m_whiteList(), +m_blacklistRegexfile(), +m_whitelistRegexfile(), m_rptAddress(), m_rptPort(0U), m_myAddress(), @@ -105,7 +107,11 @@ bool CConf::read() m_whiteList.push_back(ric); p = ::strtok(NULL, ",\r\n"); } - } else if (::strcmp(key, "RptAddress") == 0) + } else if (::strcmp(key,"BlacklistRegexfile") == 0) + m_blacklistRegexfile = value; + else if (::strcmp(key,"WhitelistRegexfile") == 0) + m_whitelistRegexfile = value; + else if (::strcmp(key, "RptAddress") == 0) m_rptAddress = value; else if (::strcmp(key, "RptPort") == 0) m_rptPort = (unsigned int)::atoi(value); @@ -155,6 +161,16 @@ std::vector CConf::getWhiteList() const return m_whiteList; } +std::string CConf::getblacklistRegexfile() const +{ + return m_blacklistRegexfile; +} + +std::string CConf::getwhitelistRegexfile() const +{ + return m_whitelistRegexfile; +} + std::string CConf::getRptAddress() const { return m_rptAddress; diff --git a/Conf.h b/Conf.h index 4a8378a..bd3be30 100644 --- a/Conf.h +++ b/Conf.h @@ -33,6 +33,8 @@ public: // The General section std::string getCallsign() const; std::vector getWhiteList() const; + std::string getblacklistRegexfile() const; + std::string getwhitelistRegexfile() const; std::string getRptAddress() const; unsigned int getRptPort() const; std::string getMyAddress() const; @@ -56,6 +58,8 @@ private: std::string m_callsign; std::vector m_whiteList; + std::string m_blacklistRegexfile; + std::string m_whitelistRegexfile; std::string m_rptAddress; unsigned int m_rptPort; std::string m_myAddress; diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index 9eb9772..60b3745 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -270,11 +270,11 @@ int CDAPNETGateway::run() std::vector regexBlacklist; std::vector regexWhitelist; - m_regexBlacklist = new CREGEX("/tmp/blregexes.txt"); + m_regexBlacklist = new CREGEX(m_conf.getblacklistRegexfile()); if (m_regexBlacklist->load()) regexBlacklist = m_regexBlacklist->get(); - m_regexWhitelist = new CREGEX("/tmp/wlregexes.txt"); + m_regexWhitelist = new CREGEX(m_conf.getwhitelistRegexfile()); if (m_regexWhitelist->load()) regexWhitelist = m_regexWhitelist->get(); @@ -312,8 +312,8 @@ int CDAPNETGateway::run() CPOCSAGMessage* message = m_dapnetNetwork->readMessage(); if (message != NULL) { bool found = true; - bool blacklistregexmatch = false; - bool whitelistregexmatch = true; + bool blacklistRegexmatch = false; + bool whitelistRegexmatch = true; // If we have a white list of RICs, use it. if (!whiteList.empty()) @@ -326,25 +326,25 @@ int CDAPNETGateway::run() bool ret = std::regex_match(messageBody,regex); //If the regex matches the message body, don't send the message if (ret) { - blacklistregexmatch = true; + blacklistRegexmatch = true; LogDebug("Blacklist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str()); } } } - if(!regexWhitelist.empty() && !blacklistregexmatch) { + if(!regexWhitelist.empty() && !blacklistRegexmatch) { for (std::regex regex : regexWhitelist) { bool ret = std::regex_match(messageBody,regex); //If the regex does not match the message body, don't send the message if (!ret) { - whitelistregexmatch = false; + whitelistRegexmatch = false; LogDebug("No whitelist REGEX match: Not queueing message to %07u, type %u, message: \"%.*s\"", message->m_ric, message->m_type, message->m_length, messageBody.c_str()); } } } - if (found && !blacklistregexmatch && whitelistregexmatch) { + if (found && !blacklistRegexmatch && whitelistRegexmatch) { switch (message->m_functional) { case FUNCTIONAL_ALPHANUMERIC: LogDebug("Queueing message to %07u, type %u, func Alphanumeric: \"%.*s\"", message->m_ric, message->m_type, message->m_length, message->m_message); diff --git a/DAPNETGateway.ini b/DAPNETGateway.ini index 104fddf..fe9abba 100644 --- a/DAPNETGateway.ini +++ b/DAPNETGateway.ini @@ -1,6 +1,8 @@ [General] Callsign=g9bf # WhiteList=12345,78901 +#BlacklistRegexfile=/tmp/blregexes.txt +#WhitelistRegexfile=/tmp/wlregexes.txt RptAddress=127.0.0.1 RptPort=3800 LocalAddress=127.0.0.1 diff --git a/README.REGEX b/README.REGEX new file mode 100644 index 0000000..9bce991 --- /dev/null +++ b/README.REGEX @@ -0,0 +1,21 @@ +The REGEX functionality allows you to whitelist or blacklist messages based on +pattern matching the message body itself (not the RIC). + +The options for this are: + + +WhitelistRegexfile= +BlacklistRegexfile= + +REGEXs are placed in the the respective textfile, one per line. These need to +be a full match, i.e. they need to match the entire message, begining with ^ +and ending with $. + +For example: + +^NAGIOS.*$ - matches all Nagios alerts + +^(G|M|2).+$ - Matches messages with the first character as a G.M or 2 - i.e. +match all UK callsigns. + + diff --git a/REGEX.cpp b/REGEX.cpp index e1b916f..5605b57 100644 --- a/REGEX.cpp +++ b/REGEX.cpp @@ -57,7 +57,7 @@ bool CREGEX::load() } size_t size = m_regex.size(); - LogInfo("Loaded %u REGEXes", size); + LogInfo("Loaded %u REGEX from file %s", size, regexFile.c_str()); size = m_regex.size(); if (size == 0U)