applications/luci-splash: implement alias interface support in luci-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                 print (client:getpeername())
20                 local ip  = luci.ip.IPv4((client:getpeername()))
21
22                 local function find_srv(section)
23                         if section.ipaddr then
24                                 local net = luci.ip.IPv4(section.ipaddr, section.netmask)
25                                 if ip and net and net:contains(ip) then
26                                         srv = section.ipaddr
27                                         return
28                                 end
29                         end
30                 end
31                 
32                 uci:foreach("network", "interface", find_srv)
33                 uci:foreach("network", "alias", find_srv)
34
35                 client:receive()
36                 client:send("HTTP/1.0 302 Found\r\nLocation: http://" .. srv ..
37                  (arg[2] or "/luci/splash") .. "\r\n\r\n")
38                 client:close() 
39         else
40                 socket.sleep(0.1)
41         end
42 end