From bdaf8451a0063042ea588f7d863517aad3b3c67e Mon Sep 17 00:00:00 2001 From: rapturate Date: Mon, 8 Jun 2026 14:25:11 -0400 Subject: [PATCH] added location info into the log_parsing output. Now when the access logs are parsed, the geo_data is now included in the p_logs print function for each entry. --- ip_to_geo/ip_to_geo.cpp | 15 +++++++++------ ip_to_geo/ip_to_geo.hpp | 12 ++++++++++-- log_parsing/log_parsing.cpp | 10 +++++++++- log_parsing/log_parsing.hpp | 11 ++++++++++- main.cpp | 9 ++------- third_party/include/GeoLite2PP.hpp | 2 +- 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/ip_to_geo/ip_to_geo.cpp b/ip_to_geo/ip_to_geo.cpp index 2f7abe3..e04f74a 100644 --- a/ip_to_geo/ip_to_geo.cpp +++ b/ip_to_geo/ip_to_geo.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include "../third_party/include/GeoLite2PP.hpp" void load_env_file(const std::string& env_path) { std::ifstream file(env_path); @@ -30,7 +30,7 @@ void load_env_file(const std::string& env_path) { } } -std::string iplookup(const std::string& ip) { +loc_data iplookup(const std::string& ip) { // 1. Load variables from the local .env file load_env_file(); @@ -41,16 +41,19 @@ std::string iplookup(const std::string& ip) { std::string db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb"; - std::string geo_string = ""; + loc_data location; try { GeoLite2PP::DB db(db_path); std::map geo_data = db.get_all_fields(ip, "en"); - - geo_string = geo_data["country"] + "," + geo_data["subdivision"] + "," + geo_data["city"] + "," + geo_data["latitude"] + "," + geo_data["longitude"] + "\n"; + location.country = geo_data["country"]; + location.subdivision = geo_data["subdivision"]; + location.city = geo_data["city"]; + location.latitutde = geo_data["latitude"]; + location.longitude = geo_data["longitude"]; } catch (const std::exception& e) { std::cerr << "Database failed to load: " << e.what() << std::endl; } - return geo_string; + return location; }; \ No newline at end of file diff --git a/ip_to_geo/ip_to_geo.hpp b/ip_to_geo/ip_to_geo.hpp index 9491eae..77dab6a 100644 --- a/ip_to_geo/ip_to_geo.hpp +++ b/ip_to_geo/ip_to_geo.hpp @@ -12,6 +12,14 @@ #pragma once #include +struct loc_data { + std::string country; + std::string subdivision; + std::string city; + std::string latitutde; + std::string longitude; +}; + /** * @brief Loads configuration keys from a local environment file. * @@ -33,6 +41,6 @@ void load_env_file(const std::string& env_path = ".env"); * @brief Takes in a pointer to an ip address string and looks up the geolocation data via Maxmind DB (local) * @note Users must alter the `.env` file to specify their own local file path for the `DB_PATH` variable pointing to the MaxMind MMDB database. * - * @return std::string + * @return loc_data */ -std::string iplookup(const std::string&); \ No newline at end of file +loc_data iplookup(const std::string&); \ No newline at end of file diff --git a/log_parsing/log_parsing.cpp b/log_parsing/log_parsing.cpp index a18741d..0aac086 100644 --- a/log_parsing/log_parsing.cpp +++ b/log_parsing/log_parsing.cpp @@ -6,7 +6,7 @@ #include #include "log_parsing.hpp" -//#include "../ip_to_geo/ip_to_geo.hpp" +#include "../ip_to_geo/ip_to_geo.hpp" p_logs::p_logs(std::string log_path) { @@ -51,6 +51,8 @@ p_logs::p_logs(std::string log_path) { else if (raw_ua.find("curl/") != std::string::npos) current_entry.browser = "curl (CLI Tool)"; else current_entry.browser = "Unknown Browser/Bot"; + current_entry.location = iplookup(current_entry.ip); + logs.push_back(current_entry); } } @@ -94,6 +96,12 @@ 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" + << "Location:" << "\n" + << "\tCountry: " << log.location.country << "\n" + << "\tSubdivision: " << log.location.subdivision << "\n" + << "\tCity: " << log.location.city << "\n" + << "\tLongitude: " << log.location.longitude << "\n" + << "\tLatitude: " << log.location.latitutde << "\n" << "Timestamp: " << log.timestamp.c_str() << "\n" << "Request: " << log.request.c_str() << "\n" << "Status: " << log.status.c_str() << "\n" diff --git a/log_parsing/log_parsing.hpp b/log_parsing/log_parsing.hpp index 442c3c2..d4afc8e 100644 --- a/log_parsing/log_parsing.hpp +++ b/log_parsing/log_parsing.hpp @@ -12,9 +12,11 @@ #pragma once #include #include +#include "../ip_to_geo/ip_to_geo.hpp" struct Entry { std::string ip; + loc_data location; std::string timestamp; std::string request; std::string status; @@ -100,7 +102,14 @@ public: * @return std::string */ std::string entryx_browser(int); - + + /** + * @brief Getter function for a specific Entry's location info. + * + * @return loc_data + */ + loc_data entryx_location(int); + /** * @brief Prints all parsed log entries * diff --git a/main.cpp b/main.cpp index 657ba0e..c968d1b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,8 @@ #include "log_parsing/log_parsing.hpp" -#include "ip_to_geo/ip_to_geo.hpp" -#include + int main(){ p_logs logs("test_logs/access.log.txt"); logs.print_logs(); - - std::string ip = "192.178.248.40"; - std::cout << iplookup(ip); return 0; -} - +} \ No newline at end of file diff --git a/third_party/include/GeoLite2PP.hpp b/third_party/include/GeoLite2PP.hpp index efc9f7a..a0a96aa 100644 --- a/third_party/include/GeoLite2PP.hpp +++ b/third_party/include/GeoLite2PP.hpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include "maxminddb.h" /** The entire GeoLite2++ library is encapsulated in the GeoLite2PP namespace to prevent namespace pollution.