Additionally, I added a .env file and started using cmake build styles rather than g++ for more efficient compiling and testing. Changes to be committed: new file: .env new file: CMakeLists.txt new file: build/CMakeCache.txt new file: build/CMakeFiles/4.3.2/CMakeCXXCompiler.cmake new file: build/CMakeFiles/4.3.2/CMakeDetermineCompilerABI_CXX.bin new file: build/CMakeFiles/4.3.2/CMakeSystem.cmake new file: build/CMakeFiles/4.3.2/CompilerIdCXX/CMakeCXXCompilerId.cpp new file: build/CMakeFiles/4.3.2/CompilerIdCXX/a.out new file: build/CMakeFiles/CMakeConfigureLog.yaml new file: build/CMakeFiles/CMakeDirectoryInformation.cmake new file: build/CMakeFiles/InstallScripts.json new file: build/CMakeFiles/Makefile.cmake new file: build/CMakeFiles/Makefile2 new file: build/CMakeFiles/TargetDirectories.txt new file: build/CMakeFiles/cmake.check_cache new file: build/CMakeFiles/parselog_cli.dir/DependInfo.cmake new file: build/CMakeFiles/parselog_cli.dir/build.make new file: build/CMakeFiles/parselog_cli.dir/cmake_clean.cmake new file: build/CMakeFiles/parselog_cli.dir/compiler_depend.internal new file: build/CMakeFiles/parselog_cli.dir/compiler_depend.make new file: build/CMakeFiles/parselog_cli.dir/compiler_depend.ts new file: build/CMakeFiles/parselog_cli.dir/depend.make new file: build/CMakeFiles/parselog_cli.dir/flags.make new file: build/CMakeFiles/parselog_cli.dir/ip_to_geo/ip_to_geo.cpp.o new file: build/CMakeFiles/parselog_cli.dir/ip_to_geo/ip_to_geo.cpp.o.d new file: build/CMakeFiles/parselog_cli.dir/link.d new file: build/CMakeFiles/parselog_cli.dir/link.txt new file: build/CMakeFiles/parselog_cli.dir/log_parsing/log_parsing.cpp.o new file: build/CMakeFiles/parselog_cli.dir/log_parsing/log_parsing.cpp.o.d new file: build/CMakeFiles/parselog_cli.dir/main.cpp.o new file: build/CMakeFiles/parselog_cli.dir/main.cpp.o.d new file: build/CMakeFiles/parselog_cli.dir/progress.make new file: build/CMakeFiles/parselog_cli.dir/third_party/src/GeoLite2PP.cpp.o new file: build/CMakeFiles/parselog_cli.dir/third_party/src/GeoLite2PP.cpp.o.d new file: build/CMakeFiles/parselog_cli.dir/third_party/src/GeoLite2PP_error_category.cpp.o new file: build/CMakeFiles/parselog_cli.dir/third_party/src/GeoLite2PP_error_category.cpp.o.d new file: build/CMakeFiles/progress.marks new file: build/Makefile new file: build/cmake_install.cmake new file: build/compile_commands.json new file: build/parselog_cli new file: compile_commands.json new file: data/GeoLite2-City.mmdb modified: ip_to_geo/ip_to_geo.cpp modified: ip_to_geo/ip_to_geo.hpp modified: main.cpp deleted: parselog_cli.exe deleted: test renamed: ip_to_geo/GeoLite2PP.hpp -> third_party/include/GeoLite2PP.hpp new file: third_party/include/GeoLite2PP_error_category.hpp new file: third_party/include/GeoLite2PP_version.hpp new file: third_party/include/maxminddb.h new file: third_party/include/maxminddb_config.h new file: third_party/lib/libmaxminddb.a new file: third_party/src/GeoLite2PP.cpp new file: third_party/src/GeoLite2PP_error_category.cpp
56 lines
1.8 KiB
C++
56 lines
1.8 KiB
C++
#include "ip_to_geo.hpp"
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <cstdlib>
|
|
#include <map>
|
|
#include <string>
|
|
#include <GeoLite2PP.hpp>
|
|
|
|
void load_env_file(const std::string& env_path) {
|
|
std::ifstream file(env_path);
|
|
if (!file.is_open()) {
|
|
std::cerr << "Warning: Could not open " << env_path << " file." << std::endl;
|
|
return;
|
|
}
|
|
|
|
std::string line;
|
|
while (std::getline(file, line)) {
|
|
// Skip empty lines or comments
|
|
if (line.empty() || line[0] == '#') continue;
|
|
|
|
std::size_t delimiter_pos = line.find('=');
|
|
if (delimiter_pos != std::string::npos) {
|
|
std::string key = line.substr(0, delimiter_pos);
|
|
std::string value = line.substr(delimiter_pos + 1);
|
|
|
|
// Set the variable globally in the execution environment
|
|
setenv(key.c_str(), value.c_str(), 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string iplookup(const std::string& ip) {
|
|
|
|
// 1. Load variables from the local .env file
|
|
load_env_file();
|
|
|
|
// 2. Fetch the path out of the environment variables
|
|
const char* env_db_path = std::getenv("DB_PATH");
|
|
// Fallback to a default path if the environment variable wasn't set
|
|
std::string db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb";
|
|
|
|
|
|
std::string geo_string = "";
|
|
|
|
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";
|
|
}
|
|
catch (const std::exception& e) {
|
|
std::cerr << "Database failed to load: " << e.what() << std::endl;
|
|
}
|
|
return geo_string;
|
|
}; |