Skip to content

Commit

Permalink
fixes for luacheck
Browse files Browse the repository at this point in the history
  • Loading branch information
maurerle committed Nov 1, 2024
1 parent 38a71b7 commit 193b322
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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')
25 changes: 6 additions & 19 deletions ffbs-mesh-vpn-parker/luasrc/usr/share/lua/nodeconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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
Expand Down
68 changes: 19 additions & 49 deletions ffbs-mesh-vpn-parker/luasrc/usr/share/lua/noderoute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,22 @@ local CONFIG_FILE = tmpdir .. "/noderoute.json"

util.loggername = "noderoute.lua"

function dump(foo)
local function dump(foo)
util.log(json.stringify(foo))
end

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
Expand All @@ -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")
Expand All @@ -82,23 +53,23 @@ 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"
)
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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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.")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
3 changes: 1 addition & 2 deletions ffbs-mesh-vpn-parker/luasrc/usr/share/lua/util.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local json = require("jsonc")
local posix = require("posix")

local util = {}
Expand All @@ -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
Expand Down

0 comments on commit 193b322

Please sign in to comment.