applications/splash: Fix blacklisting mechanism, expose status on public freifunk...
[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 luci.i18n.loadc("splash")
23
24 local clients = { }
25 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
26 local leasefile = "/tmp/dhcp.leases"
27
28 uci:foreach("dhcp", "dnsmasq",
29         function(s)
30                 if s.leasefile then leasefile = s.leasefile end
31         end)
32
33
34 uci:foreach("luci_splash", "lease",
35         function(s)
36                 if s.start and s.mac then
37                         clients[s.mac:lower()] = {
38                                 start   = tonumber(s.start),
39                                 limit   = ( tonumber(s.start) + leasetime ),
40                                 mac     = s.mac:upper(),
41                                 ipaddr  = s.ipaddr,
42                                 policy  = "normal",
43                                 packets = 0,
44                                 bytes   = 0,
45                         }
46                 end
47         end)
48
49 for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
50         if r.options and #r.options >= 2 and r.options[1] == "MAC" then
51                 if not clients[r.options[2]:lower()] then
52                         clients[r.options[2]:lower()] = {
53                                 start  = 0,
54                                 limit  = 0,
55                                 mac    = r.options[2]:upper(),
56                                 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
57                                 packets = 0,
58                                 bytes   = 0
59                         }
60                 end
61         end
62 end
63
64 for mac, client in pairs(clients) do
65         client.bytes_in    = 0
66         client.bytes_out   = 0
67         client.packets_in  = 0
68         client.packets_out = 0
69
70         if client.ipaddr then
71                 local rin  = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=client.ipaddr})
72                 local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", client.mac:upper()}})
73
74                 if rin and #rin > 0 then
75                         client.bytes_in   = rin[1].bytes
76                         client.packets_in = rin[1].packets
77                 end
78
79                 if rout and #rout > 0 then
80                         client.bytes_out   = rout[1].bytes
81                         client.packets_out = rout[1].packets
82                 end
83         end
84 end
85
86 uci:foreach("luci_splash", "whitelist",
87         function(s)
88                 if s.mac and clients[s.mac:lower()] then
89                         clients[s.mac:lower()].policy="whitelist"
90                 end
91         end)
92
93 uci:foreach("luci_splash", "blacklist",
94         function(s)
95                 if s.mac and clients[s.mac:lower()] then
96                         clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
97                 end
98         end)            
99
100 if fs.access(leasefile) then
101         for l in io.lines(leasefile) do
102                 local time, mac, ip, name = l:match("^(%d+) (%S+) (%S+) (%S+)")
103                 if time and mac and ip then
104                         local c = clients[mac:lower()]
105                         if c then
106                                 c.ip = ip
107                                 c.hostname = ( name ~= "*" ) and name or nil
108                         end
109                 end
110         end
111 end
112
113 for i, a in ipairs(luci.sys.net.arptable()) do
114         local c = clients[a["HW address"]:lower()]
115         if c and not c.ip then
116                 c.ip = a["IP address"]
117         end
118 end
119
120 local function showmac(mac)
121         if not is_admin then
122                 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
123         end
124         return mac
125 end
126
127 if luci.http.formvalue("status") == "1" then
128         local rv = {}
129         for _, c in utl.spairs(clients,
130                 function(a,b) if clients[a].policy == clients[b].policy then
131                         return (clients[a].start > clients[b].start)
132                 else
133                         return (clients[a].policy > clients[b].policy)
134                 end
135         end)
136         do
137                 if c.ip then
138                         rv[#rv+1] = {
139                                 hostname = c.hostname or "?",
140                                 ip = c.ip or "?",
141                                 mac = showmac(c.mac) or "?",
142                                 timeleft = (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or (c.policy ~= "normal") and "-" or "expired",
143                                 trafficin = wat.byte_format(c.bytes_in) or "?",
144                                 trafficout = wat.byte_format(c.bytes_out) or "?",
145                                 policy = c.policy or "?"
146                                 }
147                 end
148         end
149         luci.http.prepare_content("application/json")
150         luci.http.write_json(rv)
151         return
152 end
153 -%>
154
155
156
157 <%+header%>
158
159 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
160 <script type="text/javascript">//<![CDATA[
161
162         XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
163                 function(x, info)
164                 {
165                 var tbody = document.getElementById('splash_table');
166                         if (tbody)
167                         {
168                                 var s = '';
169                                 if (info.length == undefined) {
170                                         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                                 };
172                                 for (var idx = 0; idx < info.length; idx++)
173                                 {
174                                         var splash = info[idx];
175                                         s += String.format(
176                                                 '<tr class="cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
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</td>' +
181                                                 '<td class="cbi-section-table-cell">%s/%s</td>' +
182                                                 '<td class="cbi-section-table-cell">',
183                                                 splash.hostname, splash.ip, splash.mac, splash.timeleft, splash.trafficin, splash.trafficout);
184
185                                 <% if is_admin then %>
186                                         s += String.format('<select name="policy.%s" style="width:200px">', splash.mac.toLowerCase());
187                                         if (splash.policy == 'whitelist') {     
188                                                 s += '<option value="whitelist" selected="selected"><%:whitelisted%></option>'
189                                         } else {
190                                                 s += '<option value="whitelist"><%:whitelisted%></option>'
191                                         };
192                                         if (splash.policy == 'normal') {
193                                                 s += '<option value="normal" selected="selected"><%:splashed%></option>';
194                                                 s += '<option value="kicked"><%:temporarily blocked%></option>'
195                                         } else {
196                                                 s += 'option value="normal"><%:splashed%></option>'
197                                         };
198                                         if (splash.policy == 'blacklist') {
199                                                 s+= '<option value="blacklist" selected="selected"><%:blacklisted%></option>'
200                                         } else {
201                                                 s += '<option value="blacklist"><%:blacklisted%></option>'
202                                         };
203                                         s += String.format(
204                                                 '</select>' +
205                                                 '<input type="submit" class="cbi-button cbi-button-save" name="save.%s" value="<%:Save%>" />',
206                                                 splash.mac.toLowerCase());
207                                 <% else %>
208                                         s += String.format('%s', splash.policy);
209                                 <% end %>
210                                         s += '</td></tr></tbody>'
211                                 }
212                                 tbody.innerHTML = s;
213                         }
214                 }
215         );
216 //]]></script>
217
218
219 <div id="cbi-splash-leases" class="cbi-map">
220         <h2><a id="content" name="content"><%:Client-Splash%></a></h2>
221         <fieldset id="cbi-table-table" class="cbi-section">
222                 <legend><%:Active Clients%></legend>
223                 <div class="cbi-section-node">
224                         <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><% end %>
225                         <table class="cbi-section-table">
226                                 <thead>
227                                         <tr class="cbi-section-table-titles">
228                                                 <th class="cbi-section-table-cell"><%:Hostname%></th>
229                                                 <th class="cbi-section-table-cell"><%:IP Address%></th>
230                                                 <th class="cbi-section-table-cell"><%:MAC Address%></th>
231                                                 <th class="cbi-section-table-cell"><%:Time remaining%></th>
232                                                 <th class="cbi-section-table-cell"><%:Traffic in/out%></th>
233                                                 <th class="cbi-section-table-cell"><%:Policy%></th>
234                                         </tr>
235                                 </thead>
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                                         <tbody id="splash_table">
252                                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=2-(count%2)%>">
253                                                 <td class="cbi-section-table-cell"><%=c.hostname or "<em>" .. translate("unknown") .. "</em>"%></td>
254                                                 <td class="cbi-section-table-cell"><%=c.ip or "<em>" .. translate("unknown") .. "</em>"%></td>
255                                                 <td class="cbi-section-table-cell"><%=showmac(c.mac)%></td>
256                                                 <td class="cbi-section-table-cell"><%=
257                                                         (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or
258                                                                 (c.policy ~= "normal") and "-" or "<em>" .. translate("expired") .. "</em>"
259                                                 %></td>
260                                                 <td class="cbi-section-table-cell"><%=wat.byte_format(c.bytes_in)%> / <%=wat.byte_format(c.bytes_out)%></td>
261                                                 <td class="cbi-section-table-cell">
262                                                         <% if is_admin then %>
263                                                         <select name="policy.<%=c.mac:lower()%>" style="width:200px">
264                                                                 <option value="whitelist"<%=c.policy=="whitelist" and ' selected="selected"'%>><%:whitelisted%></option>
265                                                                 <option value="normal"<%=c.policy=="normal" and not c.kicked and ' selected="selected"'%>><%:splashed%></option>
266                                                                 <option value="blacklist"<%=c.policy=="blacklist" and ' selected="selected"'%>><%:blacklisted%></option>
267                                                                 <% if c.policy == "normal" then -%>
268                                                                         <option value="kicked"><%:temporarily blocked%></option>
269                                                                 <%- end %>
270                                                         </select>
271                                                         <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:Save%>" />
272                                                         <% else %>
273                                                         <%=c.policy%>
274                                                         <% end %>
275                                                 </td>
276                                                 </tr>
277                                                 <%- 
278                                                         end
279                                                 end
280                                                 if count == 0 then
281                                                 -%>
282                                                 <tr class="cbi-section-table-row">
283                                                         <td colspan="7" class="cbi-section-table-cell">
284                                                                 <br /><em><%:No clients connected%></em><br />
285                                                         </td>
286                                                 </tr>
287                                 <%- end -%>
288                                 </tbody>
289                         </table>
290                         <% if is_admin then %></form><% end %>
291                 </div>
292         </fieldset>
293 </div>
294
295 <%+footer%>