index.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. module("luci.controller.admin.index", package.seeall)
  4. function index()
  5. function toplevel_page(page, preflookup, preftarget)
  6. if preflookup and preftarget then
  7. if lookup(preflookup) then
  8. page.target = preftarget
  9. end
  10. end
  11. if not page.target then
  12. page.target = firstchild()
  13. end
  14. end
  15. local uci = require("luci.model.uci").cursor()
  16. local root = node()
  17. if not root.target then
  18. root.target = alias("admin")
  19. root.index = true
  20. end
  21. local page = node("admin")
  22. page.title = _("Administration")
  23. page.order = 10
  24. page.sysauth = "root"
  25. page.sysauth_authenticator = "htmlauth"
  26. page.ucidata = true
  27. page.index = true
  28. page.target = firstnode()
  29. -- Empty menu tree to be populated by addons and modules
  30. page = node("admin", "status")
  31. page.title = _("Status")
  32. page.order = 10
  33. page.index = true
  34. -- overview is from mod-admin-full
  35. toplevel_page(page, "admin/status/overview", alias("admin", "status", "overview"))
  36. page = node("admin", "system")
  37. page.title = _("System")
  38. page.order = 20
  39. page.index = true
  40. -- system/system is from mod-admin-full
  41. toplevel_page(page, "admin/system/system", alias("admin", "system", "system"))
  42. -- Only used if applications add items
  43. page = node("admin", "services")
  44. page.title = _("Services")
  45. page.order = 40
  46. page.index = true
  47. toplevel_page(page, false, false)
  48. -- Even for mod-admin-full network just uses first submenu item as landing
  49. page = node("admin", "network")
  50. page.title = _("Network")
  51. page.order = 50
  52. page.index = true
  53. toplevel_page(page, false, false)
  54. if nixio.fs.access("/etc/config/dhcp") then
  55. page = entry({"admin", "dhcplease_status"}, call("lease_status"), nil)
  56. page.leaf = true
  57. end
  58. local has_wifi = false
  59. uci:foreach("wireless", "wifi-device",
  60. function(s)
  61. has_wifi = true
  62. return false
  63. end)
  64. if has_wifi then
  65. page = entry({"admin", "wireless_assoclist"}, call("wifi_assoclist"), nil)
  66. page.leaf = true
  67. page = entry({"admin", "wireless_deauth"}, post("wifi_deauth"), nil)
  68. page.leaf = true
  69. end
  70. page = entry({"admin", "translations"}, call("action_translations"), nil)
  71. page.leaf = true
  72. -- Logout is last
  73. entry({"admin", "logout"}, call("action_logout"), _("Logout"), 999)
  74. end
  75. function action_logout()
  76. local dsp = require "luci.dispatcher"
  77. local utl = require "luci.util"
  78. local sid = dsp.context.authsession
  79. if sid then
  80. utl.ubus("session", "destroy", { ubus_rpc_session = sid })
  81. luci.http.header("Set-Cookie", "sysauth=%s; expires=%s; path=%s" %{
  82. '', 'Thu, 01 Jan 1970 01:00:00 GMT', dsp.build_url()
  83. })
  84. end
  85. luci.http.redirect(dsp.build_url())
  86. end
  87. function action_translations(lang)
  88. local i18n = require "luci.i18n"
  89. local http = require "luci.http"
  90. local fs = require "nixio".fs
  91. if lang and #lang > 0 then
  92. lang = i18n.setlanguage(lang)
  93. if lang then
  94. local s = fs.stat("%s/base.%s.lmo" %{ i18n.i18ndir, lang })
  95. if s then
  96. http.header("Cache-Control", "public, max-age=31536000")
  97. http.header("ETag", "%x-%x-%x" %{ s["ino"], s["size"], s["mtime"] })
  98. end
  99. end
  100. end
  101. http.prepare_content("application/javascript; charset=utf-8")
  102. http.write("window.TR=")
  103. http.write_json(i18n.dump())
  104. end
  105. function lease_status()
  106. local s = require "luci.tools.status"
  107. luci.http.prepare_content("application/json")
  108. luci.http.write('[')
  109. luci.http.write_json(s.dhcp_leases())
  110. luci.http.write(',')
  111. luci.http.write_json(s.dhcp6_leases())
  112. luci.http.write(']')
  113. end
  114. function wifi_assoclist()
  115. local s = require "luci.tools.status"
  116. luci.http.prepare_content("application/json")
  117. luci.http.write_json(s.wifi_assoclist())
  118. end
  119. function wifi_deauth()
  120. local iface = luci.http.formvalue("iface")
  121. local bssid = luci.http.formvalue("bssid")
  122. if iface and bssid then
  123. luci.util.ubus("hostapd.%s" % iface, "del_client", {
  124. addr = bssid,
  125. deauth = true,
  126. reason = 5,
  127. ban_time = 60000
  128. })
  129. end
  130. luci.http.status(200, "OK")
  131. end