MaxMind DB Reader (GeoIP2) for Squirrel

Started by ysc3839, Apr 12, 2019, 07:43 PM

Previous topic - Next topic

ysc3839

MaxMind DB Reader (GeoIP2) for VC:MP Squirrel Plugin.
Source code & Download: https://github.com/ysc3839/vcmp-squirrel-mmdb/releases

Documentation:
class MMDB
Constructor MMDB(filename)
Parameter types: string.
May throw error.

Function metadata()
Return type: table.
May throw error.

Function get(ip_address)
Parameter types: string. Return type: table.
May throw error.

Since version 1.1.0:
Some squirrel unsupported data types will be convert to other form.
BYTES in database will be convert to array ["BYTES", bytes_string]
UINT64 -> ["UINT64", high32bit, low32bit] on 32bit and squirrel native int on 64bit.
UINT128 -> ["UINT128", high_hi32bit, high_lo32bit, low_hi32bit, low_lo32bit] on 32bit and ["UINT128", high64bit, low64bit] on 64bit.

Usage:mmdb <- MMDB("GeoLite2-City.mmdb");
print(JSONEncoder.encode(mmdb.metadata()));
print(JSONEncoder.encode(mmdb.get("128.101.101.101")));
You can use JSONEncoder to dump tables to string.

Example output: (UTF-8 encoding)[SCRIPT]  {"description":{"en":"GeoLite2 City database"},"binary_format_major_version":2,"ip_version":6,"build_epoch":["UINT64",0,1554724656],"node_count":3854641,"languages":["de","en","es","fr","ja","pt-BR","ru","zh-CN"],"database_type":"GeoLite2-City","binary_format_minor_version":0,"record_size":28}
[SCRIPT]  {"subdivisions":[{"names":{"en":"Minnesota","ru":"Миннесота","ja":"ミネソタ州","zh-CN":"明尼苏达州","
es":"Minnesota","pt-BR":"Minesota","fr":"Minnesota"},"geoname_id":5037779,"iso_code":"MN"}],"continent":{"names":{"en":"North America","ru":"Северная Америка","ja":"北アメリカ","zh-CN":"北美洲","de":"Nordamerika","es":"Nortea
mérica","pt-BR":"América do Norte","fr":"Amérique du Nord"},"geoname_id":6255149,"code":"NA"},"country":{"names":{"en":"United States","ru":"США","ja":"アメリカ合衆国","zh-CN":"美国","de":"USA","es":"Estados Unidos","pt-BR":"Estados Unid
os","fr":"États-Unis"},"geoname_id":6252001,"iso_code":"US"},"postal":{"code":"55110"},"city":{"names":{"en":"Saint Paul","ru":"Сент-Пол","ja":"セントポール","zh-CN":"圣保罗","de":"Saint Paul","es":"Saint Paul","pt-BR":"Saint Paul","
fr":"Saint Paul"},"geoname_id":5045360},"registered_country":{"names":{"en":"United States","ru":"США","ja":"アメリカ
合衆国","zh-CN":"美国","de":"USA","es":"Estados Unidos","pt-BR":"Estados Unidos","fr":"États-Unis"},"geoname_id":6252001
,"iso_code":"US"},"location":{"latitude":45.08,"metro_code":613,"longitude":-93.0227,"time_zone":"America\/Chicago","accuracy_radius":20}

Some helper functions like old GeoIP API:
[spoiler]function geoip2_country_code_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.country.iso_code;
    }
    catch(e) {}
    return res;
}

function geoip2_country_name_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.country.names.en;
    }
    catch(e) {}
    return res;
}

function geoip2_city_name_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.city.names.en;
    }
    catch(e) {}
    return res;
}

function geoip2_subdivision_code_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.subdivisions[0].iso_code;
    }
    catch(e) {}
    return res;
}

function geoip2_subdivision_name_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.subdivisions[0].names.en;
    }
    catch(e) {}
    return res;
}

function geoip2_continent_code_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.continent.code;
    }
    catch(e) {}
    return res;
}

function geoip2_continent_name_by_addr(ip_address)
{
    local data = mmdb.get(ip_address);
    local res = null;
    try
    {
        res = data.continent.names.en;
    }
    catch(e) {}
    return res;
}
Usage:mmdb <- MMDB("GeoLite2-City.mmdb");
local ip = "128.101.101.101";
print(geoip2_country_code_by_addr(ip));
print(geoip2_country_name_by_addr(ip));
print(geoip2_city_name_by_addr(ip));
print(geoip2_subdivision_code_by_addr(ip));
print(geoip2_subdivision_name_by_addr(ip));
print(geoip2_continent_code_by_addr(ip));
print(geoip2_continent_name_by_addr(ip));
Example output:[SCRIPT]  US
[SCRIPT]  United States
[SCRIPT]  Saint Paul
[SCRIPT]  MN
[SCRIPT]  Minnesota
[SCRIPT]  NA
[SCRIPT]  North America
[/spoiler]

Free databases can be downloaded here.

umar4911

Off-topic: The encoder you gave me was not the same actually.
On-topic: Now I can actually don't need to yoi huge CSV files and make a laggy database eith few contents xD. Nice work!
RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter

ysc3839


umar4911

RT Member | Asian City Scripter | CTB Contributor | VKs Contributor | European City Contributor | EAD Scripter


ysc3839

Version 1.1.0 is released. https://github.com/ysc3839/vcmp-squirrel-mmdb/releases/tag/v1.1.0

Since version 1.1.0:
Some squirrel unsupported data types will be convert to other form.
BYTES in database will be convert to array ["BYTES", bytes_string]
UINT64 -> ["UINT64", high32bit, low32bit] on 32bit and squirrel native int on 64bit.
UINT128 -> ["UINT128", high_hi32bit, high_lo32bit, low_hi32bit, low_lo32bit] on 32bit and ["UINT128", high64bit, low64bit] on 64bit.

MatheuS

Vice City Multiplayer Server
 -------------------------------
 v0.4, (c) 2007-2014 VC:MP Team

Loaded plugin: xmlconf04rel64

Loaded plugin: announce04rel64

Loaded plugin: squirrel04rel64

Loaded plugin: sqlite04rel64

.so: cannot open shared object file: No such file or directory
Failed to load plugin: vcmp-squirrel-mmdb-rel64

[WEAPONS] No custom weapons to load.
** Started VC:MP 0.4 Server **
 Port: 6969
 Max players: 100

[MODULE] Loaded server.conf Loader for 0.4 by Stormeus
[MODULE]    >> Loaded server.conf for parsing
Segmentation fault (core dumped)

Why am I getting this error? I'm using Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-88-generic x86_64)
(and yes, I have the plugin inside plugins folder. Works correctly on my windows system, but does not work on linux)
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

habi

idk. Rename that plugin and try, because long long name.

MatheuS

Quote from: habi on Mar 02, 2020, 01:27 PMidk. Rename that plugin and try, because long long name.

I already did it.
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.