2 Copyright
2009 Jo-Philipp Wich
<jow@openwrt.org
>
3 Licensed to the public under the Apache License
2.0.
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"
18 local leasetime = tonumber(uci:get(
"luci_splash",
"general",
"leasetime") or
1) *
60 *
60
20 uci:foreach(
"luci_splash_leases",
"lease",
22 local m = ipc.checkmac(s.mac)
25 start = tonumber(s.start),
26 limit = ( tonumber(s.start) + leasetime ),
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
44 policy = ( r.target ==
"RETURN" ) and
"whitelist" or
"blacklist",
52 for mac, client in pairs(clients) do
56 client.packets_out =
0
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}})
62 if rin and #rin
> 0 then
63 client.bytes_in = rin[
1].bytes
64 client.packets_in = rin[
1].packets
67 if rout and #rout
> 0 then
68 client.bytes_out = rout[
1].bytes
69 client.packets_out = rout[
1].packets
74 uci:foreach(
"luci_splash",
"whitelist",
76 local m = ipc.checkmac(s.mac)
77 if m and clients[m] then
78 clients[m].
policy=
"whitelist"
82 uci:foreach(
"luci_splash",
"blacklist",
84 local m = ipc.checkmac(s.mac)
85 if m and clients[m] then
86 clients[m].policy=(s.kicked and
"kicked" or
"blacklist")
90 sys.net.host_hints(function(mac, v4, v6, name)
91 local c = mac and clients[mac]
94 c.hostname = c.hostname or name
98 local function showmac(mac)
100 mac = mac:gsub(
"(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)",
"%1:XX:XX:%2")
105 if luci.http.formvalue(
"status") ==
"1" then
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)
111 return (clients[a].policy
> clients[b].policy)
117 hostname = c.hostname 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
"?"
127 luci.http.prepare_content(
"application/json")
128 luci.http.write_json(rv)
137 <script type=
"text/javascript">//<![CDATA[
139 XHR.poll(
10 , '<%=REQUEST_URI%
>', { status:
1 },
142 var tbody = document.getElementById('splash_table');
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>'
149 for (var idx =
0; idx < info.length; idx++)
151 var splash = info[idx];
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);
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>'
167 s += '
<option value=
"whitelist"><%:whitelisted%
></option>'
169 if (splash.policy == 'normal') {
170 s += '
<option value=
"normal" selected=
"selected"><%:splashed%
></option>';
171 s += '
<option value=
"kicked"><%:temporarily blocked%
></option>'
173 s += '
<option value=
"normal"><%:splashed%
></option>'
175 if (splash.policy == 'blacklist') {
176 s+= '
<option value=
"blacklist" selected=
"selected"><%:blacklisted%
></option>'
178 s += '
<option value=
"blacklist"><%:blacklisted%
></option>'
182 '
<input type=
"submit" class=
"cbi-button cbi-button-save" name=
"save.%s" value=
"<%:Save%>" />',
185 s += String.format('%s', splash.policy);
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">
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>
213 <div class=
"tbody" id=
"splash_table">
217 for _, c in utl.spairs(clients,
219 if clients[a].policy == clients[b].policy then
220 return (clients[a].start
> clients[b].start)
222 return (clients[a].policy
> clients[b].policy)
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>"
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>
248 <input type=
"submit" class=
"cbi-button cbi-button-save" name=
"save.<%=c.mac:lower()%>" value=
"<%:Save%>" />
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 />
267 <% if is_admin then %
></form><% end %
>