From a635b383fc019f776226373d173eabf5a95a0dae Mon Sep 17 00:00:00 2001 From: Duko Date: Sun, 23 Sep 2018 23:01:49 +0200 Subject: [PATCH] GeoIP: Add "reload" command (#588) * Add reload option * Adjust logic * Modernize the code a little --- modules/geoip/geoip_main.cpp | 69 ++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/modules/geoip/geoip_main.cpp b/modules/geoip/geoip_main.cpp index 3e9370de..bfc39008 100644 --- a/modules/geoip/geoip_main.cpp +++ b/modules/geoip/geoip_main.cpp @@ -14,16 +14,19 @@ #include "geoip_main.h" #include "geoip_natives.h" #include "geoip_util.h" -#include +#include MMDB_s HandleDB; ke::Vector LangList; +bool NativesRegistered; void OnAmxxAttach() { if (loadDatabase()) { MF_AddNatives(GeoipNatives); + + NativesRegistered = true; } REG_SVR_COMMAND("geoip", OnGeoipCommand); @@ -38,7 +41,7 @@ void OnAmxxDetach() void OnGeoipCommand() { - const char *cmd = CMD_ARGV(1); + const auto cmd = CMD_ARGV(1); if (!strcmp(cmd, "version")) { @@ -48,7 +51,7 @@ void OnGeoipCommand() return; } - const char *meta_dump = "\n" + const auto meta_dump = "\n" " Database metadata\n" " Node count: %i\n" " Record size: %i bits\n" @@ -59,7 +62,7 @@ void OnGeoipCommand() " Languages: "; char date[40]; - strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S UTC", gmtime((const time_t *)&HandleDB.metadata.build_epoch)); + strftime(date, sizeof date, "%Y-%m-%d %H:%M:%S UTC", gmtime(reinterpret_cast(&HandleDB.metadata.build_epoch))); fprintf(stdout, meta_dump, HandleDB.metadata.node_count, @@ -100,7 +103,7 @@ void OnGeoipCommand() return; } - int num_args = CMD_ARGC(); + const auto num_args = CMD_ARGC(); if (num_args < 3) { @@ -108,12 +111,12 @@ void OnGeoipCommand() return; } - char *ip = stripPort((char *)CMD_ARGV(2)); + const auto ip = stripPort(const_cast(CMD_ARGV(2))); - int gai_error = 0; - int mmdb_error = 0; + auto gai_error = 0; + auto mmdb_error = 0; - MMDB_lookup_result_s result = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error); + auto result = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error); if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !result.found_entry) { @@ -121,17 +124,17 @@ void OnGeoipCommand() return; } - MMDB_entry_data_list_s *entry_data_list = NULL; - int status = -1; + MMDB_entry_data_list_s *entry_data_list = nullptr; + int status; - if ((status = MMDB_get_entry_data_list(&result.entry, &entry_data_list)) != MMDB_SUCCESS || entry_data_list == NULL) + if ((status = MMDB_get_entry_data_list(&result.entry, &entry_data_list)) != MMDB_SUCCESS || entry_data_list == nullptr) { MF_PrintSrvConsole("\n Could not retrieve data list - %s.\n\n", MMDB_strerror(status)); return; } - const char *file = NULL; - FILE *fp = NULL; + const char *file = nullptr; + FILE *fp = nullptr; if (num_args > 3) { @@ -141,7 +144,7 @@ void OnGeoipCommand() if (!fp) { - file = NULL; + file = nullptr; fp = stdout; } @@ -156,12 +159,29 @@ void OnGeoipCommand() MMDB_free_entry_data_list(entry_data_list); } + else if (!strcmp(cmd, "reload")) + { + const auto isDatabaseLoaded = HandleDB.filename != nullptr; + + if (isDatabaseLoaded) + { + MMDB_close(&HandleDB); + } + + if (loadDatabase() && !NativesRegistered) + { + MF_AddNatives(GeoipNatives); + + NativesRegistered = true; + } + } else { MF_PrintSrvConsole("\n"); MF_PrintSrvConsole(" Usage: geoip [argument]\n"); MF_PrintSrvConsole(" Commands:\n"); MF_PrintSrvConsole(" version - display geoip database metadata\n"); + MF_PrintSrvConsole(" reload - reload geoip database\n"); MF_PrintSrvConsole(" dump [output file] - dump all data from an IP address formatted in a JSON-ish fashion.\n"); MF_PrintSrvConsole(" An output file is mod-based and if not provided, it will print in the console.\n"); MF_PrintSrvConsole("\n"); @@ -170,7 +190,9 @@ void OnGeoipCommand() bool loadDatabase() { - if (HandleDB.filename) // Already loaded. + const auto isDatabaseLoaded = HandleDB.filename != nullptr; + + if (isDatabaseLoaded) { return true; } @@ -181,18 +203,18 @@ bool loadDatabase() "Country" // Is the default shipped database with AMXX. }; - const char *modName = MF_GetModname(); - const char *dataDir = MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"); + const auto modName = MF_GetModname(); + const auto dataDir = MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"); - char file[255]; - int status = -1; + char file[260]; + auto status = -1; - for (size_t i = 0; i < ARRAYSIZE(databases); ++i) + for (auto& database : databases) { // MF_BuildPathname not used because backslash // makes CreateFileMapping failing under windows. - ke::SafeSprintf(file, sizeof(file), "%s/%s/GeoLite2-%s.mmdb", modName, dataDir, databases[i]); + ke::SafeSprintf(file, sizeof file, "%s/%s/GeoLite2-%s.mmdb", modName, dataDir, database); status = MMDB_open(file, MMDB_MODE_MMAP, &HandleDB); @@ -200,7 +222,8 @@ bool loadDatabase() { break; } - else if (status != MMDB_FILE_OPEN_ERROR) + + if (status != MMDB_FILE_OPEN_ERROR) { MF_Log("Could not open %s - %s", file, MMDB_strerror(status));