To determine the proxy type of an IP address using IP2Location.io in OpenResty, you can use the IP2Location.io API to retrieve geolocation and related data. For proxy types supported, please see API documentation for more info.
sudo opm get ledgetech/lua-resty-http
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
resolver 8.8.8.8 ipv6=off;
server {
listen 8080 reuseport;
location / {
default_type text/html;
content_by_lua_block {
local cjson = require("cjson")
local key = "YOUR_API_KEY"
local ip = "8.8.8.8"
local httpc = require("resty.http").new()
local jsonstr = ""
local code = 0
local myhost = "api.ip2location.io"
params = {
format = "json",
key = key,
ip = ip,
}
local ok, err, ssl_session = httpc:connect({
scheme = "http",
host = myhost,
port = 80,
})
if not ok then
ngx.log(ngx.ERR, "connection failed: ", err)
return
end
local res, err = httpc:request({
path = "/",
query = params,
headers = {
["Host"] = myhost,
},
})
if not res then
ngx.log(ngx.ERR, "request failed: ", err)
return
end
local reader = res.body_reader
local buffer_size = 8192
code = res.status
repeat
local buffer, err = reader(buffer_size)
if err then
ngx.log(ngx.ERR, err)
return
end
if buffer then
jsonstr = jsonstr .. buffer
end
until not buffer
local trailer = res:read_trailers()
if trailer then
jsonstr = jsonstr .. trailer
end
local ok, err = httpc:set_keepalive()
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
if code == 200 then
local result = cjson.decode(jsonstr)
if result["proxy"] ~= nil then
ngx.say("The proxy type for IP " .. ip .. " is " .. result.proxy.proxy_type .. ".")
else
ngx.say("ERROR: The proxy_type field requires a paid subscription to the Security plan.")
end
elseif code == 400 or code == 401 then
local result = cjson.decode(jsonstr)
if result["error"] ~= nil then
ngx.say("ERROR: " .. result.error.error_message)
else
ngx.say(jsonstr)
end
else
ngx.say("ERROR: Unable to connect to API.")
end
}
}
}
}
export PATH=/usr/local/openresty/nginx/sbin:$PATH
nginx -p `pwd`/ -c test.conf
curl http://localhost:8080/
This script will output the proxy type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free