UCI API changes
[project/luci.git] / applications / luci-splash / root / usr / bin / luci-splashd
1 #!/usr/bin/lua
2
3 require("socket")
4 require("luci.ip")
5 require("luci.model.uci")
6
7 local uci = luci.model.uci.cursor_state()
8 uci:load("network")
9
10 local server = socket.bind("0.0.0.0", arg[1] or 8082)
11 server:settimeout(0, "t")
12
13 while true do
14         local client = server:accept()
15         
16         if client then
17                 client:settimeout(1)
18                 local srv
19                 local ip = luci.ip.IPv4(client:getpeername())
20                 uci:foreach("network", "interface",
21                         function (section)
22                             if section.ipaddr then
23                                 local net = luci.ip.IPv4(section.ipaddr, section.netmask)
24                                 if ip and net and net:contains(ip) then
25                                     srv = section.ipaddr
26                                     return
27                                 end
28                             end
29                         end)
30
31                 client:receive()
32                 client:send("HTTP/1.0 302 Found\r\nLocation: http://" .. srv ..
33                  (arg[2] or "/luci/splash") .. "\r\n\r\n")
34                 client:close() 
35         else
36                 socket.sleep(0.1)
37         end
38 end