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