-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipapi.lua
66 lines (54 loc) · 1.49 KB
/
ipapi.lua
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local request = require "ssl.https".request
local cjson = require "cjson"
local api = "https://api.ipquery.io/"
local not_string = function(ip)
print ("Error: " .. tostring(ip) .. " is not an IP adress...")
end
local run_over_ip_list = function(list) -- table.concat wouldn't be enough for this case, run test.lua in the `concat` branch to see it
local str = ""
for _, ip in ipairs(list) do
if type(ip) == "string" then
str = str .. ip .. ","
else
not_string(ip)
end
end
return str
end
local query_ip = function(ip, format)
ip = ip or ""
if type(ip) == "table" then
ip = run_over_ip_list(ip)
elseif type(ip) ~= "string" then
not_string(ip)
return fals
end
if format then
local f_check = false
for _, f in ipairs({"yaml", "json", "xml", "text"}) do
if f == format then f_check = true end
end
if not f_check then
print(tostring(format) .. " is not a supported format")
return false
end
end
local url = format and api..ip.."?format="..format or api..ip
local ip_info, http_code, _, status = request(url)
if http_code == 200 then
ip_info = (format or ip == "") and ip_info or cjson.decode(ip_info)
else
print(ip_info)
print(status)
return false
end
return ip_info
end
local get_own_ip = function ()
local own_ip = query_ip() -- If you leave the parameter unspecified, it will return your own IP.
return query_ip(own_ip)
end
return {
query_ip = query_ip,
get_own_ip = get_own_ip
}