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
53
geoip.lua
53
geoip.lua
@ -1,12 +1,5 @@
|
|||||||
if not governing.modgeoip then
|
|
||||||
|
|
||||||
minetest.register_privilege("geoip", {
|
function format_result(result)
|
||||||
description = "can do geoip lookups on players for governing",
|
|
||||||
give_to_singleplayer = false
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
local function format_result(result)
|
|
||||||
if result and result.status == "success" and result.data and result.data.geo then
|
if result and result.status == "success" and result.data and result.data.geo then
|
||||||
local txt = "Geoip result: "
|
local txt = "Geoip result: "
|
||||||
if result.data.geo.country_name then
|
if result.data.geo.country_name then
|
||||||
@ -35,32 +28,26 @@ end
|
|||||||
|
|
||||||
if not governing.modgeoip then
|
if not governing.modgeoip then
|
||||||
|
|
||||||
-- function(name, result)
|
minetest.register_privilege("geoip", {
|
||||||
governing.joinplayer_callback = function() end
|
description = "can do geoip lookups on players for governing",
|
||||||
|
give_to_singleplayer = false
|
||||||
|
})
|
||||||
|
|
||||||
-- query ip on join, record in logs and execute callback
|
governing.joinplayer_callback = function() end -- function(name, result)
|
||||||
minetest.register_on_joinplayer(function(player)
|
|
||||||
if not minetest.get_player_ip then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local name = player:get_player_name()
|
minetest.register_on_joinplayer(function(player) -- query ip on join, record in logs and execute callback
|
||||||
local ip = minetest.get_player_ip(name)
|
if not player then return end
|
||||||
if not ip then
|
if not player:is_player() then return end
|
||||||
return
|
if not minetest.get_player_ip then return end
|
||||||
end
|
local name = player:get_player_name()
|
||||||
|
local ip = minetest.get_player_ip(name)
|
||||||
governing.lookup(ip, function(data)
|
if not ip then return end
|
||||||
-- log to debug.txt
|
governing.lookup(ip, function(data) -- log, but TODO: record in storage log file
|
||||||
local txt = format_result(data)
|
local txt = format_result(data)
|
||||||
if txt then
|
if txt then minetest.log("info", "[goberning/geoip] result for player " .. name .. ": " .. txt)
|
||||||
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)
|
||||||
else
|
end
|
||||||
minetest.log("error", "[goberning/geoip] result for player " .. name .. ": seems fails for ip "..ip)
|
governing.joinplayer_callback(name, data) -- execute callback
|
||||||
end
|
end)
|
||||||
|
|
||||||
-- execute callback
|
|
||||||
governing.joinplayer_callback(name, data)
|
|
||||||
end)
|
end)
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
31
init.lua
31
init.lua
@ -72,31 +72,24 @@ function governing.is_creative(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function governing.lookup(ip, callback)
|
function governing.lookup(ip, callback)
|
||||||
|
|
||||||
if not httpapi then
|
if not httpapi then
|
||||||
minetest.log("error", "[governing/geoip] mod not in the trusted http mods!")
|
minetest.log("error", "[governing/geoip] mod not in the trusted http mods!")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
httpapi.fetch( {
|
||||||
-- https://forum.minetest.net/viewtopic.php?t=28636
|
url = "https://tools.keycdn.com/geo.json?host=" .. ip,
|
||||||
-- Only works at init time and must be called from the mod's main scope (not from a function).
|
extra_headers = { "User-Agent: keycdn-tools:https://minetest.org" },
|
||||||
httpapi.fetch({
|
timeout = 5,
|
||||||
url = "https://tools.keycdn.com/geo.json?host=" .. ip,
|
}, function(res)
|
||||||
extra_headers = {
|
if res.code == 200 then
|
||||||
"User-Agent: keycdn-tools:https://minetest.org"
|
local data = minetest.parse_json(res.data)
|
||||||
},
|
callback(data)
|
||||||
timeout = 5,
|
else
|
||||||
}, function(res)
|
minetest.log("warning", "[governing/geoip] http request returned status: " .. res.code)
|
||||||
if res.code == 200 then
|
end
|
||||||
local data = minetest.parse_json(res.data)
|
end)
|
||||||
callback(data)
|
|
||||||
else
|
|
||||||
minetest.log("warning", "[governing/geoip] http request returned status: " .. res.code)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
dofile(governing.modpath.."/geoip.lua")
|
dofile(governing.modpath.."/geoip.lua")
|
||||||
dofile(governing.modpath.."/commands.lua")
|
dofile(governing.modpath.."/commands.lua")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user