From 719b4b00be0a7135c5978bd666ffe5b0fd5cd505 Mon Sep 17 00:00:00 2001 From: Frank Wall Date: Sun, 31 Oct 2021 23:27:11 +0100 Subject: [PATCH] net/haproxy: improve handling of Lua scripts, fixes #2265 --- net/haproxy/pkg-descr | 11 +++++ .../OPNsense/HAProxy/forms/dialogLua.xml | 12 +++++ .../app/models/OPNsense/HAProxy/HAProxy.xml | 14 +++++- .../OPNsense/HAProxy/Migrations/M3_3_0.php | 45 +++++++++++++++++++ .../OPNsense/HAProxy/exportLuaScripts.php | 19 +++++--- .../templates/OPNsense/HAProxy/haproxy.conf | 13 ++++-- 6 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/Migrations/M3_3_0.php diff --git a/net/haproxy/pkg-descr b/net/haproxy/pkg-descr index 5cba9c35d..ed0443984 100644 --- a/net/haproxy/pkg-descr +++ b/net/haproxy/pkg-descr @@ -6,6 +6,17 @@ very high loads while needing persistence or Layer7 processing. Plugin Changelog ================ +3.7 + +Added: +* add options "preload" and "filename scheme" to Lua scripts (#2265) + +Fixed: +* unable to use the "require" function in Lua scripts (#2265) + +Changed: +* set "lua-prepend-path" so that Lua scripts can be found (#2265) + 3.6 Added: diff --git a/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/forms/dialogLua.xml b/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/forms/dialogLua.xml index 72a791e42..23ef5b030 100644 --- a/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/forms/dialogLua.xml +++ b/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/forms/dialogLua.xml @@ -17,6 +17,18 @@ text Description for this Lua script. + + lua.preload + + checkbox + Whether HAProxy should load and execute this Lua script on startup. This is the default behaviour. However, if this Lua script is included by other Lua scripts using the "require" function, then preloading should be disabled to avoid HAProxy errors. + + + lua.filename_scheme + + dropdown + + lua.content diff --git a/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/HAProxy.xml b/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/HAProxy.xml index 7eaa7c3dd..89cfe9e0f 100644 --- a/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/HAProxy.xml +++ b/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/HAProxy.xml @@ -1,6 +1,6 @@ //OPNsense/HAProxy - 3.2.0 + 3.3.0 the HAProxy load balancer @@ -2364,6 +2364,18 @@ Should be a string between 1 and 255 characters. N + + 1 + Y + + + Y + id + + Use a random ID for the filename [default] + Use the specified name as filename + + Y diff --git a/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/Migrations/M3_3_0.php b/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/Migrations/M3_3_0.php new file mode 100644 index 000000000..5714a405b --- /dev/null +++ b/net/haproxy/src/opnsense/mvc/app/models/OPNsense/HAProxy/Migrations/M3_3_0.php @@ -0,0 +1,45 @@ +getNodeByReference('luas.lua')->iterateItems() as $lua) { + $lua->filename_scheme = 'id'; + $lua->preload = '1'; + } + } +} diff --git a/net/haproxy/src/opnsense/scripts/OPNsense/HAProxy/exportLuaScripts.php b/net/haproxy/src/opnsense/scripts/OPNsense/HAProxy/exportLuaScripts.php index d0da858af..82243d95f 100755 --- a/net/haproxy/src/opnsense/scripts/OPNsense/HAProxy/exportLuaScripts.php +++ b/net/haproxy/src/opnsense/scripts/OPNsense/HAProxy/exportLuaScripts.php @@ -2,7 +2,7 @@ OPNsense->HAProxy->luas)) { } $lua_name = (string)$lua->name; $lua_id = (string)$lua->id; - if ($lua_id != "") { - $lua_content = htmlspecialchars_decode(str_replace("\r", "", (string)$lua->content)); - $lua_filename = $export_path . $lua_id . ".lua"; - file_put_contents($lua_filename, $lua_content); - chmod($lua_filename, 0600); - echo "lua script exported to " . $lua_filename . "\n"; + $lua_filename_scheme = (string)$lua->filename_scheme; + if ($lua_filename_scheme != '' and $lua_filename_scheme === 'name') { + $_name_alnum = preg_replace("/[^A-Za-z0-9]/", '', $lua_name); + $lua_filename = $export_path . $_name_alnum . '.lua'; + } else { + $lua_filename = $export_path . $lua_id . '.lua'; } + $lua_content = htmlspecialchars_decode(str_replace("\r", "", (string)$lua->content)); + file_put_contents($lua_filename, $lua_content); + chmod($lua_filename, 0600); + chown($lua_filename, 'www'); + echo "lua script exported to " . $lua_filename . "\n"; } } diff --git a/net/haproxy/src/opnsense/service/templates/OPNsense/HAProxy/haproxy.conf b/net/haproxy/src/opnsense/service/templates/OPNsense/HAProxy/haproxy.conf index d6e0624ef..44f3e25dd 100644 --- a/net/haproxy/src/opnsense/service/templates/OPNsense/HAProxy/haproxy.conf +++ b/net/haproxy/src/opnsense/service/templates/OPNsense/HAProxy/haproxy.conf @@ -995,12 +995,19 @@ global {% do logging.append(OPNsense.HAProxy.general.logging.facility) %} {% do logging.append(OPNsense.HAProxy.general.logging.level) if OPNsense.HAProxy.general.logging.level|default("") != "" %} log {{logging|join(' ')}} +{# # lua scripts #} + lua-prepend-path /tmp/haproxy/lua/?.lua {% if helpers.exists('OPNsense.HAProxy.luas.lua') %} - # lua scripts {% for lua in helpers.toList('OPNsense.HAProxy.luas.lua') %} -{% if lua.enabled == '1' %} +{% if lua.enabled == '1' and lua.preload|default('') == '1' %} +{# # select the filename scheme for lua scripts #} +{% if lua.filename_scheme|default('id') == 'name' %} +{% set lua_filename = lua.name | regex_replace ("[^A-Za-z0-9]","") %} +{% else %} +{% set lua_filename = lua.id %} +{% endif %} # lua script: {{lua.name}} - lua-load /tmp/haproxy/lua/{{lua.id}}.lua + lua-load /tmp/haproxy/lua/{{lua_filename}}.lua {% endif %} {% endfor %} {% endif %}