From ec3ff1deb92301f6d495bf1f1b411a254207d4d4 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Fri, 1 Aug 2014 16:19:50 +0200 Subject: [PATCH] Geoip: Add command to get database metadata and to dump all data of an IP address for debugging purposes. And for some reason some project files were not pushed. --- dlls/geoip/geoip_main.cpp | 143 +++++++++++++++++++++++- dlls/geoip/geoip_main.h | 1 + dlls/geoip/msvc10/geoip.vcxproj | 13 ++- dlls/geoip/msvc10/geoip.vcxproj.filters | 27 +++-- 4 files changed, 168 insertions(+), 16 deletions(-) diff --git a/dlls/geoip/geoip_main.cpp b/dlls/geoip/geoip_main.cpp index 1477d350..01a5bdeb 100644 --- a/dlls/geoip/geoip_main.cpp +++ b/dlls/geoip/geoip_main.cpp @@ -28,6 +28,8 @@ */ #include "geoip_main.h" #include "geoip_natives.h" +#include "geoip_util.h" +#include MMDB_s HandleDB; ke::Vector LangList; @@ -38,6 +40,8 @@ void OnAmxxAttach() { MF_AddNatives(GeoipNatives); } + + REG_SVR_COMMAND("geoip", OnGeoipCommand); } void OnAmxxDetach() @@ -47,6 +51,138 @@ void OnAmxxDetach() LangList.clear(); } +void OnGeoipCommand() +{ + const char *cmd = CMD_ARGV(1); + + if (!strcmp(cmd, "version")) + { + if (!HandleDB.filename) + { + printf("\n Database is not loaded.\n"); + return; + } + + const char *meta_dump = "\n" + " Database metadata\n" + " Node count: %i\n" + " Record size: %i bits\n" + " IP version: IPv%i\n" + " Binary format: %i.%i\n" + " Build epoch: %llu (%s)\n" + " Type: %s\n" + " Languages: "; + + char date[40]; + strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S UTC", gmtime((const time_t *)&HandleDB.metadata.build_epoch)); + + fprintf(stdout, meta_dump, + HandleDB.metadata.node_count, + HandleDB.metadata.record_size, + HandleDB.metadata.ip_version, + HandleDB.metadata.binary_format_major_version, + HandleDB.metadata.binary_format_minor_version, + HandleDB.metadata.build_epoch, + date, + HandleDB.metadata.database_type); + + for (size_t i = 0; i < HandleDB.metadata.languages.count; ++i) + { + fprintf(stdout, "%s", HandleDB.metadata.languages.names[i]); + + if (i language, + HandleDB.metadata.description.descriptions[i]->description); + } + fprintf(stdout, "\n"); + } + else if (!strcmp(cmd, "dump")) + { + if (!HandleDB.filename) + { + printf("\n Database is not loaded.\n\n"); + return; + } + + int num_args = CMD_ARGC(); + + if (num_args < 3) + { + printf("\n An IP address must be provided.\n\n"); + return; + } + + char *ip = stripPort((char *)CMD_ARGV(2)); + + int gai_error = 0; + int mmdb_error = 0; + + MMDB_lookup_result_s result = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error); + + if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !result.found_entry) + { + printf("\n Either look up failed or no found result.\n\n"); + return; + } + + MMDB_entry_data_list_s *entry_data_list = NULL; + int status = -1; + + if ((status = MMDB_get_entry_data_list(&result.entry, &entry_data_list)) != MMDB_SUCCESS || entry_data_list == NULL) + { + printf("\n Could not retrieve data list - %s.\n\n", MMDB_strerror(status)); + return; + } + + const char *file = NULL; + FILE *fp = NULL; + + if (num_args > 3) + { + file = CMD_ARGV(3); + fp = fopen(MF_BuildPathname("%s", file), "w"); + } + + if (!fp) + { + file = NULL; + fp = stdout; + } + + fprintf(fp, "\n"); + MMDB_dump_entry_data_list(fp, entry_data_list, 2); + fprintf(fp, "\n"); + + if (file) + { + fclose(fp); + } + + MMDB_free_entry_data_list(entry_data_list); + } + else + { + printf("\n"); + printf(" Usage: geoip [argument]\n"); + printf(" Commands:\n"); + printf(" version - display geoip database metadata\n"); + printf(" dump [output file] - dump all data from an IP address formatted in a JSON-ish fashion.\n"); + printf(" An output file is mod-based and if not provided, it will print in the console.\n"); + printf("\n"); + } +} + bool loadDatabase() { if (HandleDB.filename) // Already loaded. @@ -68,8 +204,11 @@ bool loadDatabase() for (size_t i = 0; i < ARRAYSIZE(databases); ++i) { - snprintf(file, sizeof(file)-1, "%s/%s/GeoLite2-%s.mmdb", modName, dataDir, databases[i]); // MF_BuildPathname not used because backslash - // makes CreateFileMapping failing under windows. + // MF_BuildPathname not used because backslash + // makes CreateFileMapping failing under windows. + + snprintf(file, sizeof(file)-1, "%s/%s/GeoLite2-%s.mmdb", modName, dataDir, databases[i]); + status = MMDB_open(file, MMDB_MODE_MMAP, &HandleDB); if (status == MMDB_SUCCESS) diff --git a/dlls/geoip/geoip_main.h b/dlls/geoip/geoip_main.h index e9da1f07..f6bb0ccb 100644 --- a/dlls/geoip/geoip_main.h +++ b/dlls/geoip/geoip_main.h @@ -33,5 +33,6 @@ #include "amxxmodule.h" bool loadDatabase(); +void OnGeoipCommand(); #endif // _INCLUDE_GEOIPMAIN_H \ No newline at end of file diff --git a/dlls/geoip/msvc10/geoip.vcxproj b/dlls/geoip/msvc10/geoip.vcxproj index c8645c01..3e1d5b8e 100644 --- a/dlls/geoip/msvc10/geoip.vcxproj +++ b/dlls/geoip/msvc10/geoip.vcxproj @@ -52,7 +52,7 @@ Disabled - ..\;..\sdk;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) + ..\;..\sdk;..\..\..\public\amtl;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true EnableFastChecks @@ -75,7 +75,7 @@ - ..\;..\sdk;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) + ..\;..\sdk;..\..\..\public\amtl;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreaded false @@ -96,15 +96,18 @@ - + + + - - + + + diff --git a/dlls/geoip/msvc10/geoip.vcxproj.filters b/dlls/geoip/msvc10/geoip.vcxproj.filters index be52e3c7..87f791de 100644 --- a/dlls/geoip/msvc10/geoip.vcxproj.filters +++ b/dlls/geoip/msvc10/geoip.vcxproj.filters @@ -23,23 +23,23 @@ - - Source Files - Module SDK\SDK Base GeoIP2 + + Source Files + + + Source Files + + + Source Files + - - Header Files - - - Header Files - Module SDK @@ -55,6 +55,15 @@ GeoIP2 + + Header Files + + + Header Files + + + Header Files +