Files
LumberJack_TUI/log_parsing/log_parsing.cpp

110 lines
4.0 KiB
C++

#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
#include <vector>
#include <regex>
#include <chrono>
#include "log_parsing.hpp"
p_logs::p_logs(std::string log_path) {
std::ifstream file(log_path);
if (!file.is_open()) {
std::cerr << "Error loading " << log_path << std::endl;
return;
}
std::regex log_pattern(R"((\S+)\s+-\s+-\s+\[([^\]]+)\]\s+\"([^\"]+)\"\s+(\d+)\s+(\d+|-)\s+\"([^\"]*)\"\s+\"([^\"]*)\")");
std::string line;
std::smatch match;
while (getline(file, line)) {
if (regex_search(line, match, log_pattern)) {
Entry current_entry;
current_entry.ip = match[1].str();
current_entry.timestamp = match[2].str();
current_entry.request = match[3].str();
current_entry.status = match[4].str();
current_entry.bytes = match[5].str();
current_entry.referer = match[6].str();
std::string raw_ua = match[7].str();
if (raw_ua.find("Windows NT 10.0") != std::string::npos) current_entry.os = "Windows 10/11";
else if (raw_ua.find("Windows NT 6.1") != std::string::npos) current_entry.os = "Windows 7";
else if (raw_ua.find("iPhone") != std::string::npos) current_entry.os = "iOS (iPhone)";
else if (raw_ua.find("iPad") != std::string::npos) current_entry.os = "iOS (iPad)";
else if (raw_ua.find("Android") != std::string::npos) current_entry.os = "Android";
else if (raw_ua.find("Macintosh") != std::string::npos) current_entry.os = "macOS";
else if (raw_ua.find("Linux") != std::string::npos) current_entry.os = "Linux";
else current_entry.os = "Unknown OS";
if (raw_ua.find("Edg/") != std::string::npos) current_entry.browser = "Microsoft Edge";
else if (raw_ua.find("OPR/") != std::string::npos) current_entry.browser = "Opera";
else if (raw_ua.find("Chrome/") != std::string::npos) current_entry.browser = "Google Chrome";
else if (raw_ua.find("Safari/") != std::string::npos) current_entry.browser = "Apple Safari";
else if (raw_ua.find("Firefox/") != std::string::npos) current_entry.browser = "Mozilla Firefox";
else if (raw_ua.find("curl/") != std::string::npos) current_entry.browser = "curl (CLI Tool)";
else current_entry.browser = "Unknown Browser/Bot";
logs.push_back(current_entry);
}
}
file.close();
}
std::string p_logs::entryx_ip(int x){
return logs[x].ip;
}
std::string p_logs::entryx_timestamp(int x){
return logs[x].timestamp;
}
std::string p_logs::entryx_request(int x){
return logs[x].request;
}
std::string p_logs::entryx_status(int x){
return logs[x].status;
}
std::string p_logs::entryx_bytes(int x){
return logs[x].bytes;
}
std::string p_logs::entryx_referer(int x){
return logs[x].referer;
}
std::string p_logs::entryx_os(int x){
return logs[x].os;
}
std::string p_logs::entryx_browser(int x){
return logs[x].browser;
}
// TO DO: TIME THE DIFFERENCE BETWEEN THESE TWO PRINTING METHODS
void p_logs::print_logs() {
std::ios_base::sync_with_stdio(false);
for (const auto& log : logs) {
std::cout << "IP: " << log.ip.c_str() << "\n"
<< "Timestamp: " << log.timestamp.c_str() << "\n"
<< "Request: " << log.request.c_str() << "\n"
<< "Status: " << log.status.c_str() << "\n"
<< "Bytes: " << log.bytes.c_str() << "\n"
<< "Referer: " << log.referer.c_str() << "\n"
<< "OS: " << log.os.c_str() << "\n"
<< "Browser: " << log.browser.c_str() << "\n\n"
<< "-----------------------------\n\n";
}
std::cout.flush();
}