1
0

splash.lua 4.5 KB

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