GeoIP: Add "reload" command (#588)

* Add reload option

* Adjust logic

* Modernize the code a little
This commit is contained in:
Duko 2018-09-23 23:01:49 +02:00 committed by Vincent Herbet
parent c3225afda1
commit 08213d8ead

View File

@ -14,16 +14,19 @@
#include "geoip_main.h" #include "geoip_main.h"
#include "geoip_natives.h" #include "geoip_natives.h"
#include "geoip_util.h" #include "geoip_util.h"
#include <time.h> #include <ctime>
MMDB_s HandleDB; MMDB_s HandleDB;
ke::Vector<ke::AString> LangList; ke::Vector<ke::AString> LangList;
bool NativesRegistered;
void OnAmxxAttach() void OnAmxxAttach()
{ {
if (loadDatabase()) if (loadDatabase())
{ {
MF_AddNatives(GeoipNatives); MF_AddNatives(GeoipNatives);
NativesRegistered = true;
} }
REG_SVR_COMMAND("geoip", OnGeoipCommand); REG_SVR_COMMAND("geoip", OnGeoipCommand);
@ -38,7 +41,7 @@ void OnAmxxDetach()
void OnGeoipCommand() void OnGeoipCommand()
{ {
const char *cmd = CMD_ARGV(1); const auto cmd = CMD_ARGV(1);
if (!strcmp(cmd, "version")) if (!strcmp(cmd, "version"))
{ {
@ -48,7 +51,7 @@ void OnGeoipCommand()
return; return;
} }
const char *meta_dump = "\n" const auto meta_dump = "\n"
" Database metadata\n" " Database metadata\n"
" Node count: %i\n" " Node count: %i\n"
" Record size: %i bits\n" " Record size: %i bits\n"
@ -59,7 +62,7 @@ void OnGeoipCommand()
" Languages: "; " Languages: ";
char date[40]; 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<const time_t *>(&HandleDB.metadata.build_epoch)));
fprintf(stdout, meta_dump, fprintf(stdout, meta_dump,
HandleDB.metadata.node_count, HandleDB.metadata.node_count,
@ -100,7 +103,7 @@ void OnGeoipCommand()
return; return;
} }
int num_args = CMD_ARGC(); const auto num_args = CMD_ARGC();
if (num_args < 3) if (num_args < 3)
{ {
@ -108,12 +111,12 @@ void OnGeoipCommand()
return; return;
} }
char *ip = stripPort((char *)CMD_ARGV(2)); const auto ip = stripPort(const_cast<char *>(CMD_ARGV(2)));
int gai_error = 0; auto gai_error = 0;
int mmdb_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) if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !result.found_entry)
{ {
@ -121,17 +124,17 @@ void OnGeoipCommand()
return; return;
} }
MMDB_entry_data_list_s *entry_data_list = NULL; MMDB_entry_data_list_s *entry_data_list = nullptr;
int status = -1; 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)); MF_PrintSrvConsole("\n Could not retrieve data list - %s.\n\n", MMDB_strerror(status));
return; return;
} }
const char *file = NULL; const char *file = nullptr;
FILE *fp = NULL; FILE *fp = nullptr;
if (num_args > 3) if (num_args > 3)
{ {
@ -141,7 +144,7 @@ void OnGeoipCommand()
if (!fp) if (!fp)
{ {
file = NULL; file = nullptr;
fp = stdout; fp = stdout;
} }
@ -156,12 +159,29 @@ void OnGeoipCommand()
MMDB_free_entry_data_list(entry_data_list); 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 else
{ {
MF_PrintSrvConsole("\n"); MF_PrintSrvConsole("\n");
MF_PrintSrvConsole(" Usage: geoip <command> [argument]\n"); MF_PrintSrvConsole(" Usage: geoip <command> [argument]\n");
MF_PrintSrvConsole(" Commands:\n"); MF_PrintSrvConsole(" Commands:\n");
MF_PrintSrvConsole(" version - display geoip database metadata\n"); MF_PrintSrvConsole(" version - display geoip database metadata\n");
MF_PrintSrvConsole(" reload - reload geoip database\n");
MF_PrintSrvConsole(" dump <ip> [output file] - dump all data from an IP address formatted in a JSON-ish fashion.\n"); MF_PrintSrvConsole(" dump <ip> [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(" An output file is mod-based and if not provided, it will print in the console.\n");
MF_PrintSrvConsole("\n"); MF_PrintSrvConsole("\n");
@ -170,7 +190,9 @@ void OnGeoipCommand()
bool loadDatabase() bool loadDatabase()
{ {
if (HandleDB.filename) // Already loaded. const auto isDatabaseLoaded = HandleDB.filename != nullptr;
if (isDatabaseLoaded)
{ {
return true; return true;
} }
@ -181,18 +203,18 @@ bool loadDatabase()
"Country" // Is the default shipped database with AMXX. "Country" // Is the default shipped database with AMXX.
}; };
const char *modName = MF_GetModname(); const auto modName = MF_GetModname();
const char *dataDir = MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"); const auto dataDir = MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data");
char file[255]; char file[260];
int status = -1; auto status = -1;
for (size_t i = 0; i < ARRAYSIZE(databases); ++i) for (auto& database : databases)
{ {
// MF_BuildPathname not used because backslash // MF_BuildPathname not used because backslash
// makes CreateFileMapping failing under windows. // 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); status = MMDB_open(file, MMDB_MODE_MMAP, &HandleDB);
@ -200,7 +222,8 @@ bool loadDatabase()
{ {
break; 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)); MF_Log("Could not open %s - %s", file, MMDB_strerror(status));