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.
This commit is contained in:
Arkshine 2014-08-01 16:19:50 +02:00
parent 67bfe6bb36
commit ec3ff1deb9
4 changed files with 168 additions and 16 deletions

View File

@ -28,6 +28,8 @@
*/
#include "geoip_main.h"
#include "geoip_natives.h"
#include "geoip_util.h"
#include <time.h>
MMDB_s HandleDB;
ke::Vector<ke::AString> 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 <HandleDB.metadata.languages.count - 1)
{
fprintf(stdout, " ");
}
}
fprintf(stdout, "\n");
fprintf(stdout, " Description:\n");
for (size_t i = 0; i < HandleDB.metadata.description.count; ++i)
{
fprintf(stdout, " %s: %s\n",
HandleDB.metadata.description.descriptions[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 <command> [argument]\n");
printf(" Commands:\n");
printf(" version - display geoip database metadata\n");
printf(" dump <ip> [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)

View File

@ -33,5 +33,6 @@
#include "amxxmodule.h"
bool loadDatabase();
void OnGeoipCommand();
#endif // _INCLUDE_GEOIPMAIN_H

View File

@ -52,7 +52,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\;..\sdk;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\;..\sdk;..\..\..\public\amtl;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
@ -75,7 +75,7 @@
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>..\;..\sdk;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\;..\sdk;..\..\..\public\amtl;..\GeoIP2;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;GEOIP_EXPORTS;HAVE_STDINT_H;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
@ -96,15 +96,18 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\GeoIP2\maxminddb.cpp" />
<ClCompile Include="..\geoip_amxx.cpp" />
<ClCompile Include="..\geoip_main.cpp" />
<ClCompile Include="..\geoip_natives.cpp" />
<ClCompile Include="..\geoip_util.cpp" />
<ClCompile Include="..\sdk\amxxmodule.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\GeoIP2\maxminddb-compat-util.h" />
<ClInclude Include="..\GeoIP2\maxminddb.h" />
<ClInclude Include="..\GeoIP2\maxminddb_config.h" />
<ClInclude Include="..\geoip_amxx.h" />
<ClInclude Include="..\svn_version.h" />
<ClInclude Include="..\geoip_main.h" />
<ClInclude Include="..\geoip_natives.h" />
<ClInclude Include="..\geoip_util.h" />
<ClInclude Include="..\sdk\moduleconfig.h" />
<ClInclude Include="..\sdk\amxxmodule.h" />
</ItemGroup>

View File

@ -23,23 +23,23 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\geoip_amxx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\sdk\amxxmodule.cpp">
<Filter>Module SDK\SDK Base</Filter>
</ClCompile>
<ClCompile Include="..\GeoIP2\maxminddb.cpp">
<Filter>GeoIP2</Filter>
</ClCompile>
<ClCompile Include="..\geoip_util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\geoip_main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\geoip_natives.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\geoip_amxx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\svn_version.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\sdk\moduleconfig.h">
<Filter>Module SDK</Filter>
</ClInclude>
@ -55,6 +55,15 @@
<ClInclude Include="..\GeoIP2\maxminddb-compat-util.h">
<Filter>GeoIP2</Filter>
</ClInclude>
<ClInclude Include="..\geoip_util.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\geoip_natives.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\geoip_main.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\plugins\include\geoip.inc">