bw - various fixes after LUA improvements

This commit is contained in:
florian 2023-12-30 17:09:04 +01:00
parent 077b2c1c13
commit a4f72f1c1e
No known key found for this signature in database
GPG key ID: 93EE47CC3D061500
10 changed files with 50 additions and 30 deletions

View file

@ -9,6 +9,7 @@
- [FEATURE] Add profile page to web ui and the possibility to activate the 2FA
- [FEATURE] Add setting REVERSE_PROXY_INCLUDES to manually add "include" directives in the reverse proxies
- [MISC] Fallback to default HTTPS certificate to prevent errors
- [MISC] Various internal improvements in LUA code
- [MISC] Updated Python Docker image to 3.12.1-alpine3.18 in Dockerfiles
- [DEPS] Updated ModSecurity to v3.0.11

View file

@ -102,9 +102,9 @@ function cachestore:get(key)
-- luacheck: ignore 431
local value, err, hit_level
if self.use_redis and is_cosocket_available() then
value, err, hit_level = self.cache:get(key, nil, callback, key, self.clusterstore)
value, err, hit_level = cache:get(key, nil, callback, key, self.clusterstore)
else
value, err, hit_level = self.cache:get(key, nil, callback_no_miss)
value, err, hit_level = cache:get(key, nil, callback_no_miss)
end
if value == nil and err ~= nil then
return false, err
@ -123,9 +123,9 @@ function cachestore:set(key, value, ex)
end
end
if ex then
ok, err = self.cache:set(key, { ttl = ex }, value)
ok, err = cache:set(key, { ttl = ex }, value)
else
ok, err = self.cache:set(key, nil, value)
ok, err = cache:set(key, nil, value)
end
if not ok then
return false, err
@ -160,7 +160,7 @@ function cachestore:delete(key)
logger:log(ERR, err)
end
end
ok, err = self.cache:delete(key)
ok, err = cache:delete(key)
if not ok then
return false, err
end
@ -185,7 +185,11 @@ function cachestore:del_redis(key)
end
function cachestore:purge()
return self.cache:purge(true)
return cache:purge(true)
end
function cachestore:update()
return cache:update()
end
return cachestore

View file

@ -9,6 +9,7 @@ local clusterstore = class("clusterstore")
local logger = clogger:new("CLUSTERSTORE")
local get_variable = utils.get_variable
local is_cosocket_available = utils.is_cosocket_available
local ERR = ngx.ERR
local tonumber = tonumber
@ -34,13 +35,15 @@ function clusterstore:initialize(pool)
end
-- Instantiate object
self.pool = pool == nil or pool
local redis_client, err = redis:new()
self.redis_client = redis_client
if self.redis_client == nil then
logger:log(ERR, "can't instantiate redis object : " .. err)
return
if is_cosocket_available() then
local redis_client, err = redis:new()
self.redis_client = redis_client
if self.redis_client == nil then
logger:log(ERR, "can't instantiate redis object : " .. err)
return
end
self.redis_client:set_timeout(tonumber(self.variables["REDIS_TIMEOUT"]))
end
self.redis_client:set_timeout(tonumber(self.variables["REDIS_TIMEOUT"]))
end
function clusterstore:connect()

View file

@ -16,8 +16,6 @@ local subsystem = ngx.config.subsystem
local var = ngx.var
local req = ngx.req
local ip_is_global = utils.ip_is_global
local get_integration = utils.get_integration
local get_version = utils.get_version
local is_ipv4 = utils.is_ipv4
local is_ipv6 = utils.is_ipv6
local get_variable = utils.get_variable
@ -209,9 +207,6 @@ helpers.fill_ctx = function()
-- IP data : v4 / v6
data.ip_is_ipv4 = is_ipv4(data.ip)
data.ip_is_ipv6 = is_ipv6(data.ip)
-- Misc info
data.integration = get_integration()
data.version = get_version()
end
-- Fill ctx
ctx.bw = data

View file

@ -198,7 +198,11 @@ utils.ip_is_global = function(ip)
return not matched, "success"
end
utils.get_integration = function()
utils.get_integration = function(ctx)
-- Check if already in ctx
if ctx and ctx.bw.integration then
return ctx.bw.integration
end
-- Check if already in datastore
local integration, _ = datastore:get("misc_integration", true)
if integration then
@ -248,10 +252,17 @@ utils.get_integration = function()
if not ok then
logger:log(ERR, "can't cache integration to datastore : " .. err)
end
if ctx then
ctx.bw.integration = integration
end
return integration
end
utils.get_version = function()
utils.get_version = function(ctx)
-- Check if already in ctx
if ctx and ctx.bw.version then
return ctx.bw.version
end
-- Check if already in datastore
local version, _ = datastore:get("misc_version", true)
if version then
@ -265,11 +276,14 @@ utils.get_version = function()
end
version = f:read("*a"):gsub("[\n\r]", "")
f:close()
-- Save it to datastore
-- Save version
local ok, err = datastore:set("misc_version", version, nil, true)
if not ok then
logger:log(ERR, "can't cache version to datastore : " .. err)
end
if ctx then
ctx.bw.version = version
end
return version
end
@ -725,6 +739,7 @@ utils.get_phases = function()
"init",
"init_worker",
"set",
"rewrite",
"access",
"content",
"ssl_certificate",
@ -732,16 +747,18 @@ utils.get_phases = function()
"log",
"preread",
"log_stream",
"log_default",
"log_default"
}
end
utils.is_cosocket_available = function()
local phases = {
"timer",
"rewrite",
"access",
"content",
"ssl_certificate",
"preread",
"preread"
}
local current_phase = get_phase()
for _, phase in ipairs(phases) do

View file

@ -17,7 +17,6 @@ server {
access_by_lua_block {
-- Instantiate objects and import required modules
local logger = require "bunkerweb.logger":new("API")
local api = require "bunkerweb.api":new()
local helpers = require "bunkerweb.helpers"
local ngx = ngx
@ -53,6 +52,7 @@ server {
end
-- Check IP
local api = require "bunkerweb.api":new(ctx)
local ok, err = api:is_allowed_ip()
if not ok then
logger:log(WARN, "can't validate access from IP " .. ctx.bw.remote_addr .. " : " .. err)

View file

@ -1,4 +1,4 @@
--log_by_lua_block {
log_by_lua_block {
local clogger = require "bunkerweb.logger"
local helpers = require "bunkerweb.helpers"
local cdatastore = require "bunkerweb.datastore"

View file

@ -1,5 +1,5 @@
--set $dummy_set "";
--set_by_lua_block $dummy_set {
set $dummy_set "";
set_by_lua_block $dummy_set {
local clogger = require "bunkerweb.logger"
local helpers = require "bunkerweb.helpers"
local cdatastore = require "bunkerweb.datastore"
@ -30,7 +30,7 @@
-- Update cachestore only once and before any other code
local cachestore = ccachestore:new(false)
local ok, err = cachestore.cache:update()
local ok, err = cachestore:update()
if not ok then
logger:log(ERR, "can't update cachestore : " .. err)
end

View file

@ -4,7 +4,7 @@ local utils = require "bunkerweb.utils"
local badbehavior = class("badbehavior", plugin)
local ngx
local ngx = ngx
local ERR = ngx.ERR
local WARN = ngx.WARN
local NOTICE = ngx.NOTICE

View file

@ -37,8 +37,8 @@ function bunkernet:initialize(ctx)
local id, err = self.datastore:get("plugin_bunkernet_id", true)
if id then
self.bunkernet_id = id
self.version = (self.ctx and self.ctx.bw.version) or get_version()
self.integration = (self.ctx and self.ctx.bw.integration) or get_integration()
self.version = get_version(self.ctx)
self.integration = get_integration(self.ctx)
else
self.logger:log(ERR, "can't get BunkerNet ID from datastore : " .. err)
end