2 LuCI - Lua Configuration Interface
3 Copyright
2009 Jo-Philipp Wich
<xm@leipzig.freifunk.net
>
5 Licensed under the Apache License, Version
2.0 (the
"License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-
2.0
17 local utl = require
"luci.util"
18 local ipt = require
"luci.sys.iptparser".IptParser()
19 local uci = require
"luci.model.uci".cursor_state()
20 local wat = require
"luci.tools.webadmin"
21 local fs = require
"nixio.fs"
24 local leasetime = tonumber(uci:get(
"luci_splash",
"general",
"leasetime") or
1) *
60 *
60
25 local leasefile =
"/tmp/dhcp.leases"
27 uci:foreach(
"dhcp",
"dnsmasq",
29 if s.leasefile then leasefile = s.leasefile end
33 uci:foreach(
"luci_splash_leases",
"lease",
35 if s.start and s.mac then
36 clients[s.mac:lower()] = {
37 start = tonumber(s.start),
38 limit = ( tonumber(s.start) + leasetime ),
48 for _, r in ipairs(ipt:find({
table=
"nat",
chain=
"luci_splash_leases"})) do
49 if r.options and #r.options
>=
2 and r.options[
1] ==
"MAC" then
50 if not clients[r.options[
2]:lower()] then
51 clients[r.options[
2]:lower()] = {
54 mac = r.options[
2]:upper(),
55 policy = ( r.target ==
"RETURN" ) and
"whitelist" or
"blacklist",
63 for mac, client in pairs(clients) do
67 client.packets_out =
0
70 local rin = ipt:find({
table=
"mangle",
chain=
"luci_splash_mark_in", destination=client.ipaddr})
71 local rout = ipt:find({
table=
"mangle",
chain=
"luci_splash_mark_out", options={
"MAC", client.mac:upper()}})
73 if rin and #rin
> 0 then
74 client.bytes_in = rin[
1].bytes
75 client.packets_in = rin[
1].packets
78 if rout and #rout
> 0 then
79 client.bytes_out = rout[
1].bytes
80 client.packets_out = rout[
1].packets
85 uci:foreach(
"luci_splash",
"whitelist",
87 if s.mac and clients[s.mac:lower()] then
88 clients[s.mac:lower()].
policy=
"whitelist"
92 uci:foreach(
"luci_splash",
"blacklist",
94 if s.mac and clients[s.mac:lower()] then
95 clients[s.mac:lower()].policy=(s.kicked and
"kicked" or
"blacklist")
99 if fs.access(leasefile) then
100 for l in io.lines(leasefile) do
101 local time, mac, ip, name = l:match(
"^(%d+) (%S+) (%S+) (%S+)")
102 if time and mac and ip then
103 local c = clients[mac:lower()]
106 c.hostname = ( name ~=
"*" ) and name or nil
112 for i, a in ipairs(luci.sys.net.arptable()) do
113 local c = clients[a[
"HW address"]:lower()]
114 if c and not c.ip then
115 c.ip = a[
"IP address"]
119 local function showmac(mac)
121 mac = mac:gsub(
"(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)",
"%1:XX:XX:%2")
126 if luci.http.formvalue(
"status") ==
"1" then
128 for _, c in utl.spairs(clients,
129 function(a,b) if clients[a].policy == clients[b].policy then
130 return (clients[a].start
> clients[b].start)
132 return (clients[a].policy
> clients[b].policy)
138 hostname = c.hostname or
"?",
140 mac = showmac(c.mac) or
"?",
141 timeleft = (c.limit
>= os.time()) and wat.date_format(c.limit-os.time()) or (c.policy ~=
"normal") and
"-" or
"expired",
142 trafficin = wat.byte_format(c.bytes_in) or
"?",
143 trafficout = wat.byte_format(c.bytes_out) or
"?",
144 policy = c.policy or
"?"
148 luci.http.prepare_content(
"application/json")
149 luci.http.write_json(rv)
158 <script type=
"text/javascript" src=
"<%=resource%>/cbi.js"></script>
159 <script type=
"text/javascript">//<![CDATA[
161 XHR.poll(
10 , '<%=REQUEST_URI%
>', { status:
1 },
164 var tbody = document.getElementById('splash_table');
168 if (info.length == undefined) {
169 s += '
<tr class=
"cbi-section-table-row"><td colspan=
"7" class=
"cbi-section-table-cell"><br /><em><%:No clients connected%
></em><br /></td></tr>'
171 for (var idx =
0; idx < info.length; idx++)
173 var splash = info[idx];
175 '
<tr class=
"cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
176 '
<td class=
"cbi-section-table-cell">%s
</td>' +
177 '
<td class=
"cbi-section-table-cell">%s
</td>' +
178 '
<td class=
"cbi-section-table-cell">%s
</td>' +
179 '
<td class=
"cbi-section-table-cell">%s
</td>' +
180 '
<td class=
"cbi-section-table-cell">%s/%s
</td>' +
181 '
<td class=
"cbi-section-table-cell">',
182 splash.hostname, splash.ip, splash.mac, splash.timeleft, splash.trafficin, splash.trafficout);
184 <% if is_admin then %
>
185 s += String.format('
<select name=
"policy.%s" style=
"width:200px">', splash.mac.toLowerCase());
186 if (splash.policy == 'whitelist') {
187 s += '
<option value=
"whitelist" selected=
"selected"><%:whitelisted%
></option>'
189 s += '
<option value=
"whitelist"><%:whitelisted%
></option>'
191 if (splash.policy == 'normal') {
192 s += '
<option value=
"normal" selected=
"selected"><%:splashed%
></option>';
193 s += '
<option value=
"kicked"><%:temporarily blocked%
></option>'
195 s += '
<option value=
"normal"><%:splashed%
></option>'
197 if (splash.policy == 'blacklist') {
198 s+= '
<option value=
"blacklist" selected=
"selected"><%:blacklisted%
></option>'
200 s += '
<option value=
"blacklist"><%:blacklisted%
></option>'
204 '
<input type=
"submit" class=
"cbi-button cbi-button-save" name=
"save.%s" value=
"<%:Save%>" />',
205 splash.mac.toLowerCase());
207 s += String.format('%s', splash.policy);
218 <div id=
"cbi-splash-leases" class=
"cbi-map">
219 <h2><a id=
"content" name=
"content"><%:Client-Splash%
></a></h2>
220 <fieldset id=
"cbi-table-table" class=
"cbi-section">
221 <legend><%:Active Clients%
></legend>
222 <div class=
"cbi-section-node">
223 <% if is_admin then %
><form action=
"<%=REQUEST_URI%>" method=
"post"><% end %
>
224 <table class=
"cbi-section-table">
226 <tr class=
"cbi-section-table-titles">
227 <th class=
"cbi-section-table-cell"><%:Hostname%
></th>
228 <th class=
"cbi-section-table-cell"><%:IP Address%
></th>
229 <th class=
"cbi-section-table-cell"><%:MAC Address%
></th>
230 <th class=
"cbi-section-table-cell"><%:Time remaining%
></th>
231 <th class=
"cbi-section-table-cell"><%:Traffic in/out%
></th>
232 <th class=
"cbi-section-table-cell"><%:Policy%
></th>
235 <tbody id=
"splash_table">
239 for _, c in utl.spairs(clients,
241 if clients[a].policy == clients[b].policy then
242 return (clients[a].start
> clients[b].start)
244 return (clients[a].policy
> clients[b].policy)
251 <tr class=
"cbi-section-table-row cbi-rowstyle-<%=2-(count%2)%>">
252 <td class=
"cbi-section-table-cell"><%=c.hostname or
"<em>" .. translate(
"unknown") ..
"</em>"%
></td>
253 <td class=
"cbi-section-table-cell"><%=c.ip or
"<em>" .. translate(
"unknown") ..
"</em>"%
></td>
254 <td class=
"cbi-section-table-cell"><%=showmac(c.mac)%
></td>
255 <td class=
"cbi-section-table-cell"><%=
256 (c.limit
>= os.time()) and wat.date_format(c.limit-os.time()) or
257 (c.policy ~=
"normal") and
"-" or
"<em>" .. translate(
"expired") ..
"</em>"
259 <td class=
"cbi-section-table-cell"><%=wat.byte_format(c.bytes_in)%
> / <%=wat.byte_format(c.bytes_out)%
></td>
260 <td class=
"cbi-section-table-cell">
261 <% if is_admin then %
>
262 <select name=
"policy.<%=c.mac:lower()%>" style=
"width:200px">
263 <option value=
"whitelist"<%=c.policy==
"whitelist" and '
selected=
"selected"'%
>><%:whitelisted%
></option>
264 <option value=
"normal"<%=c.policy==
"normal" and not c.kicked and '
selected=
"selected"'%
>><%:splashed%
></option>
265 <option value=
"blacklist"<%=c.policy==
"blacklist" and '
selected=
"selected"'%
>><%:blacklisted%
></option>
266 <% if c.policy ==
"normal" then -%
>
267 <option value=
"kicked"><%:temporarily blocked%
></option>
270 <input type=
"submit" class=
"cbi-button cbi-button-save" name=
"save.<%=c.mac:lower()%>" value=
"<%:Save%>" />
281 <tr class=
"cbi-section-table-row">
282 <td colspan=
"7" class=
"cbi-section-table-cell">
283 <br /><em><%:No clients connected%
></em><br />
289 <% if is_admin then %
></form><% end %
>