diff --git a/README.md b/README.md index 65ac477..35faf6b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

- + diff --git a/clamav/plugin.json b/clamav/plugin.json index 3bf4b5b..fa262fd 100644 --- a/clamav/plugin.json +++ b/clamav/plugin.json @@ -2,7 +2,7 @@ "id": "clamav", "name": "ClamAV", "description": "Automatic scan of uploaded files with ClamAV antivirus engine.", - "version": "0.1", + "version": "1.0", "stream": "no", "settings": { "USE_CLAMAV": { diff --git a/crowdsec/plugin.json b/crowdsec/plugin.json index 4c844ef..e0ef2d0 100644 --- a/crowdsec/plugin.json +++ b/crowdsec/plugin.json @@ -3,7 +3,7 @@ "stream": "partial", "name": "CrowdSec", "description": "CrowdSec bouncer for BunkerWeb.", - "version": "0.1", + "version": "1.0", "settings": { "USE_CROWDSEC": { "context": "multisite", diff --git a/discord/discord.lua b/discord/discord.lua index f300f3a..3774a77 100644 --- a/discord/discord.lua +++ b/discord/discord.lua @@ -1,82 +1,32 @@ -local _M = {} -_M.__index = _M +local class = require "middleclass" +local plugin = require "bunkerweb.plugin" +local utils = require "bunkerweb.utils" +local cjson = require "cjson" +local http = require "resty.http" -local utils = require "utils" -local logger = require "logger" -local cjson = require "cjson" -local http = require "resty.http" +local discord = class("discord", plugin) -function _M.new() - local self = setmetatable({}, _M) - local value, err = utils.get_variable("DISCORD_WEBHOOK_URL", false) - if not value then - logger.log(ngx.ERR, "DISCORD", "error while getting DISCORD_WEBHOOK_URL setting : " .. err) - return self, "error while getting DISCORD_WEBHOOK_URL setting : " .. err - end - self.webhook = value - local value, err = utils.get_variable("DISCORD_RETRY_IF_LIMITED", false) - if not value then - logger.log(ngx.ERR, "DISCORD", "error while getting DISCORD_RETRY_IF_LIMITED setting : " .. err) - return self, "error while getting DISCORD_RETRY_IF_LIMITED setting : " .. err - end - self.retry = value - return self, nil +function discord:initialize() + -- Call parent initialize + plugin.initialize(self, "discord") end -function _M:log(bypass_use_discord) +function discord:log(bypass_use_discord) + -- Check if discord is enabled if not bypass_use_discord then - -- Check if discord is activated - local check, err = utils.get_variable("USE_DISCORD") - if check == nil then - return false, "error while getting variable USE_DISCORD (" .. err .. ")" - end - if check ~= "yes" then - return true, "Discord plugin not enabled" + if self.variables["USE_DISCORD"] ~= "yes" then + return self:ret(true, "discord plugin not enabled") end end - -- Check if request is denied local reason = utils.get_reason() if reason == nil then - return true, "request not denied" - end - - -- Send request in a timer because cosocket is not allowed in log() - local function send(premature, obj, data) - local httpc, err = http.new() - if not httpc then - logger.log(ngx.ERR, "DISCORD", "can't instantiate http object : " .. err) - end - local res, err_http = httpc:request_uri(obj.webhook, { - method = "POST", - headers = { - ["Content-Type"] = "application/json", - }, - body = cjson.encode(data) - }) - httpc:close() - if not res then - logger.log(ngx.ERR, "DISCORD", "error while sending request : " .. err) - end - if obj.retry == "yes" and res.status == 429 and res.headers["Retry-After"] then - logger.log(ngx.WARN, "DISCORD", - "Discord API is rate-limiting us, retrying in " .. res.headers["Retry-After"] .. "s") - local hdr, err = ngx.timer.at(res.headers["Retry-After"], send, obj, data) - if not hdr then - logger.log(ngx.ERR, "DISCORD", "can't create report timer : " .. err) - return - end - return - end - if res.status < 200 or res.status > 299 then - logger.log(ngx.ERR, "DISCORD", "request returned status " .. tostring(res.status)) - return - end - logger.log(ngx.INFO, "DISCORD", "request sent to webhook") + return self:ret(true, "request not denied") end + -- Compute data local data = {} data.content = "```Denied request for IP " .. - ngx.var.remote_addr .. " (reason = " .. reason .. ").\n\nRequest data :\n\n" .. ngx.var.request .. "\n" + ngx.ctx.bw.remote_addr .. " (reason = " .. reason .. ").\n\nRequest data :\n\n" .. ngx.var.request .. "\n" local headers, err = ngx.req.get_headers() if not headers then data.content = data.content .. "error while getting headers : " .. err @@ -86,33 +36,65 @@ function _M:log(bypass_use_discord) end end data.content = data.content .. "```" - local hdr, err = ngx.timer.at(0, send, self, data) + -- Send request + local hdr, err = ngx.timer.at(0, self.send, self, data) if not hdr then - return false, "can't create report timer : " .. err + return self:ret(true, "can't create report timer : " .. err) + end +end + +function discord:send(premature, data) + local httpc, err = http.new() + if not httpc then + self.logger:log(ngx.ERR, "can't instantiate http object : " .. err) + end + local res, err_http = httpc:request_uri(self.variables["DISCORD_WEBHOOK_URL"], { + method = "POST", + headers = { + ["Content-Type"] = "application/json", + }, + body = cjson.encode(data) + }) + httpc:close() + if not res then + self.logger:log(ngx.ERR, "error while sending request : " .. err_http) + end + if self.variables["DISCORD_RETRY_IF_LIMITED"] == "yes" and res.status == 429 and res.headers["Retry-After"] then + self.logger:log(ngx.WARN, + "Discord API is rate-limiting us, retrying in " .. res.headers["Retry-After"] .. "s") + local hdr, err = ngx.timer.at(res.headers["Retry-After"], self.send, self, data) + if not hdr then + self.logger:log(ngx.ERR, "can't create report timer : " .. err) + return + end + return + end + if res.status < 200 or res.status > 299 then + self.logger:log(ngx.ERR, "request returned status " .. tostring(res.status)) + return end - -- Done - return true, "created report timer" + self.logger:log(ngx.INFO, "request sent to webhook") end -function _M:log_default() +function discord:log_default() -- Check if discord is activated local check, err = utils.has_variable("USE_DISCORD", "yes") if check == nil then - return false, "error while checking variable USE_DISCORD (" .. err .. ")" + return self:ret(false, "error while checking variable USE_DISCORD (" .. err .. ")") end if not check then - return true, "Discord plugin not enabled" + return self:ret(true, "Discord plugin not enabled") end -- Check if default server is disabled local check, err = utils.get_variable("DISABLE_DEFAULT_SERVER", false) if check == nil then - return false, "error while getting variable DISABLE_DEFAULT_SERVER (" .. err .. ")" + return self:ret(false, "error while getting variable DISABLE_DEFAULT_SERVER (" .. err .. ")") end if check ~= "yes" then - return true, "default server not disabled" + return self:ret(true, "default server not disabled") end -- Call log method return self:log(true) end -return _M +return discord \ No newline at end of file diff --git a/discord/plugin.json b/discord/plugin.json index 09fabf0..c9a3ffd 100644 --- a/discord/plugin.json +++ b/discord/plugin.json @@ -2,7 +2,7 @@ "id": "discord", "name": "Discord", "description": "Send alerts to a Discord channel (using webhooks).", - "version": "0.1", + "version": "1.0", "stream": "yes", "settings": { "USE_DISCORD": { diff --git a/slack/plugin.json b/slack/plugin.json index 592b991..5775200 100644 --- a/slack/plugin.json +++ b/slack/plugin.json @@ -2,7 +2,7 @@ "id": "slack", "name": "Slack", "description": "Send alerts to a Slack channel (using webhooks).", - "version": "0.1", + "version": "1.0", "stream": "partial", "settings": { "USE_SLACK": { diff --git a/slack/slack.lua b/slack/slack.lua index 0e0a41b..313dd1d 100644 --- a/slack/slack.lua +++ b/slack/slack.lua @@ -1,117 +1,100 @@ -local _M = {} -_M.__index = _M +local class = require "middleclass" +local plugin = require "bunkerweb.plugin" +local utils = require "bunkerweb.utils" +local cjson = require "cjson" +local http = require "resty.http" -local utils = require "utils" -local logger = require "logger" -local cjson = require "cjson" -local http = require "resty.http" +local slack = class("slack", plugin) -function _M.new() - local self = setmetatable({}, _M) - local value, err = utils.get_variable("SLACK_WEBHOOK_URL", false) - if not value then - logger.log(ngx.ERR, "SLACK", "error while getting SLACK_WEBHOOK_URL setting : " .. err) - return self, "error while getting SLACK_WEBHOOK_URL setting : " .. err - end - self.webhook = value - local value, err = utils.get_variable("SLACK_RETRY_IF_LIMITED", false) - if not value then - logger.log(ngx.ERR, "SLACK", "error while getting SLACK_RETRY_IF_LIMITED setting : " .. err) - return self, "error while getting SLACK_RETRY_IF_LIMITED setting : " .. err - end - self.retry = value - return self, nil +function slack:initialize() + -- Call parent initialize + plugin.initialize(self, "slack") end -function _M:log(bypass_use_slack) +function slack:log(bypass_use_slack) + -- Check if slack is enabled if not bypass_use_slack then - -- Check if Slack is activated - local check, err = utils.get_variable("USE_SLACK") - if check == nil then - return false, "error while getting variable USE_SLACK (" .. err .. ")" - end - if check ~= "yes" then - return true, "Slack plugin not enabled" + if self.variables["USE_SLACK"] ~= "yes" then + return self:ret(true, "slack plugin not enabled") end end - -- Check if request is denied local reason = utils.get_reason() if reason == nil then - return true, "request not denied" - end - - -- Send request in a timer because cosocket is not allowed in log() - local function send(premature, obj, data) - local httpc, err = http.new() - if not httpc then - logger.log(ngx.ERR, "SLACK", "can't instantiate http object : " .. err) - end - local res, err_http = httpc:request_uri(obj.webhook, { - method = "POST", - headers = { - ["Content-Type"] = "application/json", - }, - body = cjson.encode(data) - }) - httpc:close() - if not res then - logger.log(ngx.ERR, "SLACK", "error while sending request : " .. err) - end - if obj.retry == "yes" and res.status == 429 and res.headers["Retry-After"] then - logger.log(ngx.WARN, "SLACK", "Slack API is rate-limiting us, retrying in " .. res.headers["Retry-After"] .. "s") - local hdr, err = ngx.timer.at(res.headers["Retry-After"], send, obj, data) - if not hdr then - logger.log(ngx.ERR, "SLACK", "can't create report timer : " .. err) - return - end - return - end - if res.status < 200 or res.status > 299 then - logger.log(ngx.ERR, "SLACK", "request returned status " .. tostring(res.status)) - return - end - logger.log(ngx.INFO, "SLACK", "request sent to webhook") + return self:ret(true, "request not denied") end + -- Compute data local data = {} - data.text = "```Denied request for IP " .. - ngx.var.remote_addr .. " (reason = " .. reason .. ").\n\nRequest data :\n\n" .. ngx.var.request .. "\n" + data.content = "```Denied request for IP " .. + ngx.ctx.bw.remote_addr .. " (reason = " .. reason .. ").\n\nRequest data :\n\n" .. ngx.var.request .. "\n" local headers, err = ngx.req.get_headers() if not headers then - data.text = data.text .. "error while getting headers : " .. err + data.content = data.content .. "error while getting headers : " .. err else for header, value in pairs(headers) do - data.text = data.text .. header .. ": " .. value .. "\n" + data.content = data.content .. header .. ": " .. value .. "\n" end end - data.text = data.text .. "```" - local hdr, err = ngx.timer.at(0, send, self, data) + data.content = data.content .. "```" + -- Send request + local hdr, err = ngx.timer.at(0, self.send, self, data) if not hdr then - return false, "can't create report timer : " .. err + return self:ret(true, "can't create report timer : " .. err) + end +end + +function slack:send(premature, data) + local httpc, err = http.new() + if not httpc then + self.logger:log(ngx.ERR, "can't instantiate http object : " .. err) + end + local res, err_http = httpc:request_uri(self.variables["SLACK_WEBHOOK_URL"], { + method = "POST", + headers = { + ["Content-Type"] = "application/json", + }, + body = cjson.encode(data) + }) + httpc:close() + if not res then + self.logger:log(ngx.ERR, "error while sending request : " .. err_http) + end + if self.variables["SLACK_RETRY_IF_LIMITED"] == "yes" and res.status == 429 and res.headers["Retry-After"] then + self.logger:log(ngx.WARN, + "slack API is rate-limiting us, retrying in " .. res.headers["Retry-After"] .. "s") + local hdr, err = ngx.timer.at(res.headers["Retry-After"], self.send, self, data) + if not hdr then + self.logger:log(ngx.ERR, "can't create report timer : " .. err) + return + end + return + end + if res.status < 200 or res.status > 299 then + self.logger:log(ngx.ERR, "request returned status " .. tostring(res.status)) + return end - -- Done - return true, "created report timer" + self.logger:log(ngx.INFO, "request sent to webhook") end -function _M:log_default() - -- Check if SLACK is activated +function slack:log_default() + -- Check if slack is activated local check, err = utils.has_variable("USE_SLACK", "yes") if check == nil then - return false, "error while checking variable USE_SLACK (" .. err .. ")" + return self:ret(false, "error while checking variable USE_slack (" .. err .. ")") end if not check then - return true, "Slack plugin not enabled" + return self:ret(true, "slack plugin not enabled") end -- Check if default server is disabled local check, err = utils.get_variable("DISABLE_DEFAULT_SERVER", false) if check == nil then - return false, "error while getting variable DISABLE_DEFAULT_SERVER (" .. err .. ")" + return self:ret(false, "error while getting variable DISABLE_DEFAULT_SERVER (" .. err .. ")") end if check ~= "yes" then - return true, "default server not disabled" + return self:ret(true, "default server not disabled") end -- Call log method return self:log(true) end -return _M +return slack \ No newline at end of file diff --git a/virustotal/plugin.json b/virustotal/plugin.json index aa678f9..9c16c54 100644 --- a/virustotal/plugin.json +++ b/virustotal/plugin.json @@ -2,7 +2,7 @@ "id": "virustotal", "name": "VirusTotal", "description": "Automatic scan of uploaded files and ips optionaly with the VirusTotal API.", - "version": "0.1", + "version": "1.0", "stream": "partial", "settings": { "USE_VIRUSTOTAL": {