1
0

cshark.htm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <%#
  2. LuCI - Lua Configuration Interface
  3. Copyright (C) 2014, QA Cafe, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. $Id$
  9. -%>
  10. <fieldset class="cbi-section">
  11. <legend><%:Start network capture%></legend>
  12. <div class="cbi-section-node">
  13. <table class="cbi-section-table">
  14. <tr>
  15. <th><%:Interface%></th>
  16. <th colspan='2'><%:seconds, packets, bytes%></th>
  17. <th><%:Filter%></th>
  18. <th><%:Actions%></th>
  19. </tr>
  20. <tr>
  21. <td>
  22. <select title="<%:Interface%>" style="width:auto" id="s_interfaces">
  23. <%
  24. local nixio = require "nixio"
  25. for k, v in ipairs(nixio.getifaddrs()) do
  26. if v.family == "packet" then
  27. %>
  28. <option value="<%=v.name%>"><%=v.name%> </option>
  29. <%
  30. end
  31. end
  32. %>
  33. <option value="any"><%:any%></option>
  34. </select>
  35. </td>
  36. <td colspan='2'>
  37. <input id="tx_value" type="text" value="0" />
  38. <select title="<%:timeout, bytes, seconds%>" id="s_value_type" style="width:auto">
  39. <option value="T"><%:seconds%></option>
  40. <option value="P"><%:packets%></option>
  41. <option value="S"><%:bytes%></option>
  42. </select>
  43. </td>
  44. <td>
  45. <input style="margin: 5px 0" type="text" title="<%:Filter%>" placeholder="filter" id="i_filter" />
  46. </td>
  47. <td>
  48. <input type="button" id="bt_action" data-action="start" value="<%:Start capture%>" class="cbi-button" />
  49. </td>
  50. </tr>
  51. </table>
  52. </div>
  53. </fieldset>
  54. <fieldset class="cbi-section">
  55. <span id="cshark-rc-output"></span>
  56. </fieldset>
  57. <hr/>
  58. <fieldset class="cbi-section">
  59. <legend><%:Capture links%></legend>
  60. <div class="cbi-section-node">
  61. <table id="t_link_list" class="cbi-section-table">
  62. <tr class="cbi-section-table-titles">
  63. <th class="cbi-section-table-cell"><%:Capture URL%></th>
  64. <th class="cbi-section-table-cell"><%:Capture time%></th>
  65. </tr>
  66. </table>
  67. </div>
  68. </fieldset>
  69. <fieldset class="cbi-section">
  70. <a href="https://support.cloudshark.org/openwrt/openwrt-cloudshark.html" target="_blank">Visit support.cloudshark.org for help.</a>
  71. </fieldset>
  72. <hr/>
  73. <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
  74. <script type="text/javascript">//<![CDATA[
  75. var capture_running = 0;
  76. var pid_file = 0;
  77. var bt_action = document.getElementById('bt_action');
  78. var a_clear_links = document.getElementById('a_clear_links');
  79. var output = document.getElementById('cshark-rc-output');
  80. var loader = '<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" width="16" height="16" style="vertical-align:middle" /> ';
  81. var msg = { 'start' : '<%:Waiting for capture to complete...%>', 'stop' : '<%:Waiting for upload to complete...%>' };
  82. var status_msg = msg['start'];
  83. function get_date(timestamp)
  84. {
  85. function pad_str(str)
  86. {
  87. return (str < 10) ? "0" + str : str;
  88. }
  89. var current_date = new Date(timestamp * 1000);
  90. return current_date.getFullYear() + "-" +
  91. pad_str(current_date.getMonth() + 1) + "-" +
  92. pad_str(current_date.getDate()) + " " +
  93. pad_str(current_date.getHours()) + ":" +
  94. pad_str(current_date.getMinutes()) + ":" +
  95. pad_str(current_date.getSeconds());
  96. }
  97. bt_action.onclick = function()
  98. {
  99. var action = this.getAttribute("data-action");
  100. var csxhr = new XHR();
  101. if (action == "stop")
  102. {
  103. update_status(action);
  104. bt_action.disabled = true;
  105. csxhr.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_iface_dump_stop', null,
  106. function(x)
  107. {
  108. if (!x || x.responseText.trim() != "0")
  109. {
  110. update_status("failed", "Invalid response on stop.");
  111. }
  112. });
  113. }
  114. else if (action == "start")
  115. {
  116. var s_interfaces = document.getElementById('s_interfaces');
  117. var s_value_type = document.getElementById('s_value_type');
  118. var i_filter = document.getElementById('i_filter');
  119. var if_n = s_interfaces.selectedIndex;
  120. var t_n = s_value_type.selectedIndex;
  121. var ifname = s_interfaces.options[if_n].value.trim();
  122. var filter_val = i_filter.value.trim();
  123. var tx_val = document.getElementById('tx_value').value.trim();
  124. var type_val = s_value_type.options[t_n].value.trim();
  125. if (type_val != 'P' && type_val != 'T' && type_val != 'S') type_val = 'T';
  126. if (!ifname || !type_val) return;
  127. if (isNaN(tx_val)) return alert("<%:value for [seconds, packets, bytes] must be Integer%>");
  128. update_status(action);
  129. csxhr.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_iface_dump_start/' + ifname + '/' + tx_val + '/' + type_val + '/'+ filter_val, null,
  130. function(x)
  131. {
  132. if (!x)
  133. update_status("failed", "Invalid response on start.");
  134. else
  135. update_status("running");
  136. });
  137. }
  138. }
  139. function update_status(status, message)
  140. {
  141. switch (status)
  142. {
  143. case 'start':
  144. case 'stop':
  145. status_msg = msg[status];
  146. output.innerHTML = loader + status_msg;
  147. break
  148. case 'running':
  149. if (capture_running) break;;
  150. output.innerHTML = loader + status_msg;
  151. bt_action.value = '<%:Stop capture%>';
  152. bt_action.setAttribute('data-action', 'stop');
  153. capture_running = 1;
  154. break;
  155. case 'completed':
  156. case 'failed':
  157. if (!capture_running) break;
  158. if (status == "completed")
  159. {
  160. link_list_update();
  161. }
  162. output.innerHTML = "<pre>" + message + "</pre>";
  163. bt_action.value = '<%:Start capture%>';
  164. bt_action.setAttribute('data-action', 'start');
  165. bt_action.disabled = false;
  166. capture_running = 0;
  167. break;
  168. }
  169. }
  170. function check_status()
  171. {
  172. XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "network")%>/cshark_check_status', null,
  173. function(x, data)
  174. {
  175. if (!x)
  176. {
  177. if (capture_running)
  178. update_status("failed", "Invalid response when fetching status.");
  179. return;
  180. }
  181. console.log(data)
  182. update_status( (data.status == 1) && "running" || "completed", data.msg);
  183. })
  184. }
  185. function link_list_clear()
  186. {
  187. var csxhr_del = new XHR();
  188. csxhr_del.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_link_list_clear', null,
  189. function(x)
  190. {
  191. if (!x)
  192. return false;
  193. link_list_update();
  194. });
  195. }
  196. function link_list_update()
  197. {
  198. var t_link = document.getElementById("t_link_list");
  199. if (!t_link) return;
  200. var row_count = t_link.rows.length;
  201. while(--row_count) t_link.deleteRow(row_count);
  202. var cell = t_link.insertRow(-1).insertCell(0);
  203. cell.colSpan = 2;
  204. cell.innerHTML = loader;
  205. var csxhr_link = new XHR();
  206. csxhr_link.get('<%=luci.dispatcher.build_url("admin", "network")%>/cshark_link_list_get', null,
  207. function(x, entries)
  208. {
  209. var row = t_link.deleteRow(1);
  210. if (!x) return;
  211. if (!entries || !entries.length)
  212. {
  213. var cell = t_link.insertRow(-1).insertCell(0);
  214. cell.colSpan = 2;
  215. cell.innerHTML = '<em><br />There are no captures available yet.</em>';
  216. return;
  217. }
  218. for (var i = 0, len = entries.length; i < len ; i++)
  219. {
  220. var entry = entries[i][0];
  221. if (!entry) continue;
  222. var data = entry.split(",");
  223. var url = data[0];
  224. var timestamp = data[1];
  225. var row = t_link.insertRow(-1);
  226. row.insertCell(0).innerHTML = '<a href="'+url+'" target="_blank">'+url+'</a>';
  227. row.insertCell(1).innerHTML = get_date(timestamp);
  228. }
  229. var cell = t_link.insertRow(-1).insertCell(0);
  230. cell.colSpan = 2;
  231. cell.style.textAlign="center";
  232. cell.innerHTML = '<input type="button" onclick="link_list_clear()" class="cbi-button" value ="<%:Clear list%>" />';
  233. })
  234. }
  235. check_status();
  236. link_list_update();
  237. //]]></script>