-
Notifications
You must be signed in to change notification settings - Fork 0
/
iphub_legacy.lua
58 lines (51 loc) · 1.57 KB
/
iphub_legacy.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
local ip_hub_api = {}
local email = block_vps.get_setting("block_vps_email") or "YOUR_EMAIL@example.com"
function ip_hub_api:generate_request(ip)
return { url = "http://legacy.iphub.info/api.php?showtype=4&ip=" .. ip .. "&email=" ..email }
end
function ip_hub_api:is_api_available()
return not self.temp_disable
end
function ip_hub_api:is_response_valid(response)
if response.succeeded == false or response.code ~= 200 then
if response.timeout then
core.log("error", "[block_vps] Getting IP info took too long.")
elseif response.code == 429 then
core.log("error", "[block_vps] Too many IP lookups, got rate limited")
self.temp_disable = true
-- wait 60 seconds if we are rate limited before trying again
core.after(60, function()
self.temp_disable = false
end)
else
core.log("error", "[block_vps] Failed to look up ip address, error code :" .. tostring(response.code))
end
return false
end
return true
end
function ip_hub_api:handle_response_data(ip, data_json)
local data = core.parse_json(data_json)
if not data then
return nil
end
local asn = 0
local isp_info = string.split(data.asn, " ")
if isp_info[1] then
asn = tonumber(isp_info[1]:sub(3))
end
isp_info[1] = ""
local info = {}
info.is_blocked = (data.proxy == 1)
info.asn = asn
info.isp = string.trim(table.concat(isp_info, " "))
info.hostname = data.hostname
if data.countryName ~= "" then
info.country = data.countryName
end
if data.countryCode ~= "" then
info.country_code = data.countryCode
end
return info
end
block_vps.register_datasource("iphub_legacy", ip_hub_api)