2 Copyright 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
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 fs = require "nixio.fs"
15 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
16 local leasefile = "/tmp/dhcp.leases"
18 uci:foreach("dhcp", "dnsmasq",
20 if s.leasefile then leasefile = s.leasefile end
24 uci:foreach("luci_splash_leases", "lease",
26 if s.start and s.mac then
27 clients[s.mac:lower()] = {
28 start = tonumber(s.start),
29 limit = ( tonumber(s.start) + leasetime ),
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()] = {
45 mac = r.options[2]:upper(),
46 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
54 for mac, client in pairs(clients) do
58 client.packets_out = 0
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()}})
64 if rin and #rin > 0 then
65 client.bytes_in = rin[1].bytes
66 client.packets_in = rin[1].packets
69 if rout and #rout > 0 then
70 client.bytes_out = rout[1].bytes
71 client.packets_out = rout[1].packets
76 uci:foreach("luci_splash", "whitelist",
78 if s.mac and clients[s.mac:lower()] then
79 clients[s.mac:lower()].policy="whitelist"
83 uci:foreach("luci_splash", "blacklist",
85 if s.mac and clients[s.mac:lower()] then
86 clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
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()]
97 c.hostname = ( name ~= "*" ) and name or nil
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"]
110 local function showmac(mac)
112 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
117 if luci.http.formvalue("status") == "1" then
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)
123 return (clients[a].policy > clients[b].policy)
129 hostname = c.hostname 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 "?"
139 luci.http.prepare_content("application/json")
140 luci.http.write_json(rv)
149 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
150 <script type="text/javascript">//<![CDATA[
152 XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
155 var tbody = document.getElementById('splash_table');
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>'
162 for (var idx = 0; idx < info.length; idx++)
164 var splash = info[idx];
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);
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>'
180 s += '<option value="whitelist"><%:whitelisted%></option>'
182 if (splash.policy == 'normal') {
183 s += '<option value="normal" selected="selected"><%:splashed%></option>';
184 s += '<option value="kicked"><%:temporarily blocked%></option>'
186 s += '<option value="normal"><%:splashed%></option>'
188 if (splash.policy == 'blacklist') {
189 s+= '<option value="blacklist" selected="selected"><%:blacklisted%></option>'
191 s += '<option value="blacklist"><%:blacklisted%></option>'
195 '<input type="submit" class="cbi-button cbi-button-save" name="save.%s" value="<%:Save%>" />',
196 splash.mac.toLowerCase());
198 s += String.format('%s', splash.policy);
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">
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>
226 <tbody id="splash_table">
230 for _, c in utl.spairs(clients,
232 if clients[a].policy == clients[b].policy then
233 return (clients[a].start > clients[b].start)
235 return (clients[a].policy > clients[b].policy)
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>"
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>
261 <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:Save%>" />
272 <tr class="cbi-section-table-row">
273 <td colspan="7" class="cbi-section-table-cell">
274 <br /><em><%:No clients connected%></em><br />
280 <% if is_admin then %></form><% end %>