Main Menu

GeoIP Module

Started by Vortrex, March 14, 2020, 10:55:56 AM

Previous topic - Next topic

Vortrex

Here is a GeoIP module for those who want to use it for their server.

Download: https://github.com/VortrexFTW/mod_geoip/releases

First you'll need an MMDB database file from MaxMind. You'll need to make an account, receive a license key, and then download the database file (it's free). After you have the database file, copy/upload to your server, and use the name for it in the file arg for the GeoIP functions below. Directory starts with the main server folder, but relative paths are supported.

Place into your server's "modules" folder and put "<module src="mod_geoip" />" into the modules section of your server XML. Be sure to include the name of your modules folder (it's relative to the main server directory) and do **not** include an extension. It should look similar to this:
<modules>
    <module src="modules/mod_geoip" />
</modules>


JavaScript Example
addEventHandler("OnPlayerJoined", function(event, client) {
let countryName = module.geoip.getCountryName("geoip-city.mmdb", client.ip);
console.log(client.name + " connected from " + countryName);
});

Lua Example
addEventHandler("OnPlayerJoined", function(event, client)
local countryISO = module.geoip.getCountryName("geoip-city.mmdb", client:ip)
print(client:name .. " connected from " .. countryName)
end)


All functions below return strings.
// Country info
module.geoip.getCountryName(string dbFile, string ipAddress)
module.geoip.getCountryISO(string dbFile, string ipAddress)

// Continent info
module.geoip.getContinentName(string dbFile, string ipAddress)
module.geoip.getContinentCode(string dbFile, string ipAddress)

// For states, provinces, etc
module.geoip.getSubdivisionName(string dbFile, string ipAddress)
module.geoip.getSubdivisionISO(string dbFile, string ipAddress)

// Misc
module.geoip.getCityName(string dbFile, string ipAddress)
module.geoip.getPostalCode(string dbFile, string ipAddress)