remote API - basic send of bad IPs
This commit is contained in:
@@ -83,30 +83,23 @@ if use_remote_api then
|
||||
|
||||
-- Save version
|
||||
local f = io.open("/opt/bunkerized-nginx/VERSION", "r")
|
||||
ngx.shared.remote_api:set("version", f:read("*all"), 0)
|
||||
ngx.shared.remote_api:set("version", f:read("*all"):gsub("[\r\n]", ""), 0)
|
||||
f:close()
|
||||
|
||||
-- Save and ask a machine ID if needed
|
||||
local f = io.open("/etc/nginx/machine.id", "rw")
|
||||
-- Save machine ID
|
||||
local f = io.open("/etc/nginx/machine.id", "r")
|
||||
if f == nil then
|
||||
id = nil
|
||||
id = "empty"
|
||||
logger.log(ngx.ERR, "REMOTE API", "USE_REMOTE_API is set to yes but machine ID is not generated - communication with {{ REMOTE_API_SERVER }} won't work")
|
||||
else
|
||||
id = f:read("*all")
|
||||
id = f:read("*all"):gsub("[\r\n]", "")
|
||||
logger.log(ngx.ERR, "REMOTE API", "*NOT AN ERROR* Machine ID = " .. id)
|
||||
f:close()
|
||||
end
|
||||
f:close()
|
||||
|
||||
-- Test the machine ID
|
||||
if id ~= nil then
|
||||
local res, pong = remoteapi.ping()
|
||||
if not res or pong ~= "pong" then
|
||||
logger.log(ngx.ERR, "REMOTE API", "Ping failed, the remote server may be down or your machine ID is invalid")
|
||||
else
|
||||
logger.log(ngx.ERR, "REMOTE API", "*NOT AN ERROR* Ping successful")
|
||||
end
|
||||
end
|
||||
ngx.shared.remote_api:set("id", id, 0)
|
||||
|
||||
-- TODO : ping (blocking socket)
|
||||
-- TODO : load database
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
@@ -21,17 +21,29 @@ end
|
||||
local use_remote_api = {% if USE_REMOTE_API == "yes" %}true{% else %}false{% endif +%}
|
||||
local remoteapi = require "remoteapi"
|
||||
|
||||
if use_remote_api then
|
||||
if use_remote_api and ngx.shared.remote_api:get("id") ~= "empty" 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 res, data = remoteapi.ip(ngx.var.remote_addr, reason)
|
||||
if res then
|
||||
logger.log(ngx.NOTICE, "REMOTE API", "Successfully reported ip " .. ngx.var.remote_addr)
|
||||
else
|
||||
logger.log(ngx.ERR, "REMOTE API", "Error while reporting ip " .. ngx.var.remote_addr .. " : " .. data)
|
||||
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 ?
|
||||
-- if res then
|
||||
-- logger.log(ngx.ERR, "REMOTE API", "Successfully reported ip " .. ngx.var.remote_addr)
|
||||
-- else
|
||||
-- logger.log(ngx.ERR, "REMOTE API", "Error while reporting ip " .. ngx.var.remote_addr .. " : " .. data)
|
||||
-- end
|
||||
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)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user