5 require("luci.model.uci")
8 luci.model.uci.set_savedir(luci.model.uci.savedir_state)
9 local uci = luci.model.uci
16 if cmd == "status" then
21 if iswhitelisted(arg) then
33 elseif cmd == "add" then
38 if not haslease(arg) then
41 print("already leased!")
45 elseif cmd == "remove" then
52 elseif cmd == "sync" then
56 print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
61 -- Add a lease to state and invoke add_rule
62 function add_lease(mac)
63 uci.section("luci_splash", "lease", nil, {
73 -- Remove a lease from state and invoke remove_rule
74 function remove_lease(mac)
77 uci.foreach("luci_splash", "lease",
79 if section.mac:lower() == mac then
81 uci.delete("luci_splash", section[".name"])
89 -- Add an iptables rule
90 function add_rule(mac)
91 return os.execute("iptables -t nat -I luci_splash_leases -m mac --mac-source '"..mac.."' -j RETURN")
95 -- Remove an iptables rule
96 function remove_rule(mac)
97 return os.execute("iptables -t nat -D luci_splash_leases -m mac --mac-source '"..mac.."' -j RETURN")
101 -- Check whether a MAC-Address is listed in the lease state list
102 function haslease(mac)
106 uci.foreach("luci_splash", "lease",
108 if section.mac:lower() == mac then
118 -- Check whether a MAC-Address is whitelisted
119 function iswhitelisted(mac)
122 uci.foreach("luci_splash", "whitelist",
124 if section.mac:lower() == mac then
134 -- Returns a list of MAC-Addresses for which a rule is existing
136 local cmd = "iptables -t nat -L luci_splash_leases | grep RETURN |"
137 cmd = cmd .. "egrep -io [0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+"
138 return luci.util.split(luci.sys.exec(cmd))
142 -- Synchronise leases, remove abandoned rules
145 local time = os.time()
147 -- Current leases in state files
148 local leases = uci.get_all("luci_splash")
150 -- Convert leasetime to seconds
151 local leasetime = tonumber(uci.get("luci_splash", "general", "leasetime")) * 3600
154 uci.load("luci_splash")
155 uci.revert("luci_splash")
159 for k, v in pairs(leases) do
160 if v[".type"] == "lease" then
161 if os.difftime(time, tonumber(v.start)) > leasetime then
166 uci.section("luci_splash", "lease", nil, {
170 written[v.mac:lower()] = 1
176 -- Delete rules without state
177 for i, r in ipairs(listrules()) do
178 if #r > 0 and not written[r:lower()] then
183 uci.save("luci_splash")