Merge pull request #1684 from dibdot/adblock
[project/luci.git] / applications / luci-app-splash / luasrc / view / admin_status / splash.htm
index 61b32ba..37f6777 100644 (file)
@@ -1,42 +1,30 @@
 <%#
-LuCI - Lua Configuration Interface
-Copyright 2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-$Id$
-
+ Copyright 2009 Jo-Philipp Wich <jow@openwrt.org>
+ Licensed to the public under the Apache License 2.0.
 -%>
 
 <%-
 
 local utl = require "luci.util"
+local sys = require "luci.sys"
+local ipc = require "luci.ip"
 local ipt = require "luci.sys.iptparser".IptParser()
 local uci = require "luci.model.uci".cursor_state()
 local wat = require "luci.tools.webadmin"
+local ipc = require "luci.ip"
 local fs  = require "nixio.fs"
 
 local clients = { }
 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
-local leasefile = "/tmp/dhcp.leases"
-
-uci:foreach("dhcp", "dnsmasq",
-       function(s)
-               if s.leasefile then leasefile = s.leasefile end
-       end)
-
 
 uci:foreach("luci_splash_leases", "lease",
        function(s)
-               if s.start and s.mac then
-                       clients[s.mac:lower()] = {
+               local m = ipc.checkmac(s.mac)
+               if m and s.start then
+                       clients[m] = {
                                start   = tonumber(s.start),
                                limit   = ( tonumber(s.start) + leasetime ),
-                               mac     = s.mac:upper(),
+                               mac     = m,
                                ipaddr  = s.ipaddr,
                                policy  = "normal",
                                packets = 0,
@@ -47,11 +35,12 @@ uci:foreach("luci_splash_leases", "lease",
 
 for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
        if r.options and #r.options >= 2 and r.options[1] == "MAC" then
-               if not clients[r.options[2]:lower()] then
-                       clients[r.options[2]:lower()] = {
+               local m = ipc.checkmac(r.options[2])
+               if m and not clients[m] then
+                       clients[m] = {
                                start  = 0,
                                limit  = 0,
-                               mac    = r.options[2]:upper(),
+                               mac    = m,
                                policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
                                packets = 0,
                                bytes   = 0
@@ -68,7 +57,7 @@ for mac, client in pairs(clients) do
 
        if client.ipaddr then
                local rin  = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=client.ipaddr})
-               local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", client.mac:upper()}})
+               local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", client.mac}})
 
                if rin and #rin > 0 then
                        client.bytes_in   = rin[1].bytes
@@ -84,37 +73,27 @@ end
 
 uci:foreach("luci_splash", "whitelist",
        function(s)
-               if s.mac and clients[s.mac:lower()] then
-                       clients[s.mac:lower()].policy="whitelist"
+               local m = ipc.checkmac(s.mac)
+               if m and clients[m] then
+                       clients[m].policy="whitelist"
                end
        end)
 
 uci:foreach("luci_splash", "blacklist",
        function(s)
-               if s.mac and clients[s.mac:lower()] then
-                       clients[s.mac:lower()].policy=(s.kicked and "kicked" or "blacklist")
+               local m = ipc.checkmac(s.mac)
+               if m and clients[m] then
+                       clients[m].policy=(s.kicked and "kicked" or "blacklist")
                end
        end)            
 
-if fs.access(leasefile) then
-       for l in io.lines(leasefile) do
-               local time, mac, ip, name = l:match("^(%d+) (%S+) (%S+) (%S+)")
-               if time and mac and ip then
-                       local c = clients[mac:lower()]
-                       if c then
-                               c.ip = ip
-                               c.hostname = ( name ~= "*" ) and name or nil
-                       end
-               end
-       end
-end
-
-for i, a in ipairs(luci.sys.net.arptable()) do
-       local c = clients[a["HW address"]:lower()]
-       if c and not c.ip then
-               c.ip = a["IP address"]
+sys.net.host_hints(function(mac, v4, v6, name)
+       local c = mac and clients[mac]
+       if c then
+               c.ip = c.ip or v4
+               c.hostname = c.hostname or name
        end
-end
+end)
 
 local function showmac(mac)
        if not is_admin then
@@ -182,7 +161,7 @@ end
                                                splash.hostname, splash.ip, splash.mac, splash.timeleft, splash.trafficin, splash.trafficout);
 
                                <% if is_admin then %>
-                                       s += String.format('<select name="policy.%s" style="width:200px">', splash.mac.toLowerCase());
+                                       s += String.format('<select name="policy.%s" style="width:200px">', splash.mac);
                                        if (splash.policy == 'whitelist') {     
                                                s += '<option value="whitelist" selected="selected"><%:whitelisted%></option>'
                                        } else {
@@ -202,7 +181,7 @@ end
                                        s += String.format(
                                                '</select>' +
                                                '<input type="submit" class="cbi-button cbi-button-save" name="save.%s" value="<%:Save%>" />',
-                                               splash.mac.toLowerCase());
+                                               splash.mac);
                                <% else %>
                                        s += String.format('%s', splash.policy);
                                <% end %>
@@ -216,11 +195,11 @@ end
 
 
 <div id="cbi-splash-leases" class="cbi-map">
-       <h2><a id="content" name="content"><%:Client-Splash%></a></h2>
+       <h2 name="content"><%:Client-Splash%></h2>
        <fieldset id="cbi-table-table" class="cbi-section">
                <legend><%:Active Clients%></legend>
                <div class="cbi-section-node">
-                       <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><% end %>
+                       <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><input type="hidden" name="token" value="<%=token%>" /><% end %>
                        <table class="cbi-section-table">
                                <thead>
                                        <tr class="cbi-section-table-titles">