-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip2proxywebservice.lua
More file actions
92 lines (77 loc) · 2.16 KB
/
ip2proxywebservice.lua
File metadata and controls
92 lines (77 loc) · 2.16 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
local http = require("socket.http")
local ltn12 = require("ltn12")
local json = require("JSON")
local urlencode = require("urlencode")
-- for debugging purposes
local function printme(stuff)
local inspect = require('inspect')
print(inspect(stuff))
end
ip2proxywebservice = {
apikey = "",
apipackage = "",
usessl = false
}
ip2proxywebservice.__index = ip2proxywebservice
ip2proxyresult = {
response = '',
countryCode = '',
countryName = '',
regionName = '',
cityName = '',
isp = '',
proxyType = '',
isProxy = '',
domain = '',
usageType = '',
asn = '',
as = '',
lastSeen = '',
threat = '',
provider = ''
}
ip2proxyresult.__index = ip2proxyresult
-- initialize the component with the web service configuration
function ip2proxywebservice:open(apikey, apipackage, usessl)
local x = {}
setmetatable(x, ip2proxywebservice) -- make ip2proxywebservice handle lookup
x.apikey = apikey
x.apipackage = apipackage
x.usessl = usessl
return x
end
-- main query
function ip2proxywebservice:lookup(ipaddress)
local protocol = "http"
if self.usessl then
protocol = "https"
end
local t = {}
local status, code, headers = http.request {
method = "GET",
url = protocol .. "://api.ip2proxy.com/?key=" .. urlencode.encode_url(self.apikey) .. "&package=" .. urlencode.encode_url(self.apipackage) .. "&ip=" .. urlencode.encode_url(ipaddress),
sink = ltn12.sink.table(t)
}
local jsonstr = table.concat(t)
local result = json:decode(jsonstr)
setmetatable(result, ip2proxyresult)
return result
end
-- check web service credit balance
function ip2proxywebservice:get_credit()
local protocol = "http"
if self.usessl then
protocol = "https"
end
local t = {}
local status, code, headers = http.request {
method = "GET",
url = protocol .. "://api.ip2proxy.com/?key=" .. urlencode.encode_url(self.apikey) .. "&check=true",
sink = ltn12.sink.table(t)
}
local jsonstr = table.concat(t)
local result = json:decode(jsonstr)
setmetatable(result, ip2proxyresult)
return result
end
return ip2proxywebservice