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.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <GeoLite2PP.hpp>
|
||||
#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<std::string, std::string> 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;
|
||||
};
|
||||
@@ -12,6 +12,14 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
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&);
|
||||
loc_data iplookup(const std::string&);
|
||||
Reference in New Issue
Block a user