Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pirania] shared-state-priania - Fixes #507

Merged
merged 6 commits into from May 2, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Reduce vouhcer by voucher.key

  • Loading branch information...
gmarcos87 committed Apr 24, 2019
commit d786e17b0fd5ddd75230dac0b24a2486208c11c5
@@ -32,15 +32,56 @@ local function shell(command)
return result
end

function macs2string(macs)
local function macs2string(macs)
macs = macs or {}
return table.concat(macs, '+')
end

local function reduce(cb, tab, default)
local result = default
for k, act in pairs(tab) do
result = cb(result,act)
end
return result
end

local initialHash = shell('md5sum '..dbPath..' | cut -d \' \' -f 1'):gsub("\n","")
local sortedTable = {}

-- Get vouchers data
for key,sharedData in pairs(inputTable) do
local vo = sharedData.data
table.insert(outputTable, vo.name..','..vo.voucher..','..vo.expires..',10,10,'..vo.macsAllowed..','..macs2string(vo.macs))
table.insert(sortedTable,sharedData.data)
end

-- Sort vouchers by secret key
table.sort(sortedTable, function (left, right)
return left.voucher < right.voucher
end)

-- Reduce vouchers to update macs and expires values
local result = reduce(function(prev, act)
if prev[act.voucher] == nil then
prev[act.voucher] = act
elseif tonumber(act.expires) == 0 then
prev[act.voucher] = act
elseif #prev[act.voucher].macs < #act.macs then
prev[act.voucher] = act
end
return prev
end , sortedTable, {})

-- Generate db lines
for key,vo in pairs(result) do
table.insert(outputTable, vo.name..','..vo.voucher..','..vo.expires..',10,10,'..vo.macsAllowed..','..macs2string(vo.macs))
end


-- Write result and calc new md5hash
io.output(dbPath):write(table.concat(outputTable, '\n').."\n")
shell('captive-portal')
local finalHash = shell('md5sum '..dbPath..' | cut -d \' \' -f 1')

-- If db change trigger captive-portal and setup the new state
if initialHash ~= finalHash then
shell('captive-portal')
shell('/etc/shared-state/publishers/shared-state-publish_vouchers')
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.