(no commit message)
[project/luci.git] / contrib / package / ffluci-splash / src / luci-splash / htdocs / cgi-bin / index.cgi
1 #!/usr/bin/haserl --shell=luac
2 package.path = "/usr/lib/lua/?.lua;/usr/lib/lua/?/init.lua;" .. package.path
3 package.cpath = "/usr/lib/lua/?.so;" .. package.cpath
4
5 require("ffluci.http")
6 require("ffluci.sys")
7 require("ffluci.model.uci")
8
9 local srv
10 local net
11 local ip = ffluci.http.remote_addr()
12 for k, v in pairs(ffluci.model.uci.sections("network")) do
13 if v[".type"] == "interface" and v.ipaddr then
14 local p = ffluci.sys.net.mask4prefix(v.netmask)
15 if ffluci.sys.net.belongs(ip, v.ipaddr, p) then
16 net = k
17 srv = v.ipaddr
18 break
19 end
20 end
21 end
22
23 local stat = false
24 for k, v in pairs(ffluci.model.uci.sections("luci_splash")) do
25 if v[".type"] == "iface" and v.network == net then
26 stat = true
27 end
28 end
29
30 if not srv then
31 ffluci.http.textheader()
32 return print("Unable to detect network settings!")
33 end
34
35 if not stat then
36 ffluci.http.redirect("http://" .. srv)
37 end
38
39 local action = "splash"
40
41 local mac = ffluci.sys.net.ip4mac(ip)
42 if not mac then
43 action = "unknown"
44 end
45
46 local status = ffluci.sys.execl("luci-splash status "..mac)[1]
47
48 if status == "whitelisted" or status == "lease" then
49 action = "allowed"
50 end
51
52 ffluci.http.redirect("http://" .. srv .. "/cgi-bin/luci-splash/" .. action)