diff --git a/pirania-app/files/etc/pirania/governance.json b/pirania-app/files/etc/pirania/governance.json new file mode 100644 index 0000000..27c9ae2 --- /dev/null +++ b/pirania-app/files/etc/pirania/governance.json @@ -0,0 +1,18 @@ +{ + "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 + } +} \ No newline at end of file diff --git a/pirania-app/files/usr/libexec/rpcd/pirania-app b/pirania-app/files/usr/libexec/rpcd/pirania-app index 0f0a4e0..c61ca45 100755 --- a/pirania-app/files/usr/libexec/rpcd/pirania-app +++ b/pirania-app/files/usr/libexec/rpcd/pirania-app @@ -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')) + printJson(result) +end + local function get_clients(msg) local result = {} result.clients = {} @@ -76,7 +82,8 @@ local methods = { body = 'value', logo = 'value', rules = 'value' - } + }, + show_governance = { no_params = 0 }, } if arg[1] == 'list' then @@ -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 \ No newline at end of file diff --git a/pirania-app/files/usr/share/rpcd/acl.d/pirania-app.json b/pirania-app/files/usr/share/rpcd/acl.d/pirania-app.json index 2eda2b2..ebed3f8 100644 --- a/pirania-app/files/usr/share/rpcd/acl.d/pirania-app.json +++ b/pirania-app/files/usr/share/rpcd/acl.d/pirania-app.json @@ -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" ] } } }, diff --git a/pirania/files/usr/bin/voucher b/pirania/files/usr/bin/voucher index 12bccbb..13ce70e 100755 --- a/pirania/files/usr/bin/voucher +++ b/pirania/files/usr/bin/voucher @@ -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") @@ -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) @@ -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 captive_portal.list_vouchers = function() local result = {} local vName = 1 @@ -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] @@ -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 --[[ @@ -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 @@ -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") @@ -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 diff --git a/pirania/files/usr/libexec/rpcd/pirania b/pirania/files/usr/libexec/rpcd/pirania index ccf49fb..a09bef8 100755 --- a/pirania/files/usr/libexec/rpcd/pirania +++ b/pirania/files/usr/libexec/rpcd/pirania @@ -35,6 +35,12 @@ local function shell(command) return result end +local function show_active_vouchers(msg) + local result = {} + result = json.decode(shell('voucher show_active_vouchers')) + printJson({ vouchers = result }) +end + local function list_vouchers(msg) local result = {} result = json.decode(shell('voucher list_vouchers')) @@ -42,7 +48,7 @@ local function list_vouchers(msg) end local function renew_voucher(msg) - local name = msg.name + local name = msg.voucher local date = msg.date local result = {} result = json.decode(shell('voucher renew_voucher '..name..' '..date)) @@ -50,12 +56,28 @@ local function renew_voucher(msg) end local function remove_voucher(msg) - local name = msg.name + local name = msg.voucher local result = {} result = json.decode(shell('voucher remove_voucher '..name)) printJson(result) end +local function renew_many_vouchers(msg) + local date = msg.date + local result = {} + local vouchers = ft.map( + function(val) + local v = {} + v.voucher = val + v.date = date + return v + end, msg.vouchers) + result.vouchers = vouchers + local voucher = io.popen("voucher renew_many_vouchers", 'w') + result.success = assert(voucher:write(table.concat(vouchers, '\n'))) + printJson(result) +end + local function add_many_vouchers(msg) local result = {} local vouchers = ft.map(function(val) return json.encode(val) end, msg.vouchers) @@ -64,6 +86,56 @@ local function add_many_vouchers(msg) printJson(result) end +local function add_visitor_voucher(msg) + local result = {} + local hostnameOutput = shell("cat /proc/sys/kernel/hostname") + local hostname = string.gsub(hostnameOutput, "%s+", "") + local noteHiph = string.gsub(msg.note, "%s+", "-") + local note = string.lower(noteHiph) + local secret = msg.secret + local epoc = msg.epoc + local key = hostname..'-visitor-'..note + result.hostname = hostname + result.key = key + local vouchers = ft.map( + function (val) + local randomKey = val.key + local newKey = key..'-'..randomKey + return json.encode(val) + end + , msg.vouchers) + local voucher = io.popen("voucher add_many_vouchers", 'w') + result.table = table.concat(vouchers, '\n') + -- result.success = assert(voucher:write(table.concat(vouchers, '\n'))) + printJson(result) +end + +local function add_member_voucher(msg) + local hostnameOutput = shell("cat /proc/sys/kernel/hostname") + local hostname = string.gsub(hostnameOutput, "%s+", "") + local result = {} + local noteHiph = string.gsub(msg.note, "%s+", "-") + local note = string.lower(noteHiph) + local secret = msg.secret + -- Should come from a shared-state json + local epoc = msg.epoc + local key = 'm-'..hostname..'-'..note + local output = shell('voucher add_voucher '..key..' '..secret..' '..epoc..' 10 10 1') + local parsedOutput = string.gsub(output, "%s+", "") + if tonumber(parsedOutput) == 0 then + result.message = 'Error creating voucher' + else + result.hostname = hostname + result.key = key + result.secret = secret + result.epoc = epoc + result.upload = 10 + result.download = 10 + result.amountofmacsallowed = 1 + end + printJson(result) +end + local function add_voucher(msg) local result = {} local key = msg.key @@ -132,6 +204,15 @@ end local methods = { + add_visitor_voucher = { + note = 'value', + vouchers = 'value' + }, + add_member_voucher = { + secret = 'value', + note = 'value', + epoc = 0 + }, add_voucher = { key = 'value', secret = 'value', @@ -144,8 +225,10 @@ local methods = { auth_voucher = { mac = 'value', voucher = 'value' }, print_valid_macs = { no_params = 0 }, list_vouchers = { no_params = 0 }, - remove_voucher = { name = 'value' }, - renew_voucher = { name = 'value', date = 0 }, + show_active_vouchers = { no_params = 0 }, + remove_voucher = { voucher = 'value' }, + renew_voucher = { voucher = 'value', date = 0 }, + renew_many_vouchers = { date = 0, vouchers = 'value' }, show_url = { no_params = 0 }, change_url = { url = 'value' }, } @@ -158,12 +241,16 @@ if arg[1] == 'call' then local msg = io.read() msg = json.decode(msg) if arg[2] == 'add_voucher' then add_voucher(msg) + elseif arg[2] == 'add_member_voucher' then add_member_voucher(msg) + elseif arg[2] == 'add_visitor_voucher' then add_visitor_voucher(msg) elseif arg[2] == 'add_many_vouchers' then add_many_vouchers(msg) elseif arg[2] == 'auth_voucher' then auth_voucher(msg) elseif arg[2] == 'print_valid_macs' then print_valid_macs(msg) elseif arg[2] == 'list_vouchers' then list_vouchers(msg) + elseif arg[2] == 'show_active_vouchers' then show_active_vouchers(msg) elseif arg[2] == 'remove_voucher' then remove_voucher(msg) elseif arg[2] == 'renew_voucher' then renew_voucher(msg) + elseif arg[2] == 'renew_many_vouchers' then renew_many_vouchers(msg) elseif arg[2] == 'show_url' then show_url(msg) elseif arg[2] == 'change_url' then change_url(msg) else printJson({ error = "Method not found" }) diff --git a/pirania/files/usr/share/rpcd/acl.d/pirania.json b/pirania/files/usr/share/rpcd/acl.d/pirania.json index 8ec720e..0f0a0cd 100644 --- a/pirania/files/usr/share/rpcd/acl.d/pirania.json +++ b/pirania/files/usr/share/rpcd/acl.d/pirania.json @@ -3,7 +3,15 @@ "description": "pirania voucher public access", "write": { "ubus": { - "pirania": [ "auth_voucher", "get_clients", "print_valid_macs", "show_url" ] + "pirania": [ "auth_voucher", "get_clients", "print_valid_macs", "show_url", "show_active_vouchers" ] + } + } + }, + "lime-app": { + "description": "lime-app public access", + "read": { + "ubus": { + "pirania": [ "auth_voucher", "get_clients", "print_valid_macs", "show_url", "show_active_vouchers" ] } } },