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