-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathban.lua
68 lines (53 loc) · 1.92 KB
/
ban.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
local utils = require "plugins.crowdsec.utils"
local template = require "plugins.crowdsec.template"
local M = {_TYPE='module', _NAME='ban.funcs', _VERSION='1.0-0'}
M.template_str = ""
M.redirect_location = ""
M.ret_code = ngx.HTTP_FORBIDDEN
function M.new(template_path, redirect_location, ret_code, contact_us_url)
M.redirect_location = redirect_location
ret_code_ok = false
if ret_code ~= nil and ret_code ~= 0 and ret_code ~= "" then
for k, v in pairs(utils.HTTP_CODE) do
if k == ret_code then
M.ret_code = utils.HTTP_CODE[ret_code]
ret_code_ok = true
break
end
end
if ret_code_ok == false then
ngx.log(ngx.ERR, "RET_CODE '" .. ret_code .. "' is not supported, using default HTTP code " .. M.ret_code)
end
end
template_file_ok = false
if (template_path ~= nil and template_path ~= "" and utils.file_exist(template_path) == true) then
local template_data = {}
template_data["contact_us_url"] = contact_us_url
local ban_template = utils.read_file(template_path)
local view = template.compile(ban_template, template_data)
M.template_str = view
if M.template_str ~= nil then
template_file_ok = true
end
end
if template_file_ok == false and (M.redirect_location == nil or M.redirect_location == "") then
ngx.log(ngx.ERR, "BAN_TEMPLATE_PATH and REDIRECT_LOCATION variable are empty, will return HTTP " .. M.ret_code .. " for ban decisions")
end
return nil
end
function M.apply()
if M.redirect_location ~= "" then
ngx.redirect(M.redirect_location)
return
end
if M.template_str ~= "" then
ngx.header.content_type = "text/html"
ngx.status = M.ret_code
ngx.say(M.template_str)
ngx.exit(M.ret_code)
return
end
ngx.exit(M.ret_code)
return
end
return M