working geoip now, tune up sintax, organize code
* privilegie registration and callback on join player detection of geoip mod in same conditional * check nil player, vali player and engine feature at the joining process of the player
This commit is contained in:
parent
bcbacc997c
commit
d731f8dbd9
45
geoip.lua
45
geoip.lua
@ -1,12 +1,5 @@
|
||||
if not governing.modgeoip then
|
||||
|
||||
minetest.register_privilege("geoip", {
|
||||
description = "can do geoip lookups on players for governing",
|
||||
give_to_singleplayer = false
|
||||
})
|
||||
end
|
||||
|
||||
local function format_result(result)
|
||||
function format_result(result)
|
||||
if result and result.status == "success" and result.data and result.data.geo then
|
||||
local txt = "Geoip result: "
|
||||
if result.data.geo.country_name then
|
||||
@ -35,32 +28,26 @@ end
|
||||
|
||||
if not governing.modgeoip then
|
||||
|
||||
-- function(name, result)
|
||||
governing.joinplayer_callback = function() end
|
||||
minetest.register_privilege("geoip", {
|
||||
description = "can do geoip lookups on players for governing",
|
||||
give_to_singleplayer = false
|
||||
})
|
||||
|
||||
-- query ip on join, record in logs and execute callback
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
if not minetest.get_player_ip then
|
||||
return
|
||||
end
|
||||
governing.joinplayer_callback = function() end -- function(name, result)
|
||||
|
||||
minetest.register_on_joinplayer(function(player) -- query ip on join, record in logs and execute callback
|
||||
if not player then return end
|
||||
if not player:is_player() then return end
|
||||
if not minetest.get_player_ip then return end
|
||||
local name = player:get_player_name()
|
||||
local ip = minetest.get_player_ip(name)
|
||||
if not ip then
|
||||
return
|
||||
end
|
||||
|
||||
governing.lookup(ip, function(data)
|
||||
-- log to debug.txt
|
||||
if not ip then return end
|
||||
governing.lookup(ip, function(data) -- log, but TODO: record in storage log file
|
||||
local txt = format_result(data)
|
||||
if txt then
|
||||
minetest.log("action", "[goberning/geoip] result for player " .. name .. ": " .. txt)
|
||||
else
|
||||
minetest.log("error", "[goberning/geoip] result for player " .. name .. ": seems fails for ip "..ip)
|
||||
if txt then minetest.log("info", "[goberning/geoip] result for player " .. name .. ": " .. txt)
|
||||
else minetest.log("error", "[goberning/geoip] result for player " .. name .. ": seems fails for ip "..ip)
|
||||
end
|
||||
|
||||
-- execute callback
|
||||
governing.joinplayer_callback(name, data)
|
||||
governing.joinplayer_callback(name, data) -- execute callback
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
11
init.lua
11
init.lua
@ -72,19 +72,13 @@ function governing.is_creative(name)
|
||||
end
|
||||
|
||||
function governing.lookup(ip, callback)
|
||||
|
||||
if not httpapi then
|
||||
minetest.log("error", "[governing/geoip] mod not in the trusted http mods!")
|
||||
return
|
||||
end
|
||||
|
||||
-- https://forum.minetest.net/viewtopic.php?t=28636
|
||||
-- Only works at init time and must be called from the mod's main scope (not from a function).
|
||||
httpapi.fetch({
|
||||
httpapi.fetch( {
|
||||
url = "https://tools.keycdn.com/geo.json?host=" .. ip,
|
||||
extra_headers = {
|
||||
"User-Agent: keycdn-tools:https://minetest.org"
|
||||
},
|
||||
extra_headers = { "User-Agent: keycdn-tools:https://minetest.org" },
|
||||
timeout = 5,
|
||||
}, function(res)
|
||||
if res.code == 200 then
|
||||
@ -96,7 +90,6 @@ function governing.lookup(ip, callback)
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
dofile(governing.modpath.."/geoip.lua")
|
||||
dofile(governing.modpath.."/commands.lua")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user