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