applications/luci-splash: fix mac address detection in mixed IPv4/IPv6 environments
[project/luci.git] / applications / luci-splash / luasrc / controller / splash / splash.lua
1 module("luci.controller.splash.splash", package.seeall)
2
3 function index()
4 entry({"admin", "services", "splash"}, cbi("splash/splash"), "Client-Splash")
5
6 node("splash").target = call("action_dispatch")
7 node("splash", "activate").target = call("action_activate")
8 node("splash", "splash").target = template("splash_splash/splash")
9 end
10
11 function action_dispatch()
12 local mac = luci.sys.net.ip4mac(luci.http.getenv("REMOTE_ADDR")) or ""
13 local status = luci.util.execl("luci-splash status "..mac)[1]
14 if #mac > 0 and ( status == "whitelisted" or status == "lease" ) then
15 luci.http.redirect(luci.dispatcher.build_url())
16 else
17 luci.http.redirect(luci.dispatcher.build_url("splash", "splash"))
18 end
19 end
20
21 function action_activate()
22 local ip = luci.http.getenv("REMOTE_ADDR") or "127.0.0.1"
23 local mac = luci.sys.net.ip4mac(
24 ip:match("::") and (ip:gsub("^%[::ffff:(.+)%]", "%1")) or ip
25 )
26 if mac and luci.http.formvalue("accept") then
27 os.execute("luci-splash add "..mac.." >/dev/null 2>&1")
28 luci.http.redirect(luci.model.uci.cursor():get("freifunk", "community", "homepage"))
29 else
30 luci.http.redirect(luci.dispatcher.build_url())
31 end
32 end