X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=applications%2Fluci-splash%2Froot%2Fusr%2Fsbin%2Fluci-splash;h=0b645fab75e5b1fdeda5b2383b6d0966bf493bc7;hb=e080fcebd2396c138d8c916d1cf385a88e0db246;hp=f62d45c45aea8bac984ce3ed2dccecf74e4629f2;hpb=365d80765d3f08c457b0a373c395c8e1940e44be;p=project%2Fluci.git diff --git a/applications/luci-splash/root/usr/sbin/luci-splash b/applications/luci-splash/root/usr/sbin/luci-splash old mode 100644 new mode 100755 index f62d45c45..0b645fab7 --- a/applications/luci-splash/root/usr/sbin/luci-splash +++ b/applications/luci-splash/root/usr/sbin/luci-splash @@ -1,13 +1,12 @@ #!/usr/bin/lua -package.path = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path -package.cpath = "/usr/lib/lua/?.so;" .. package.cpath require("luci.http") -require("luci.sys") +require("luci.util") require("luci.model.uci") -- Init state session -uci = luci.model.uci.StateSession() +luci.model.uci.set_savedir(luci.model.uci.savedir_state) +local uci = luci.model.uci function main(argv) @@ -61,23 +60,34 @@ end -- Add a lease to state and invoke add_rule function add_lease(mac) - local key = uci:add("luci_splash", "lease") - uci:set("luci_splash", key, "mac", mac) - uci:set("luci_splash", key, "start", os.time()) + uci.section("luci_splash", "lease", nil, { + mac = mac, + start = os.time() + }) add_rule(mac) + + uci.save() end -- Remove a lease from state and invoke remove_rule function remove_lease(mac) mac = mac:lower() + local del = {} - for k, v in pairs(uci:sections("luci_splash")) do - if v[".type"] == "lease" and v.mac:lower() == mac then - remove_rule(mac) - uci:del("luci_splash", k) - end + uci.foreach("luci_splash", "lease", + function (section) + if section.mac:lower() == mac then + table.insert(del, section[".name"]) + end + end) + + for i,j in ipairs(del) do + remove_rule(j) + uci.delete("luci_splash", j) end + + uci.save() end @@ -96,14 +106,17 @@ end -- Check whether a MAC-Address is listed in the lease state list function haslease(mac) mac = mac:lower() + local stat = false - for k, v in pairs(uci:sections("luci_splash")) do - if v[".type"] == "lease" and v.mac and v.mac:lower() == mac then - return true - end - end + uci.foreach("luci_splash", "lease", + function (section) + if section.mac:lower() == mac then + stat = true + return + end + end) - return false + return stat end @@ -111,11 +124,13 @@ end function iswhitelisted(mac) mac = mac:lower() - for k, v in pairs(uci:sections("luci_splash")) do - if v[".type"] == "whitelist" and v.mac and v.mac:lower() == mac then - return true - end - end + uci.foreach("luci_splash", "whitelist", + function (section) + if section.mac:lower() == mac then + stat = true + return + end + end) return false end @@ -125,7 +140,7 @@ end function listrules() local cmd = "iptables -t nat -L luci_splash_leases | grep RETURN |" cmd = cmd .. "egrep -io [0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+" - return luci.util.split(luci.sys.exec(cmd)) + return luci.util.split(luci.util.exec(cmd)) end @@ -134,16 +149,15 @@ function sync() local written = {} local time = os.time() - uci:t_load("luci_splash") - -- Current leases in state files - local leases = uci:t_sections("luci_splash") + local leases = uci.get_all("luci_splash") -- Convert leasetime to seconds - local leasetime = tonumber(uci:t_get("luci_splash", "general", "leasetime")) * 3600 + local leasetime = tonumber(uci.get("luci_splash", "general", "leasetime")) * 3600 -- Clean state file - uci:t_revert("luci_splash") + uci.load("luci_splash") + uci.revert("luci_splash") -- For all leases @@ -154,9 +168,10 @@ function sync() remove_rule(v.mac) else -- Rewrite state - local n = uci:t_add("luci_splash", "lease") - uci:t_set("luci_splash", n, "mac", v.mac) - uci:t_set("luci_splash", n, "start", v.start) + uci.section("luci_splash", "lease", nil, { + mac = v.mac, + start = v.start + }) written[v.mac:lower()] = 1 end end @@ -170,7 +185,7 @@ function sync() end end - uci:t_save("luci_splash") + uci.save("luci_splash") end main(arg) \ No newline at end of file