splash.htm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <%#
  2. Copyright 2009 Jo-Philipp Wich <jow@openwrt.org>
  3. Licensed to the public under the Apache License 2.0.
  4. -%>
  5. <%-
  6. local utl = require "luci.util"
  7. local sys = require "luci.sys"
  8. local ipc = require "luci.ip"
  9. local ipt = require "luci.sys.iptparser".IptParser()
  10. local uci = require "luci.model.uci".cursor_state()
  11. local wat = require "luci.tools.webadmin"
  12. local ipc = require "luci.ip"
  13. local fs = require "nixio.fs"
  14. local clients = { }
  15. local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime") or 1) * 60 * 60
  16. uci:foreach("luci_splash_leases", "lease",
  17. function(s)
  18. local m = ipc.checkmac(s.mac)
  19. if m and s.start then
  20. clients[m] = {
  21. start = tonumber(s.start),
  22. limit = ( tonumber(s.start) + leasetime ),
  23. mac = m,
  24. ipaddr = s.ipaddr,
  25. policy = "normal",
  26. packets = 0,
  27. bytes = 0,
  28. }
  29. end
  30. end)
  31. for _, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
  32. if r.options and #r.options >= 2 and r.options[1] == "MAC" then
  33. local m = ipc.checkmac(r.options[2])
  34. if m and not clients[m] then
  35. clients[m] = {
  36. start = 0,
  37. limit = 0,
  38. mac = m,
  39. policy = ( r.target == "RETURN" ) and "whitelist" or "blacklist",
  40. packets = 0,
  41. bytes = 0
  42. }
  43. end
  44. end
  45. end
  46. for mac, client in pairs(clients) do
  47. client.bytes_in = 0
  48. client.bytes_out = 0
  49. client.packets_in = 0
  50. client.packets_out = 0
  51. if client.ipaddr then
  52. local rin = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=client.ipaddr})
  53. local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", client.mac}})
  54. if rin and #rin > 0 then
  55. client.bytes_in = rin[1].bytes
  56. client.packets_in = rin[1].packets
  57. end
  58. if rout and #rout > 0 then
  59. client.bytes_out = rout[1].bytes
  60. client.packets_out = rout[1].packets
  61. end
  62. end
  63. end
  64. uci:foreach("luci_splash", "whitelist",
  65. function(s)
  66. local m = ipc.checkmac(s.mac)
  67. if m and clients[m] then
  68. clients[m].policy="whitelist"
  69. end
  70. end)
  71. uci:foreach("luci_splash", "blacklist",
  72. function(s)
  73. local m = ipc.checkmac(s.mac)
  74. if m and clients[m] then
  75. clients[m].policy=(s.kicked and "kicked" or "blacklist")
  76. end
  77. end)
  78. sys.net.host_hints(function(mac, v4, v6, name)
  79. local c = mac and clients[mac]
  80. if c then
  81. c.ip = c.ip or v4
  82. c.hostname = c.hostname or name
  83. end
  84. end)
  85. local function showmac(mac)
  86. if not is_admin then
  87. mac = mac:gsub("(%S%S:%S%S):%S%S:%S%S:(%S%S:%S%S)", "%1:XX:XX:%2")
  88. end
  89. return mac
  90. end
  91. if luci.http.formvalue("status") == "1" then
  92. local rv = {}
  93. for _, c in utl.spairs(clients,
  94. function(a,b) if clients[a].policy == clients[b].policy then
  95. return (clients[a].start > clients[b].start)
  96. else
  97. return (clients[a].policy > clients[b].policy)
  98. end
  99. end)
  100. do
  101. if c.ip then
  102. rv[#rv+1] = {
  103. hostname = c.hostname or "?",
  104. ip = c.ip or "?",
  105. mac = showmac(c.mac) or "?",
  106. timeleft = (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or (c.policy ~= "normal") and "-" or "expired",
  107. trafficin = wat.byte_format(c.bytes_in) or "?",
  108. trafficout = wat.byte_format(c.bytes_out) or "?",
  109. policy = c.policy or "?"
  110. }
  111. end
  112. end
  113. luci.http.prepare_content("application/json")
  114. luci.http.write_json(rv)
  115. return
  116. end
  117. -%>
  118. <%+header%>
  119. <script>
  120. XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
  121. function(x, info)
  122. {
  123. var tbody = document.getElementById('splash_table');
  124. if (tbody)
  125. {
  126. var s = '';
  127. if (info.length == undefined) {
  128. s += '<div class="tr cbi-section-table-row"><div colspan="7" class="td cbi-section-table-cell"><br /><em><%:No clients connected%></em><br /></div></div>'
  129. };
  130. for (var idx = 0; idx < info.length; idx++)
  131. {
  132. var splash = info[idx];
  133. s += String.format(
  134. '<div class="tr cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
  135. '<div class="td cbi-section-table-cell">%s</div>' +
  136. '<div class="td cbi-section-table-cell">%s</div>' +
  137. '<div class="td cbi-section-table-cell">%s</div>' +
  138. '<div class="td cbi-section-table-cell">%s</div>' +
  139. '<div class="td cbi-section-table-cell">%s/%s</div>' +
  140. '<div class="td cbi-section-table-cell">',
  141. splash.hostname, splash.ip, splash.mac, splash.timeleft, splash.trafficin, splash.trafficout);
  142. <% if is_admin then %>
  143. s += String.format('<select name="policy.%s" style="width:200px">', splash.mac);
  144. if (splash.policy == 'whitelist') {
  145. s += '<option value="whitelist" selected="selected"><%:whitelisted%></option>'
  146. } else {
  147. s += '<option value="whitelist"><%:whitelisted%></option>'
  148. };
  149. if (splash.policy == 'normal') {
  150. s += '<option value="normal" selected="selected"><%:splashed%></option>';
  151. s += '<option value="kicked"><%:temporarily blocked%></option>'
  152. } else {
  153. s += '<option value="normal"><%:splashed%></option>'
  154. };
  155. if (splash.policy == 'blacklist') {
  156. s+= '<option value="blacklist" selected="selected"><%:blacklisted%></option>'
  157. } else {
  158. s += '<option value="blacklist"><%:blacklisted%></option>'
  159. };
  160. s += String.format(
  161. '</select>' +
  162. '<input type="submit" class="cbi-button cbi-button-save" name="save.%s" value="<%:Save%>" />',
  163. splash.mac);
  164. <% else %>
  165. s += String.format('%s', splash.policy);
  166. <% end %>
  167. s += '</div></div>'
  168. }
  169. tbody.innerHTML = s;
  170. }
  171. }
  172. );
  173. </script>
  174. <div id="cbi-splash-leases" class="cbi-map">
  175. <h2 name="content"><%:Client-Splash%></h2>
  176. <fieldset id="cbi-table-table" class="cbi-section">
  177. <legend><%:Active Clients%></legend>
  178. <div class="cbi-section-node">
  179. <% if is_admin then %><form action="<%=REQUEST_URI%>" method="post"><input type="hidden" name="token" value="<%=token%>" /><% end %>
  180. <div class="table cbi-section-table">
  181. <div class="thead">
  182. <div class="tr cbi-section-table-titles">
  183. <div class="th cbi-section-table-cell"><%:Hostname%></div>
  184. <div class="th cbi-section-table-cell"><%:IP Address%></div>
  185. <div class="th cbi-section-table-cell"><%:MAC Address%></div>
  186. <div class="th cbi-section-table-cell"><%:Time remaining%></div>
  187. <div class="th cbi-section-table-cell"><%:Traffic in/out%></div>
  188. <div class="th cbi-section-table-cell"><%:Policy%></div>
  189. </div>
  190. </div>
  191. <div class="tbody" id="splash_table">
  192. <%-
  193. local count = 0
  194. for _, c in utl.spairs(clients,
  195. function(a,b)
  196. if clients[a].policy == clients[b].policy then
  197. return (clients[a].start > clients[b].start)
  198. else
  199. return (clients[a].policy > clients[b].policy)
  200. end
  201. end)
  202. do
  203. if c.ip then
  204. count = count + 1
  205. -%>
  206. <div class="tr cbi-section-table-row cbi-rowstyle-<%=2-(count%2)%>">
  207. <div class="td cbi-section-table-cell"><%=c.hostname or "<em>" .. translate("unknown") .. "</em>"%></div>
  208. <div class="td cbi-section-table-cell"><%=c.ip or "<em>" .. translate("unknown") .. "</em>"%></div>
  209. <div class="td cbi-section-table-cell"><%=showmac(c.mac)%></div>
  210. <div class="td cbi-section-table-cell"><%=
  211. (c.limit >= os.time()) and wat.date_format(c.limit-os.time()) or
  212. (c.policy ~= "normal") and "-" or "<em>" .. translate("expired") .. "</em>"
  213. %></div>
  214. <div class="td cbi-section-table-cell"><%=wat.byte_format(c.bytes_in)%> / <%=wat.byte_format(c.bytes_out)%></div>
  215. <div class="td cbi-section-table-cell">
  216. <% if is_admin then %>
  217. <select name="policy.<%=c.mac:lower()%>" style="width:200px">
  218. <option value="whitelist"<%=c.policy=="whitelist" and ' selected="selected"'%>><%:whitelisted%></option>
  219. <option value="normal"<%=c.policy=="normal" and not c.kicked and ' selected="selected"'%>><%:splashed%></option>
  220. <option value="blacklist"<%=c.policy=="blacklist" and ' selected="selected"'%>><%:blacklisted%></option>
  221. <% if c.policy == "normal" then -%>
  222. <option value="kicked"><%:temporarily blocked%></option>
  223. <%- end %>
  224. </select>
  225. <input type="submit" class="cbi-button cbi-button-save" name="save.<%=c.mac:lower()%>" value="<%:Save%>" />
  226. <% else %>
  227. <%=c.policy%>
  228. <% end %>
  229. </div>
  230. </div>
  231. <%-
  232. end
  233. end
  234. if count == 0 then
  235. -%>
  236. <div class="tr cbi-section-table-row">
  237. <div colspan="7" class="td cbi-section-table-cell">
  238. <br /><em><%:No clients connected%></em><br />
  239. </div>
  240. </div>
  241. <%- end -%>
  242. </div>
  243. </div>
  244. <% if is_admin then %></form><% end %>
  245. </div>
  246. </fieldset>
  247. </div>
  248. <%+footer%>