-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathaccess_check.lua
More file actions
40 lines (35 loc) · 834 Bytes
/
access_check.lua
File metadata and controls
40 lines (35 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local function log(...)
ngx.log(ngx.ERR, ...)
end
local function is_ok(ip, forwarded_for)
if forwarded_for then
string.gsub(forwarded_for, "[^,]+", function(w) ip=w end )
end
ip,_ = string.gsub(ip," ","")
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("192.168.93.130", 6379)
if not ok then
log("failed to connect: ", err)
return false
end
local res, err = red:auth("test")
if not res then
log("failed to authenticate: ", err)
return false
end
local key = "ip:"..ip
local ip_result, _ = red: incr(key)
if ip_result=1 then
local res, err = red:expire(key, 3600)
end
if ip_result<100 then
return true
end
return false
end
if not is_ok(ngx.var.remote_addr,ngx.var.http_x_forwarded_for) then
ngx.exit(ngx.HTTP_BAD_REQUEST)
return
end