applications/luci-splash: remove debugging code from splashd
[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
21                 local function find_srv(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                 uci:foreach("network", "interface", find_srv)
32                 uci:foreach("network", "alias", find_srv)
33
34                 client:receive()
35                 client:send("HTTP/1.0 302 Found\r\nLocation: http://" .. srv ..
36                  (arg[2] or "/luci/splash") .. "\r\n\r\n")
37                 client:close() 
38         else
39                 socket.sleep(0.1)
40         end
41 end