diff --git a/ip_to_geo/ip_to_geo.cpp b/ip_to_geo/ip_to_geo.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ip_to_geo/ip_to_geo.h b/ip_to_geo/ip_to_geo.h new file mode 100644 index 0000000..e69de29 diff --git a/log_parsing/log_parsing.cpp b/log_parsing/log_parsing.cpp new file mode 100644 index 0000000..699c4a5 --- /dev/null +++ b/log_parsing/log_parsing.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "log_parsing.h" + +parse::parse(const std::smatch& match) { + Entry entry; + entry.ip = match[1].str(); + entry.timestamp = match[2].str(); + entry.request = match[3].str(); + entry.status = match[4].str(); + entry.bytes = match[5].str(); + entry.referer = match[6].str(); + + std::string raw_ua = match[7].str(); + + if (raw_ua.find("Windows NT 10.0") != std::string::npos) entry.os = "Windows 10/11"; + else if (raw_ua.find("Windows NT 6.1") != std::string::npos) entry.os = "Windows 7"; + else if (raw_ua.find("iPhone") != std::string::npos) entry.os = "iOS (iPhone)"; + else if (raw_ua.find("iPad") != std::string::npos) entry.os = "iOS (iPad)"; + else if (raw_ua.find("Android") != std::string::npos) entry.os = "Android"; + else if (raw_ua.find("Macintosh") != std::string::npos) entry.os = "macOS"; + else if (raw_ua.find("Linux") != std::string::npos) entry.os = "Linux"; + else entry.os = "Unknown OS"; + + if (raw_ua.find("Edg/") != std::string::npos) entry.browser = "Microsoft Edge"; + else if (raw_ua.find("OPR/") != std::string::npos) entry.browser = "Opera"; + else if (raw_ua.find("Chrome/") != std::string::npos) entry.browser = "Google Chrome"; + else if (raw_ua.find("Safari/") != std::string::npos) entry.browser = "Apple Safari"; + else if (raw_ua.find("Firefox/") != std::string::npos) entry.browser = "Mozilla Firefox"; + else if (raw_ua.find("curl/") != std::string::npos) entry.browser = "curl (CLI Tool)"; + else entry.browser = "Unknown Browser/Bot"; +} + +std::vector parse_file(std::string log_url) { + std::ifstream file(log_url); + + if(!file.is_open()) { + std::cerr << "Error loading " << log_url << std::endl; + EXIT_FAILURE; + } + + std::vector lines; + std::string line; + while(std::getline(file, line)){ + lines.push_back(std::move(line)); + } + file.close(); + + std::vector all_logs; + std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")"); + + for(const auto& log_line : lines) { + std::smatch match; + if(std::regex_search(log_line, match, log_pattern)) { + all_logs.emplace_back(match); + } + } + return all_logs; +} \ No newline at end of file diff --git a/log_parsing/log_parsing.h b/log_parsing/log_parsing.h new file mode 100644 index 0000000..309e22f --- /dev/null +++ b/log_parsing/log_parsing.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include + +struct Entry { + std::string ip; + std::string timestamp; + std::string request; + std::string status; + std::string bytes; + std::string referer; + std::string os; + std::string browser; +}; + +class parsed_logs { +private: + Entry entry; + std::vector logs; + std::string url; +public: + parsed_logs(std::string url); + void parse(const std::smatch& match); + + std::vector parse_file(std::string log_url); + std::string get_ip(); + std::string get_timestamp(); + std::string get_request(); + std::string get_status(); + std::string get_bytes(); + std::string get_referer(); + std::string get_os(); + std::string get_browser(); +}; \ No newline at end of file diff --git a/main b/main deleted file mode 100755 index 599262c..0000000 Binary files a/main and /dev/null differ diff --git a/main.cpp b/main.cpp index 38f89c4..9f8ce80 100644 --- a/main.cpp +++ b/main.cpp @@ -1,83 +1,5 @@ -#include -#include -#include -#include -#include -#include +#include "log_parsing/log_parsing.h" -class log_entry { -public: - std::string ip; - std::string timestamp; - std::string request; - std::string status; - std::string bytes; - std::string referer; - std::string os; - std::string browser; - - log_entry(const std::smatch& match) { - ip = match[1].str(); - timestamp = match[2].str(); - request = match[3].str(); - status = match[4].str(); - bytes = match[5].str(); - referer = match[6].str(); - - std::string raw_ua = match[7].str(); - - if (raw_ua.find("Windows NT 10.0") != std::string::npos) os = "Windows 10/11"; - else if (raw_ua.find("Windows NT 6.1") != std::string::npos) os = "Windows 7"; - else if (raw_ua.find("iPhone") != std::string::npos) os = "iOS (iPhone)"; - else if (raw_ua.find("iPad") != std::string::npos) os = "iOS (iPad)"; - else if (raw_ua.find("Android") != std::string::npos) os = "Android"; - else if (raw_ua.find("Macintosh") != std::string::npos) os = "macOS"; - else if (raw_ua.find("Linux") != std::string::npos) os = "Linux"; - else os = "Unknown OS"; - - if (raw_ua.find("Edg/") != std::string::npos) browser = "Microsoft Edge"; - else if (raw_ua.find("OPR/") != std::string::npos) browser = "Opera"; - else if (raw_ua.find("Chrome/") != std::string::npos) browser = "Google Chrome"; - else if (raw_ua.find("Safari/") != std::string::npos) browser = "Apple Safari"; - else if (raw_ua.find("Firefox/") != std::string::npos) browser = "Mozilla Firefox"; - else if (raw_ua.find("curl/") != std::string::npos) browser = "curl (CLI Tool)"; - else browser = "Unknown Browser/Bot"; - } -}; - -int main(int argc, char* argv[]) { - std::ifstream file(argv[1]); - - if(!file.is_open()) { - std::cerr << "Error loading " << argv[1] << std::endl; - return 1; - } - - - std::vector lines; - std::string line; - while(std::getline(file, line)){ - lines.push_back(std::move(line)); - } - file.close(); - - std::vector all_logs; - std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")"); - - for(const auto& log_line : lines) { - std::smatch match; - if(std::regex_search(log_line, match, log_pattern)) { - all_logs.emplace_back(match); - } - } - - for (size_t i = 0; i < all_logs.size(); ++i) { - std::cout << "Entry #" << i + 1 << ":\n" - << " IP: " << all_logs[i].ip << "\n" - << " OS: " << all_logs[i].os << "\n" - << " Browser: " << all_logs[i].browser << "\n" - << " Status: " << all_logs[i].status << "\n\n"; - } - - return 0; +int main(){ + parse logs; } \ No newline at end of file