e8c275209e4ce54011769c59d3c57c32bb049d57
[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 luci.model.uci.set_savedir(luci.model.uci.savedir_state)
8
9 local server = socket.bind("0.0.0.0", arg[1] or 8082)
10 server:settimeout(0, "t")
11
12 while true do
13         local client = server:accept()
14         
15         if client then
16                 client:settimeout(1)
17                 local srv
18                 local ip = luci.ip.IPv4(client:getpeername())
19                 luci.model.uci.foreach("network", "interface",
20                         function (section)
21                             if section.ipaddr then
22                                 local net = luci.ip.IPv4(section.ipaddr, section.netmask)
23                                 if ip and net and net:contains(ip) then
24                                     srv = section.ipaddr
25                                     return
26                                 end
27                             end
28                         end)
29
30                 client:receive()
31                 client:send("HTTP/1.0 302 Found\r\nLocation: http://" .. srv ..
32                  (arg[2] or "/luci/splash") .. "\r\n\r\n")
33                 client:close() 
34         else
35                 socket.sleep(0.1)
36         end
37 end