summary refs log tree commit diff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c75
1 files changed, 66 insertions, 9 deletions
diff --git a/main.c b/main.c

index 18e9f80..9ab4181 100644 --- a/main.c +++ b/main.c
@@ -29,6 +29,10 @@ bool print_allowed_pattern = false; bool print_already_banned_ip = false; bool print_indeterminate = false; bool print_bans = true; +bool print_only = false; +bool print_progress = true; + +bool dump_patterns = false; char* allowed_patterns_path = ALLOWED_PATTERNS_FILE; char* allowed_ips_path = ALLOWED_IPS_FILE; @@ -85,6 +89,7 @@ void read_lines_cb(const char* filename, void cb(char*, void*), void* cbData) int count = 0; char* buf = xmalloc(10000); + clock_t times[1000]; while (fgets(buf, 10000, file) != NULL) { if (buf[0] == '#') @@ -95,17 +100,17 @@ void read_lines_cb(const char* filename, void cb(char*, void*), void* cbData) buf[strcspn(buf, "\n")] = 0; char* line = strdup(buf); - if (cb != nullptr) - cb(line, cbData); + cb(line, cbData); free(line); - if (count % 10 == 0) + times[count % 1000] = clock(); + if (print_progress && count % 10 == 0) { double elapsed = (clock() - lastPrint) * 1000.0 / CLOCKS_PER_SEC; + double totalElapsed = (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC; if (elapsed > 50.0) { - fprintf(stderr, "\rRead %d lines from %s in %.3f ms (%.3f/s)\r", count, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC, - count * 1000.0 / (clock() - ctime)); + fprintf(stderr, "\rRead %d lines from %s in %.3f ms (%.3f/s)\r", count, filename, totalElapsed, count / (totalElapsed / 1000.0)); lastPrint = clock(); } } @@ -114,7 +119,7 @@ void read_lines_cb(const char* filename, void cb(char*, void*), void* cbData) free(buf); fclose(file); - printf("Read %d definitions from %s in %.3f ms (%.3f/s)\n", count, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC, count * 1000.0 / (clock() - ctime)); + fprintf(stderr, "Read %d definitions from %s in %.3f ms (%.3f/s)\n", count, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC, count * 1000.0 / (clock() - ctime)); } char** read_lines(const char* filename) @@ -171,9 +176,8 @@ char** read_lines(const char* filename) buf[strcspn(buf, "\n")] = 0; lines[i] = strdup(buf); - double elapsed = (clock() - lastPrint) * 1000.0 / CLOCKS_PER_SEC; - if (elapsed > 50) + if (print_progress && elapsed > 50) { fprintf(stderr, "\rRead %d lines from %s in %.3f ms\r", i + 1, filename, (clock() - ctime) * 1000.0 / CLOCKS_PER_SEC); lastPrint = clock(); @@ -199,6 +203,11 @@ char** grow_string_array(char** array, size_t newSize) return newArray; } +void ban_ip(char* ip) +{ + +} + void process_line(char* line, void* cbData) { MatchRules* rules = cbData; @@ -269,6 +278,8 @@ void process_line(char* line, void* cbData) if (print_bans) printf("[Bans=%6lu] %15s matched pattern %4d (\"%s\"): %s\n", rules->bannedIpCount, ip, i, rules->patterns[i], line); + if (print_only) return; + __pid_t pid = fork(); if (pid == 0) { @@ -300,11 +311,12 @@ void process_line(char* line, void* cbData) int main(int argc, char* argv[]) { - printf("Hello, World!\n"); + fprintf(stderr, "Hello, World!\n"); print_allowed_ip = getenv("D_PRINT_ALLOWED_IP") != NULL && strcmp(getenv("D_PRINT_ALLOWED_IP"), "1") == 0; print_allowed_pattern = getenv("D_PRINT_ALLOWED_PATTERN") != NULL && strcmp(getenv("D_PRINT_ALLOWED_PATTERN"), "1") == 0; print_already_banned_ip = getenv("D_PRINT_BANNED_IP") != NULL && strcmp(getenv("D_PRINT_BANNED_IP"), "1") == 0; + print_progress = isatty(STDERR_FILENO); for (int i = 1; i < argc; i++) { @@ -326,6 +338,12 @@ int main(int argc, char* argv[]) print_bans = strstr(argv[i], "=false") == NULL; else if (strstr(argv[i], "--print-indeterminate") != NULL) print_indeterminate = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--print-only") != NULL) + print_only = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--print-progress") != NULL) + print_progress = strstr(argv[i], "=false") == NULL; + else if (strstr(argv[i], "--dump-patterns") != NULL) + dump_patterns = true; else if (strcmp(argv[i], "--debug") == 0) { print_allowed_ip = true; @@ -334,6 +352,25 @@ int main(int argc, char* argv[]) print_indeterminate = true; print_bans = true; } + else if (strstr(argv[i], "--help")) + { + printf("Usage: %s [options]\n", argv[0]); + printf("Options:\n"); + printf(" --allowed-ips <path> Path to the allowed IPs file (default: %s)\n", ALLOWED_IPS_FILE); + printf(" --allowed-patterns <path> Path to the allowed patterns file (default: %s)\n", ALLOWED_PATTERNS_FILE); + printf(" --patterns <path> Path to the patterns file (default: %s)\n", PATTERNS_FILE); + printf(" --access-log <path> Path to the access log file (default: %s)\n", ACCESS_LOG_FILE); + printf(" --print-allowed-ip[=false] Print allowed IPs (default: %s)\n", print_allowed_ip ? "true" : "false"); + printf(" --print-allowed-pattern[=false] Print allowed patterns (default: %s)\n", print_allowed_pattern ? "true" : "false"); + printf(" --print-already-banned-ip[=false] Print already banned IPs (default: %s)\n", print_already_banned_ip ? "true" : "false"); + printf(" --print-bans[=false] Print bans (default: %s)\n", print_bans ? "true" : "false"); + printf(" --print-indeterminate[=false] Print indeterminate lines (default: %s)\n", print_indeterminate ? "true" : "false"); + printf(" --print-only[=false] Print only the lines that match the rules (default: %s)\n", print_only ? "true" : "false"); + printf(" --print-progress[=false] Print progress while reading file (default: %s)\n", print_progress ? "true" : "false"); + printf(" --debug Enable debug mode (prints all information)\n"); + printf(" --dump-patterns Dump pattern table and exit\n"); + return 0; + } else { fprintf(stderr, "Unknown argument: %s\n", argv[i]); @@ -366,8 +403,28 @@ int main(int argc, char* argv[]) rules.bannedIps = xmalloc(sizeof(char**) * rules.bannedIpSize); //rules.bannedIps[0] = ; + if (dump_patterns) + { + fprintf(stderr, "Dumping patterns:\n"); + for (int i = 0; rules.patterns[i] != NULL; i++) + { + printf("[PATTERN %4d] %s\n", i, rules.patterns[i]); + } + for (int i = 0; rules.allowed_patterns[i] != NULL; i++) + { + printf("[ALLOWED %4d] %s\n", i, rules.allowed_patterns[i]); + } + for (int i = 0; rules.allowedIps[i] != NULL; i++) + { + printf("[ALL. IP %4d] %s\n", i, rules.allowedIps[i]); + } + return 0; + } + read_lines_cb(access_log_path, process_line, &rules); + fprintf(stderr, "Banned %lu IPs:\n", rules.bannedIpCount); + for (int i = 0; rules.bannedIps[i] != NULL; i++) { free(rules.bannedIps[i]);