-
Notifications
You must be signed in to change notification settings - Fork 96
/
voucher
executable file
·117 lines (100 loc) · 2.66 KB
/
voucher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/lua
local vouchera = require('voucher.vouchera')
local utils = require('voucher.utils')
local uci = require('uci')
local json = require 'luci.jsonc'
local uci_cursor = uci.cursor()
local arguments
local action
local context
captive_portal = {}
vouchera.init()
captive_portal.activate = function(code, mac)
local res = vouchera.activate(code, mac)
if res then
print('Voucher activated!')
return true
else
return nil, "Invalid voucher code '" .. code .. "' or invalid MAC '" .. mac .. "'"
end
end
-- Checks if the mac of the given context is allowed to browse.
captive_portal.is_mac_authorized = function(mac)
if vouchera.is_mac_authorized(mac) then
print('true')
return true
else
print('false')
os.exit(1)
end
end
local function _list_by_status(status)
for _, voucher in pairs(vouchera.vouchers) do
if voucher.status() == status then
print(voucher.tostring())
end
end
return true
end
captive_portal.list_available = function()
return _list_by_status('available')
end
captive_portal.list_active = function()
return _list_by_status('active')
end
captive_portal.list_expired = function()
return _list_by_status('expired')
end
-- List all vouchers
captive_portal.list = function()
for _, voucher in pairs(vouchera.vouchers) do
print(voucher.tostring())
end
return true
end
captive_portal.deactivate = function(id)
return vouchera.deactivate(id)
end
captive_portal.invalidate = function(id)
return vouchera.invalidate(id)
end
captive_portal.add = function(name, duration_m, activation_deadline)
if type(duration_m) == "string" then
duration_m = tonumber(duration_m)
end
local qty = 1
res, msg = vouchera.create(name, qty, tonumber(duration_m), tonumber(activation_deadline))
if res ~= nil then
local voucher = vouchera.get_by_id(res[1].id)
print(voucher.tostring())
os.exit(0)
else
print(msg)
os.exit(1)
end
end
captive_portal.show_authorized_macs = function()
for _, voucher in pairs(vouchera.vouchers) do
if voucher.is_active() then
print(voucher.mac)
end
end
return true
end
-- if is main
if debug.getinfo(2).name == nil then
local arguments = { ... }
local action = table.remove(arguments, 1)
local context = arguments
local f = captive_portal[action]
if f ~= nil then
res, msg = f(unpack(context))
if not res then
print(msg or 'error')
os.exit(1)
end
else
print("invalid command: " .. tostring(action))
end
end
return captive_portal