Skip to content

Commit

Permalink
Lint and format lua files + Fix luacheck warnings and errors + Update…
Browse files Browse the repository at this point in the history
… pre-commit-config hooks
  • Loading branch information
TheophileDiot committed Jan 5, 2024
1 parent 4cb896c commit c4bcaba
Show file tree
Hide file tree
Showing 20 changed files with 205 additions and 201 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
exclude: (^LICENSE.md$|^src/VERSION$|^src/(bw/misc/root-ca.pem$|deps/src/|common/core/modsecurity/files|ui/static/js/(editor/|utils/purify/|tsparticles\.bundle\.min\.js))|\.(svg|drawio|patch\d?|ascii|tf|tftpl)$)
exclude: (^LICENSE.md$|^src/VERSION$|^env/|^src/(bw/misc/root-ca.pem$|deps/src/|common/core/modsecurity/files|ui/static/js/(editor/|utils/purify/|tsparticles\.bundle\.min\.js))|\.(svg|drawio|patch\d?|ascii|tf|tftpl|key)$)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: c4a0b883114b00d8d76b479c820ce7950211c99b # frozen: v4.5.0
Expand All @@ -16,15 +16,15 @@ repos:
args: ["--allow-multiple-documents"]
- id: check-case-conflict

- repo: https://github.com/ambv/black
rev: 2a1c67e0b2f81df602ec1f6e7aeb030b9709dc7c # frozen: 23.11.0
- repo: https://github.com/psf/black
rev: ec91a2be3c44d88e1a3960a4937ad6ed3b63464e # frozen: 23.12.1
hooks:
- id: black
name: Black Python Formatter
language_version: python3.9

- repo: https://github.com/pre-commit/mirrors-prettier
rev: ffb6a759a979008c0e6dff86e39f4745a2d9eac4 # frozen: v3.1.0
rev: f12edd9c7be1c20cfa42420fd0e6df71e42b51ea # frozen: v4.0.0-alpha.8
hooks:
- id: prettier
name: Prettier Code Formatter
Expand All @@ -36,21 +36,21 @@ repos:
exclude: ^src/(bw/lua/middleclass.lua|common/core/antibot/captcha.lua)$

- repo: https://github.com/lunarmodules/luacheck
rev: ababb6d403d634eb74d2c541035e9ede966e710d # frozen: v1.1.1
rev: 418f48976c73be697fe64b0eba9ea9821ac9bca8 # frozen: v1.1.2
hooks:
- id: luacheck
exclude: ^src/(bw/lua/middleclass.lua|common/core/antibot/captcha.lua)$
args: ["--std", "min", "--codes", "--ranges", "--no-cache"]

- repo: https://github.com/pycqa/flake8
rev: 10f4af6dbcf93456ba7df762278ae61ba3120dc6 # frozen: 6.1.0
rev: 7d37d9032d0d161634be4554273c30efd4dea0b3 # frozen: 7.0.0
hooks:
- id: flake8
name: Flake8 Python Linter
args: ["--max-line-length=250", "--ignore=E266,E402,E722,W503"]

- repo: https://github.com/dosisod/refurb
rev: a25b5d6087bba1509f96654c583efcd7796452cd # frozen: v1.24.0
rev: a7c461fcfaa2ca3248d489cdf7fed8e2d4fd8520 # frozen: v1.26.0
hooks:
- id: refurb
name: Refurb Python Refactoring Tool
Expand Down
9 changes: 5 additions & 4 deletions src/bw/lua/bunkerweb/api.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local ngx = ngx
local ngx_req = ngx.req
local cdatastore = require "bunkerweb.datastore"
local cjson = require "cjson"
local class = require "middleclass"
local cdatastore = require "bunkerweb.datastore"
local clogger = require "bunkerweb.logger"
local helpers = require "bunkerweb.helpers"
local process = require "ngx.process"
local rsignal = require "resty.signal"
local upload = require "resty.upload"
local utils = require "bunkerweb.utils"
local helpers = require "bunkerweb.helpers"

local api = class("api")

Expand Down Expand Up @@ -287,13 +287,14 @@ function api:do_api_call()
return false, resp["msg"], HTTP_INTERNAL_SERVER_ERROR, encode(resp)
end
for _, plugin in ipairs(list) do
local plugin_lua, err = require_plugin(plugin.id)
local plugin_lua, _ = require_plugin(plugin.id)
if plugin_lua and plugin_lua.api ~= nil then
local ok, plugin_obj = new_plugin(plugin_lua, self.ctx)
if not ok then
logger:log(ERR, "can't instantiate " .. plugin.id .. " : " .. plugin_obj)
else
local ok, ret = call_plugin(plugin_obj, "api")
local ret
ok, ret = call_plugin(plugin_obj, "api")
if not ok then
logger:log(ERR, "error while executing " .. plugin.id .. ":api() : " .. ret)
else
Expand Down
4 changes: 3 additions & 1 deletion src/bw/lua/bunkerweb/cachestore.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local ngx = ngx
local class = require "middleclass"
local clusterstore = require "bunkerweb.clusterstore"
local clogger = require "bunkerweb.logger"
local clusterstore = require "bunkerweb.clusterstore"
local mlcache = require "resty.mlcache"
local utils = require "bunkerweb.utils"
local cachestore = class("cachestore")
Expand Down Expand Up @@ -184,10 +184,12 @@ function cachestore:del_redis(key)
return true
end

-- luacheck: ignore 212
function cachestore:purge()
return cache:purge(true)
end

-- luacheck: ignore 212
function cachestore:update()
return cache:update()
end
Expand Down
16 changes: 8 additions & 8 deletions src/bw/lua/bunkerweb/clusterstore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function clusterstore:initialize(pool)
["REDIS_SENTINEL_HOSTS"] = "",
["REDIS_SENTINEL_USERNAME"] = "",
["REDIS_SENTINEL_PASSWORD"] = "",
["REDIS_SENTINEL_MASTER"] = ""
["REDIS_SENTINEL_MASTER"] = "",
}
-- Set them for later use
self.variables = {}
Expand All @@ -57,7 +57,6 @@ function clusterstore:initialize(pool)
keepalive_poolsize = tonumber(self.variables["REDIS_KEEPALIVE_POOL"]),
connection_options = {
ssl = self.variables["REDIS_SSL"] == "yes",

},
host = self.variables["REDIS_HOST"],
port = tonumber(self.variables["REDIS_PORT"]),
Expand All @@ -68,7 +67,7 @@ function clusterstore:initialize(pool)
sentinel_password = self.variables["REDIS_SENTINEL_PASSWORD"],
master_name = self.variables["REDIS_SENTINEL_MASTER"],
role = "master",
sentinels = {}
sentinels = {},
}
self.pool = pool == nil or pool
if self.pool then
Expand All @@ -83,7 +82,7 @@ function clusterstore:initialize(pool)
else
sport = tonumber(sport)
end
table.insert(options.sentinel, {host = shost, port = sport})
table.insert(options.sentinel, { host = shost, port = sport })
end
end
self.options = options
Expand All @@ -110,12 +109,13 @@ function clusterstore:connect(readonly)
-- Connect to sentinels if needed
local redis_client, err
if #self.options.sentinels > 0 then
local redis_sentinel, err = self.redis_connector:connect()
local redis_sentinel
redis_sentinel, err = self.redis_connector:connect()
if not redis_sentinel then
return false, "error while connecting to sentinels : " .. err
end
if readonly then
local redis_clients, err = rs.get_slaves(redis_sentinel, self.options.master_name)
local redis_clients, _ = rs.get_slaves(redis_sentinel, self.options.master_name)
if redis_clients then
redis_client = redis_clients[random(#redis_clients)]
else
Expand All @@ -124,7 +124,7 @@ function clusterstore:connect(readonly)
else
redis_client, err = rs.get_master(redis_sentinel, self.options.master_name)
end
-- Classic connection
-- Classic connection
else
redis_client, err = self.redis_connector:connect()
end
Expand Down Expand Up @@ -155,7 +155,7 @@ function clusterstore:close()
local ok, err
if self.pool then
ok, err = self.redis_connector:set_keepalive(self.redis_client)
-- No pool
-- No pool
else
ok, err = self.redis_client:close()
end
Expand Down
109 changes: 50 additions & 59 deletions src/bw/lua/bunkerweb/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
-- * allow passing of context
-- * updated to work with new 1.19.x apis

local ffi = require "ffi"
local base = require "resty.core.base"
local ffi = require "ffi"
require "resty.core.ctx"


local C = ffi.C
local ngx = ngx
local var = ngx.var
local ngx_log = ngx.log
local ngx_WARN = ngx.WARN
local tonumber = tonumber
local registry = debug.getregistry()
local subsystem = ngx.config.subsystem
Expand All @@ -34,75 +31,69 @@ local ngx_ERR = ngx.ERR

local ngx_lua_ffi_get_ctx_ref
if subsystem == "http" then
ngx_lua_ffi_get_ctx_ref = C.ngx_http_lua_ffi_get_ctx_ref
ngx_lua_ffi_get_ctx_ref = C.ngx_http_lua_ffi_get_ctx_ref
elseif subsystem == "stream" then
ngx_lua_ffi_get_ctx_ref = C.ngx_stream_lua_ffi_get_ctx_ref
ngx_lua_ffi_get_ctx_ref = C.ngx_stream_lua_ffi_get_ctx_ref
end


local in_ssl_phase = ffi.new("int[1]")
local ssl_ctx_ref = ffi.new("int[1]")


local FFI_NO_REQ_CTX = base.FFI_NO_REQ_CTX


local _M = {}


function _M.stash_ref(ctx)
local r = get_request()
if not r then
logger:log(ngx_ERR, "could not stash ngx.ctx ref: no request found")
return
end

do
local ctx_ref = var.ctx_ref
if not ctx_ref or ctx_ref ~= "" then
return
end

if not ctx then
local _ = ngx.ctx -- load context if not previously loaded
end
end
local ctx_ref = ngx_lua_ffi_get_ctx_ref(r, in_ssl_phase, ssl_ctx_ref)
if ctx_ref == FFI_NO_REQ_CTX then
logger:log(ngx_ERR, "could not stash ngx.ctx ref: no ctx found")
return
end

var.ctx_ref = ctx_ref
local r = get_request()
if not r then
logger:log(ngx_ERR, "could not stash ngx.ctx ref: no request found")
return
end

do
local ctx_ref = var.ctx_ref
if not ctx_ref or ctx_ref ~= "" then
return
end

if not ctx then
local _ = ngx.ctx -- load context if not previously loaded
end
end
local ctx_ref = ngx_lua_ffi_get_ctx_ref(r, in_ssl_phase, ssl_ctx_ref)
if ctx_ref == FFI_NO_REQ_CTX then
logger:log(ngx_ERR, "could not stash ngx.ctx ref: no ctx found")
return
end

var.ctx_ref = ctx_ref
end


function _M.apply_ref()
local r = get_request()
if not r then
logger:log(ngx_ERR, "could not apply ngx.ctx: no request found")
return
end

local ctx_ref = var.ctx_ref
if not ctx_ref or ctx_ref == "" then
return
end

ctx_ref = tonumber(ctx_ref)
if not ctx_ref then
return
end

local orig_ctx = registry.ngx_lua_ctx_tables[ctx_ref]
if not orig_ctx then
logger:log(ngx_ERR, "could not apply ngx.ctx: no ctx found")
return
end

ngx.ctx = orig_ctx
var.ctx_ref = ""
local r = get_request()
if not r then
logger:log(ngx_ERR, "could not apply ngx.ctx: no request found")
return
end

local ctx_ref = var.ctx_ref
if not ctx_ref or ctx_ref == "" then
return
end

ctx_ref = tonumber(ctx_ref)
if not ctx_ref then
return
end

local orig_ctx = registry.ngx_lua_ctx_tables[ctx_ref]
if not orig_ctx then
logger:log(ngx_ERR, "could not apply ngx.ctx: no ctx found")
return
end

ngx.ctx = orig_ctx
var.ctx_ref = ""
end


return _M
6 changes: 3 additions & 3 deletions src/bw/lua/bunkerweb/helpers.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local ngx = ngx
local ngx = ngx
local base = require "resty.core.base"
local bwctx = require "bunkerweb.ctx"
local cjson = require "cjson"
local utils = require "bunkerweb.utils"
local bwctx = require "bunkerweb.ctx"
local base = require "resty.core.base"

local open = io.open
local decode = cjson.decode
Expand Down
5 changes: 2 additions & 3 deletions src/bw/lua/bunkerweb/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function plugin:initialize(id, ctx)
"header_filter",
"body_filter",
"log",
"preread"
"preread",
} do
if current_phase == check_phase then
is_request = true
Expand All @@ -44,8 +44,7 @@ function plugin:initialize(id, ctx)
if self.is_request then
self.ctx = ctx or ngx.ctx
self.datastore = get_ctx_obj("datastore", self.ctx) or datastore:new()
self.cachestore = get_ctx_obj("cachestore", self.ctx)
or cachestore:new(use_redis == "yes", self.ctx)
self.cachestore = get_ctx_obj("cachestore", self.ctx) or cachestore:new(use_redis == "yes", self.ctx)
self.clusterstore = get_ctx_obj("clusterstore", self.ctx) or clusterstore:new()
self.cachestore_local = get_ctx_obj("cachestore_local", self.ctx) or cachestore:new(false, self.ctx)
else
Expand Down
17 changes: 15 additions & 2 deletions src/bw/lua/bunkerweb/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ utils.get_phases = function()
"log",
"preread",
"log_stream",
"log_default"
"log_default",
}
end

Expand All @@ -780,7 +780,7 @@ utils.is_cosocket_available = function()
"access",
"content",
"ssl_certificate",
"preread"
"preread",
}
local current_phase = get_phase()
for _, phase in ipairs(phases) do
Expand Down Expand Up @@ -808,4 +808,17 @@ utils.get_ctx_obj = function(obj, ctx)
return nil
end

utils.read_files = function(files)
local data = {}
for _, file in ipairs(files) do
local f, err = open(file, "r")
if not f then
return false, file .. " = " .. err
end
table.insert(data, f:read("*a"))
f:close()
end
return true, data
end

return utils
2 changes: 1 addition & 1 deletion src/common/core/antibot/antibot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local to_hex = str.to_hex
local http_new = http.new
local decode = cjson.decode

local template = nil
local template
local render = nil
if subsystem == "http" then
template = require "resty.template"
Expand Down
Loading

0 comments on commit c4bcaba

Please sign in to comment.