2 LuCI - Lua Configuration Interface
3 Copyright 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
17 local utl = require "luci.util"
18 local ipt = require "luci.sys.iptparser".IptParser()
19 local uci = require "luci.model.uci".cursor_state()
20 local wat = require "luci.tools.webadmin"
21 local fs = require "nixio.fs"
22 luci.i18n.loadc("splash")
25 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
26 local leasefile = "/tmp/dhcp.leases"
28 uci:foreach("dhcp", "dnsmasq",
30 if s.leasefile then leasefile = s.leasefile end
34 uci:foreach("luci_splash", "lease",
36 if s.start and s.mac then
37 clients[s.mac:lower()] = {
38 start = tonumber(s.start),
39 limit = ( tonumber(s.start) + leasetime ),
49 for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
50 if r.options and #r.options >= 2 and r.options[1] == "MAC" then
51 if not clients[r.options[2]:lower()] then
52 clients[r.options[2]:lower()] = {
55 mac = r.options[2]:upper(),
56 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
64 for mac, client in pairs(clients) do
68 client.packets_out = 0
71 local rin = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=client.ipaddr})
72 local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", client.mac:upper()}})
74 if rin and #rin > 0 then
75 client.bytes_in = rin[1].bytes
76 client.packets_in = rin[1].packets
79 if rout and #rout > 0 then
80 client.bytes_out = rout[1].bytes
81 client.packets_out = rout[1].packets
86 uci:foreach("luci_splash", "whitelist",
88 if s.mac and clients[s.mac:lower()] then
89 clients[s.mac:lower()].policy="whitelist"
93 uci:foreach("luci_splash", "blacklist",
95 if s.mac and clients[s.mac:lower()] then
96 clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
100 if fs.access(leasefile) then
101 for l in io.lines(leasefile) do
102 local time, mac, ip, name = l:match("^(%d+) (%S+) (%S+) (%S+)")
103 if time and mac and ip then
104 local c = clients[mac:lower()]
107 c.hostname = ( name ~= "*" ) and name or nil
113 for i, a in ipairs(luci.sys.net.arptable()) do
114 local c = clients[a["HW address"]:lower()]
115 if c and not c.ip then
116 c.ip = a["IP address"]
120 local function showmac(mac)
122 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
131 <div id="cbi-splash-leases" class="cbi-map">
132 <h2><a id="content" name="content"><%:Client-Splash%></a></h2>
133 <fieldset id="cbi-table-table" class="cbi-section">
134 <legend><%:Active Clients%></legend>
135 <div class="cbi-section-node">
136 <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><% end %>
137 <table class="cbi-section-table">
138 <tr class="cbi-section-table-titles">
139 <th class="cbi-section-table-cell"><%:Hostname%></th>
140 <th class="cbi-section-table-cell"><%:IP Address%></th>
141 <th class="cbi-section-table-cell"><%:MAC Address%></th>
142 <th class="cbi-section-table-cell"><%:Time remaining%></th>
143 <th class="cbi-section-table-cell"><%:Traffic in/out%></th>
144 <th class="cbi-section-table-cell"><%:Policy%></th>
149 for _, c in utl.spairs(clients,
151 if clients[a].policy == clients[b].policy then
152 return (clients[a].start > clients[b].start)
154 return (clients[a].policy > clients[b].policy)
161 <tr class="cbi-section-table-row cbi-rowstyle-<%=2-(count%2)%>">
162 <td class="cbi-section-table-cell"><%=c.hostname or "<em>" .. translate("unknown") .. "</em>"%></td>
163 <td class="cbi-section-table-cell"><%=c.ip or "<em>" .. translate("unknown") .. "</em>"%></td>
164 <td class="cbi-section-table-cell"><%=showmac(c.mac)%></td>
165 <td class="cbi-section-table-cell"><%=
166 (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or
167 (c.policy ~= "normal") and "-" or "<em>" .. translate("expired") .. "</em>"
169 <td class="cbi-section-table-cell"><%=wat.byte_format(c.bytes_in)%> / <%=wat.byte_format(c.bytes_out)%></td>
170 <td class="cbi-section-table-cell">
171 <% if is_admin then %>
172 <select name="policy.<%=c.mac:lower()%>" style="width:200px">
173 <option value="whitelist"<%=c.policy=="whitelist" and ' selected="selected"'%>><%:whitelisted%></option>
174 <option value="normal"<%=c.policy=="normal" and not c.kicked and ' selected="selected"'%>><%:splashed%></option>
175 <option value="blacklist"<%=c.policy=="blacklist" and ' selected="selected"'%>><%:blacklisted%></option>
176 <% if c.policy == "normal" then -%>
177 <option value="kicked"><%:temporarily blocked%></option>
180 <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:Save%>" />
192 <tr class="cbi-section-table-row">
193 <td colspan="7" class="cbi-section-table-cell">
194 <br /><em><%:No clients connected%></em><br />
199 <% if is_admin then %></form><% end %>