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 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"
16 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
17 local leasefile = "/tmp/dhcp.leases"
19 uci:foreach("dhcp", "dnsmasq",
21 if s.leasefile then leasefile = s.leasefile end
25 uci:foreach("luci_splash_leases", "lease",
27 if s.start and s.mac then
28 clients[s.mac:lower()] = {
29 start = tonumber(s.start),
30 limit = ( tonumber(s.start) + leasetime ),
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()] = {
46 mac = r.options[2]:upper(),
47 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
55 for mac, client in pairs(clients) do
59 client.packets_out = 0
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()}})
65 if rin and #rin > 0 then
66 client.bytes_in = rin[1].bytes
67 client.packets_in = rin[1].packets
70 if rout and #rout > 0 then
71 client.bytes_out = rout[1].bytes
72 client.packets_out = rout[1].packets
77 uci:foreach("luci_splash", "whitelist",
79 if s.mac and clients[s.mac:lower()] then
80 clients[s.mac:lower()].policy="whitelist"
84 uci:foreach("luci_splash", "blacklist",
86 if s.mac and clients[s.mac:lower()] then
87 clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
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()]
98 c.hostname = ( name ~= "*" ) and name or nil
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()
113 local function showmac(mac)
115 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
120 if luci.http.formvalue("status") == "1" then
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)
126 return (clients[a].policy > clients[b].policy)
132 hostname = c.hostname 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 "?"
142 luci.http.prepare_content("application/json")
143 luci.http.write_json(rv)
152 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
153 <script type="text/javascript">//<![CDATA[
155 XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
158 var tbody = document.getElementById('splash_table');
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>'
165 for (var idx = 0; idx < info.length; idx++)
167 var splash = info[idx];
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);
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>'
183 s += '<option value="whitelist"><%:whitelisted%></option>'
185 if (splash.policy == 'normal') {
186 s += '<option value="normal" selected="selected"><%:splashed%></option>';
187 s += '<option value="kicked"><%:temporarily blocked%></option>'
189 s += '<option value="normal"><%:splashed%></option>'
191 if (splash.policy == 'blacklist') {
192 s+= '<option value="blacklist" selected="selected"><%:blacklisted%></option>'
194 s += '<option value="blacklist"><%:blacklisted%></option>'
198 '<input type="submit" class="cbi-button cbi-button-save" name="save.%s" value="<%:Save%>" />',
199 splash.mac.toLowerCase());
201 s += String.format('%s', splash.policy);
212 <div id="cbi-splash-leases" class="cbi-map">
213 <h2 name="content"><%:Client-Splash%></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"><input type="hidden" name="token" value="<%=token%>" /><% end %>
218 <table class="cbi-section-table">
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>
229 <tbody id="splash_table">
233 for _, c in utl.spairs(clients,
235 if clients[a].policy == clients[b].policy then
236 return (clients[a].start > clients[b].start)
238 return (clients[a].policy > clients[b].policy)
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>"
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>
264 <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:Save%>" />
275 <tr class="cbi-section-table-row">
276 <td colspan="7" class="cbi-section-table-cell">
277 <br /><em><%:No clients connected%></em><br />
283 <% if is_admin then %></form><% end %>