From 193b3222f2db21e8af51d3935b1ce0f8859bec9a Mon Sep 17 00:00:00 2001 From: Florian Maurer Date: Sat, 2 Nov 2024 00:42:51 +0100 Subject: [PATCH] fixes for luacheck --- .../lib/gluon/upgrade/906-parker-nodeconfig | 6 +- .../gluon/upgrade/996-parker-dhcp-ntp-option | 2 +- .../luasrc/usr/share/lua/nodeconfig.lua | 25 ++----- .../luasrc/usr/share/lua/noderoute.lua | 68 ++++++------------- .../luasrc/usr/share/lua/util.lua | 3 +- 5 files changed, 30 insertions(+), 74 deletions(-) diff --git a/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/906-parker-nodeconfig b/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/906-parker-nodeconfig index d2cc84e1..76347b55 100755 --- a/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/906-parker-nodeconfig +++ b/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/906-parker-nodeconfig @@ -3,15 +3,15 @@ local site = require 'gluon.site' local uci = require('simple-uci').cursor() -site_config_server = site.parker.config_server() +local site_config_server = site.parker.config_server() uci:section('parker', 'nodeconfig', 'nodeconfig', { config_server = site_config_server } ) uci:save('parker') -site_config_pubkey = site.parker.config_pubkey() +local site_config_pubkey = site.parker.config_pubkey() os.execute("mkdir -p /etc/parker/") -pub_fh = io.open("/etc/parker/node-config-pub.key", "w") +local pub_fh = io.open("/etc/parker/node-config-pub.key", "w") pub_fh:write("untrusted comment: signify public key\n") pub_fh:write(site_config_pubkey) pub_fh:close() diff --git a/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/996-parker-dhcp-ntp-option b/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/996-parker-dhcp-ntp-option index 31d06316..06110f83 100755 --- a/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/996-parker-dhcp-ntp-option +++ b/ffbs-mesh-vpn-parker/luasrc/lib/gluon/upgrade/996-parker-dhcp-ntp-option @@ -6,4 +6,4 @@ local ntp_server = site.ntp_servers()[1] local uci = require('uci') local x = uci.cursor() x:set('dhcp', 'client' , 'dhcp_option', {'option:ntp-server,' .. ntp_server}) -x:commit('dhcp') +x:commit('dhcp') diff --git a/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/nodeconfig.lua b/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/nodeconfig.lua index 8194a2fc..7ba71f87 100644 --- a/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/nodeconfig.lua +++ b/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/nodeconfig.lua @@ -5,23 +5,11 @@ local config_file = arg[1] local nonce = arg[2] local tmpdir = arg[3] -local DHCP_IFACE = "client" local PRIVKEY = "/etc/parker/wg-privkey" util.loggername = "nodeconfig.lua" -local function dump(obj) - print(json.stringify(obj)) -end - -local function empty(obj) - for _, _ in pairs(obj) do - return false - end - return true -end - -function conf_wg_iface(iface, privkey, peers, keepalive) +local function conf_wg_iface(iface, privkey, peers, keepalive) local cmd = "wg set " .. iface .. " fwmark 1 " if privkey ~= nil then cmd = cmd .. " private-key " .. privkey @@ -33,7 +21,7 @@ function conf_wg_iface(iface, privkey, peers, keepalive) os.execute(cmd) end -function apply_wg(conf) +local function apply_wg(conf) local current = util.get_wg_info() local target_ifaces = {} for _, conc in pairs(conf.concentrators) do @@ -131,7 +119,7 @@ function apply_wg(conf) return true end -function apply_time(conf) +local function apply_time(conf) local t = conf.time if math.abs(os.time() - t) > 60 then util.log("System time set to " .. t) @@ -142,8 +130,7 @@ end util.log("Starting up") -conf = json.parse(util.read_file(config_file)) --- dump(conf) +local conf = json.parse(util.read_file(config_file)) if conf.nonce ~= nonce then util.log("nonce does not match") @@ -152,8 +139,8 @@ end if conf.id ~= nil then -- we got data, let's do stuff - res_time = apply_time(conf) - res_wg = apply_wg(conf) + apply_time(conf) + local res_wg = apply_wg(conf) -- the config has been validated. -- do an atomic replace in $tmpdir where noderoute.lua will diff --git a/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/noderoute.lua b/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/noderoute.lua index 1f7e8821..c0a73c5e 100644 --- a/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/noderoute.lua +++ b/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/noderoute.lua @@ -11,7 +11,7 @@ local CONFIG_FILE = tmpdir .. "/noderoute.json" util.loggername = "noderoute.lua" -function dump(foo) +local function dump(foo) util.log(json.stringify(foo)) end @@ -19,43 +19,14 @@ local function empty(obj) return next(obj) == nil end -function get_wg_info() - local output = util.check_output("wg show all dump") - local results = {} - for lineRaw in string.gmatch(output, "[^\n]+") do - local line = util.str_split(lineRaw, "%S+") - if not results[line[1]] then - local device = {} - device["private_key"] = line[2] - device["public_key"] = line[3] - device["listen_port"] = tonumber(line[4]) - device["peers"] = {} - results[line[1]] = device - else - local peer = {} - if line[3] ~= "(none)" then - peer["preshared_key"] = line[3] - end - peer["endpoint"] = line[4] - peer["allowed-ips"] = util.str_split(line[5], "[^,]+") - peer["latest_handshake"] = tonumber(line[6]) - peer["transfer_rx"] = tonumber(line[7]) - peer["transfer_tx"] = tonumber(line[8]) - peer["presistent_keepalive"] = tonumber(line[8]) - results[line[1]]["peers"][line[2]] = peer - end - end - return results -end - -function get_handshake_ages() +local function get_handshake_ages() local result = {} local now = os.time() - local wg = get_wg_info() + local wg = util.get_wg_info() for iface, data in pairs(wg) do local peers = data["peers"] if util.tablelength(peers) == 1 then - for k, v in pairs(peers) do + for _, v in pairs(peers) do table.insert(result, { now - v["latest_handshake"], iface }) util.log("wg-handshake age on " .. iface .. ": " .. (now - v["latest_handshake"])) end @@ -64,7 +35,7 @@ function get_handshake_ages() return result end -function get_wg_routes() +local function get_wg_routes() local result = {} local output = util.check_output("ip r show proto " .. RT_PROTO) util.log("Checking for wg routes") @@ -82,14 +53,14 @@ function get_wg_routes() return result end -function set_wg_route(iface, conc) +local function set_wg_route(iface, conc) local res = os.execute("ip -4 r replace default via " .. conc["address4"] .. " dev " .. iface .. " proto " .. RT_PROTO) return res + os.execute("ip -6 r replace default via " .. conc["address6"] .. " dev " .. iface .. " proto " .. RT_PROTO) end -function uci_delete(config, section, option) +local function uci_delete(config, section, option) if not uci.delete(config, section, option) then util.log( "uci.delete(" .. tostring(config) .. ", " .. tostring(section) .. ", " .. tostring(option) .. ") failed" @@ -97,8 +68,8 @@ function uci_delete(config, section, option) end end -function uci_set(config, section, option, value) - local result = false +local function uci_set(config, section, option, value) + local result if value == nil then result = uci.set(config, section, option) else @@ -119,13 +90,13 @@ function uci_set(config, section, option, value) end end -function uci_commit(config) +local function uci_commit(config) if not uci.commit(config) then util.log("uci.commit(" .. tostring(config) .. ") failed") end end -function apply_network(conf, target_state) +local function apply_network(conf, target_state) if uci.get("dhcp", DHCP_IFACE) == nil then uci_set("dhcp", DHCP_IFACE, "dhcp") end @@ -211,7 +182,7 @@ function apply_network(conf, target_state) local range6 = util.read_file("/tmp/range6") if (target_state and range6 ~= conf.range6) or radvd_config_deleted then if conf.range6 ~= nil and target_state then - f = io.open("/tmp/range6", "w") + local f = io.open("/tmp/range6", "w") f:write(conf.range6) f:close() @@ -232,7 +203,7 @@ function apply_network(conf, target_state) return true end -function update() +local function update(report) -- if there already are changes in uci, abort if not empty(uci.changes()) then util.log("UCI is dirty. Refusing to reconfigure node.") @@ -241,7 +212,6 @@ function update() end local active = {} - local state_to_apply = false for _, elem in ipairs(get_handshake_ages()) do if elem[1] < 180 then @@ -289,8 +259,8 @@ function update() if #active == 0 then util.log("No active tunnels. Removing default routes via wg_x.") - local output = util.check_output("ip -4 r show") - for line in string.gmatch(output, "[^\n]+") do + local ip4route = util.check_output("ip -4 r show") + for line in string.gmatch(ip4route, "[^\n]+") do if string.find(line, "default via") then if string.find(line, "wg_") then for gw in string.gmatch(line, "via%s+(%S+)") do @@ -300,8 +270,8 @@ function update() end end - local output = util.check_output("ip -6 r show") - for line in string.gmatch(output, "[^\n]+") do + local ip6route = util.check_output("ip -6 r show") + for line in string.gmatch(ip6route, "[^\n]+") do if string.find(line, "default via") then if string.find(line, "wg_") then for gw in string.gmatch(line, "via%s+(%S+)") do @@ -340,8 +310,8 @@ function update() end util.log("Starting up") -report = io.open("/tmp/nodeconfig-report.tmp", "w") -update() +local report = io.open("/tmp/nodeconfig-report.tmp", "w") +update(report) report:close() os.execute("mv /tmp/nodeconfig-report.tmp /tmp/nodeconfig-report") util.log("Done") diff --git a/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/util.lua b/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/util.lua index 14323e32..41f6c2ed 100644 --- a/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/util.lua +++ b/ffbs-mesh-vpn-parker/luasrc/usr/share/lua/util.lua @@ -1,4 +1,3 @@ -local json = require("jsonc") local posix = require("posix") local util = {} @@ -22,7 +21,7 @@ function util.str_split(str, pattern) end function util.has_value(tab, val) - for index, value in ipairs(tab) do + for _, value in ipairs(tab) do if value == val then return true end