forked from libremesh/lime-packages-ui
/
lime-voucher
116 lines (103 loc) · 3.18 KB
/
lime-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
#!/usr/bin/env lua
--[[
Copyright 2018 Marcos Gutierrez <gmarcos87@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-3.0
]]--
require "ubus"
local json = require 'luci.json'
local function printJson (obj)
print(json.encode(obj))
end
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end
local function shell(command)
-- TODO(nicoechaniz): sanitize or evaluate if this is a security risk
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
return result
end
local function add_voucher(msg)
local result = {}
local key = msg.key
local secret = msg.secret
local epoc = msg.epoc
local upload = msg.upload
local download = msg.download
local amountofmacsallowed = msg.amountofmacsallowed
result.key = key
result.secret = secret
result.epoc = epoc
result.upload = upload
result.download = download
result.amountofmacsallowed = amountofmacsallowed
shell('voucher add_voucher '..key..' '..secret..' '..epoc..' '..download..' '..upload..' '..amountofmacsallowed)
printJson(result)
shell('/usr/bin/captive-portal')
end
local function auth_voucher(msg)
local result = {}
local mac = msg.mac
local key = msg.key
result.mac = mac
result.key = key
shell('voucher auth_voucher '..mac..' '..key)
printJson(result)
shell('/usr/bin/captive-portal')
end
local function print_valid_macs(msg)
local result = {}
local shell_output = shell('voucher print_valid_macs')
result.macs = shell_output
printJson(result);
end
local function status(msg)
local result = {}
printJson(result);
end
local function get_clients(msg)
local result = {}
result.clients = {}
local output = shell("for ip in $(ip n show | grep -v IP | awk '{print $1}' | sort -ut '|' -k 1,2); do grep $ip /tmp/dhcp.leases; done")
for line in output:gmatch("[^\n]+") do
local words = {}
for w in line:gmatch("%S+") do if w ~= "" then table.insert(words, w) end end
local mac = words[2]
local station = words[4]
table.insert(result.clients, { station=station, mac=mac })
end
printJson(result);
end
local methods = {
add_voucher = {
key = 'value',
secret = 'value',
epoc = 0,
upload = 0,
download = 0,
amountofmacsallowed = 0
},
auth_voucher = { mac = 'value', key = 'value' },
print_valid_macs = { no_params = 0 },
status = { no_params = 0 },
get_clients = { no_params = 0 }
}
if arg[1] == 'list' then
printJson(methods)
end
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] == 'auth_voucher' then auth_voucher(msg)
elseif arg[2] == 'print_valid_macs' then print_valid_macs(msg)
elseif arg[2] == 'status' then status(msg)
elseif arg[2] == 'get_clients' then get_clients(msg)
else printJson({ error = "Method not found" })
end
end