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