添加网段支持
parent
b3278cd5eb
commit
790acc7a65
51
init.lua
51
init.lua
|
@ -236,10 +236,52 @@ function get_boundary()
|
||||||
return match(header, ";%s*boundary=([^\",;]+)")
|
return match(header, ";%s*boundary=([^\",;]+)")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function string.split(str, delimiter)
|
||||||
|
if str==nil or str=='' or delimiter==nil then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local result = {}
|
||||||
|
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
|
||||||
|
table.insert(result, match)
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function innet(ip, network)
|
||||||
|
local star = ''
|
||||||
|
for i in string.gmatch(network, '%*') do
|
||||||
|
star = star..i
|
||||||
|
end
|
||||||
|
|
||||||
|
local ip = string.split(ip, '%.')
|
||||||
|
local network = string.split(network, '%.')
|
||||||
|
if ip == nil or network == nil then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local ip_prefix = {}
|
||||||
|
local network_prefix = {}
|
||||||
|
for i=1, 4-string.len(star) do
|
||||||
|
ip_prefix[i] = ip[i]
|
||||||
|
network_prefix[i] = network[i]
|
||||||
|
end
|
||||||
|
|
||||||
|
ip_prefix = table.concat(ip_prefix, '.')
|
||||||
|
network_prefix = table.concat(network_prefix, '.')
|
||||||
|
|
||||||
|
if ip_prefix == network_prefix then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function whiteip()
|
function whiteip()
|
||||||
if next(ipWhitelist) ~= nil then
|
if next(ipWhitelist) ~= nil then
|
||||||
for _,ip in pairs(ipWhitelist) do
|
ip = getClientIp()
|
||||||
if getClientIp()==ip then
|
for _,wip in pairs(ipWhitelist) do
|
||||||
|
if ip = wip or innet(ip, wip) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -249,8 +291,9 @@ end
|
||||||
|
|
||||||
function blockip()
|
function blockip()
|
||||||
if next(ipBlocklist) ~= nil then
|
if next(ipBlocklist) ~= nil then
|
||||||
for _,ip in pairs(ipBlocklist) do
|
ip = getClientIp()
|
||||||
if getClientIp()==ip or ip=="0.0.0.0" then
|
for _,bip in pairs(ipBlocklist) do
|
||||||
|
if ip == bip or ip=="0.0.0.0" or innet(ip, bip) then
|
||||||
ngx.exit(403)
|
ngx.exit(403)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue