splash.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. module("luci.controller.splash.splash", package.seeall)
  2. local uci = luci.model.uci.cursor()
  3. local util = require "luci.util"
  4. local ipc = require "luci.ip"
  5. function index()
  6. entry({"admin", "services", "splash"}, cbi("splash/splash"), _("Client-Splash"), 90).acl_depends = { "luci-app-splash" }
  7. entry({"admin", "services", "splash", "splashtext" }, form("splash/splashtext"), _("Splashtext"), 10)
  8. local e
  9. e = node("splash")
  10. e.target = call("action_dispatch")
  11. node("splash", "activate").target = call("action_activate")
  12. node("splash", "splash").target = template("splash_splash/splash")
  13. node("splash", "blocked").target = template("splash/blocked")
  14. entry({"admin", "status", "splash"}, post("action_status_admin"), _("Client-Splash"))
  15. local page = node("splash", "publicstatus")
  16. page.target = call("action_status_public")
  17. page.leaf = true
  18. end
  19. function ip_to_mac(ip)
  20. local i, n
  21. for i, n in ipairs(ipc.neighbors({ dest = ip })) do
  22. local mac = ipc.checkmac(n.mac)
  23. if mac then return mac end
  24. end
  25. end
  26. function action_dispatch()
  27. local uci = luci.model.uci.cursor_state()
  28. local mac = ip_to_mac(luci.http.getenv("REMOTE_ADDR"))
  29. local access = false
  30. if mac then
  31. uci:foreach("luci_splash", "lease", function(s)
  32. if ipc.checkmac(s.mac) == mac then
  33. access = true
  34. return false
  35. end
  36. end)
  37. uci:foreach("luci_splash", "whitelist", function(s)
  38. if ipc.checkmac(s.mac) == mac then
  39. access = true
  40. return false
  41. end
  42. end)
  43. end
  44. if access then
  45. luci.http.redirect(luci.dispatcher.build_url())
  46. else
  47. luci.http.redirect(luci.dispatcher.build_url("splash", "splash"))
  48. end
  49. end
  50. function blacklist()
  51. leased_macs = { }
  52. uci:foreach("luci_splash", "blacklist", function(s)
  53. local m = ipc.checkmac(s.mac)
  54. if m then leased_macs[m] = true end
  55. end)
  56. return leased_macs
  57. end
  58. function action_activate()
  59. local ipc = require "luci.ip"
  60. local mac = ip_to_mac(luci.http.getenv("REMOTE_ADDR") or "127.0.0.1")
  61. local uci_state = require "luci.model.uci".cursor_state()
  62. local blacklisted = false
  63. if mac and luci.http.formvalue("accept") then
  64. uci:foreach("luci_splash", "blacklist", function(s)
  65. if ipc.checkmac(s.mac) == mac then
  66. blacklisted = true
  67. return false
  68. end
  69. end)
  70. if blacklisted then
  71. luci.http.redirect(luci.dispatcher.build_url("splash" ,"blocked"))
  72. else
  73. local id = tostring(mac):gsub(':', ''):lower()
  74. local redirect_url = uci:get("luci_splash", "general", "redirect_url")
  75. if not redirect_url then
  76. redirect_url = uci_state:get("luci_splash_locations", id, "location")
  77. end
  78. if not redirect_url then
  79. redirect_url = luci.model.uci.cursor():get("freifunk", "community", "homepage") or 'http://www.freifunk.net'
  80. end
  81. remove_redirect(id)
  82. os.execute("luci-splash lease "..tostring(mac).." >/dev/null 2>&1")
  83. luci.http.redirect(redirect_url)
  84. end
  85. else
  86. luci.http.redirect(luci.dispatcher.build_url())
  87. end
  88. end
  89. function action_status_admin()
  90. local uci = luci.model.uci.cursor_state()
  91. local macs = luci.http.formvaluetable("save")
  92. local changes = {
  93. whitelist = { },
  94. blacklist = { },
  95. lease = { },
  96. remove = { }
  97. }
  98. local key, _
  99. for key, _ in pairs(macs) do
  100. local policy = luci.http.formvalue("policy.%s" % key)
  101. local mac = luci.http.protocol.urldecode(key)
  102. if policy == "whitelist" or policy == "blacklist" then
  103. changes[policy][#changes[policy]+1] = mac
  104. elseif policy == "normal" then
  105. changes["lease"][#changes["lease"]+1] = mac
  106. elseif policy == "kicked" then
  107. changes["remove"][#changes["remove"]+1] = mac
  108. end
  109. end
  110. if #changes.whitelist > 0 then
  111. os.execute("luci-splash whitelist %s >/dev/null"
  112. % util.shellquote(table.concat(changes.whitelist)))
  113. end
  114. if #changes.blacklist > 0 then
  115. os.execute("luci-splash blacklist %s >/dev/null"
  116. % util.shellquote(table.concat(changes.blacklist)))
  117. end
  118. if #changes.lease > 0 then
  119. os.execute("luci-splash lease %s >/dev/null"
  120. % util.shellquote(table.concat(changes.lease)))
  121. end
  122. if #changes.remove > 0 then
  123. os.execute("luci-splash remove %s >/dev/null"
  124. % util.shellquote(table.concat(changes.remove)))
  125. end
  126. luci.template.render("admin_status/splash", { is_admin = true })
  127. end
  128. function action_status_public()
  129. luci.template.render("admin_status/splash", { is_admin = false })
  130. end
  131. function remove_redirect(id)
  132. local uci = require "luci.model.uci".cursor_state()
  133. local redirects = uci:get_all("luci_splash_locations")
  134. --uci:load("luci_splash_locations")
  135. uci:revert("luci_splash_locations")
  136. -- For all redirects
  137. local k, v
  138. for k, v in pairs(redirects) do
  139. if v[".type"] == "redirect" then
  140. if v[".name"] ~= id then
  141. -- Rewrite state
  142. uci:section("luci_splash_locations", "redirect", v[".name"], {
  143. location = v.location
  144. })
  145. end
  146. end
  147. end
  148. uci:save("luci_splash_redirects")
  149. end