mckaygerhard
ad6e49f63f
* set the improved version of ip check, using apikey, if the apikey is not set or empty, it will use simple geoip * the command `govip` will check ip in all ways, if error, i will suggest to try `geoip` command * for geoip comman, no matter if mod its present or not.. it will detect geoip mod and provide command and log for * registered geoip privilegie if geoip mod is not present * registered governing privilegie * license of code and license headers * improve README documentation
133 lines
4.6 KiB
Lua
133 lines
4.6 KiB
Lua
-- mod governor by mckaygerhard
|
|
-- Copyright 2020
|
|
|
|
----------------------------------------------------------------------------
|
|
-- this program can be used free but cannot be used commertially or
|
|
-- modified for, licenced CC-BY-SA-NC 4.0
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
local modname = "governing"
|
|
local modpath = minetest.get_modpath("governing")
|
|
local modstor = minetest.get_mod_storage()
|
|
local worlddir = minetest.get_worldpath()
|
|
local modmail = minetest.get_modpath("mail")
|
|
local modgeoip = minetest.get_modpath("geoip")
|
|
local moddefault = minetest.get_modpath("default")
|
|
local modkillme = minetest.get_modpath("killme")
|
|
local modcommand = minetest.get_modpath("game_commands")
|
|
local modcreative = minetest.get_modpath("creative")
|
|
local checkapikey = minetest.settings:get("governing.checkapikey") or ""
|
|
|
|
local httpapi = minetest.request_http_api and minetest.request_http_api() -- Only works at init time and must be called from the mod's main scope (not from a function).
|
|
|
|
local S
|
|
|
|
if minetest.get_translator ~= nil then
|
|
S = minetest.get_translator(modname) -- 5.x translation function
|
|
else
|
|
if minetest.get_modpath("intllib") then
|
|
dofile(minetest.get_modpath("intllib") .. "/init.lua")
|
|
if intllib.make_gettext_pair then
|
|
gettext, ngettext = intllib.make_gettext_pair() -- new gettext method
|
|
else
|
|
gettext = intllib.Getter() -- old text file method
|
|
end
|
|
S = gettext
|
|
else -- boilerplate function
|
|
S = function(str, ...)
|
|
local args = {...}
|
|
return str:gsub("@%d+", function(match)
|
|
return args[tonumber(match:sub(2))]
|
|
end)
|
|
end
|
|
end
|
|
end
|
|
|
|
local creative_mode_cache = minetest.settings:get_bool("creative_mode")
|
|
|
|
governing = {}
|
|
|
|
governing.S = S
|
|
governing.httpapi = httpapi -- must be called early at the beggining to work
|
|
governing.modname = modname -- name of the mod
|
|
governing.modpath = modpath -- path of the mod
|
|
governing.modstor = modstor -- request of storage for the mod
|
|
governing.worlddir = worlddir -- path of the world were the mod is running
|
|
governing.modgeoip = modgeoip -- path of the geoip mod if available for sending messages internaly on private
|
|
governing.modmail = modmail -- path of the mail mod if available for sending messages internaly on private
|
|
governing.moddefault = moddefault -- path of default mod if availalbe
|
|
governing.modkillme = modkillme -- if killme is present as mod if available
|
|
governing.modcommand = modcommand -- if same killme and commands is present as mod if available
|
|
governing.checkapikey = checkapikey -- need for geoip improved ip information, then geoip normal will be used
|
|
|
|
function governing.is_creative(name)
|
|
if creative then
|
|
return creative.is_creative(name)
|
|
else
|
|
return minetest.check_player_privs(name, {creative = true}) or creative_mode_cache
|
|
end
|
|
end
|
|
|
|
minetest.register_privilege("governing", {
|
|
description = "can do geoip lookups on players for governing",
|
|
give_to_singleplayer = false
|
|
})
|
|
|
|
if not governing.modgeoip then
|
|
minetest.register_privilege("geoip", {
|
|
description = "governing: ip checks + lookups on players",
|
|
give_to_singleplayer = false
|
|
})
|
|
end
|
|
|
|
function governing.lookup(ip, callback)
|
|
if not httpapi then
|
|
minetest.log("error", "[governing/geoip] mod not in the trusted http mods!")
|
|
return
|
|
end
|
|
httpapi.fetch( {
|
|
url = "https://tools.keycdn.com/geo.json?host=" .. ip,
|
|
extra_headers = { "User-Agent: keycdn-tools:https://minetest.org" },
|
|
timeout = 5,
|
|
}, function(res)
|
|
if res.code == 200 then
|
|
local data = minetest.parse_json(res.data)
|
|
callback(data)
|
|
else
|
|
minetest.log("warning", "[governing/geoip] http request returned status: " .. res.code)
|
|
end
|
|
end)
|
|
end
|
|
|
|
function governing.checkip(ip, callback)
|
|
if not httpapi then
|
|
minetest.log("error", "[governing] mod not in the trusted http mods!")
|
|
return
|
|
end
|
|
httpapi.fetch( {
|
|
url = "https://vpnapi.io/api/" .. ip .. "?key==" .. governing.checkapikey,
|
|
extra_headers = { "User-Agent: VenenuX-minenux:https://git.minetest.io/minenux" },
|
|
timeout = 5,
|
|
}, function(res)
|
|
if res.code == 200 then
|
|
local data = minetest.parse_json(res.data)
|
|
callback(data)
|
|
else
|
|
minetest.log("warning", "[governing/ip] http request returned status: " .. res.code)
|
|
end
|
|
end)
|
|
end
|
|
|
|
dofile(governing.modpath.."/geoip.lua")
|
|
dofile(governing.modpath.."/commands.lua")
|
|
|
|
if governing.checkapikey == "" then minetest.log("warning", "[goberning] governing.checkapikey not set or empty") end
|
|
|
|
print("[MOD] governing mod loaded" )
|
|
|