4151c66c3812da0c9367580fc93e36d537ae7bd4
[project/luci.git] / applications / luci-splash / luasrc / view / admin_status / splash.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
4
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
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12
13 -%>
14
15 <%-
16
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
23 local clients = { }
24 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
25 local leasefile = "/tmp/dhcp.leases"
26
27 uci:foreach("dhcp", "dnsmasq",
28         function(s)
29                 if s.leasefile then leasefile = s.leasefile end
30         end)
31
32
33 uci:foreach("luci_splash", "lease",
34         function(s)
35                 if s.start and s.mac then
36                         clients[s.mac:lower()] = {
37                                 start   = tonumber(s.start),
38                                 limit   = ( tonumber(s.start) + leasetime ),
39                                 mac     = s.mac:upper(),
40                                 ipaddr  = s.ipaddr,
41                                 policy  = "normal",
42                                 packets = 0,
43                                 bytes   = 0,
44                         }
45                 end
46         end)
47
48 for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
49         if r.options and #r.options >= 2 and r.options[1] == "MAC" then
50                 if not clients[r.options[2]:lower()] then
51                         clients[r.options[2]:lower()] = {
52                                 start  = 0,
53                                 limit  = 0,
54                                 mac    = r.options[2]:upper(),
55                                 policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
56                                 packets = 0,
57                                 bytes   = 0
58                         }
59                 end
60         end
61 end
62
63 for mac, client in pairs(clients) do
64         client.bytes_in    = 0
65         client.bytes_out   = 0
66         client.packets_in  = 0
67         client.packets_out = 0
68
69         if client.ipaddr then
70                 local rin  = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=client.ipaddr})
71                 local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", client.mac:upper()}})
72
73                 if rin and #rin > 0 then
74                         client.bytes_in   = rin[1].bytes
75                         client.packets_in = rin[1].packets
76                 end
77
78                 if rout and #rout > 0 then
79                         client.bytes_out   = rout[1].bytes
80                         client.packets_out = rout[1].packets
81                 end
82         end
83 end
84
85 uci:foreach("luci_splash", "whitelist",
86         function(s)
87                 if s.mac and clients[s.mac:lower()] then
88                         clients[s.mac:lower()].policy="whitelist"
89                 end
90         end)
91
92 uci:foreach("luci_splash", "blacklist",
93         function(s)
94                 if s.mac and clients[s.mac:lower()] then
95                         clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
96                 end
97         end)            
98
99 if fs.access(leasefile) then
100         for l in io.lines(leasefile) do
101                 local time, mac, ip, name = l:match("^(%d+) (%S+) (%S+) (%S+)")
102                 if time and mac and ip then
103                         local c = clients[mac:lower()]
104                         if c then
105                                 c.ip = ip
106                                 c.hostname = ( name ~= "*" ) and name or nil
107                         end
108                 end
109         end
110 end
111
112 for i, a in ipairs(luci.sys.net.arptable()) do
113         local c = clients[a["HW address"]:lower()]
114         if c and not c.ip then
115                 c.ip = a["IP address"]
116         end
117 end
118
119 local function showmac(mac)
120         if not is_admin then
121                 mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
122         end
123         return mac
124 end
125
126 -%>
127
128 <%+header%>
129
130 <div id="cbi-splash-leases" class="cbi-map">
131         <h2><a id="content" name="content"><%:ff_splash Client-Splash%></a></h2>
132         <fieldset id="cbi-table-table" class="cbi-section">
133                 <legend><%:ff_splash_clients Active Clients%></legend>
134                 <div class="cbi-section-node">
135                         <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><% end %>
136                         <table class="cbi-section-table">
137                                 <tr class="cbi-section-table-titles">
138                                         <th class="cbi-section-table-cell"><%:ff_splash_hostname Hostname%></th>
139                                         <th class="cbi-section-table-cell"><%:ff_splash_ip IP Address%></th>
140                                         <th class="cbi-section-table-cell"><%:ff_splash_mac MAC Address%></th>
141                                         <th class="cbi-section-table-cell"><%:ff_splash_timeleft Time remaining%></th>
142                                         <th class="cbi-section-table-cell"><%:ff_splash_traffic Traffic in/out%></th>
143                                         <th class="cbi-section-table-cell"><%:ff_splash_policy Policy%></th>
144                                 </tr>
145
146                                 <%-
147                                         local count = 0
148                                         for _, c in utl.spairs(clients,
149                                                 function(a,b)
150                                                         if clients[a].policy == clients[b].policy then
151                                                                 return (clients[a].start > clients[b].start)
152                                                         else
153                                                                 return (clients[a].policy > clients[b].policy)
154                                                         end
155                                                 end)
156                                         do
157                                                 if c.ip then
158                                                         count = count + 1
159                                 -%>
160                                         <tr class="cbi-section-table-row cbi-rowstyle-<%=2-(count%2)%>">
161                                                 <td class="cbi-section-table-cell"><%=c.hostname or "<em>" .. translate("ff_splash_unknown", "unknown") .. "</em>"%></td>
162                                                 <td class="cbi-section-table-cell"><%=c.ip or "<em>" .. translate("ff_splash_unknown", "unknown") .. "</em>"%></td>
163                                                 <td class="cbi-section-table-cell"><%=showmac(c.mac)%></td>
164                                                 <td class="cbi-section-table-cell"><%=
165                                                         (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or
166                                                                 (c.policy ~= "normal") and "-" or "<em>" .. translate("ff_splash_expired", "expired") .. "</em>"
167                                                 %></td>
168                                                 <td class="cbi-section-table-cell"><%=wat.byte_format(c.bytes_in)%> / <%=wat.byte_format(c.bytes_out)%></td>
169                                                 <td class="cbi-section-table-cell">
170                                                         <% if is_admin then %>
171                                                         <select name="policy.<%=c.mac:lower()%>" style="width:200px">
172                                                                 <option value="whitelist"<%=c.policy=="whitelist" and ' selected="selected"'%>><%:ff_splash_whitelisted whitelisted%></option>
173                                                                 <option value="normal"<%=c.policy=="normal" and not c.kicked and ' selected="selected"'%>><%:ff_splash_splashed splashed%></option>
174                                                                 <option value="blacklist"<%=c.policy=="blacklist" and ' selected="selected"'%>><%:ff_splash_blacklisted blacklisted%></option>
175                                                                 <% if c.policy == "normal" then -%>
176                                                                         <option value="kicked"><%:ff_splash_tempblock temporarily blocked%></option>
177                                                                 <%- end %>
178                                                         </select>
179                                                         <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:save Save%>" />
180                                                         <% else %>
181                                                         <%=c.policy%>
182                                                         <% end %>
183                                                 </td>
184                                         </tr>
185                                 <%- 
186                                                 end
187                                         end
188
189                                         if count == 0 then
190                                 -%>
191                                         <tr class="cbi-section-table-row">
192                                                 <td colspan="7" class="cbi-section-table-cell">
193                                                         <br /><em><%:ff_splash_noclients No clients connected%></em><br />
194                                                 </td>
195                                         </tr>
196                                 <%- end -%>
197                         </table>
198                         <% if is_admin then %></form><% end %>
199                 </div>
200         </fieldset>
201 </div>
202
203 <%+footer%>