To detect if an IP address is a VPN using IP2Location.io in Lua, you can use the IP2Location.io API to retrieve geolocation and related data.
luarocks install json-lua
luarocks install luasocket
local http = require("socket.http")
local ltn12 = require("ltn12")
local json = require("JSON")
local t = {}
local ip = "8.8.8.8"
local key = "YOUR_API_KEY"
local full_url = "https://api.ip2location.io/?format=json&key=" .. key .. "&ip=" .. ip
local status, code, headers = http.request({
method = "GET",
url = full_url,
sink = ltn12.sink.table(t),
})
local jsonstr = table.concat(t)
if code == 200 then
local result = json:decode(jsonstr)
if result.proxy ~= nil then
if result.proxy.is_vpn then
print("The IP " .. ip .. " is a VPN.")
else
print("The IP " .. ip .. " is NOT a VPN.")
end
else
error("ERROR: The is_vpn field requires a paid subscription to the Security plan.")
end
elseif code == 400 or code == 401 then
if jsonstr:find("error_message") then
local result = json:decode(jsonstr)
error("ERROR: " .. result.error.error_message)
else
error(jsonstr)
end
else
error("ERROR: Unable to call API.")
end
lua test.lua
This script will check if the specified IP address is a VPN. 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