Skip to content

Commit

Permalink
bw 1.5.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0ppy-d1sk committed May 23, 2023
1 parent e9b6428 commit ce0c385
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 162 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>

<p align="center">
<img src="https://img.shields.io/badge/bunkerweb-1.4.2-blue" />
<img src="https://img.shields.io/badge/bunkerweb-1.5.0-blue" />
<img src="https://img.shields.io/github/last-commit/bunkerity/bunkerweb-plugins" />
<img src="https://img.shields.io/github/workflow/status/bunkerity/bunkerweb-plugins/Tests?label=tests" />
<img src="https://img.shields.io/github/issues/bunkerity/bunkerweb-plugins">
Expand Down
2 changes: 1 addition & 1 deletion clamav/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion crowdsec/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"stream": "partial",
"name": "CrowdSec",
"description": "CrowdSec bouncer for BunkerWeb.",
"version": "0.1",
"version": "1.0",
"settings": {
"USE_CROWDSEC": {
"context": "multisite",
Expand Down
134 changes: 58 additions & 76 deletions discord/discord.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion discord/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion slack/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Loading

0 comments on commit ce0c385

Please sign in to comment.