5 require("luci.model.uci")
8 luci.model.uci.set_savedir(luci.model.uci.savedir_state)
9 local uci = luci.model.uci
10 local cfg = uci.config
17 if cmd == "status" then
22 if iswhitelisted(arg) then
34 elseif cmd == "add" then
39 if not haslease(arg) then
42 print("already leased!")
46 elseif cmd == "remove" then
53 elseif cmd == "sync" then
57 print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
62 -- Add a lease to state and invoke add_rule
63 function add_lease(mac)
64 cfg.luci_splash[""] = "lease"
65 cfg.luci_splash[""].mac = mac
66 cfg.luci_splash[""].start = os.time()
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(cfg.luci_splash.general.leasetime) * 3600
154 uci.revert("luci_splash")
158 for k, v in pairs(leases) do
159 if v[".type"] == "lease" then
160 if os.difftime(time, tonumber(v.start)) > leasetime then
165 cfg.luci_splash[""] = "lease"
166 cfg.luci_splash[""].mac = v.mac
167 cfg.luci_splash[""].start = v.start
168 written[v.mac:lower()] = 1
174 -- Delete rules without state
175 for i, r in ipairs(listrules()) do
176 if #r > 0 and not written[r:lower()] then
181 uci.save("luci_splash")