move http request api work to main init.lua as api of minetest demands
* if not will not work due security implementations * https://forum.minetest.net/viewtopic.php?t=28636
This commit is contained in:
parent
8601309f80
commit
57765dfa30
25
geoip.lua
25
geoip.lua
@ -6,31 +6,6 @@ if not governing.modgeoip then
|
||||
})
|
||||
end
|
||||
|
||||
function governing.lookup(ip, callback)
|
||||
|
||||
local http = minetest.request_http_api()
|
||||
|
||||
if not http then
|
||||
minetest.log("error", "[governing/geoip] mod not in the trusted http mods!")
|
||||
return
|
||||
end
|
||||
|
||||
http.fetch({
|
||||
url = "https://tools.keycdn.com/geo.json?host=" .. ip,
|
||||
extra_headers = {
|
||||
"User-Agent: keycdn-tools:https://minetest.org"
|
||||
},
|
||||
timeout = 1,
|
||||
}, function(res)
|
||||
if res.code == 200 and callback 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
|
||||
|
||||
local function format_result(result)
|
||||
if result and result.status == "success" and result.data and result.data.geo then
|
||||
local txt = "Geoip result: "
|
||||
|
31
init.lua
31
init.lua
@ -22,6 +22,8 @@ local modkillme = minetest.get_modpath("killme")
|
||||
local modcommand = minetest.get_modpath("game_commands")
|
||||
local modcreative = minetest.get_modpath("creative")
|
||||
|
||||
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
|
||||
@ -50,11 +52,12 @@ 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 mail mod if available for sending messages internaly on private
|
||||
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
|
||||
@ -68,6 +71,32 @@ function governing.is_creative(name)
|
||||
end
|
||||
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({
|
||||
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
|
||||
|
||||
|
||||
dofile(governing.modpath.."/geoip.lua")
|
||||
dofile(governing.modpath.."/commands.lua")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user