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