From dde020ea546dd75696543bb71f8a2f1acb383ca8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 28 Jun 2020 22:03:06 +0200 Subject: [PATCH] Modify regex-test mode for better batch-processing capabilities Signed-off-by: DL6ER --- src/args.c | 12 +++++ src/args.h | 2 + src/database/gravity-db.c | 2 +- src/dnsmasq_interface.c | 2 +- src/regex.c | 104 +++++++++++--------------------------- src/regex_r.h | 2 +- 6 files changed, 47 insertions(+), 77 deletions(-) diff --git a/src/args.c b/src/args.c index bfcdac04..fc23c657 100644 --- a/src/args.c +++ b/src/args.c @@ -274,3 +274,15 @@ const char __attribute__ ((const)) *cli_done(void) { return is_term() ? COL_GREEN"done!"COL_NC : "done!"; } + +// Sets font to bold +const char __attribute__ ((const)) *cli_bold(void) +{ + return is_term() ? COL_BOLD : ""; +} + +// Resets font to normal +const char __attribute__ ((const)) *cli_normal(void) +{ + return is_term() ? COL_NC : ""; +} diff --git a/src/args.h b/src/args.h index 2eed4e0a..c6a78137 100644 --- a/src/args.h +++ b/src/args.h @@ -21,5 +21,7 @@ const char *cli_cross(void) __attribute__ ((const)); const char *cli_info(void) __attribute__ ((const)); const char *cli_qst(void) __attribute__ ((const)); const char *cli_done(void) __attribute__ ((const)); +const char *cli_bold(void) __attribute__ ((const)); +const char *cli_normal(void) __attribute__ ((const)); #endif //ARGS_H diff --git a/src/database/gravity-db.c b/src/database/gravity-db.c index 3f11031f..3d763027 100644 --- a/src/database/gravity-db.c +++ b/src/database/gravity-db.c @@ -797,7 +797,7 @@ bool in_whitelist(const char *domain, const int clientID, clientsData* client) // optimization as the database lookup will most likely hit (a) more domains and (b) // will be faster (given a sufficiently large number of regex whitelisting filters). return domain_in_list(domain, stmt, "whitelist") || - match_regex(domain, clientID, REGEX_WHITELIST) != -1; + match_regex(domain, clientID, REGEX_WHITELIST, false) != -1; } bool in_gravity(const char *domain, const int clientID, clientsData* client) diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index 09c30180..a04d3020 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -101,7 +101,7 @@ static bool check_domain_blocked(const char *domain, const int clientID, // Skipped when the domain is whitelisted or blocked by exact blacklist or gravity int regex_idx = 0; if(!query->whitelisted && !blockDomain && - (regex_idx = match_regex(domain, clientID, REGEX_BLACKLIST)) > -1) + (regex_idx = match_regex(domain, clientID, REGEX_BLACKLIST, false)) > -1) { // We block this domain blockDomain = true; diff --git a/src/regex.c b/src/regex.c index e82c1042..11ce3d22 100644 --- a/src/regex.c +++ b/src/regex.c @@ -68,7 +68,7 @@ static bool compile_regex(const char *regexin, const int index, const enum regex return true; } -int match_regex(const char *input, const int clientID, const enum regex_type regexid) +int match_regex(const char *input, const int clientID, const enum regex_type regexid, const bool regextest) { int match_idx = -1; #ifdef USE_TRE_REGEX @@ -136,9 +136,31 @@ int match_regex(const char *input, const int clientID, const enum regex_type reg input, regexbuffer[regexid][index]); } - // Always check all regular expressions for clientID = -1 - if(clientID >= 0) + if(regextest && regexid == REGEX_CLI) + { + // CLI provided regular expression + logg(" %s%s%s matches", + cli_bold(), regexbuffer[regexid][index], cli_normal()); + } + else if(regextest && regexid == REGEX_BLACKLIST) + { + // Database-sourced regular expression + logg(" %s%s%s matches (regex blacklist, DB ID %i)", + cli_bold(), regexbuffer[regexid][index], cli_normal(), + regex_id[regexid][index]); + } + else if(regextest && regexid == REGEX_WHITELIST) + { + // Database-sourced regular expression + logg(" %s%s%s matches (regex whitelist, DB ID %i)", + cli_bold(), regexbuffer[regexid][index], cli_normal(), + regex_id[regexid][index]); + } + else + { + // Only check the first match when not in regex-test mode break; + } } // Print no match message when in regex debug mode @@ -329,21 +351,6 @@ void read_regex_from_database(void) counters->clients, timer_elapsed_msec(REGEX_TIMER)); } -// Get regex string from rowid in database -static const char __attribute__ ((pure)) *get_regex_from_rowid(const enum regex_type regexid, const int matchid) -{ - unsigned int index; - for(index = 0; index < counters->num_regex[regexid]; index++) - { - if(regex_id[regexid][index] == matchid) - break; - } - if(index < counters->num_regex[regexid]) - return regexbuffer[regexid][index]; - else - return "[not available]"; -} - int regex_test(const bool debug_mode, const char *domainin, const char *regexin) { // Prepare counters and regex memories @@ -359,59 +366,23 @@ int regex_test(const bool debug_mode, const char *domainin, const char *regexin) // Re-enable terminal output log_ctrl(false, true); - bool matched = false; - int matchidx; + int matchidx = -1; if(regexin == NULL) { // Read and compile regex lists from database - logg("%s Loading regex filters from database...", cli_info()); - timer_start(REGEX_TIMER); read_regex_table(REGEX_BLACKLIST); read_regex_table(REGEX_WHITELIST); - logg(" Compiled %i black- and %i whitelist regex filters in %.3f msec\n", - counters->num_regex[REGEX_BLACKLIST], - counters->num_regex[REGEX_WHITELIST], - timer_elapsed_msec(REGEX_TIMER)); // Check user-provided domain against all loaded regular blacklist expressions - logg("%s Checking domain against blacklist...", cli_info()); - timer_start(REGEX_TIMER); - matchidx = match_regex(domainin, -1, REGEX_BLACKLIST); - logg(" Time: %.3f msec", timer_elapsed_msec(REGEX_TIMER)); - - if(matchidx > -1) - { - matched = true; - logg("%s \"%s\" matched by \"%s\"\n", cli_tick(), - domainin, get_regex_from_rowid(REGEX_BLACKLIST, matchidx)); - } - else - { - logg("%s No result for \"%s\"\n", cli_cross(), domainin); - } + matchidx = match_regex(domainin, -1, REGEX_BLACKLIST, true); // Check user-provided domain against all loaded regular whitelist expressions - logg("%s Checking domain against whitelist...", cli_info()); - timer_start(REGEX_TIMER); - matchidx = match_regex(domainin, -1, REGEX_WHITELIST); - logg(" Time: %.3f msec", timer_elapsed_msec(REGEX_TIMER)); - - if(matchidx > -1) - { - matched = true; - logg("%s \"%s\" matched by \"%s\"\n", cli_tick(), - domainin, get_regex_from_rowid(REGEX_WHITELIST, matchidx)); - } - else - { - logg("%s No result for \"%s\"\n", cli_cross(), domainin); - } + matchidx = match_regex(domainin, -1, REGEX_WHITELIST, true); } else { // Compile CLI regex - logg("%s Compiling regex filter...", cli_info()); counters->num_regex[REGEX_BLACKLIST] = counters->num_regex[REGEX_WHITELIST] = 0; counters->num_regex[REGEX_CLI] = 1; @@ -427,26 +398,11 @@ int regex_test(const bool debug_mode, const char *domainin, const char *regexin) regex_available[REGEX_CLI][0] = true; else return EXIT_FAILURE; - logg(" Compiled regex filter in %.3f msec\n", timer_elapsed_msec(REGEX_TIMER)); // Check user-provided domain against user-provided regular expression - logg("%s Checking domain...", cli_info()); - timer_start(REGEX_TIMER); - matchidx = match_regex(domainin, -1, REGEX_CLI); - logg(" Time: %.3f msec", timer_elapsed_msec(REGEX_TIMER)); - - if(matchidx > -1) - { - matched = true; - logg("%s \"%s\" matched by \"%s\"\n", - cli_tick(), domainin, get_regex_from_rowid(REGEX_CLI, matchidx)); - } - else - { - logg("%s No result for \"%s\"\n", cli_cross(), domainin); - } + matchidx = match_regex(domainin, -1, REGEX_CLI, true); } // Return status 0 = MATCH, 1 = ERROR, 2 = NO MATCH - return matched ? EXIT_SUCCESS : 2; + return matchidx > -1 ? EXIT_SUCCESS : 2; } \ No newline at end of file diff --git a/src/regex_r.h b/src/regex_r.h index de0cc123..8c1591df 100644 --- a/src/regex_r.h +++ b/src/regex_r.h @@ -15,7 +15,7 @@ extern const char *regextype[]; -int match_regex(const char *input, const int clientID, const enum regex_type); +int match_regex(const char *input, const int clientID, const enum regex_type, const bool regextest); void allocate_regex_client_enabled(clientsData *client, const int clientID); void read_regex_from_database(void);