aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/ip.lua
blob: 79ac6e284193c08b376242adafdadd517e02abe6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local milla = require("milla")
local os = require("os")
local json = require("json")

-- this function should be global
-- one string arg that holds all args
-- should only return one string value
function milla_get_ip(arg)
    -- setting the proxy value before loading the http module
    -- this way, only this script will be using this proxy
    os.setenv("http_proxy", "http://172.17.0.1:8120")

    local http = require("http")

    local url = "http://ip-api.com/json/" .. arg

    local response, err = http.request("GET", url)
    if err ~= nil then print(err) end

    local json_response, err = json.decode(response.body)
    if err ~= nil then print(err) end
    for k, v in pairs(json_response) do print(k, v) end

    local result = ""
    for key, value in pairs(json_response) do
        result = result .. key .. ": " .. value .. " -- "
    end

    return result
end

-- script_path, command_name, function_name
milla.register_cmd("/plugins/ip.lua", "ip", "milla_get_ip")