From 7493c803fa8b9a73538b908dea98b7d51d23461d Mon Sep 17 00:00:00 2001
From: tieshagr <tieshagr@student.21>
Date: Wed, 5 Mar 2025 19:23:52 +0300
Subject: [PATCH] parser beta 0.1

---
 src/Makefile   | 14 ++++++++
 src/main.cpp   | 42 +++++++++++++++++++++++
 src/parser.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/parser.hpp | 19 +++++++++++
 4 files changed, 166 insertions(+)
 create mode 100644 src/Makefile
 create mode 100644 src/main.cpp
 create mode 100644 src/parser.cpp
 create mode 100644 src/parser.hpp

diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..cbe17c2
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,14 @@
+CXX = g++
+CXXFLAGS = -Iinclude/
+
+all: my_app
+
+# Сюда дописывать файлики для компиляции
+my_app:
+	$(CXX) $(CXXFLAGS) ./main.cpp ./parser.cpp -o my_app
+
+rebuild:
+	rm -f my_app && make my_app
+
+clean:
+	rm -f my_app
\ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..cf1ec65
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,42 @@
+#include "parser.hpp" 
+
+using namespace std;
+
+// Компиляция из директории вызвать команду make my_app
+// Запуск: ./my_app [флаги и аргументы к ним] (начать можно с флага --help) 
+int main(int argc, char *argv[]) {
+    Options opts;
+
+    int error = parse_params(argc, argv, opts);
+    if (!error)
+    {
+        if (opts.attack_type == "flood") {
+            cout << "type attack: " << opts.attack_type << "\n";
+            cout << "domain: " << opts.domain << "\n";
+            cout << "ip: " << opts.ip << "\n";
+            cout << "port: " << opts.port << "\n";
+            cout << "log file path: " << opts.log_file << "\n";
+        }
+        else if (opts.attack_type == "scan") {
+            cout << "type attack: " << opts.attack_type << "\n";
+            cout << "domain: " << opts.domain << "\n";
+            cout << "ip: " << opts.ip << "\n";
+            cout << "log file path: " << opts.log_file << "\n";
+        }
+
+        if (!opts.telegram_id.empty()) {
+            cout << "telegram_id: " << opts.telegram_id << "\n";
+        }
+
+        if (!opts.telegram_token.empty()) {
+            cout << "telegram_token: " << opts.telegram_token << "\n";
+        }
+        
+        
+    }
+    
+    
+    
+
+    return 0;
+}
\ No newline at end of file
diff --git a/src/parser.cpp b/src/parser.cpp
new file mode 100644
index 0000000..097020f
--- /dev/null
+++ b/src/parser.cpp
@@ -0,0 +1,91 @@
+#include "parser.hpp" 
+
+// Гарантируется наличие минимума нужных аргументов для flood и scan
+// Гарантируется, что после работы парсера мы получим только валидный тип атаки 
+
+// Добавить:
+// 1. Валидацию IP, port
+
+
+
+int parse_params(int argc, char** argv, Options& opts) {
+    // Короткие опции (с двоеточием для параметров)
+    const char* short_options = "a:d:i:p:l:t:b:h";
+    
+    // Длинные опции
+    const struct option long_options[] = {
+        {"attack",    required_argument, NULL, 'a'},
+        {"domain",    required_argument, NULL, 'd'},
+        {"ip",        required_argument, NULL, 'i'},
+        {"port",      required_argument, NULL, 'p'},
+        {"log",       required_argument, NULL, 'l'},
+        {"telegram",  required_argument, NULL, 't'},
+        {"token",     required_argument, NULL, 'b'},
+        {"help",      no_argument,       NULL, 'h'},
+        {NULL,     0,                 NULL,  0}
+    };
+
+    int opt;
+    while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
+        switch (opt) {
+            case 'a':
+                opts.attack_type = optarg;
+                break;
+            case 'd':
+                opts.domain = optarg;
+                break;
+            case 'i':
+                opts.ip = optarg;
+                break;
+            case 'p':
+                opts.port = optarg;
+                break;
+            case 'l':
+                opts.log_file = optarg;
+                break;
+            case 't':
+                opts.telegram_id = optarg;
+                break;
+            case 'b':
+                opts.telegram_token = optarg;
+                break;
+            case 'h':
+                std::cout << "Usage: " << argv[0] << " [options]\n"
+                          << "Required:\n"
+                          << "  -a, --attack TYPE    Type of attack (scan|flood)\n"
+                          << "  -d, --domain DOMAIN  Target domain\n"
+                          << "  -i, --ip IP          Target IP\n"
+                          << "  -p, --port PORT      Port. Required only for flood type!\n"
+                          << "Optional:\n"
+                          << "  -l, --log FILE       Log file\n"
+                          << "  -t, --telegram ID    Telegram ID\n"
+                          << "  -b, --token TOKEN    Telegram bot token\n";
+                return 1;
+            case '?':
+                std::cerr << "Unknown option!\n.--help for info\n";
+                return 1;
+            default:
+                return 1;
+        }
+    }
+
+    // Проверка обязательных параметров
+    if (opts.attack_type.empty()) {
+        std::cerr << "Error: Missing required parameters!\n--help for more info\n";
+        return 1;
+    }
+    else if (opts.attack_type != "flood" && opts.attack_type != "scan"){
+        std::cerr << "Error: Only scan or flood type of attack!\n--help for more info\n";
+        return 1;
+    }
+    else if (opts.attack_type == "flood" && (opts.domain.empty() || opts.ip.empty() || opts.port.empty())) {
+        std::cerr << "Error: Missing required parameters!\n--help for more info\n";
+        return 1;
+    }
+    else if (opts.attack_type == "scan" && (opts.domain.empty() || opts.ip.empty())) {
+        std::cerr << "Error: Missing required parameters!\n--help for more info\n";
+        return 1;
+    }
+
+    return 0;
+}
\ No newline at end of file
diff --git a/src/parser.hpp b/src/parser.hpp
new file mode 100644
index 0000000..cd8115a
--- /dev/null
+++ b/src/parser.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <iostream>
+#include <string>
+#include <unistd.h>
+#include <getopt.h> // Для struct option
+
+struct Options {
+    std::string attack_type;                   // Обязательный параметр
+    std::string domain;                        // Обязательный параметр
+    std::string ip;                            // Обязательный параметр
+    std::string port;                          // Обязательный параметр (не обязательный для скана)
+    std::string log_file = "/var/logs/DosAtk"; // Значение по умолчанию, не обязательный
+    std::string telegram_id;                   // Не обязательный параметр
+    std::string telegram_token;                // Не обязательный параметр
+};
+
+// Прототип функции парсинга
+int parse_params(int argc, char** argv, Options& opts);
\ No newline at end of file