Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ logdir = "/usr/local/nginx/logs/hack/"
UrlDeny="on"
Redirect="on"
CookieMatch="on"
postMatch="on"
whiteModule="on"
ipWhitelist={"127.0.0.1"}
ipBlocklist={"1.0.0.1"}
postMatch="on"
whiteModule="on"
ipWhitelist={"127.0.0.1","192.168.1.0-192.168.255.255"}
ipBlocklist={"1.0.0.1","2.0.0.0-2.0.0.255"}
CCDeny="off"
CCrate="100/60"
html=[[Please go away~~ ]]
61 changes: 47 additions & 14 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local ngxmatch=ngx.re.match
local unescape=ngx.unescape_uri
local get_headers = ngx.req.get_headers
local optionIsOn = function (options) return options == "on" and true or false end
logpath = logdir
logpath = logdir
rulepath = RulePath
UrlDeny = optionIsOn(UrlDeny)
PostCheck = optionIsOn(postMatch)
Expand All @@ -17,13 +17,25 @@ Redirect=optionIsOn(Redirect)
function getClientIp()
IP = ngx.req.get_headers()["X-Real-IP"]
if IP == nil then
IP = ngx.var.remote_addr
IP = ngx.var.remote_addr
end
if IP == nil then
IP = "unknown"
end
return IP
end
function ipToDecimal(ckip)
local n = 4
local decimalNum = 0
local pos = 0
for s, e in function() return string.find(ckip, '.', pos, true) end do
n = n - 1
decimalNum = decimalNum + string.sub(ckip, pos, s-1) * (256 ^ n)
pos = e + 1
if n == 1 then decimalNum = decimalNum + string.sub(ckip, pos, string.len(ckip)) end
end
return decimalNum
end
function write(logfile,msg)
local fd = io.open(logfile,"ab")
if fd == nil then return end
Expand Down Expand Up @@ -81,7 +93,7 @@ function whiteurl()
if wturlrules ~=nil then
for _,rule in pairs(wturlrules) do
if ngxmatch(ngx.var.request_uri,rule,"imjo") then
return true
return true
end
end
end
Expand Down Expand Up @@ -203,23 +215,44 @@ end

function whiteip()
if next(ipWhitelist) ~= nil then
local cIP = getClientIp()
local numIP = 0
if cIP ~= "unknown" then numIP = tonumber(ipToDecimal(cIP)) end
for _,ip in pairs(ipWhitelist) do
if getClientIp()==ip then
local s, e = string.find(ip, '-', 0, true)
if s == nil and cIP == ip then
return true
elseif s ~= nil then
sIP = tonumber(ipToDecimal(string.sub(ip, 0, s - 1)))
eIP = tonumber(ipToDecimal(string.sub(ip, e + 1, string.len(ip))))
if numIP >= sIP and numIP <= eIP then
return true
end
end
end
end
return false
return false
end

function blockip()
if next(ipBlocklist) ~= nil then
for _,ip in pairs(ipBlocklist) do
if getClientIp()==ip then
ngx.exit(403)
return true
end
end
end
return false
if next(ipBlocklist) ~= nil then
local cIP = getClientIp()
local numIP = 0
if cIP ~= "unknown" then numIP = tonumber(ipToDecimal(cIP)) end
for _,ip in pairs(ipBlocklist) do
local s, e = string.find(ip, '-', 0, true)
if s == nil and cIP == ip then
ngx.exit(403)
return true
elseif s ~= nil then
sIP = tonumber(ipToDecimal(string.sub(ip, 0, s - 1)))
eIP = tonumber(ipToDecimal(string.sub(ip, e + 1, string.len(ip))))
if numIP >= sIP and numIP <= eIP then
ngx.exit(403)
return true
end
end
end
end
return false
end