luci-0.11: merge outstanding trunk changes
[project/luci.git] / applications / luci-splash / luasrc / view / admin_status / splash.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
4
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
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12
13 -%>
14
15 <%-
16
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"
22
23 local clients = { }
24 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
25 local leasefile = "/tmp/dhcp.leases"
26
27 uci:foreach("dhcp", "dnsmasq",
28 function(s)
29 if s.leasefile then leasefile = s.leasefile end
30 end)
31
32
33 uci:foreach("luci_splash", "lease",
34 function(s)
35 if s.start and s.mac then
36 clients[s.mac:lower()] = {
37 start = tonumber(s.start),
38 limit = ( tonumber(s.start) + leasetime ),
39 mac = s.mac:upper(),
40 ipaddr = s.ipaddr,
41 policy = "normal",
42 packets = 0,
43 bytes = 0,
44 }
45 end
46 end)
47
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()] = {
52 start = 0,
53 limit = 0,
54 mac = r.options[2]:upper(),
55 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
56 packets = 0,
57 bytes = 0
58 }
59 end
60 end
61 end
62
63 for mac, client in pairs(clients) do
64 client.bytes_in = 0
65 client.bytes_out = 0
66 client.packets_in = 0
67 client.packets_out = 0
68
69 if client.ipaddr then
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()}})
72
73 if rin and #rin > 0 then
74 client.bytes_in = rin[1].bytes
75 client.packets_in = rin[1].packets
76 end
77
78 if rout and #rout > 0 then
79 client.bytes_out = rout[1].bytes
80 client.packets_out = rout[1].packets
81 end
82 end
83 end
84
85 uci:foreach("luci_splash", "whitelist",
86 function(s)
87 if s.mac and clients[s.mac:lower()] then
88 clients[s.mac:lower()].policy="whitelist"
89 end
90 end)
91
92 uci:foreach("luci_splash", "blacklist",
93 function(s)
94 if s.mac and clients[s.mac:lower()] then
95 clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
96 end
97 end)
98
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()]
104 if c then
105 c.ip = ip
106 c.hostname = ( name ~= "*" ) and name or nil
107 end
108 end
109 end
110 end
111
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"]
116 end
117 end
118
119 local function showmac(mac)
120 if not is_admin then
121 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
122 end
123 return mac
124 end
125
126 if luci.http.formvalue("status") == "1" then
127 local rv = {}
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)
131 else
132 return (clients[a].policy > clients[b].policy)
133 end
134 end)
135 do
136 if c.ip then
137 rv[#rv+1] = {
138 hostname = c.hostname or "?",
139 ip = c.ip 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 "?"
145 }
146 end
147 end
148 luci.http.prepare_content("application/json")
149 luci.http.write_json(rv)
150 return
151 end
152 -%>
153
154
155
156 <%+header%>
157
158 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
159 <script type="text/javascript">//<![CDATA[
160
161 XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
162 function(x, info)
163 {
164 var tbody = document.getElementById('splash_table');
165 if (tbody)
166 {
167 var s = '';
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>'
170 };
171 for (var idx = 0; idx < info.length; idx++)
172 {
173 var splash = info[idx];
174 s += String.format(
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);
183
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>'
188 } else {
189 s += '<option value="whitelist"><%:whitelisted%></option>'
190 };
191 if (splash.policy == 'normal') {
192 s += '<option value="normal" selected="selected"><%:splashed%></option>';
193 s += '<option value="kicked"><%:temporarily blocked%></option>'
194 } else {
195 s += '<option value="normal"><%:splashed%></option>'
196 };
197 if (splash.policy == 'blacklist') {
198 s+= '<option value="blacklist" selected="selected"><%:blacklisted%></option>'
199 } else {
200 s += '<option value="blacklist"><%:blacklisted%></option>'
201 };
202 s += String.format(
203 '</select>' +
204 '<input type="submit" class="cbi-button cbi-button-save" name="save.%s" value="<%:Save%>" />',
205 splash.mac.toLowerCase());
206 <% else %>
207 s += String.format('%s', splash.policy);
208 <% end %>
209 s += '</td></tr>'
210 }
211 tbody.innerHTML = s;
212 }
213 }
214 );
215 //]]></script>
216
217
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">
225 <thead>
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>
233 </tr>
234 </thead>
235 <tbody id="splash_table">
236
237 <%-
238 local count = 0
239 for _, c in utl.spairs(clients,
240 function(a,b)
241 if clients[a].policy == clients[b].policy then
242 return (clients[a].start > clients[b].start)
243 else
244 return (clients[a].policy > clients[b].policy)
245 end
246 end)
247 do
248 if c.ip then
249 count = count + 1
250 -%>
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>"
258 %></td>
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>
268 <%- end %>
269 </select>
270 <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:Save%>" />
271 <% else %>
272 <%=c.policy%>
273 <% end %>
274 </td>
275 </tr>
276 <%-
277 end
278 end
279 if count == 0 then
280 -%>
281 <tr class="cbi-section-table-row">
282 <td colspan="7" class="cbi-section-table-cell">
283 <br /><em><%:No clients connected%></em><br />
284 </td>
285 </tr>
286 <%- end -%>
287 </tbody>
288 </table>
289 <% if is_admin then %></form><% end %>
290 </div>
291 </fieldset>
292 </div>
293
294 <%+footer%>