50 lines
2.0 KiB
Plaintext
50 lines
2.0 KiB
Plaintext
log_by_lua_block {
|
|
|
|
local logger = require "logger"
|
|
|
|
-- bad behavior
|
|
local use_bad_behavior = {% if USE_BAD_BEHAVIOR == "yes" %}true{% else %}false{% endif +%}
|
|
local behavior = require "behavior"
|
|
local bad_behavior_status_codes = {% raw %}{{% endraw %}{% if BAD_BEHAVIOR_STATUS_CODES != "" %}{% set elements = BAD_BEHAVIOR_STATUS_CODES.split(" ") %}{% for i in range(0, elements|length) %}"{{ elements[i] }}"{% if i < elements|length-1 %},{% endif %}{% endfor %}{% endif %}{% raw %}}{% endraw +%}
|
|
local bad_behavior_threshold = {{ BAD_BEHAVIOR_THRESHOLD }}
|
|
local bad_behavior_count_time = {{ BAD_BEHAVIOR_COUNT_TIME }}
|
|
local bad_behavior_ban_time = {{ BAD_BEHAVIOR_BAN_TIME }}
|
|
|
|
if use_bad_behavior then
|
|
local new_bad_behavior_ban = false
|
|
if not behavior.is_banned() then
|
|
new_bad_behavior_ban = behavior.count(bad_behavior_status_codes, bad_behavior_threshold, bad_behavior_count_time, bad_behavior_ban_time)
|
|
end
|
|
end
|
|
|
|
-- remote API
|
|
local use_remote_api = {% if USE_REMOTE_API == "yes" %}true{% else %}false{% endif +%}
|
|
local remoteapi = require "remoteapi"
|
|
local iputils = require "resty.iputils"
|
|
|
|
if use_remote_api and not iputils.ip_in_cidrs(ngx.var.remote_addr, ngx.shared.reserved_ips:get("data")) and ngx.shared.remote_api:get("id") ~= "empty" and ngx.shared.remote_api:get("ping") ~= "ko" then
|
|
if ngx.status == ngx.HTTP_FORBIDDEN then
|
|
local reason = "other"
|
|
if use_bad_behavior and new_bad_behavior_ban then
|
|
reason = "behavior"
|
|
end
|
|
local report_ip = function (premature, ip, reason)
|
|
if premature then
|
|
return
|
|
end
|
|
local remoteapi = require "remoteapi"
|
|
local logger = require "logger"
|
|
local res, data = remoteapi.ip(ip, reason)
|
|
-- TODO : find a way to log
|
|
end
|
|
local ok, err = ngx.timer.at(0, report_ip, ngx.var.remote_addr, reason)
|
|
if not ok then
|
|
logger.log(ngx.ERR, "REMOTE API", "Error while creating report timer " .. err)
|
|
else
|
|
logger.log(ngx.NOTICE, "REMOTE API", "Reporting " .. ngx.var.remote_addr .. "(reason: " .. reason .. ") to the remote API")
|
|
end
|
|
end
|
|
end
|
|
|
|
}
|