summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-splash/root/usr/bin/luci-splashd
blob: c94537fbc9bc54804eead48c1f049ea32b2f24fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/lua

require("socket")
require("luci.ip")
require("luci.model.uci")

local uci = luci.model.uci.cursor_state()
uci:load("network")

local server = socket.bind("0.0.0.0", arg[1] or 8082)
server:settimeout(0, "t")

while true do
	local client = server:accept()
	
	if client then
		client:settimeout(1)
		local srv
		print (client:getpeername())
		local ip  = luci.ip.IPv4((client:getpeername()))

		local function find_srv(section)
			if section.ipaddr then
				local net = luci.ip.IPv4(section.ipaddr, section.netmask)
				if ip and net and net:contains(ip) then
					srv = section.ipaddr
					return
				end
			end
		end
		
		uci:foreach("network", "interface", find_srv)
		uci:foreach("network", "alias", find_srv)

		client:receive()
		client:send("HTTP/1.0 302 Found\r\nLocation: http://" .. srv ..
		 (arg[2] or "/luci/splash") .. "\r\n\r\n")
		client:close() 
	else
		socket.sleep(0.1)
	end
end