Skip to content
This repository has been archived by the owner on Aug 2, 2020. It is now read-only.

WIP: Lime-App integration #60

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions pirania-app/files/etc/pirania/governance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
luandro marked this conversation as resolved.
Show resolved Hide resolved
"provider": {
"payday": 24,
"cost": "150,00",
"name": "Internet Service Provider Name",
"speed": "10Mbs"
},
"community": {
"payday": 20,
"maintenance": "200,00",
"reserve": "00,00",
"currency": "R$"
},
"member": {
"cost": "50,00",
"vouchers": 5
}
}
16 changes: 12 additions & 4 deletions pirania-app/files/usr/libexec/rpcd/pirania-app
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ local function shell(command)
return result
end

local function show_governance(msg)
local result = {}
result = json.decode(shell('cat /etc/pirania/governance.json'))
luandro marked this conversation as resolved.
Show resolved Hide resolved
printJson(result)
end

local function get_clients(msg)
local result = {}
result.clients = {}
Expand Down Expand Up @@ -76,7 +82,8 @@ local methods = {
body = 'value',
logo = 'value',
rules = 'value'
}
},
show_governance = { no_params = 0 },
}

if arg[1] == 'list' then
Expand All @@ -86,9 +93,10 @@ end
if arg[1] == 'call' then
local msg = io.read()
msg = json.decode(msg)
if arg[2] == 'read_content' then read_content(msg)
elseif arg[2] == 'get_clients' then get_clients(msg)
elseif arg[2] == 'write_content' then write_content(msg)
if arg[2] == 'read_content' then read_content(msg)
elseif arg[2] == 'get_clients' then get_clients(msg)
elseif arg[2] == 'write_content' then write_content(msg)
elseif arg[2] == 'show_governance' then show_governance(msg)
else printJson({ error = "Method not found" })
end
end
12 changes: 10 additions & 2 deletions pirania-app/files/usr/share/rpcd/acl.d/pirania-app.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{
"unauthenticated": {
"description": "Pirania public access",
"write": {
"read": {
"ubus": {
"pirania-app": [ "get_clients", "read_content", "show_governance" ]
}
}
},
"lime-app": {
"description": "lime-app public access",
"read": {
"ubus": {
"pirania-app": [ "get_clients", "read_content" ]
"pirania-app": [ "get_clients", "read_content", "show_governance" ]
}
}
},
Expand Down
141 changes: 129 additions & 12 deletions pirania/files/usr/bin/voucher
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ local context

captive_portal = {}

local function shell(command)
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
return result
end

local function printJson (obj)
print(json.encode(obj))
end

local function dateNow()
local output = shell('date +%s000')
local parsed = string.gsub(output, "%s+", "")
local dateNow = tonumber(parsed)
return dateNow
end

local function shell(command)
local handle = io.popen(command)
local result = handle:read("*a")
Expand Down Expand Up @@ -51,19 +65,19 @@ captive_portal.url = function(context)
end

-- Update voucher date
local function updateVoucher(name, date)
local function updateVoucher(secret, date)
local result = {}
local db = dba.load(config.db)
local toRenew = {}
for _, voucher in pairs (db.data) do
if (voucher[1] == name) then
if (voucher[2] == secret) then
toRenew = voucher
end
end
if (#toRenew > 0) then
result.success = true
toRenew[3] = tonumber(date)
local validVouchers = ft.filter(function(row, index) return row[1] ~= name end, db.data)
local validVouchers = ft.filter(function(row, index) return row[2] ~= secret end, db.data)
local newDb = db
newDb.data = validVouchers
table.insert(newDb.data, toRenew)
Expand Down Expand Up @@ -97,14 +111,51 @@ end
--[[
--Checks if the mac of the given context is allowed to browse.
--]]
captive_portal.status = function(context)
captive_portal.voucher_status = function(context)
local mac = context[1]

local db = dba.load(config.db)
print ( logic.status(db, mac) )
print ( logic.voucher_status(db, mac) )
end

-- List all
-- Show numbers of active vouchers
captive_portal.show_active_vouchers = function()
local result = {}
local vName = 1
local vExpire = 3
local usedMacs = 7
members = 0
visitors = 0
local db = dba.load(config.db)
for _, v in pairs(db.data) do
local expireDate = tonumber(v[vExpire]) or 0
local active = function ()
if (#v[usedMacs] > 0) then
return true
end
return false
end
if (expireDate > dateNow() and active() == true) then
local nameInfo = {}
for word in v[vName]:gmatch("[^-]+") do
table.insert(nameInfo, word)
end
if (nameInfo[1] == 'm') then
local nextN = members + 1
members = nextN
else
local nextN = visitors + 1
visitors = nextN
end
end
result.members = members
result.visitors = visitors
end
printJson(result)
end


-- List all vouchers
luandro marked this conversation as resolved.
Show resolved Hide resolved
captive_portal.list_vouchers = function()
local result = {}
local vName = 1
Expand All @@ -115,6 +166,35 @@ captive_portal.list_vouchers = function()
local db = dba.load(config.db)
for _, v in pairs(db.data) do
result[_] = {}
local startName = 3
local nameInfo = {}
for word in v[vName]:gmatch("[^-]+") do
table.insert(nameInfo, word)
end
if (nameInfo[1] == 'm') then
result[_].type = 'member'
else
result[_].node = nameInfo[1]
result[_].type = 'visitor'
startName = 2
end
if (nameInfo[2] and startName == 3) then
result[_].node = nameInfo[2]
end
if (#nameInfo > startName) then
local t = nameInfo[startName]
for k,v in ipairs(nameInfo) do
if (k > startName) then
t = t..'-'..v
end
end
result[_].note = t
else result[_].note = nameInfo[startName]
end
local expireDate = tonumber(v[vExpire]) or 0
if (expireDate < dateNow()) then
result[_].type = 'invalid'
end
result[_].name = v[vName]
result[_].expires = v[vExpire]
result[_].voucher = v[vSecret]
Expand All @@ -126,15 +206,15 @@ end

-- Renew voucher
captive_portal.renew_voucher = function(context)
local voucherName = context[1]
local voucherSecret = context[1]
local renewDate = context[2]
printJson(updateVoucher(voucherName, renewDate))
printJson(updateVoucher(voucherSecret, renewDate))
end

-- Remove voucher
captive_portal.remove_voucher = function(context)
local voucherName = context[1]
printJson(updateVoucher(voucherName, 0))
local voucherSecret = context[1]
printJson(updateVoucher(voucherSecret, 0))
end

--[[
Expand All @@ -151,7 +231,7 @@ captive_portal.add_voucher = function(context)

local db = dba.load(config.db)
for _, v in pairs (db.data) do
if (v[1] == key) then
if (v[2] == voucher) then
exists = true
end
end
Expand All @@ -164,6 +244,43 @@ captive_portal.add_voucher = function(context)
end
end

captive_portal.renew_many_vouchers = function(context, data)
local result = {}
local table = utils.split(data, "\n")
local db = dba.load(config.db)
local voucherList = ft.map(function(val) return json.encode(val) end, table)
local unchangedVouchers = {}
local vouchersToUpdate = ft.map(
function(row, index)
if (row[2] == voucherList.voucher) then
unchangedVouchers
else unchangedVouchers
end
end, db.data
)
-- local vouchers = ft.map(function(val) return json.encode(val) end, vouchersToUpdate)
-- for _,val in pairs (table) do
-- local jdata = json.decode(val)
-- local toRenew = {}
-- for _, voucher in pairs (db.data) do
-- if (voucher[2] == jdata.voucher) then
-- toRenew = voucher
-- end
-- end
-- if (#toRenew > 0) then
-- result.success = true
-- toRenew[3] = tonumber(jdata.date)
-- local validVouchers = ft.filter(function(row, index) return row[2] ~= jdata.voucher end, db.data)
-- newDb.data = validVouchers
-- table.insert(newDb.data, toRenew)
-- else result.success = false
-- end
-- end
result.newDb = newDb
-- dba.save(config.db, newDb)
printJson(result)
end

captive_portal.add_many_vouchers = function(context, data)
local result = {}
local table = utils.split(data, "\n")
Expand Down Expand Up @@ -196,7 +313,7 @@ if debug.getinfo(2).name == nil then
arguments = { ... }
action = arguments[1]
context = ft.filter(function(row, index) return index > 1 end, arguments)
if (action == 'add_many_vouchers') then stdin = io.stdin:read("*all") end
if (action == 'add_many_vouchers' or action == 'renew_many_vouchers') then stdin = io.stdin:read("*all") end
captive_portal[action](context, stdin)
end

Expand Down
Loading