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 <cstdlib>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <GeoLite2PP.hpp>
|
#include "../third_party/include/GeoLite2PP.hpp"
|
||||||
|
|
||||||
void load_env_file(const std::string& env_path) {
|
void load_env_file(const std::string& env_path) {
|
||||||
std::ifstream file(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
|
// 1. Load variables from the local .env file
|
||||||
load_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 db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb";
|
||||||
|
|
||||||
|
|
||||||
std::string geo_string = "";
|
loc_data location;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
GeoLite2PP::DB db(db_path);
|
GeoLite2PP::DB db(db_path);
|
||||||
std::map<std::string, std::string> geo_data = db.get_all_fields(ip, "en");
|
std::map<std::string, std::string> geo_data = db.get_all_fields(ip, "en");
|
||||||
|
location.country = geo_data["country"];
|
||||||
geo_string = geo_data["country"] + "," + geo_data["subdivision"] + "," + geo_data["city"] + "," + geo_data["latitude"] + "," + geo_data["longitude"] + "\n";
|
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) {
|
catch (const std::exception& e) {
|
||||||
std::cerr << "Database failed to load: " << e.what() << std::endl;
|
std::cerr << "Database failed to load: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
return geo_string;
|
return location;
|
||||||
};
|
};
|
||||||
@@ -12,6 +12,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#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.
|
* @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)
|
* @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.
|
* @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&);
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include "log_parsing.hpp"
|
#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) {
|
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 if (raw_ua.find("curl/") != std::string::npos) current_entry.browser = "curl (CLI Tool)";
|
||||||
else current_entry.browser = "Unknown Browser/Bot";
|
else current_entry.browser = "Unknown Browser/Bot";
|
||||||
|
|
||||||
|
current_entry.location = iplookup(current_entry.ip);
|
||||||
|
|
||||||
logs.push_back(current_entry);
|
logs.push_back(current_entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,6 +96,12 @@ void p_logs::print_logs() {
|
|||||||
std::ios_base::sync_with_stdio(false);
|
std::ios_base::sync_with_stdio(false);
|
||||||
for (const auto& log : logs) {
|
for (const auto& log : logs) {
|
||||||
std::cout << "IP: " << log.ip.c_str() << "\n"
|
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"
|
<< "Timestamp: " << log.timestamp.c_str() << "\n"
|
||||||
<< "Request: " << log.request.c_str() << "\n"
|
<< "Request: " << log.request.c_str() << "\n"
|
||||||
<< "Status: " << log.status.c_str() << "\n"
|
<< "Status: " << log.status.c_str() << "\n"
|
||||||
|
|||||||
@@ -12,9 +12,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "../ip_to_geo/ip_to_geo.hpp"
|
||||||
|
|
||||||
struct Entry {
|
struct Entry {
|
||||||
std::string ip;
|
std::string ip;
|
||||||
|
loc_data location;
|
||||||
std::string timestamp;
|
std::string timestamp;
|
||||||
std::string request;
|
std::string request;
|
||||||
std::string status;
|
std::string status;
|
||||||
@@ -100,7 +102,14 @@ public:
|
|||||||
* @return std::string
|
* @return std::string
|
||||||
*/
|
*/
|
||||||
std::string entryx_browser(int);
|
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
|
* @brief Prints all parsed log entries
|
||||||
*
|
*
|
||||||
|
|||||||
9
main.cpp
9
main.cpp
@@ -1,13 +1,8 @@
|
|||||||
#include "log_parsing/log_parsing.hpp"
|
#include "log_parsing/log_parsing.hpp"
|
||||||
#include "ip_to_geo/ip_to_geo.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
int main(){
|
int main(){
|
||||||
p_logs logs("test_logs/access.log.txt");
|
p_logs logs("test_logs/access.log.txt");
|
||||||
logs.print_logs();
|
logs.print_logs();
|
||||||
|
|
||||||
std::string ip = "192.178.248.40";
|
|
||||||
std::cout << iplookup(ip);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
2
third_party/include/GeoLite2PP.hpp
vendored
2
third_party/include/GeoLite2PP.hpp
vendored
@@ -9,7 +9,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
#include <maxminddb.h>
|
#include "maxminddb.h"
|
||||||
|
|
||||||
|
|
||||||
/** The entire GeoLite2++ library is encapsulated in the GeoLite2PP namespace to prevent namespace pollution.
|
/** The entire GeoLite2++ library is encapsulated in the GeoLite2PP namespace to prevent namespace pollution.
|
||||||
|
|||||||
Reference in New Issue
Block a user