-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_ip.lua
81 lines (72 loc) · 1.86 KB
/
process_ip.lua
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local _M = {}
local mt = { __index = _M }
local function block(it)
local blocked = ngx.shared.blocked
for _, v in ipairs(it) do
ngx.log(ngx.ERR, "block ip in blocked: ", v)
blocked:set(v, 1)
end
end
local function free(it)
local blocked = ngx.shared.blocked
for _, v in ipairs(it) do
ngx.log(ngx.ERR, "delete ip in blocked: ", v)
blocked:delete(v)
end
end
local functor = {block = block, free = free}
function _M.process()
local blocked = ngx.shared.blocked
local ips = ngx.var.arg_ips
local action = ngx.var.arg_action
if not ips or not action then
ngx.log(ngx.ERR, "request ips or action is nil")
return
end
for it,err in ngx.re.gmatch(ips, "([^',']+)") do
if not it then
ngx.log(ngx.ERR, "gmatch {} ips error: ", action, err)
return
end
functor[action](it)
end
ngx.say("ok")
end
function _M.free_ip()
local blocked = ngx.shared.blocked
local ips = ngx.var.arg_ips
if not ips then
ngx.log(ngx.ERR, "request free ips is nil")
return
end
for it,err in ngx.re.gmatch(ips, "([^',']+)") do
if not it then
ngx.log(ngx.ERR, "gmatch block ips error: ", err)
return
end
for _, v in ipairs(it) do
blocked:delete(v)
end
end
ngx.say("ok")
end
function _M.block_ip()
local blocked = ngx.shared.blocked
local ips = ngx.var.arg_ips
if not ips then
ngx.log(ngx.ERR, "request block ips is nil")
return
end
for it,err in ngx.re.gmatch(ips, "([^',']+)") do
if not it then
ngx.log(ngx.ERR, "gmatch block ips error: ", err)
return
end
for _, v in ipairs(it) do
blocked:set(v, "1")
ngx.log(ngx.ERR, "add block ip: ", v)
end
end
ngx.say("ok")
end
return _M