openresty 获取 client ip
来源:https://stackoverflow.com/questions/42140346/get-client-ip-address-with-nginx-lua
local ip = ngx.var.remote_addr;
判断IP是否为局域网网段
来源:https://blog.csdn.net/modstartcms/article/details/128914573
function is_lan_ip(ip)
-- Split the IP address into its octets
local octets = {}
for octet in ip:gmatch("%d+") do
table.insert(octets, tonumber(octet))
end
-- Check if the IP is in the range of private IP addresses
if octets[1] == 10 then
return true
elseif octets[1] == 172 and octets[2] >= 16 and octets[2] <= 31 then
return true
elseif octets[1] == 192 and octets[2] == 168 then
return true
else
return false
end
end
调用
print(is_lan_ip("192.168.1.1")) -- true
print(is_lan_ip("8.8.8.8")) -- false