Add config options for REGEX support. Also add README.REGEX

This commit is contained in:
Simon 2018-11-17 16:37:11 +00:00
parent 4c47986da0
commit 08157c342c
6 changed files with 53 additions and 10 deletions

View file

@ -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<uint32_t> 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;

4
Conf.h
View file

@ -33,6 +33,8 @@ public:
// The General section
std::string getCallsign() const;
std::vector<unsigned int> 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<unsigned int> m_whiteList;
std::string m_blacklistRegexfile;
std::string m_whitelistRegexfile;
std::string m_rptAddress;
unsigned int m_rptPort;
std::string m_myAddress;

View file

@ -270,11 +270,11 @@ int CDAPNETGateway::run()
std::vector<std::regex> regexBlacklist;
std::vector<std::regex> 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);

View file

@ -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

21
README.REGEX Normal file
View file

@ -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.

View file

@ -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)